Skip to content

Commit 4ca2910

Browse files
authored
v0.1.0 - Rewrite to AST-based Obfuscations
v0.1.0 - Rewrite to AST-based Obfuscations Merge pull request #25 from davidteather/v0.1.0
2 parents 4d9c7b4 + 4ece184 commit 4ca2910

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+5508
-309
lines changed

.github/workflows/package-test.yml

Lines changed: 70 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
name: CI Test
1+
name: CI
2+
23
on:
34
push:
45
branches:
@@ -7,28 +8,82 @@ on:
78
branches:
89
- main
910
- nightly
10-
- 'releases/*'
11+
- "releases/*"
12+
13+
permissions:
14+
contents: read
1115

1216
jobs:
13-
Unit-Tests:
14-
timeout-minutes: 10
15-
runs-on: ${{ matrix.os }}
17+
lint:
18+
name: Lint & format
19+
runs-on: ubuntu-latest
20+
steps:
21+
- uses: actions/checkout@v4
22+
23+
- name: Set up Python
24+
uses: actions/setup-python@v5
25+
with:
26+
python-version: "3.12"
27+
28+
- name: Install Poetry
29+
run: curl -sSL https://install.python-poetry.org | python - -y
30+
31+
- name: Update PATH
32+
run: echo "$HOME/.local/bin" >> $GITHUB_PATH
33+
34+
- name: Install dependencies
35+
run: poetry install --no-interaction
36+
37+
- name: Ruff (lint)
38+
run: poetry run ruff check python_obfuscator tests
39+
40+
- name: Black (format check)
41+
run: poetry run black --check python_obfuscator tests
42+
43+
- name: isort (import order check)
44+
run: poetry run isort --check-only python_obfuscator tests
45+
46+
- name: Pyright (type check)
47+
run: poetry run pyright python_obfuscator
48+
49+
test:
50+
name: Test (Python ${{ matrix.python-version }})
51+
runs-on: ubuntu-latest
1652
strategy:
1753
fail-fast: false
1854
matrix:
19-
os: [macos-latest, windows-latest, ubuntu-latest]
20-
python-version: [3.6, 3.7, 3.8, 3.9]
55+
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
2156
steps:
22-
- uses: actions/checkout@v2
57+
- uses: actions/checkout@v4
58+
2359
- name: Install Python ${{ matrix.python-version }}
24-
uses: actions/setup-python@v2
60+
uses: actions/setup-python@v5
2561
with:
2662
python-version: ${{ matrix.python-version }}
27-
- name: Setup dependencies
63+
allow-prereleases: true
64+
65+
- name: Install Poetry
66+
run: curl -sSL https://install.python-poetry.org | python - -y
67+
68+
- name: Update PATH
69+
run: echo "$HOME/.local/bin" >> $GITHUB_PATH
70+
71+
- name: Install dependencies
72+
run: poetry install --no-interaction
73+
74+
- name: Type check (mypy)
75+
run: poetry run mypy python_obfuscator
76+
77+
- name: Run tests with coverage
2878
run: |
29-
python -m pip install --upgrade pip
30-
pip install pytest
31-
python setup.py install
79+
poetry run coverage run -m pytest
80+
poetry run coverage report
81+
poetry run coverage xml
3282
33-
- name: Run Tests
34-
run: pytest tests
83+
- name: Upload coverage to Codecov
84+
if: matrix.python-version == '3.12'
85+
uses: codecov/codecov-action@v5
86+
with:
87+
token: ${{ secrets.CODECOV_TOKEN }}
88+
slug: davidteather/python-obfuscator
89+
files: ./coverage.xml

.github/workflows/publish.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Publish Package
2+
3+
on:
4+
release:
5+
types: [created]
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: read
10+
id-token: write
11+
12+
jobs:
13+
pypi-publish:
14+
name: Upload release to PyPI
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v4
19+
with:
20+
token: ${{ secrets.GITHUB_TOKEN }}
21+
22+
- name: Set up Python 3.x
23+
uses: actions/setup-python@v5
24+
with:
25+
python-version: "3.x"
26+
27+
- name: Install Poetry
28+
run: |
29+
curl -sSL https://install.python-poetry.org | python - -y
30+
31+
- name: Update PATH
32+
run: echo "$HOME/.local/bin" >> $GITHUB_PATH
33+
34+
- name: Update Poetry configuration
35+
run: poetry config virtualenvs.create false
36+
37+
- name: Install dependencies
38+
run: poetry install --sync --no-interaction
39+
40+
- name: Package project
41+
run: poetry build
42+
43+
- name: Publish package distributions to PyPI
44+
uses: pypa/gh-action-pypi-publish@release/v1

.github/workflows/python-publish.yml

Lines changed: 0 additions & 31 deletions
This file was deleted.

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,10 @@ __pycache__
33
dist
44
build
55
python_obfuscator.egg-info
6-
.pytest_cache
6+
.pytest_cache
7+
.mypy_cache
8+
.ruff_cache
9+
obfuscated/
10+
.coverage
11+
coverage.xml
12+
htmlcov/

.pre-commit-config.yaml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
repos:
2+
- repo: https://github.com/psf/black
3+
rev: 25.1.0
4+
hooks:
5+
- id: black
6+
language_version: python3
7+
args: [--line-length=88]
8+
9+
- repo: https://github.com/pycqa/isort
10+
rev: 5.13.2
11+
hooks:
12+
- id: isort
13+
args: [--profile=black]
14+
15+
- repo: https://github.com/pycqa/flake8
16+
rev: 7.0.0
17+
hooks:
18+
- id: flake8
19+
args:
20+
[
21+
"--max-line-length=88",
22+
"--extend-ignore=E203,W503,E501,F401,F541",
23+
]
24+
25+
- repo: local
26+
hooks:
27+
- id: mypy
28+
name: mypy
29+
entry: poetry run mypy python_obfuscator
30+
language: system
31+
pass_filenames: false
32+
always_run: true
33+
stages: [commit]
34+
35+
- id: pytest-unit
36+
name: Run unit tests
37+
entry: poetry run pytest tests/ -v
38+
language: system
39+
pass_filenames: false
40+
always_run: true
41+
stages: [commit]
42+
43+
- id: pytest-coverage-report
44+
name: pytest coverage report (manual)
45+
entry: poetry run pytest tests --cov=python_obfuscator --cov-report=term-missing --cov-report=html
46+
language: system
47+
pass_filenames: false
48+
always_run: true
49+
stages: [manual]
50+
51+
default_language_version:
52+
python: python3
53+
54+
fail_fast: true
55+
56+
default_stages: [commit]

CHANGELOG.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
6+
This project uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
---
9+
10+
## [0.1.0] — AST-Based Rewrite
11+
12+
Complete rewrite from a regex-based implementation to a fully AST-based pipeline.
13+
14+
### Added
15+
16+
- **`ObfuscationConfig`** — immutable frozen dataclass for selecting techniques.
17+
Factory methods: `all_enabled()`, `only(*names)`, `.without(*names)`, `.with_added(*names)`.
18+
- **Technique registry**`@register` class decorator; techniques self-register by name.
19+
`all_technique_names()` and `get_transforms(enabled)` for pipeline construction.
20+
- **`variable_renamer`** — two-pass AST renamer. First pass collects renameable names
21+
(excludes builtins, imports, dunders, and attribute-accessed names); second pass applies
22+
the mapping. Also renames `nonlocal` and `global` statement name lists.
23+
- **`string_hex_encoder`** — converts string literals to `bytes.fromhex(…).decode('utf-8')`
24+
call nodes. Skips f-strings where replacing a `Constant` with a `Call` is invalid.
25+
- **`dead_code_injector`** — recursively injects dead variable assignments at every scope
26+
level: module body, function bodies, class bodies, if/for/while/try/with branches.
27+
Some assignments reference earlier dead variables (intra-scope cross-references) to
28+
simulate computation. Accepts `InjectionParams` and a seeded `random.Random` for
29+
reproducible output.
30+
- **`exec_wrapper`** — wraps the entire module in `exec(ast.unparse(tree))`, reducing the
31+
top-level AST to one statement.
32+
- **`Obfuscator` class** — caches the transform pipeline across multiple `obfuscate()` calls.
33+
- **`obfuscate()` module-level helper** — convenience wrapper for one-shot use.
34+
- **CLI** (`pyobfuscate`) — `--disable/-d` flag accepts technique names; `--stdout`; `--version/-V`.
35+
- **E2E test suite** with correctness and benchmark tests across six complex programs.
36+
- **Per-technique runtime benchmarks** showing individual overhead contribution.
37+
- 100 % branch coverage enforced in CI.
38+
- Python 3.10–3.14 test matrix.
39+
40+
### Changed
41+
42+
- Priority ordering now encoded in `TechniqueMetadata.priority` rather than list position.
43+
- `VariableNameGenerator` and `RandomDataTypeGenerator` accept an optional `rng` argument
44+
for deterministic testing.
45+
- `_NameCollector` exposes `all_bound_names` (assigned + imported + builtins) for use by
46+
the dead-code injector's exclusion set.
47+
48+
### Removed
49+
50+
- All regex-based technique implementations (`techniques.py`).
51+
- `regex` runtime dependency (was used only for the old hex-encoding approach).
52+
- `SourceTransform` base class and `source_transforms` package.
53+
54+
### Fixed
55+
56+
- `VariableRenamer` now updates `nonlocal` and `global` statement name lists, preventing
57+
`SyntaxError: no binding for nonlocal '…' found` after renaming.
58+
- `VariableRenamer` excludes attribute-accessed names from renaming to prevent
59+
`AttributeError` when calling methods by the original name after their definition is renamed.
60+
- `_interleave` preserves relative ordering of injected statements (sorted random slots)
61+
so intra-scope cross-references never appear before their definitions.
62+
63+
---
64+
65+
## [0.0.2] — prior release
66+
67+
Initial public release with regex-based obfuscation techniques.
68+
69+
- `one_liner` — collapsed newlines into semicolons (superceded by `exec_wrapper`).
70+
- `variable_renamer` — regex-based name replacement.
71+
- `add_random_variables` — prepended/appended random assignments at module level.
72+
- `str_to_hex_bytes` — regex-based string-to-hex conversion.

0 commit comments

Comments
 (0)