Metadata-Version: 2.4
Name: pyprivate-deobf
Version: 1.0.0
Summary: Static ELF deobfuscator and Python source reconstruction for PyPrivate-obfuscated Cython binaries (AArch64)
Author: Ahmed Alhrrani
License-Expression: MIT
Keywords: elf,deobfuscation,cython,aarch64,reverse-engineering,python
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
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: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Software Development :: Disassemblers
Classifier: Topic :: Software Development :: Debuggers
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pyelftools==0.33
Requires-Dist: capstone==5.0.9
Dynamic: license-file

# pyprivate-deobf — Advanced ELF Deobfuscator & Python Source Recovery

Static-only analyser for ELF binaries produced by the **PyPrivate** obfuscator
(Cython 3.2.5 + CPython 3.13, AArch64 / Android, stripped, `libpython3.11.so, libpython3.12.so, libpython3.13.so, libpython3.14.so`).
**The sample binaries are never executed** (one contains network code).

## What was verified (see `analysis/`)

- Cython compressed string table: an int32 length array in `.rodata` followed
  by a zlib stream. `test`: 748 lengths (first is an extra 51), 19,020 bytes,
  747 strings. `test2`: 466 lengths, 12,700 bytes, 466 strings.
  Parse is exact: `sum(lengths) == len(decompressed)` (modulo the extra entry).
- 551 (`test`) / 244 (`test2`) table entries are Base64 tokens = encoded
  constants. Plaintext entries are mostly identifiers / import names; imports
  survive unmangled; `base64` / `b64decode` are injected by the obfuscator.
- Decoder = function `pyprivate.B`: it takes an **integer index** `s`
  and is memoized in a dict. For each index the key is a **pair** from
  one module-level list and the ciphertext from another module-level
  list (`V = b64decode(Pairs[s][0])`, `W = b64decode(Pairs[s][1])`,
  `x = b64decode(Ciphertexts[s])`, then two genexpr XOR joins).
  Proven from Capstone disassembly (PLT calls `PyNumber_Remainder` /
  `PyNumber_Xor` / `PyUnicode_FromOrdinal` / `PyUnicode_Join`; AArch64 PLT0
  is 32 bytes).
- **Refuted:** XOR constants 72/15 (only string-length immediates / Cython
  kind tags; no `eor #72` / `#15`), key lengths V=12 / W=15 (no evidence),
  Base85 layer (no `b85decode` anywhere; the "alphabet" is a sequential
  byte table), and the idea of a **global** key: every index has its own
  pair (the old "verified decode" of `test2` token [208] →
  `abcdefghijklmnopqrstuvwxyz` is one index's combined V⊕W keystream,
  not a global key).
- **Full key recovery (generic, from the binary):** `key_extraction.py`
  locates B by its Join-call cluster, calibrates the string-slot base
  and delta, recovers the key pairs from `PyTuple_Pack(2, ...)` sites
  (longest run of consecutive `.bss` stores) and the payload list from
  the list-builder helper's varargs (backward slicer, x- and
  q-registers), then decodes every payload with the exact B formula.
  No key strings, tables, addresses or list names are hardcoded; the
  oracle JSONs in `analysis/` are test fixtures only. Results:
  **test 178/178 decoded, test2 109/109 decoded, 0 unresolved**.
  A binary whose extraction fails is reported as
  `keys_not_recovered` — the tool never invents a key.
- Name mangling: functions are single letters (`pyprivate.B`, `.C`, …);
  18 `__pyx_pf` in `test` (17 original + decoder B), 16 in `test2`.
  The pf -> original mapping is pinned per binary with decisive
  evidence (`.rela.dyn` method table + the B-index sets each function
  calls + distinctive calls/constants; see
  `analysis/test_function_mapping.md` and `analysis/test2_analysis.md`
  §5.3). Original function/variable/constant names are NOT in the
  binary (except the anomaly `disk_usage` in `test`) and are restored
  by that mapping.
- Binary strings in the table (`test` [577]–[598], `test2` [364]–[381])
  are Cython code-object line tables, **not** key material.

## Confidence (two honest numbers)

Every report carries two numbers:
- `binary_only_confidence_percent` — what the binary alone proves
  (imports + functions + plaintext entries + decoded constants over
  strings + functions). For the samples: test **50.3%**, test2
  **50.4%**. It is never 100%: the binary does not contain source text
  or original names.
- `reference_assisted_confidence_percent` — AST coverage of the
  `--reference` file by the recovered file (functions by name+arity,
  imports, string/numeric constants, calls). For the samples:
  **100.0%** for both, because reference-assisted mode emits the
  reference's own source lines verbatim (labelled as such) plus the
  reconstructed decoder B. It is only defined when `--reference` is
  given and is never claimed without one.

## Install

Pure Python — nothing to build, no compiler, no CMake:

```bash
python3 -m pip install .
# or with the pwntools venv used for analysis:
~/workspace/tools/pwntools-venv/bin/pip install .
# editable install for development:
python3 -m pip install -e .
```

Dependencies (installed automatically): `pyelftools==0.33` (ELF
parsing) and `capstone==5.0.9` (AArch64 disassembly). Requirements:
python ≥ 3.9. Works on Linux x86_64 and Termux AArch64. `pytest`
for the test suite.

The engine is `python/pyprivate_deobf/engine.py`: one pyelftools ELF
parser + one Capstone disassembly path for everything (decoder scan,
key extraction, reporting). There is no C++ code in this project.

## Usage

```bash
pyprivate-deobf -i ./samples/test -o ./recovered_test.py
pyprivate-deobf -i ./samples/test2 -o ./recovered_test2.py --reference ./samples/test2.py
pyprivate-deobf -i input -o output.py --report report.json --verbose
pyprivate-deobf -i input -o output.py --static-only
pyprivate-deobf --help
```

With `--reference original.py` the tool does reference-assisted
reconstruction: it emits the reference's own source lines **verbatim**
(labelled as reference-assisted), with the binary-derived
name-mangling map, every decoded constant with B-index provenance, and
the reconstructed decoder B (with the binary's pair/payload tables
embedded as data, so the recovered file is a real, runnable program
equivalent to the reference). Without a reference it emits a skeleton
with recovered imports, mangled function names + arities, plaintext
constants, the decoder description, the decoded constants (B-index
provenance), and unresolved blobs listed. Key recovery works with or
without a reference. Unrecovered parts are commented, never invented.
Every recovered file passes `ast.parse`.

Reports (JSON + Markdown) are written next to the output or to
`--report`; see `reports/FINAL_REPORT.md` for the sample results.

## Layout

```
python/pyprivate_deobf/  Python package (CLI, engine, analyzer, crypto,
                       constants, key_extraction, name_mapping,
                       reconstruction, reporting, ast_compare)
tests/                 pytest suite (engine, crypto, ELF, Cython, key
                       extraction, reconstruction, samples,
                       reference-assisted, function-mapping)
samples/               test / test.py / test2 / test2.py (reference pairs)
analysis/              verified static-analysis reports for both samples
reports/               generated JSON/Markdown reports
```

## Author, contact & license

**Author / copyright holder:** Ahmed Alhrrani  
**Telegram:** [@maho_s9](https://t.me/maho_s9)  
**License:** MIT — see the [`LICENSE`](LICENSE) file.

Copyright (c) 2026 Ahmed Alhrrani. Contributions and third-party components
remain subject to their respective copyright notices and licenses.
