Metadata-Version: 2.4
Name: pe-doctor
Version: 1.0.9
Summary: Diagnose and restore broken PE/DLL files. Explains why a DLL fails to load (bad entry point, wiped export table, XOR-encrypted section, truncated file) and repairs it.
Author: gem3match-dev
License: MIT
Project-URL: Homepage, https://github.com/gem3match-dev/pe-doctor
Project-URL: Source, https://github.com/gem3match-dev/pe-doctor
Project-URL: Issues, https://github.com/gem3match-dev/pe-doctor/issues
Project-URL: Changelog, https://github.com/gem3match-dev/pe-doctor/releases
Project-URL: Desktop GUI (Windows), https://gem3matchdev.itch.io/pe-doctor-pro
Keywords: pe,dll,exe,diagnose,diagnosis,forensic,forensics,repair,fix,load-failure,reverse-engineering,windows,malware-analysis,binary,packer,unpack,disassembly,triage,crash,analysis
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Information Technology
Classifier: Intended Audience :: System Administrators
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Security
Classifier: Topic :: Software Development :: Debuggers
Classifier: Topic :: Software Development :: Disassemblers
Classifier: Topic :: System :: Systems Administration
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: capstone>=5.0
Provides-Extra: dev
Requires-Dist: pytest>=7; extra == "dev"
Dynamic: license-file

# pe-doctor

**Diagnose and restore broken PE/DLL files.** Two command-line tools, one dependency.

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

[![PyPI](https://img.shields.io/badge/pypi-pe--doctor-blue)](https://pypi.org/project/pe-doctor/)
[![License](https://img.shields.io/badge/license-MIT-green)](LICENSE)
[![Python](https://img.shields.io/badge/python-3.9%2B-blue)](https://www.python.org/)

```bash
pip install pe-doctor
```

```bash
pe-doctor diagnose suspect.dll          # why won't it load
pe-doctor compare broken.dll good.dll   # which section changed
pe-restore auto broken.dll good.dll     # transplant only the damaged sections
pe-restore brutexor broken.dll --section .text --apply
```

---

## The point most tools miss

**Entropy cannot detect XOR encryption.**

XOR is a bijection — it permutes byte frequencies, so Shannon entropy is *identical* before and after. Measured on a real 471 KB `.text` section:

```
original     entropy 6.235   disassembly coverage 99.95%
XOR 0x5A     entropy 6.235   disassembly coverage  0.2%
```

Same entropy. 500× difference in decodable instructions. A tool that only measures entropy looks at a XOR-encrypted section and reports `clean`.

So the decisive test here is **capstone disassembly coverage**, not entropy. You can verify the invariance yourself — `make_samples.py` generates an original and an XORed variant from the same synthetic PE, and the byte histograms are provably the same shape.

---

## What `diagnose` checks

**Structure** — DOS/PE signatures, `e_lfanew`, COFF header, OptionalHeader magic, section table truncation, section raw ranges past EOF, overlapping sections.

**Semantics** — `IMAGE_FILE_DLL` bit vs. presence of an export directory (with an export table but no DLL bit, the loader treats the image as an EXE and `GetProcAddress` fails — a defect packers introduce constantly), import directory resolvability, export table RVA inside readable bytes, entry point inside a section.

**Content** — per-section entropy, head entropy, padding-byte ratio, **capstone disassembly coverage** (code sections only), known packer signatures (UPX, Themida, VMProtect, ASPack, PECompact, MPRESS, Enigma, Obsidium, PEtite, FSG, MEW).

**Verdict** — `fatal` blocks loading · `warn` is suspicious · `info` is a note.

---

## Measured on real binaries

Seven production DLLs from a Windows install, **zero false positives**:

| File | Size | Exit | Fatal | Time |
|---|---|---|---|---|
| advapi32.dll | 0.7 MB | 0 | 0 | 0.15 s |
| kaldifst_core.dll | 1.1 MB | 0 | 0 | 0.10 s |
| kaldifst_fstscript.dll | 11.3 MB | 0 | 0 | 0.70 s |
| mshtml.dll | 22.9 MB | 0 | 0 | 1.50 s |
| edgehtml.dll | 24.8 MB | 0 | 0 | 1.63 s |
| nvcuvid.dll | 30.8 MB | 0 | 0 | 2.08 s |

Time scales linearly. A 30 MB NVIDIA decoder DLL takes 2 seconds.

Windows executables also stay clean — `calc.exe`, `notepad.exe`, `cmd.exe` all exit 0. This matters because small EXEs legitimately carry near-all-zero `.data` and `.reloc` sections, and a naive wiped-section check flags them.

## Detection, on the shipped samples

`python make_samples.py` builds eight fixtures from a hand-written synthetic PE. Each one is a single defect:

| Sample | Defect | Detected as |
|---|---|---|
| `A0_original.dll` | none | exit 0, `no-import-dir` info only |
| `A1_xor_text.dll` | `.text` XOR 0x5A | `code-not-disassemblable` |
| `B1_no_export.dll` | export directory zeroed | `no-export-dir` |
| `B2_no_dll_bit.dll` | DLL bit cleared, exports kept | `not-dll-but-exports` |
| `C1_bad_ep.dll` | entry point → 0x7FFFFFF0 | `ep-outside-sections` |
| `D1_truncated.dll` | truncated to 60% | `section-out-of-range` |
| `E1_not_pe.dll` | `MZ` → `ZZ` | `not-mz` |
| `E2_bad_lfanew.dll` | `e_lfanew` → 0x7FFFFFF0 | `no-pe-header` |

All eight classify correctly. Run `python -m pytest tests/` to check.

## Restoration, verified by hash

| Path | Result |
|---|---|
| XOR brute force | key `0x5A` scored 1.000 (runner-up `0xA8` at 0.053) → coverage 0.002 → 0.9995 |
| Section transplant | coverage 0.2% → 100%, SHA256 byte-identical to the reference |
| Export table rebuild | exports 0 → 853, no blocking findings |

The transplant result is the one that matters: the repaired file hashes to the same SHA256 as the original `advapi32.dll`. Not "it loads" — byte-identical.

---

## Exit codes

Drive CI with these directly:

```bash
pe-doctor diagnose artifact.dll || echo "blocking structural defect"
```

- `0` — structurally clean
- `1` — blocking problem found
- `2` — file unreadable or not a PE

---

## Boundaries

**Does:**
- Structural damage (section table, directories, entry point, characteristics bits)
- Single-byte XOR
- Transplanting any section from a same-version reference
- Rebuilding a zeroed export table

**Does not, and says so:**
- **Multi-byte XOR / RC4 / AES** — key space is not brute-forceable; needs a reference or a memory dump
- **Themida / VMProtect / Enigma** — virtual-machine protection, no general unpack. The realistic path is a runtime memory dump plus IAT rebuild, and the output is not the original DLL
- **Truncated files without a backup** — the lost bytes cannot be invented
- **Different section layout from the reference** — needs section-table reconstruction; the tool refuses instead of forcing it

These boundaries are part of the tool, not a defect. A tool that claims to fix anything is not trustworthy at step one.

---

## Install from source

```bash
git clone https://github.com/gem3match-dev/pe-doctor.git
cd pe-doctor
pip install -e .
python make_samples.py        # build the test fixtures
python -m pytest tests/ -v
```

**CI** is defined in `ci/github-actions-tests.yml`. It is not active because the token used to publish this repo has only the `repo` scope and GitHub requires `workflow` to write under `.github/workflows/`. Move it there to enable.

---

## Requirements

Python 3.9+ · `capstone>=5.0` (installed automatically)

`pefile` is **not** used — PE parsing is hand-written so every field offset is controlled, printable, and checkable.

MIT licensed.
