[De1CTF2019]SSRF ME

问就是物理太难了 内网渗透太难了

还好这道题不难 不然我心态崩了qaq

提示flag在./flag.txt中

一进入题目就给了一大段源代码

#! /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',port=80)

代码审计就好了 我感觉没什么可说的?

仔细看代码就会发现

(算了 我想去看物理了 就随便写一下吧

我们可以利用

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

得到我们想要的hash值

例如:

在题目中我们想要读到flag.txt,那么需要在经过成功checkSign,然后将其内容scan后写到result.txt中,再经过open后放入返回的json中。

要想同时实现写和读,那么在action中要同时含有scan和read,刚好代码在实现时使用了in,所以可以将action的值设为reanscan。

首先想要通过checkSign的验证,看看checkSign

def checkSign(self):
if (getSign(self.action, self.param) == self.sign):
return True
else:
return False

再看看getSign

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

所以如果能得到我们想要的self.sign就可以通过其验证。

再看这个路由:

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

所以可以构造param为flag.txtread,这样就成了:

hashlib.md5(secert_key + flag.txtread + scan).hexdigest()=hashlib.md5(secert_key + flag.txt + readscan).hexdigest()

再将得到的hash值放入sign中:

好了。

看了下wp还可以用md5长度扩展攻击

Author: Neorah
Link: https://neorah.me/ctf/De1CTF2019/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.