[De1CTF 2019]SSRF Me
[De1CTF 2019]SSRF Me
整理代码
#!/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
import imp
imp.reload(sys)
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.request.unquote(request.args.get("param", ""))
action = "scan"
return getSign(action, param)
@app.route('/De1ta',methods=['GET','POST'])
def challenge():
action = urllib.request.unquote(request.cookies.get("action")) # Get action from cookie
param = urllib.request.unquote(request.args.get("param", "")) # Get param from url query
sign = urllib.request.unquote(request.cookies.get("sign")) # Get sign from cookie
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.request.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): # param should not start with gopher or file
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)
三个路由:
/geneSign
传递参数param和设置好的action=scan传入getSign生成签名
/De1ta
从cookie中获取action和sign,再获取参数param以及ip然后传入Task类中,以json形式返回Task->Exec()
/
首页,读取code.txt中的源码返回
waf()过滤了param中gopher和file关键字
def waf(param): # param should not start with gopher or file
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(),这里有两种方法可以直接写文件名读取本地文件
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.txtread,拿到这个签名
hashlib.md5(secert_key + 'flag.txt' + 'scan').hexdigest()
45e041f7cedbaa834de7d143f345a2b2
这里为了找到key,令/geneSign?param=flag.txt,action是固定值,得出的md5是拼接字符,反编译后得到key值,而输入flag.txtread的原因是因为action是scan,而在getSign里几个字符是.号会连接起来,所以就变成了key.flag.txtreadscan,这样sign就出现了我们想要的md5值,避免出现过多操作导致失误
接着在/De1ta路由传入param,并在cookie设置action和sign即可得到flag,注意action里既要有read也要有scan,因为要先将flag写入result后再读出
总体思路就是利用getSign拼接顺序的漏洞绕过Task.checkSign和geneSign共同构成的对可用action的限制
令param='flag.txtread',通过geneSign拼接而成的md5(secret_key + 'flag.txtreadscan')同指定param='flag.txt' action='readscan'相同