MENU

[De1CTF 2019]SSRF Me

October 7, 2020 • Read: 158 • CTF

# ! /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)):
            os.mkdir(self.sandbox)

    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

    def checkSign(self):
        if (getSign(self.action, self.param) == self.sign):
            return True
        else:
            return False


@app.route("/geneSign", methods=['GET', 'POST'])
def geneSign():
    param = urllib.unquote(request.args.get("param", ""))
    action = "scan"
    return getSign(action, param)


@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"


def getSign(action, param):
    return hashlib.md5(secert_key + param + action).hexdigest()


def md5(content):
    return hashlib.md5(content).hexdigest()


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)

简单整理了一下代码,又是Flask框架,出于对框架的不熟悉,全程边百度边做。。。

这里需要传递三个参数

action = urllib.unquote(request.cookies.get("action"))
    param = urllib.unquote(request.args.get("param", ""))
    sign = urllib.unquote(request.cookies.get("sign"))

这题主要要解决的就是Exec这个函数,首先就要对Sign进行检测,检测规则如下:

    def checkSign(self):
        if (getSign(self.action, self.param) == self.sign):
            return True
        else:
            return False

这里的self.sign是不知道的,要想办法获得这个sign。在路由的地方能发现geneSign函数,因为action被固定为scan,题目的hint告诉flag在flag.txt中,所以通过传参构造getSign函数,使其变成:

secert_key + flag.txt + scan

!!!注意:这是错误的sign,下面会提到
生成证书:

1.jpg

现在有了证书,继续往下看。它说action中必须有scan和read,刚才的action是scan,那生成的证书用的字符串为:secert_key + flag.txt + scan ,这里面并没有read呀。仔细看了看geneSign函数只是做了一个字符串拼接,所以输入的是不是flag.txt都无所谓,secery_key + flag.txtread + scan 这样就可以构成action=readscan的sign了,那么重新生成证书

2.jpg

这次生成的才是正确的证书 64018209a1d693e3e57dd8bcec147564

接下来只需要传参带入/De1ta中就能获取flag了

5.jpg