[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_ey = os.urandom(16)

class Task:
    def __init__(self, action, param, sign, ip):#python的构造方法

        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):#定义的命令执行函数,此处调用了scan这个自定义的函数

        result = {}

        result['code'] = 500

        if (self.checkSign()):

            if "scan" in self.action:#action要写scan

                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:#action要加read

                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):#这个是关键点,此处注意顺序先是param后是action

    return hashlib.md5(secert_key + param + action).hexdigest()

def md5(content):

    return hashlib.md5(content).hexdigest()

def waf(param):#这个waf貌似没什么用

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

先看三个参数:

action = urllib.unquote(request.cookies.get("action"))
param = urllib.unquote(request.args.get("param", ""))
sign = urllib.unquote(request.cookies.get("sign"))

action,sign用cookie传,paramGET传。

两个页面:

/geneSign,/De1ta

def geneSign():
    param = urllib.unquote(request.args.get("param", ""))
    action = "scan"
    return getSign(action, param)

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

校验:

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

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

也就是说,我们传入三个参数action,sign,param到 /De1ta 页面,然后执行task.Exec()函数

进去之后: if (self.checkSign()) 进行校验。

getSign返回 secret_key+param+action的md5值的16进制字符串

我们不知道secret_key,但是在 /geneSign 我们可以获得一个getSign返回值。

且getSign函数的加密顺序是,key+param+action

这里相当于拼接字符串,题目提示flag在./flag.txt,且geneSign()自带action :scan

所以构造:

/geneSign?param=flag.txtread,可以获取正确的getSign返回值:

param=flag.txt,action=readscan,和一个正确的sign

然后构造:

/De1ta?param=flag.txt

cookie:readscan;sign:a91e34188f892b2904d7f3b10410df4c

成功获得flag

吐了,python代码看起来还是费劲。

代码审计,审着审着就会了。

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇