当前位置: 代码迷 >> 综合 >> buuctf [De1CTF 2019]SSRF Me
  详细解决方案

buuctf [De1CTF 2019]SSRF Me

热度:99   发布时间:2023-11-24 20:34:36.0

[De1CTF 2019]SSRF Me

打开题目发现一堆不知名乱码
在这里插入图片描述

不过仔细观察发现应该是python源码

#! /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 = actionself.param = paramself.sign = signself.sandbox = md5(ip)if(not os.path.exists(self.sandbox)):          #SandBox For Remote_Addros.mkdir(self.sandbox)def Exec(self):result = {
    }result['code'] = 500if (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'] = respelse:print resptmpfile.write(resp)tmpfile.close()result['code'] = 200if "read" in self.action:f = open("./%s/result.txt" % self.sandbox, 'r')result['code'] = 200result['data'] = f.read()if result['code'] == 500:result['data'] = "Action Error"else:result['code'] = 500result['msg'] = "Sign Error"return resultdef checkSign(self):if (getSign(self.action, self.param) == self.sign):return Trueelse: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_addrif(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 Trueelse:return Falseif __name__ == '__main__':app.debug = Falseapp.run(host='0.0.0.0',port=80)

代码审计:
Flask框架下 有三个路由
/geneSign:用于测试页面返回的是md5加密后的secert_key + param + action

#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)

/De1ta:获取一系列的数据,waf(param)过滤了gopher和file,生成Task对象,调用Exec()命令执行函数去读取文件

@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_addrif(waf(param)):return "No Hacker!!!!"task = Task(action, param, sign, ip)return json.dumps(task.Exec())

/:get返回源代码
就是一打开题目看到的那一堆源码

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

waf:禁止了flie和gopher协议
if判断

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

传入的sign要和getSign()生成的sign要一致
可以访问/geneSign路由get上传?param=flag.txtread
访问/geneSign得到的就是当action=readscan param=flag.txt时 页面返回的md5加密后的值
param=flag.txt, action=readscan和param=flag.txtread, action=scan
最后的哈希值应该是一样的
因为action在生成的时候已经固定为scan了 所以只需要param=readscan
解题:
构造sign值
访问url/geneSign,构造param=flag.txtread,通过action获得对应哈希值
在这里插入图片描述访问url/De1ta?param=flag.txt
添加cookie:sign=哈希值,action=readscan绕过checkSign检查cookie中的getSign(self.action, self.param) == self.sign)
在这里插入图片描述
就达到利用exce()读取flag.txt
在这里插入图片描述