知识点

  • python代码审计
  • SSRF
  • 哈希长度拓展攻击

WP

首先进入环境,是一串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)):

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

然后就是python的代码审计了。这题自己没做出来的主要原因是自己PHP审的挺多的,但是python审的太少了,对于python中可能出现的问题并没有敏锐地察觉到。

主要的问题在这里:

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"

print(resp)并不会真正的回显,而是回显到那个运行这个python代码的主机的命令行里,因此我们要想办法得到回显,就必须read文件。
而这里的判断用的是in,而不是==。因此就可以考虑action是readscan这样的,因此也就引出了这题的三种解法(其实可以看成2种)

解法一:字符串拼接

虽然secert_key我们不知道,但是产生sign的是这样的:

secert_key + param + scan

然后再md5,而我们传入的是这样的:

secert_key + param + readscan

因此可以产生sign中param是flag.txtread,下面的param是flag.txt:

在这里插入图片描述
在这里插入图片描述

解法二:哈希长度拓展攻击

原理请参考下面这篇文章:
Hash Length Extension Attack
利用的话就是利用hashpump工具:
HashPump

先获得param是flag.txt的时候的md5值:
在这里插入图片描述
然后利用工具:

root@iZbp14tgce8absspjkxi3iZ:~/ctf/tools/HashPump# hashpump
Input Signature: 4e345490ee4069a597e5da48310878ca
Input Data: scan
Input Key Length: 24
Input Data to Add: read
a26732f28dda1aff57cc352375a7d311
scan\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xe0\x00\x00\x00\x00\x00\x00\x00read

再把\x换成%:

str=r'scan\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xe0\x00\x00\x00\x00\x00\x00\x00read'
str=str.replace(r"\x","%")
print(str)

在这里插入图片描述

解法三:利用local_file来绕过waf

其实这个解法和解法一没什么太大的区别,唯一的区别就是姿势非常的新,很有意思,可以学到新姿势。
首先就是利用local_file来过滤waf对file的过滤,但这里最好就是使用相对路径,是local_file:

在这里插入图片描述

在这里插入图片描述
使用绝对路径的话需要一个骚姿势:

/proc/self/cwd/flag.txt

只能说,知道这个姿势的,用起来就很舒服。

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐