[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 = action
        self.param = param
        self.sign = sign
        self.sandbox = md5(ip)
        if(not os.path.exists(self.sandbox)):
            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

@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=9999)

 

Flask框架,先看路由,geneSign是对传入的param与其他字符串拼接并返回其md5值,De1ta是主要,传入3个参数,以及ip,先判断param是否是gopher或者file开头的参数,不是则过到Task中,并且返回task的Exec()函数结果,另外hint给出提示在flag.txt中有flag

假设我们现在打开De1ta,并传参action=scan,param=flag.txt,sign=f0ab9e8d4f117d1064b85fa18f637063,一步步操作来找出条件,

先传参,再进入Exec()函数

先使用checkSign函数检验sign,所以我们需要getSign(self.action, self.param) == self.sign成立,这个可以通过geneSign来获取

然后2个if,判断scan或read 是否在action内,在则,前者是把param对应文件的内容写入result.txt,后者是把result.txt取出来并返还给我们,也就是说,这2个if我们都需要调用,这样就能获取到flag,而scan和read的判断是包含,所以我们的action就可以是readscan,或者scanread,那sign怎么获取呢?

我们要绕过getSign(self.action, self.param) == self.sign的同时还需要action为readscan,或者scanread。

在geneSign里的getsign函数

[De1CTF 2019]SSRF Me_第1张图片

他是用param和action做拼接的!而action是固定的scan ,也就是说,我们先使用geneSign,传参为flag.txtread,这样只会param+action拼接为   flag.txtreadscan,生成sign保存下来,

[De1CTF 2019]SSRF Me_第2张图片

再去De1ta中传参数param=flag.txt   action=readscan   sign=8b06f2e308dd30f742d8128ca521726b

再来看getSign(self.action, self.param) == self.sign,直接看getSign,  param+action="flag.txt" + "readscan"="flag.txtreadscan"

这样形成的sign刚好就能对应到。而这样又能将param中文件内容读出并返回给我们,以此获取flag

[De1CTF 2019]SSRF Me_第3张图片

 

 

学习了Flask框架,感觉和java反射路由都很像,因此理解也相对容易点。

 

 

 

你可能感兴趣的:(渗透笔记)