De1ta2019 ssrfme

代码审计

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#! /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): //对Task类内变量进行初始化
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()): //检查`self.action+self.param+secret_key`是否与`self.sign`相等
if "scan" in self.action: //如果`scan`在`self.action`中则执行
tmpfile = open("./%s/result.txt" % self.sandbox, 'w')
//创建一个临时文件`result`
resp = scan(self.param)
//读取`self.param`的内容赋值给`resp`
if (resp == "Connection Timeout"):
result['data'] = resp
else:
print resp
tmpfile.write(resp)
tmpfile.close()
//将`resp`写入临时文件`result`中
result['code'] = 200
if "read" in self.action: //如果`read`在`self.action`中则执行
f = open("./%s/result.txt" % self.sandbox, 'r')
result['code'] = 200
result['data'] = f.read()
//将临时文件的内容赋值给`result['data']`
if result['code'] == 500:
result['data'] = "Action Error"
else:
result['code'] = 500
result['msg'] = "Sign Error"
return result //返回`result`

def checkSign(self): //检查`self.action`与`self.param`
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", "")) //以`GET`方式获取`param`
action = "scan"
return getSign(action, param) //生成`sign`值


@app.route('/De1ta',methods=['GET','POST'])
def challenge():
action = urllib.unquote(request.cookies.get("action")) //在`cookie`中获取
param = urllib.unquote(request.args.get("param", ""))
sign = urllib.unquote(request.cookies.get("sign")) //在`cookie`中获取
ip = request.remote_addr
if(waf(param)): //检查`waf`
return "No Hacker!!!!"
task = Task(action, param, sign, ip) //初始化
return json.dumps(task.Exec()) //显示`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] //读取`param`文件中的前50个字符
except:
return "Connection Timeout"



def getSign(action, param):
return hashlib.md5(secert_key + param + action).hexdigest() //进行`md5`加密生成`sign`


def md5(content):
return hashlib.md5(content).hexdigest()


def waf(param):
check=param.strip().lower()
if check.startswith("gopher") or check.startswith("file"): //检查字符串是否以`gopher`和`file`开头
return True
else:
return False


if __name__ == '__main__':
app.debug = False
app.run(host='0.0.0.0')

思路分析

代码审计后对代码有了大致的了解,我们肯定是要读取flag.txt中的内容的,在Task中的Exec()方法可以利用self.action=readscan的方式(至于为什么不是scanread之后会说)先将flag.txt中的内容写入临时文件中,再读取临时文件的内容赋值给resulr['data'],但是前面有一个检查sign值的判断条件。

前面说了将self.action赋值readscan,,而又因为self.param必须等于flag.txt,所以在checksign()getsign(self.action, self.param)==hashlib.md5(secert_key + flag.txt + readscan).hexdigest(),但是这里的生成值是无法判断的。

所以现在必须得想办法获取到生成之后的值,这里就必须利用到/geneSign路由下的geneSign方法,这里的action的值已经被确定了,但是param的值却是可控的,这里我们要构造``hashlib.md5(secert_key + flag.txtreadscan).hexdigest(),所以param的值为flag.txtread这样生成的sign`就与之前的相同了。

结果