题目直接给了题目源码: 就是看着有点难受
#! /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()): //Exec 这边还有个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): //getSign()方法,结果与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’]) //De1ta应该是关键的页面获取了三个参数,其中两个是从cookie中获取的:
def challenge():
action = urllib.unquote(request.cookies.get(“action”)) //第一个参数action是传入read和scan的,
第二个参数看着应该是传入一个文件名, 第三个参数sign是一个md5值,
param = urllib.unquote(request.args.get(“param”, “”))
sign = urllib.unquote(request.cookies.get(“sign”)) //cookie
ip = request.remote_addr
if(waf(param)):
return “No Hacker!!!!”
task = Task(action, param, sign, ip) // 这边的action param sign参数传递到Task
return json.dumps(task.Exec()) //并且调用了 Task类中的Exec()
@app.route(‘/‘)
def index():
return open(“code.txt”,”r”).read() //这边index 把源码给打开了
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() //这边需要一个secert_key
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的flask框架,三个路由
最终目的就是读取flag.txt
所以我们需要构造参数!第一个参数action应该包含read,,第二个param参数应该是文件名
最关键就是第三个参数,由于我们不知道secert_key的值,所以不能自己加密
burp 访问一下geneSign这个页面 参数param=flag.txt得到一串MD5值b7e7fa9d237b24617caedc3fa85fe2af 这个就是md5(secert_key+flag.txtscan)的值
param 需要read 和scan的值 8a75379bb998db129cca0db40be1a07b geneSign?param=flag.txtread传入这个后得到的
也就是md5(secert_key+flag.txtreadscan)的md5值 再访问De1ta页面进行参数传递,拿到flag
De1ta?param=flag.txt
cookie: action=readscan;sign=8a75379bb998db129cca0db40be1a07b;
就能拿到flag了