CTF

[De1CTF 2019]SSRF Me(python代码审计)

Posted on 2020-03-03,4 min read

进去就有源码~

#! /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)

    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


#generate Sign For Action Scan.
@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')

python代码一般就是先看路由
这题有三个路由。
第一个路由就是显示源码
第二个路由是数据签名
暂时只知道它接受一个param参数。然后和密钥+'scan'组合md5

三个路由是De1ta。应该是代码关键处
最后进入Task类中的EXEC函数

进行签名验证。

总结一下:

geneSign。输入param可以得到。此时一个参数可控
md5(密钥+param+action)
checksign。输入param/action/sign。注意。此时三个参数都可控
md5(密钥+param+action)==sign
Task.Exec。action中得包含scan和read。不然你scan读取了flag。怎么看。要么算md5(ip)沙箱名称。。我没成功

从签名的两步中。密钥是不变的。而action定死为scan了
可变的只有中间的param
那么md5(密钥+flag.txtread+scan)是否等于md5(密钥+flag.txt+readscan)呢
答案是肯定的。python直接将他们连接了起来。没有任何操作

答案显而易见。我们可以在geneSign处。将param设为flag.txtread。我们就可以得到md5(密钥+flag.txtread+scan)
在验证签名时。我们输入sign为之前获取的sign。param为flag.txt 。action为readscan
那么它验证签名的时候md5(密钥+flag.txt+readscan)是相等的

第二种解法。利用hashpump进行哈希扩展攻击
首先。去/geneSign。获取密钥+flag.txt+scan的md5
利用hash扩展攻击。在最后附加一个read

在python2.7下还支持local_file和local-file协议。
local-file:////proc/self/cwd//etc/passwd
/proc/self/cwd/==当前路径

下一篇: 友链→