[De1CTF 2019]SSRF Me
python-flask框架学习笔记(一)–route(路由):https://blog.csdn.net/weixin_44923175/article/details/89339607
直接分析源代码
#! /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')
是个用python写的后端程序,web的flash框架,那就先看路由(route)
-
先看geneSign,基本信息:上传方法有get和post 上传的参数为param与其他字符串拼接,然后进行的操作action是扫描(scan),进入getsign(这里还不知道这个sign的作用),到了下边
-
-他是用param和action做拼接的!而action是固定的scan ,也就是说,我们先使用geneSign,传参为flag.txtread,这样只会param+action拼接为 flag.txtreadscan,生成sign保存下来
-
再看De1ta,这个应该是重点,
action = urllib.unquote(request.cookies.get("action")) param = urllib.unquote(request.args.get("param", "")) sign = urllib.unquote(request.cookies.get("sign"))
这里的action和sign的request都是在cookie中执行的,这里的sign应该就是需要上一步的操作得到,然后这里的param和上一步的一样。
执行之后传到task返回Exec()。
-
再分析task里的exec(),里边还有两个if,然后2个if,判断scan或read 是否在action内,在则,前者是把param对应文件的内容写入result.txt,后者是把result.txt取出来并返还给我们,也就是说,这2个if我们都需要调用,这样就能获取到flag,而scan和read的判断是包含,所以我们的action就可以是readscan,或者scanread
-
再来看getSign(self.action, self.param) == self.sign,直接看getSign, param+action=“flag.txt” + “readscan”=“flag.txtreadscan”
这样形成的sign刚好就能对应到。而这样又能将param中文件内容读出并返回给我们,以此获取flag
sign=bb6563b7ec0913efbad4e0626e5897bb
flag{87c2c2d9-61aa-4b52-81de-ef7403bdede9}