在Python中反编译加密的pyc文件

在反编译python生成可执行文件exe时,引用的类库文件经常遇到使用Crypto 模块AES算法加密,解包生成的并不是pyc文件,而是加密的pyc. encrypted文件,当然它也无法查看编译。当然,它也是可以解密的。

解密流程

第一步,获取Crypto 的key,这是打包时由开发者指定的。解包完成后将在根目录形成名为"pyimod00_crypto_key.pyc"的文件,将它转为py文件即可查看key文件;

第二步,编写解密处理的脚本代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import glob
import zlib
import tinyaes
from pathlib import Path
 
CRYPT_BLOCK_SIZE = 16
 
# key obtained from pyimod00_crypto_key
key = bytes('MySup3rS3cr3tK3y', 'utf-8')
 
for p in Path("PYZ-00.pyz_extracted").glob("**/*.pyc.encrypted"):
    inf = open(p, 'rb') # encrypted file input
    outf = open(p.with_name(p.stem), 'wb') # output file
 
    # Initialization vector
    iv = inf.read(CRYPT_BLOCK_SIZE)
 
    cipher = tinyaes.AES(key, iv)
 
    # Decrypt and decompress
    plaintext = zlib.decompress(cipher.CTR_xcrypt_buffer(inf.read()))
 
    # Write pyc header
    # The header below is for Python 3.8
    outf.write(b'\x55\x0d\x0d\x0a\0\0\0\0\0\0\0\0\0\0\0\0')
 
    # Write decrypted data
    outf.write(plaintext)
 
    inf.close()
    outf.close()
 
    # Delete .pyc.encrypted file
    p.unlink()

 在前一步中获取的key是必须文件,否则无法进行解密;对于不同pyton版本头文件(header)也不相同,2.7~3.10如下所示:

Python 2.7: \x03\xf3\x0d\x0a\0\0\0\0

Python 3.0: \x3b\x0c\x0d\x0a\0\0\0\0

Python 3.1: \x4f\x0c\x0d\x0a\0\0\0\0

Python 3.2: \x6c\x0c\x0d\x0a\0\0\0\0

Python 3.3: \x9e\x0c\x0d\x0a\0\0\0\0\0\0\0\0

Python 3.4: \xee\x0c\x0d\x0a\0\0\0\0\0\0\0\0

Python 3.5: \x17\x0d\x0d\x0a\0\0\0\0\0\0\0\0

Python 3.6: \x33\x0d\x0d\x0a\0\0\0\0\0\0\0\0

Python 3.7: \x42\x0d\x0d\x0a\0\0\0\0\0\0\0\0\0\0\0\0

Python 3.8: \x55\x0d\x0d\x0a\0\0\0\0\0\0\0\0\0\0\0\0

Python 3.9: \x61\x0d\x0d\x0a\0\0\0\0\0\0\0\0\0\0\0\0

Python 3.10: \x6f\x0d\x0d\x0a\0\0\0\0\0\0\0\0\0\0\0\0

第三步,执行脚本文件,即可将加密的pyc. encrypted文件转成不加密的pyc文件。

参考资料

python exe文件解包方法总结-软件逆向

Frequently Asked Questions · extremecoders-re/pyinstxtractor Wiki · GitHub

[CISCN2021]华北区_ctf_re_imnotavirus - 简书 (jianshu.com)

posted @   我也是个傻瓜  阅读(4068)  评论(3编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
历史上的今天:
2020-02-14 CefSharp应用——High DPI问题
2015-02-14 2014年抢票总结
点击右上角即可分享
微信分享提示