AI分析辅助破解加密的post请求参数

AI辅助分析破解加密的post请求参数

背景:计算当月平均工时的小工具突然不能用了😖,查看原因应该是请求工时数据报错了,于是F12查看请求接口,发现请求参数变成一长串字符串!!!并且所有页面响应也搜索不到这串字符串😧

image-20260713143923524

原请求参数如下:

data = {
 "hrId": "xxxxxx", 
 "locale": "zh", 
 "platform": "PC"
}

查看jsonParam什么时候构造的

大致猜测一下,可能是前端什么数据加密得到的😑,全局搜索一下jsonParam,看到前端jQuery响应加密代码,通过AI分析一下😃

image-20260713150339892

image-20260713150736563

进一步查看encodeObj的实现

image-20260713151014516

image-20260713151446140

找到ue()函数的实现

image-20260713151651352

image-20260713152726814

通过请求参数的字符串推测加密方式

自定义流密码,使用线性同余生成器(LCG)作为伪随机数生成器,与Base64后的明文进行异或(XOR)操作

算法流程:

  1. Base64编码:r = btoa(plaintext)
  2. 密钥推导:将密钥 "hr.@123" 各字符的 charCode 拼接成数字串 n
  3. LCG参数:
  • 乘数 i = 14641
  • 增量 a = 4
  • 模数 s = 2^31 - 1
  1. 随机种子:生成8位随机数 l,拼接到 n 末尾
  2. 压缩n:反复将前10位与剩余部分相加,直到长度 ≤ 10
  3. 初始化LCG:n = (i * n + a) % s
  4. XOR加密:对每个 Base64 字符,与 LCG 输出的伪随机字节异或,结果转2位十六进制
  5. 附加种子:末尾追加 l 的8位十六进制表示

关键点:密文最后8个十六进制字符就是随机种子 l,所以可以完全解密!

加密、解密算法实现

import base64
import random

def ue_decrypt(ciphertext_hex, key="hr.@123"):
    """解密 ue 函数加密的密文"""
    # 1. 提取末尾8个hex字符作为随机种子 l
    seed_hex = ciphertext_hex[-8:]
    l = int(seed_hex, 16)

    # 2. 密钥推导:将key各字符charCode拼接
    n = ""
    for g in range(len(key)):
        n += str(ord(key[g]))

    # n = "1041144664495051"

    # 3. 计算LCG参数
    o = len(n) // 5  # 3
    i = int(n[o] + n[2*o] + n[3*o] + n[4*o] + n[5*o])  # 14641
    a = -(-len(key) // 2)  # ceil(7/2) = 4
    s = (2**31) - 1  # 2147483647

    # 4. 拼接随机种子
    n = n + str(l)

    # 5. 压缩n到10位以内
    while len(n) > 10:
        n = str(int(n[:10]) + int(n[10:]))

    # 6. 初始化LCG状态
    n = (i * int(n) + a) % s

    # 7. XOR解密(去掉末尾8个hex字符=4字节种子)
    encrypted_hex = ciphertext_hex[:-8]
    # 每两个hex字符为一个字节
    encrypted_bytes = []
    for g in range(0, len(encrypted_hex), 2):
        encrypted_bytes.append(int(encrypted_hex[g:g+2], 16))

    base64_chars = []
    for g in range(len(encrypted_bytes)):
        xor_key = (n * 255) // s  # Math.floor(n / s * 255)
        original_byte = encrypted_bytes[g] ^ xor_key
        base64_chars.append(chr(original_byte))
        n = (i * n + a) % s

    base64_str = "".join(base64_chars)

    # 8. Base64解码
    plaintext = base64.b64decode(base64_str).decode("utf-8")
    return plaintext


def ue_encrypt(plaintext, key="hr.@123"):
    """加密函数,与原始ue逻辑一致"""
    # 1. Base64编码
    r = base64.b64encode(plaintext.encode("utf-8")).decode("ascii")

    # 2. 密钥推导
    n = ""
    for g in range(len(key)):
        n += str(ord(key[g]))

    # 3. LCG参数
    o = len(n) // 5
    i = int(n[o] + n[2*o] + n[3*o] + n[4*o] + n[5*o])
    a = -(-len(key) // 2)
    s = (2**31) - 1

    if i < 2:
        raise ValueError("Please choose a more complex or longer password.")

    # 4. 随机种子
    l = random.randint(0, 99999999) % 100000000

    # 5. 拼接种子
    n = n + str(l)

    # 6. 压缩n
    while len(n) > 10:
        n = str(int(n[:10]) + int(n[10:]))

    # 7. 初始化LCG
    n = (i * int(n) + a) % s

    # 8. XOR加密
    u = ""
    for g in range(len(r)):
        e = ord(r[g]) ^ ((n * 255) // s)
        c = e
        u += f"{c:02x}" if c >= 16 else f"0{c:x}"
        n = (i * n + a) % s

    # 9. 附加种子hex
    l_hex = format(l, 'x')
    while len(l_hex) < 8:
        l_hex = "0" + l_hex

    return u + l_hex


if __name__ == "__main__":
    # 测试解密
    ciphertext = "74d0ad502630f3ff92d7c3dba0d53813a04b2049ef0862dc4797c0d23542146bda07e9ed7594295499e2729ee6a22ba3a5b4b78817dcaf7ec20c04a035cbacc302dfba9f"
    plaintext = ue_decrypt(ciphertext)
    print(f"解密结果: {plaintext}")

    # 测试加密(验证加解密一致性)
    encrypted = ue_encrypt(plaintext)
    print(f"重新加密: {encrypted}")

    # 验证重新解密
    re_decrypted = ue_decrypt(encrypted)
    print(f"再次解密: {re_decrypted}")
posted @ 2026-07-13 15:49  Glory1020  阅读(12)  评论(0)    收藏  举报