js hook AES-CBC 并用python代码代替解密

部署运行你感兴趣的模型镜像
(function() {
    // 保存原始的decrypt函数引用
    const originalDecrypt = crypto.subtle.decrypt;

    // 重写decrypt函数
    crypto.subtle.decrypt = async function(algorithm, key, data) {
        // 在解密之前执行你的代码
        console.log('Decrypting data...');
        debugger;
        // 假设你在断点处
        // 查看algorithm对象
        console.log('Algorithm object:', algorithm);

        // 如果algorithm是AES-CBC,你可以这样获取iv
        const iv = algorithm.iv;
        console.log('Initialization Vector (IV):', new Uint8Array(iv)); // 将iv转换为Uint8Array以便查看

        // 查看data
        const encryptedData = new Uint8Array(data);
        console.log('Encrypted data:', encryptedData); // 将data转换为Uint8Array以便查看

        // 导出key(这需要原始的CryptoKey是可导出的)
        crypto.subtle.exportKey('raw', key).then(exportedKey => {
            console.log('Exported key:', new Uint8Array(exportedKey)); // 将导出的密钥转换为Uint8Array以便查看
        }).catch(error => {
            console.error('Key export failed:', error);
        });
        // 调用原始的decrypt函数进行解密
        const result = await originalDecrypt.apply(this, arguments);

        // 在解密之后执行你的代码
        console.log('Data decrypted.');

        // 返回解密结果
        return result;
    };
})();

我们需要的参数如下,通过脚本可以断点并获取值:

  • iv = b'...' # 初始化向量(从JavaScript中获取)
  • encrypted_data = b'...' # 要解密的数据(从JavaScript中获取,也可以从接口获取)
  • key = b'...' # 密钥(从JavaScript中获取)

转换成python代码:

from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad

# JavaScript对象转换为Python字节序列
iv = bytes([57, 233, 12, 46, 56, 33, 70, 15, 47, 149, 127, 207, 122, 98, 220, 249])  # 替换你hook的值
data = bytes([
    21, 78, 114, 236, 56, 217, 136, 85, 191, 7, 36, 20, 10, 30, 147, 102,
    231, 4, 127, 144, 17, 36, 62, 31, 145, 41, 3, 213, 237, 150, 184, 195,
    108, 187, 30, 241, 205, 52, 5, 91, 225, 54, 138, 63, 59, 57, 211, 169,
    243, 46, 65, 237, 8, 190, 186, 81, 43, 241, 194, 162, 61, 30, 227, 146
])  # 替换你hook的值
key = bytes([74, 53, 219, 97, 50, 91, 239, 53, 232, 81, 58, 18, 137, 197, 11, 220])   # 替换你hook的值

# 创建一个AES-CBC解密器
cipher = AES.new(key, AES.MODE_CBC, iv)

# 解密数据并去除填充
try:
    original_data = unpad(cipher.decrypt(data), AES.block_size)
    print('Decrypted data:', original_data)
except ValueError as e:
    print('Unpad error:', e)

但是我们从接口拿到的,一般是base64格式,需要转换成bytes格式:

import base64

# Base64编码的字符串
base64_data = "1d0KbgHFAg6tvG/pSUlIUfrN6N+5VP+XLB+FH6m/aOfAmDTzelb/77oaBKHP7fsylmvaQr4j1TB3Jn106PPAyHBZNDZWsp0Kh27BCl3Km0jBzoc2Y5LmQjZAR9pAb426"

# 解码Base64字符串
data = base64.b64decode(base64_data)

您可能感兴趣的与本文相关的镜像

Python3.10

Python3.10

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

### MMTLS 协议中使用的 AES 加密模式 MMTLS(Mobile Messaging Transport Layer Security)是微信基于 TLS 协议优化的一种自定义安全通信协议,用于保障微信客户端与服务端之间的数据传输安全。在 MMTLS 协议中,加密算法主要采用 AES(Advanced Encryption Standard),其操作模式通常为 AES-GCM 或 AES-CBC,具体取决于握手阶段协商的加密套件[^1]。 AES-GCM(Galois/Counter Mode)是一种结合计数器模式(CTR)与 Galois 消息认证码(GMAC)的加密模式,能够同时提供数据加密与完整性验证,适用于需要高性能和低延迟的移动通信场景。而 AES-CBC(Cipher Block Chaining)则是一种经典的分组加密模式,通过前一个密文块与当前明文块进行异或后再加密,以增强加密数据的随机性。 由于 MMTLS 是微信私有协议,其具体加密细节并未完全公开,但根据逆向分析和相关研究,MMTLS 握手阶段通常使用 ECDH 进行密钥交换,并通过 HKDF 算法派生出用于 AES-GCM 或 AES-CBC 的密钥和初始向量(IV)[^2]。 ### 实现对应的解密算法 要实现 MMTLS 中的 AES 解密,首先需要获取以下关键参数: - 加密密钥(key) - 初始向量(IV) - 数据长度信息(如 AEAD 加密中的附加认证数据 AAD) - 使用的加密模式(如 AES-GCM) 以下是一个基于 PythonAES-GCM 解密示例,适用于已知 key、IV 和认证数据的场景: ```python from Crypto.Cipher import AES from Crypto.Util import Counter def decrypt_aes_gcm(ciphertext, aad, tag, key, nonce): cipher = AES.new(key, AES.MODE_GCM, nonce=nonce) cipher.update(aad) plaintext = cipher.decrypt_and_verify(ciphertext, tag) return plaintext # 示例参数 key = bytes.fromhex("2e3d7c6b4a5f1e2a3c8d9e7f0a1b2c3d") nonce = bytes.fromhex("a1b2c3d4e5f67890") aad = bytes.fromhex("00112233445566778899aabbccddeeff") ciphertext = bytes.fromhex("c8c45d1b2e3f8a7d4c1b6e3f8d7c2b1a") tag = bytes.fromhex("5a6b3c8d4e2f1a9e") plaintext = decrypt_aes_gcm(ciphertext, aad, tag, key, nonce) print(plaintext.hex()) ``` 若使用 AES-CBC 模式,则可以采用以下代码: ```python from Crypto.Cipher import AES from Crypto.Util.Padding import unpad def decrypt_aes_cbc(ciphertext, key, iv): cipher = AES.new(key, AES.MODE_CBC, iv=iv) decrypted = cipher.decrypt(ciphertext) return unpad(decrypted, AES.block_size) # 示例参数 key = bytes.fromhex("2e3d7c6b4a5f1e2a3c8d9e7f0a1b2c3d") iv = bytes.fromhex("a1b2c3d4e5f678901234567890abcdef") ciphertext = bytes.fromhex("c8c45d1b2e3f8a7d4c1b6e3f8d7c2b1a") plaintext = decrypt_aes_cbc(ciphertext, key, iv) print(plaintext.hex()) ``` ### 注意事项 - 在实际使用中,密钥和 IV 需从 MMTLS 握手过程中提取,通常可以通过 Hook 微信客户端中的密钥派生函数来获取。 - 若使用 AES-GCM 模式,需确保提供正确的 AAD 和 tag 值,否则验证将失败。 - 解密前需去除协议头部信息(如 MMTLS 的 header)以提取加密数据体。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值