diff --git a/pywxdump/wx_info/decryption.py b/pywxdump/wx_info/decryption.py index d4436c0..e913864 100644 --- a/pywxdump/wx_info/decryption.py +++ b/pywxdump/wx_info/decryption.py @@ -8,7 +8,6 @@ # 加密文件的每一个页都有一个随机的初始化向量,它被保存在每一页的末尾。 # 加密文件的每一页都存有着消息认证码,算法使用的是HMAC-SHA1(安卓数据库使用的是SHA512)。它也被保存在每一页的末尾。 # 每一个数据库文件的开头16字节都保存了一段唯一且随机的盐值,作为HMAC的验证和数据的解密。 -# 用来计算HMAC的key与解密的key是不同的,解密用的密钥是主密钥和之前提到的16字节的盐值通过PKCS5_PBKF2_HMAC1密钥扩展算法迭代64000次计算得到的。而计算HMAC的密钥是刚提到的解密密钥和16字节盐值异或0x3a的值通过PKCS5_PBKF2_HMAC1密钥扩展算法迭代2次计算得到的。 # 为了保证数据部分长度是16字节即AES块大小的整倍数,每一页的末尾将填充一段空字节,使得保留字段的长度为48字节。 # 综上,加密文件结构为第一页4KB数据前16字节为盐值,紧接着4032字节数据,再加上16字节IV和20字节HMAC以及12字节空字节;而后的页均是4048字节长度的加密数据段和48字节的保留段。 # ------------------------------------------------------------------------------- @@ -24,7 +23,6 @@ SQLITE_FILE_HEADER = "SQLite format 3\x00" # SQLite文件头 KEY_SIZE = 32 DEFAULT_PAGESIZE = 4096 -DEFAULT_ITER = 64000 # 通过密钥解密数据库 @@ -53,33 +51,25 @@ def decrypt(key: str, db_path, out_path): return False, f"[-] db_path:'{db_path}' {e}!" salt = blist[:16] - byteKey = hashlib.pbkdf2_hmac("sha1", password, salt, DEFAULT_ITER, KEY_SIZE) - first = blist[16:DEFAULT_PAGESIZE] + first = blist[16:4096] if len(salt) != 16: return False, f"[-] db_path:'{db_path}' File Error!" - mac_salt = bytes([(salt[i] ^ 58) for i in range(16)]) - mac_key = hashlib.pbkdf2_hmac("sha1", byteKey, mac_salt, 2, KEY_SIZE) - hash_mac = hmac.new(mac_key, first[:-32], hashlib.sha1) + byteHmac = hashlib.pbkdf2_hmac("sha1", password, salt, 64000, KEY_SIZE) + mac_key = hashlib.pbkdf2_hmac("sha1", byteHmac, mac_salt, 2, KEY_SIZE) + hash_mac = hmac.new(mac_key, blist[16:4064], hashlib.sha1) hash_mac.update(b'\x01\x00\x00\x00') if hash_mac.digest() != first[-32:-12]: return False, f"[-] Key Error! (key:'{key}'; db_path:'{db_path}'; out_path:'{out_path}' )" - newblist = [blist[i:i + DEFAULT_PAGESIZE] for i in range(DEFAULT_PAGESIZE, len(blist), DEFAULT_PAGESIZE)] - with open(out_path, "wb") as deFile: deFile.write(SQLITE_FILE_HEADER.encode()) - t = AES.new(byteKey, AES.MODE_CBC, first[-48:-32]) - decrypted = t.decrypt(first[:-48]) - deFile.write(decrypted) - deFile.write(first[-48:]) + for i in range(0, len(blist), 4096): + tblist = blist[i:i + 4096] if i > 0 else blist[16:i + 4096] + deFile.write(AES.new(byteHmac, AES.MODE_CBC, tblist[-48:-32]).decrypt(tblist[:-48])) + deFile.write(tblist[-48:]) - for i in newblist: - t = AES.new(byteKey, AES.MODE_CBC, i[-48:-32]) - decrypted = t.decrypt(i[:-48]) - deFile.write(decrypted) - deFile.write(i[-48:]) return True, [db_path, out_path, key] @@ -164,22 +154,3 @@ def batch_decrypt(key: str, db_path: Union[str, List[str]], out_path: str, is_lo print(f"[+] 共 {len(result)} 个文件, 成功 {success_count} 个, 失败 {fail_count} 个") print("=" * 32) return True, result - - -def encrypt(key: str, db_path, out_path): - """ - 通过密钥加密数据库 - :param key: 密钥 64位16进制字符串 - :param db_path: 待加密的数据库路径(必须是文件) - :param out_path: 加密后的数据库输出路径(必须是文件) - :return: - """ - import subprocess - if not os.path.exists(db_path) or not os.path.isfile(db_path): - return False, f"[-] db_path:'{db_path}' File not found!" - if not os.path.exists(os.path.dirname(out_path)): - return False, f"[-] out_path:'{out_path}' File not found!" - - if len(key) != 64: - return False, f"[-] key:'{key}' Len Error!" - return False, f"error!"