reverse-skills-max: initial public release (restored, redacted, hardened)
This commit is contained in:
@@ -0,0 +1,168 @@
|
||||
# Python 打包产物最小解析示例
|
||||
|
||||
以下示例以本机 Python 3.14.7 与 PyInstaller 6.22.2 为标本,命令与输出可原样复现(工具链:`python3`、`pip install pyinstaller pyinstxtractor-ng`;系统 pip 受 PEP 668 限制时先 `python3 -m venv` 再装)。结构定义见 [[layout]]。
|
||||
|
||||
## 1. pyc 头解析(python3 本机可复现)
|
||||
|
||||
### 生成 pyc
|
||||
|
||||
```sh
|
||||
python3 -c "import py_compile; py_compile.compile('sample.py', cfile='sample.pyc')"
|
||||
# sample.py 内容:import base64
|
||||
# k = base64.b64decode("czNjcmV0")
|
||||
# print(k)
|
||||
```
|
||||
|
||||
### struct 解析头 + marshal 验证 + 反汇编
|
||||
|
||||
```python
|
||||
import struct, marshal, dis
|
||||
|
||||
d = open('sample.pyc', 'rb').read()
|
||||
magic, flags, mtime, size = struct.unpack('<IIII', d[:16])
|
||||
print('magic=0x%08x flags=%d mtime=%d size=%d' % (magic, flags, mtime, size))
|
||||
print('code 对象从偏移 16 开始,类型字节 0x%02x' % d[16])
|
||||
code = marshal.loads(d[16:]) # 与解释器同版本的权威解析
|
||||
print('argcount=%d nlocals=%d stacksize=%d' % (code.co_argcount, code.co_nlocals, code.co_stacksize))
|
||||
dis.dis(code)
|
||||
```
|
||||
|
||||
本机实测输出:
|
||||
|
||||
```
|
||||
magic=0x0a0d0e2b flags=0 mtime=1787648948 size=56
|
||||
code 对象从偏移 16 开始,类型字节 0xe3
|
||||
argcount=0 nlocals=0 stacksize=3
|
||||
0 RESUME 0
|
||||
1 LOAD_SMALL_INT 0
|
||||
LOAD_CONST 1 (None)
|
||||
IMPORT_NAME 0 (base64)
|
||||
STORE_NAME 0 (base64)
|
||||
...
|
||||
```
|
||||
|
||||
对照要点:`magic` 低 2 字节 `0x0e2b` = 3.14;`flags=0` = timestamp 校验;`size=56` = 源文件字节数;类型字节 `0xe3` = `0x63|0x80`(code + REF 标志),PyInstaller 解出的条目标 `0x63`——两种都合法,别拿类型字节判损坏。
|
||||
|
||||
### 字节样例(pyc 头 16 字节,逐字段标注)
|
||||
|
||||
```
|
||||
00000000: 2b0e 0d0a 0000 0000 9e5b 8d6a 3800 0000 +......[.j8...
|
||||
magic=0x0a0d0e2b─┘ └flags=0 └size=56(0x38)
|
||||
mtime=0x6a8d5b9e──────┘
|
||||
00000010: e300 0000 0000 0000 0000 0000 0003 0000 ................
|
||||
类型0xe3(code+REF) argcount=0 posonly=0 kwonly=0 nlocals=3
|
||||
```
|
||||
|
||||
手工核对:`xxd sample.pyc | head -2` 与上述一致;PyInstaller 解出的 pyc 头 mtime/size 为 0(清零正常,见 [[layout]] 头字段表)。
|
||||
|
||||
## 2. PyInstaller onefile:构建 → 解包 → cookie 解析
|
||||
|
||||
### 构建与解包
|
||||
|
||||
```sh
|
||||
pip install pyinstaller pyinstxtractor-ng
|
||||
pyinstaller --onefile sample_app.py
|
||||
python3 -m pyinstxtractor_ng dist/sample_app
|
||||
```
|
||||
|
||||
本机实测解包输出(节选):
|
||||
|
||||
```
|
||||
[+] Possible entry point: sample_app.pyc
|
||||
[+] Found 130 files in PYZ archive
|
||||
[+] Successfully extracted pyinstaller archive: dist/sample_app
|
||||
```
|
||||
|
||||
产物结构见 [[layout]] §2 末;入口 pyc 头 `2b 0e 0d 0a | 00000000 | 00000000 | 00000000`——flags/mtime/size 全 0(清零正常),code 对象从 0x10 开始(`63 00 00 00 ...`)。
|
||||
|
||||
### CArchive cookie 手工解析(大端,`!8sIIII64s`)
|
||||
|
||||
```python
|
||||
import struct
|
||||
|
||||
d = open('dist/sample_app', 'rb').read()
|
||||
magic = b'MEI\x0c\x0b\x0a\x0b\x0e'
|
||||
i = d.rfind(magic) # cookie 在归档末尾,取最后一次出现
|
||||
ln, toc, toclen, pyver = struct.unpack_from('!IIII', d, i + 8)
|
||||
name = d[i + 24:i + 88].split(b'\0')[0].decode()
|
||||
start = i + 88 - ln # 归档起点 = cookie 尾 − archive_length
|
||||
print('cookie @0x%x' % i)
|
||||
print('archive_length=%d toc_offset=0x%x toc_length=%d pyver=%d' % (ln, toc, toclen, pyver))
|
||||
print('pylib=%s archive 起点=0x%x' % (name, start))
|
||||
```
|
||||
|
||||
本机实测输出:
|
||||
|
||||
```
|
||||
cookie @0x974549
|
||||
archive_length=9847480 toc_offset=0x963220 toc_length=4160 pyver=314
|
||||
pylib=libpython3.14.so.1.0 archive 起点=0x102e9
|
||||
```
|
||||
|
||||
对照要点:pyver=314 即 Python 3.14(`major*100+minor`);`toc_offset + toc_length + 88 == archive_length`(实测 0x963220+4160+88=9847480 吻合);onefile 下归档起点之前是 bootloader——所有归档内偏移都要加该起点换算;**起点 = cookie 尾 − archive_length**(cookie 自身 88 字节不算进归档,算错会整体偏移 88 字节)。
|
||||
|
||||
### TOC 条目遍历(变长,16 字节对齐)
|
||||
|
||||
```python
|
||||
import struct
|
||||
|
||||
d = open('dist/sample_app', 'rb').read()
|
||||
magic = b'MEI\x0c\x0b\x0a\x0b\x0e'
|
||||
i = d.rfind(magic)
|
||||
ln, toc, toclen, pyver = struct.unpack_from('!IIII', d, i + 8)
|
||||
start = i + 88 - ln # 归档起点
|
||||
pos = start + toc
|
||||
end = pos + toclen
|
||||
while pos < end:
|
||||
entry_len, off, clen, dlen, comp, tc = struct.unpack_from('!IIIIBc', d, pos)
|
||||
name = d[pos + 18:pos + entry_len].split(b'\0')[0].decode('latin1')
|
||||
print('%-26s type=%s off=0x%-7x clen=%-7d' % (name[:26], tc, off, clen))
|
||||
pos += entry_len
|
||||
```
|
||||
|
||||
本机实测输出(节选):
|
||||
|
||||
```
|
||||
struct type=m off=0x0 clen=232
|
||||
pyimod01_archive type=m off=0xe8 clen=2896
|
||||
pyimod02_importers type=m off=0xc38 clen=13829
|
||||
pyimod03_ctypes type=m off=0x423d clen=2874
|
||||
pyiboot01_bootstrap type=s off=0x4d77 clen=1158
|
||||
pyi_rth_inspect type=s off=0x51fd clen=1486
|
||||
sample_app type=s off=0x57cb clen=366
|
||||
libbrotlicommon.so.1 type=b off=0x5939 clen=63578
|
||||
...(共 58 条,遍历终点 == cookie 位置 0x974549)
|
||||
base_library.zip type=z off=0x761256 clen=415154
|
||||
PYZ.pyz type=z off=0x7c6808 clen=1690136
|
||||
```
|
||||
|
||||
对照要点:`type=m` = 内嵌引导模块(zlib 压缩 pyc 块)、`s` = 脚本类(入口/启动钩子)、`b` = 二进制数据、`z` = PYZ 类条目(含 base_library.zip 与 PYZ.pyz);名字无扩展名且是变长的——按 `entry_length` 跳(条目对齐到 16 字节),不能按定长扫。
|
||||
|
||||
## 3. PYZ 头解析
|
||||
|
||||
```python
|
||||
import struct
|
||||
d = open('sample_app_extracted/PYZ.pyz', 'rb').read()
|
||||
print('MAGIC=%r pyc_magic=%s toc_offset=0x%x' % (
|
||||
d[:4], d[4:8].hex(), struct.unpack('!i', d[8:12])[0]))
|
||||
```
|
||||
|
||||
本机实测输出:
|
||||
|
||||
```
|
||||
MAGIC=b'PYZ\x00' pyc_magic=2b0e0d0a toc_offset=0x19b92b
|
||||
```
|
||||
|
||||
对照要点:PYZ 头 = `PYZ\0` + pyc magic(同 pyc 版本判定)+ 大端 TOC 偏移;TOC 在文件尾部(marshal 列表),模块条目按 (name, (typecode, offset, length)) 索引 zlib 压缩的 code 对象。
|
||||
|
||||
## 实现教训(内化)
|
||||
|
||||
- 版本先于一切:pyc magic → marshal 版本 → PyInstaller pyver 三处分别判版本,混用即错位
|
||||
- 归档偏移是相对的:onefile 的 cookie 用 `rfind` 定位,归档起点 = cookie − archive_length
|
||||
- TOC 条目按 entry_length 跳,16 对齐,别定长扫
|
||||
- 官方 `marshal.loads` + `dis` 是权威解析路径;手写解析只到头部字段为止
|
||||
|
||||
## 使用注意
|
||||
|
||||
- 样例为本地构建产物,仅作结构对照;分析真实样本以样本自身头字段为准([[layout]] 各表)
|
||||
- 动态取明文(进入桩解密窗口内抓取)在沙箱内执行([[re-sandbox]],见 [[re-analyze/platform-tips]])
|
||||
@@ -0,0 +1,125 @@
|
||||
# Python 打包产物布局:pyc / PyInstaller / PyArmor
|
||||
|
||||
三类产物的分析入口不同:`.pyc` 直接按头 + marshal 解析;PyInstaller 先按 CArchive/PYZ 解出 pyc 再走前一条;PyArmor 是「外壳 pyc + native runtime」组合,结构与版本强绑定。本文件字段值以本机实测(Python 3.14.7 / PyInstaller 6.22.2)为准,版本差异单独标注。
|
||||
|
||||
## 1. pyc 文件布局
|
||||
|
||||
pyc = 16 字节头 + 一个 marshal 序列化的 code 对象(3.7+ 统一;3.7 前头为 12 字节)。
|
||||
|
||||
### 头字段(16 字节,3.7+)
|
||||
|
||||
| 偏移 | 名称 | 长度 | 说明 |
|
||||
|---|---|---|---|
|
||||
| 0x00 | magic | 4 | `2b 0e 0d 0a`(3.14 实测)或小端 u16 读取(0x0e2b);版本判定依据 |
|
||||
| 0x04 | flags | 4 | 0=timestamp 校验;低 1 位=1 为 hash-based(PEP 552,mtime/size 无意义) |
|
||||
| 0x08 | mtime | 4 | 源文件 mtime;PyInstaller 写 PYZ 时清零(非损坏) |
|
||||
| 0x0c | size | 4 | 源文件字节数;同被 PyInstaller 清零 |
|
||||
| 0x10 | code 对象 | — | marshal 流从此开始 |
|
||||
|
||||
### magic 版本对照(小端 u16)
|
||||
|
||||
| 值 | 版本 | 值 | 版本 |
|
||||
|---|---|---|---|
|
||||
| 0x0d33 | 3.6 | 0x0d6f | 3.10 |
|
||||
| 0x0d42 | 3.7 | 0x0da7 | 3.11 |
|
||||
| 0x0d55 | 3.8 | 0x0dcb | 3.12 |
|
||||
| 0x0d61 | 3.9 | 0x0e2b | 3.14(本机实测) |
|
||||
|
||||
3.13 及以后:不写死数值,按目标机器换算——`python3 -c "import importlib.util; print(hex(int.from_bytes(importlib.util.MAGIC_NUMBER[:2],'little')))"`。
|
||||
|
||||
### marshal code 对象概览
|
||||
|
||||
- code 对象 marshal 类型字节 = `0x63`('c');带引用标志 `FLAG_REF`(0x80) 时为 `0xe3`——`py_compile` 产物顶层为 `0xe3`,PyInstaller 解出条目标 `0x63`,两种都合法
|
||||
- 3.14 起 marshal 版本为 5(`marshal.version`),code 对象内部字段布局与旧版不同——手写 marshal 解析必须按 magic 版本分派,别假设字段偏移
|
||||
- 反编译兜底一律用匹配版本的官方 `marshal.loads` + `dis`,而不是手写解析器
|
||||
|
||||
## 2. PyInstaller 布局
|
||||
|
||||
### 产物形态
|
||||
|
||||
```
|
||||
onefile: [bootloader(ELF/PE/Mach-O)] [CArchive 数据] [cookie]
|
||||
onedir : 程序目录 = bootloader 可执行文件 + 同目录 _internal/(内含 PKG 归档或散文件 + PYZ + 依赖)
|
||||
```
|
||||
|
||||
- 两种形态都含一个 PYZ(模块字节码归档)与一个 PKG/CArchive(其余资源)
|
||||
- onefile 运行时把 CArchive 解到 `_MEIxxxxxx` 临时目录再执行(`strings` 里的 `_MEI` 特征即此)
|
||||
|
||||
### CArchive cookie(归档尾,88 字节,大端)
|
||||
|
||||
格式 `!8sIIII64s`(实测 PyInstaller 6.22.2 源码 `_COOKIE_FORMAT`):
|
||||
|
||||
| 偏移 | 名称 | 长度 | 说明 |
|
||||
|---|---|---|---|
|
||||
| 0x00 | MAGIC | 8 | `MEI\x0c\x0b\x0a\x0b\x0e` |
|
||||
| 0x08 | archive_length | 4 | 归档总长(= toc_offset + toc_len + 88) |
|
||||
| 0x0c | toc_offset | 4 | TOC 起始偏移(相对归档起始) |
|
||||
| 0x10 | toc_length | 4 | TOC 字节数 |
|
||||
| 0x14 | pyvers | 4 | `major*100+minor`(314 = Python 3.14,实测) |
|
||||
| 0x18 | pylib_name | 64 | Python 动态库名(`libpython3.14.so.1.0`,实测) |
|
||||
|
||||
定位:在文件中扫描 MAGIC,取最后一次出现(cookie 在归档末尾);onefile 下归档前是 bootloader,**归档起点 = cookie 位置 + 88 − archive_length**(cookie 自身不算进归档,少加 88 会整体偏移)。
|
||||
|
||||
### CArchive TOC 条目(变长,对齐 16)
|
||||
|
||||
条目 = 18 字节头(`!IIIIBc`,大端)+ UTF-8 名字(含 NUL),整条目补齐到 16 字节:
|
||||
|
||||
| 偏移 | 名称 | 长度 | 说明 |
|
||||
|---|---|---|---|
|
||||
| 0x00 | entry_length | 4 | 本条目总长(含名字与补齐) |
|
||||
| 0x04 | data_offset | 4 | 数据相对归档起始的偏移 |
|
||||
| 0x08 | compressed_length | 4 | 压缩后长度 |
|
||||
| 0x0c | data_length | 4 | 原始长度 |
|
||||
| 0x10 | compress | 1 | 0=不压缩,1=zlib |
|
||||
| 0x11 | typecode | 1 | 实测:'m'=内嵌引导模块(zlib 压缩 pyc)、's'=脚本类(入口/启动钩子)、'b'=二进制数据、'z'=PYZ 类条目(含 base_library.zip 与 PYZ.pyz) |
|
||||
| 0x12 | name | 变长 | UTF-8 + NUL 结尾(名字无扩展名),条目 16 对齐 |
|
||||
|
||||
### PYZ(模块字节码归档)
|
||||
|
||||
头 16 字节 + 模块数据 + 尾部 TOC(marshal list):
|
||||
|
||||
| 偏移 | 名称 | 说明 |
|
||||
|---|---|---|
|
||||
| 0x00 | MAGIC | `PYZ\0`(4 字节) |
|
||||
| 0x04 | pyc magic | 与 pyc 相同的字节码 magic(实测 `2b 0e 0d 0a`) |
|
||||
| 0x08 | toc_offset | 大端 i32,TOC 相对 PYZ 起始的偏移 |
|
||||
| 0x0c | 保留 | 4 字节 |
|
||||
|
||||
- 模块条目:`zlib.compress(marshal.dumps(code_object))` 顺序写,无独立头
|
||||
- TOC:marshal 序列化的 `(name, (typecode, offset, length))` 列表,位于文件尾部;typecode 有模块(PYMODULE)/包(PKG,`__init__`)/命名空间包(NSPKG,0 长度)
|
||||
- pyinstxtractor-ng 解包 PYZ 后为 `PYZ.pyz_extracted/` 目录,每个模块重建 16 字节 pyc 头(mtime/size 为 0)
|
||||
|
||||
### 解包产物结构(pyinstxtractor-ng,实测)
|
||||
|
||||
```
|
||||
sample_app_extracted/
|
||||
├── sample_app.pyc # 入口脚本(code 对象从 0x10 开始)
|
||||
├── PYZ.pyz # 模块归档
|
||||
├── PYZ.pyz_extracted/ # 解出的模块 pyc
|
||||
├── python3.14/ # 版本目录
|
||||
│ └── lib-dynload/ # 扩展模块 .so
|
||||
├── base_library.zip # 标准库 zip
|
||||
└── lib*.so.1.0 # 运行时依赖
|
||||
```
|
||||
|
||||
## 3. PyArmor 布局(版本强绑定)
|
||||
|
||||
PyArmor 产物形态随版本变化(外壳结构/密钥派生/加密算法都变过),以下为通用骨架,细节按版本核实:
|
||||
|
||||
- 加固后的 pyc 是「外壳」:真实字节码加密,运行期由 `pyarmor_runtime`(随样本交付的 native 扩展)解密执行
|
||||
- 产物含大量无引用的包装函数(confusion code)干扰静态分析
|
||||
- 常见三件套形态:加密数据(字节串常量)+ 引导调用 + native 解密组件;函数级加解密窗口极短(进入桩解密、执行、离开桩重加密)
|
||||
- 解包思路与工具见 SKILL.md:按版本选 PyArmor-Unpacker 三方法之一;还原结论必须标注精确 PyArmor 版本
|
||||
|
||||
## 实现教训(内化)
|
||||
|
||||
- 一切解析先读 magic 定版本:pyc 头、marshal 格式、PyInstaller cookie 的 pyvers 三处各自判版本
|
||||
- PyInstaller 归档偏移是「相对归档起始」不是文件起始(onefile 前有 bootloader);cookie 里 archive_length 是换算基准
|
||||
- TOC 条目是变长的(名字补齐 16 字节),按 entry_length 跳,别按定长扫
|
||||
- pyc 头 mtime/size 为 0 是 PyInstaller 正常行为,不是解包失败
|
||||
|
||||
## 使用注意
|
||||
|
||||
- 静态解包/反编译免沙箱;动态取明文([[re-sandbox]])按最高原则
|
||||
- 与 [[re-managed]](本技能归属网关)、[[re-triage]](初勘输入)、[[re-script-deob]](纯脚本混淆)配合
|
||||
- Cython/Nuitka 产物无 pyc,直接转 [[re-format-elf]]/[[re-ghidra]] 分析 native
|
||||
Reference in New Issue
Block a user