Metadata-Version: 2.4
Name: peobf
Version: 1.1.1
Summary: Detect XOR-style obfuscation in PE sections by disassembly coverage, because Shannon entropy is blind to bijections.
Author: gem3match-dev
License: MIT
Project-URL: Homepage, https://github.com/gem3match-dev/pe-obfuscation-detection
Project-URL: Repository, https://github.com/gem3match-dev/pe-obfuscation-detection
Project-URL: Desktop GUI (Windows), https://gem3matchdev.itch.io/pe-doctor-pro
Project-URL: Tool Comparison, https://github.com/gem3match-dev/pe-obfuscation-detection/blob/main/COMPARISON.md
Keywords: pe,portable-executable,obfuscation,deobfuscation,xor,xor-key,entropy,shannon-entropy,disassembly,disassembler,capstone,reverse-engineering,malware-analysis,static-analysis,packer,unpacking,binary-analysis,pe-analysis,windows,dll,exe
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Topic :: Security
Classifier: Topic :: Software Development :: Disassemblers
Classifier: Topic :: Software Development :: Debuggers
Classifier: Topic :: System :: Software Distribution
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Information Technology
Classifier: Intended Audience :: System Administrators
Classifier: Environment :: Console
Classifier: Environment :: Win32 (MS Windows)
Classifier: Typing :: Typed
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pefile>=2023.2.7
Requires-Dist: capstone>=5.0
Dynamic: license-file

# peobf

**Prefer a GUI?** [PE Inspector for Windows](https://gem3matchdev.itch.io/pe-doctor-pro) — one window showing sections, imports, exports, strings, signature and hardening flags side by side with entropy and decodable coverage. A **free 10 MB build** reads the first 3 sections; the full 30 MB build reads everything. No Python, no install: unzip, double-click.

Detect XOR-style obfuscation in PE sections by **disassembly coverage**, because Shannon entropy is blind to bijections.

```
pip install peobf
peobf check C:\Windows\System32\version.dll
```

## The problem

Most packer/obfuscation detectors threshold on Shannon entropy: above ~7.0 means "compressed or encrypted". That test misses the most common cheap obfuscation there is.

XOR is a **bijection** — it permutes the byte-value labels one-to-one. Shannon entropy is computed from the multiset of byte frequencies, so the entropy is *exactly* unchanged:

```
H(XOR(data, k)) == H(data)     for every key k
```

Measured on real Windows binaries:

```
C:\Windows\System32\version.dll   .text  12,288 bytes

  case          entropy    disassembly coverage
  original      5.4067     100.00%
  xor 0x5A      5.4067       2.54%
  xor 0xFF      5.4067       0.69%
  xor 0x01      5.4067       0.13%
  permuted      5.4067       0.41%
```

Entropy delta on every transformed row: **+0.0000**. A tool that only measures entropy reports "normal" for a section whose code is entirely undecodable.

```
C:\Windows\System32\advapi32.dll  .text  471,040 bytes

  case          entropy    disassembly coverage
  original      6.2347      22.14%
  xor 0x5A      6.2347       0.00%
  xor 0xFF      6.2347       0.00%
  permuted      6.2347       0.01%
```

## What peobf measures instead

**Decodable instruction coverage**: start a disassembler at the section base and count what fraction of bytes valid instructions consume.

- Real x64 code is typically 20–100% (not 100% — alignment padding and inline data sit between functions).
- XOR-obfuscated code drops to **0.00%**: the decoder cannot form a single valid instruction.

That gap is the signal. It needs no key, no unpacking, no execution.

## Usage

```bash
# 快速看所有节区（不反汇编，毫秒级）
peobf sections suspicious.dll

# 深入分析一个节区
peobf check suspicious.dll --section .text
```

`check` prints the entropy and coverage for the original plus four bijective transforms, then gives a verdict:

| verdict | meaning |
|---|---|
| `looks_normal` | section decodes normally; no obfuscation evidence |
| `possibly_encrypted` | coverage <5% **and** entropy >6 |
| `undecodable_low_entropy` | coverage <5% but entropy low — transformed, or filled with data |

## As a library

```python
from peobf import section_report, summarize

rep = section_report(r"C:\Windows\System32\version.dll", ".text")
s = summarize(rep)

print(s["verdict"])                 # looks_normal
print(s["base_coverage"])           # 1.0
print(s["entropy_blind_cases"])     # 4  ← transforms entropy missed entirely
```

`list_sections(path)` returns name / VA / raw size / entropy / permissions for every section without disassembling.

## Real section layout, for calibration

`advapi32.dll` (778,352 bytes, PE32+, 8 sections):

```
  name        VA          raw size    entropy   perms
  .text       0x1000       471,040    6.2347    RX
  fothk       0x74000        4,096    0.0159    RX
  .rdata      0x75000      233,472    4.6996    R
  .data       0xae000       12,288    0.8552    RW
  .pdata      0xb3000       20,480    5.2377    R
  .didat      0xb8000        4,096    1.2959    RW
  .rsrc       0xb9000        4,096    1.6339    R
  .reloc      0xba000        8,192    5.0584    R
```

Both directions of false positive are real. `fothk` has entropy **0.0159** — near zero because it is a jump table of identical stubs. Entropy flags nothing; coverage would flag it as suspicious because almost nothing decodes. Reading it shows it is benign. That is why the verdict combines both numbers instead of thresholding one.

## Limits — read before trusting it

- **Does not recover the key.** Single-byte XOR is trivially brute-forced (try 255 keys, score coverage); longer keys and block ciphers need real work.
- **Does not prove maliciousness.** Protectors and packers obfuscate legitimately. The measurement says "this code is not decodable as-is", nothing more.
- **Coverage near 0 can be benign** if a section legitimately holds data with code-like flags. Check permissions and the section name before concluding.
- **Only PE.** ELF/Mach-O need different parsers; the coverage idea transfers, the code does not.

## Requirements

Python 3.9+. `pefile` and `capstone` are installed as dependencies.

## License

MIT
