SSRFMe

[De1CTF 2019]SSRF Me

python 类型题目做太少了 所以看见这个题目时一脸懵逼

首先规范一下代码格式

#! /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)): # 检查参数是否包含 gopher file
        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)

代码审计

首先查看各个路由的作用

image-20210321220139907image-20210321220139907

image-20210321220155658image-20210321220155658

这里利用传入的参数生成返回一个加密的MD5值

image-20210321220252839image-20210321220252839

使用cookie与get方法传入了一些参数,使用waf检查了一下param里面是否包含某些危险的东西,最后定义了类并调用了函数

image-20210321220545688image-20210321220545688

image-20210321220612023image-20210321220612023

这里先检查了验证了一下登录,然后再使用in判断了action中是否包含了read或者scan

因为这里判断action使用的时in,而不是==所以就会造成漏洞

解题

先构造特殊的md5,再利用这个绕过验证,使用scan和read先写入文件再读取文件

hashlib.md5(secert_key + flag.txtread + scan).hexdigest() # 最开始构造的特殊MD5

image-20210321221107881image-20210321221107881

然后利用这里构造的特殊md5伪造身份访问

image-20210321221215305image-20210321221215305

这题比较简单子需要我们读懂代码就可以了

最后修改于:2021年03月31日 21:50

添加新评论