Skip to main content

Python and C code obfuscation tool

Project description

Mistode

PyPI version Python versions License Tests Coverage

๐ŸŒ Languages: English ไธญๆ–‡

Mistode (Mist Code, pronounced like "Miss Told") is a lightweight code obfuscation tool supporting Python and C. It focuses on making code difficult to read while maintaining functional integrity.

Features

  • Encrypted Token Generator: Highly customizable encrypted token generation supporting various configurations:
    • Random Seed: Supports setting a random seed to ensure reproducibility.
    • Length Control: Custom token length, range [8, 32] characters.
    • Style Configuration: Supports two obfuscation styles:
      • Similar Character Style: Uses visually similar character groups to enhance obfuscation (e.g., Oo0, iIlL1, b6B8, Zz2, Ss5).
      • Random Character Style: Uses random alphanumeric combinations.
    • Smart Deduplication: Automatically maintains a set of generated tokens to avoid duplicates.
  • Python Support:
    • AST Parsing: Accurate obfuscation based on Abstract Syntax Trees, ensuring complete preservation of code format.
    • Smart Identifier Classification: Automatically identifies and preserves imported modules, functions, and built-in methods.
    • Docstring Obfuscation: Replaces docstrings with hash values.
    • Fully Reversible: Supports full restoration using generated key files or embedded metadata.
    • Lossless Restoration: Achieves lossless consistency with the original file (preserving all comments and formatting) via distributed annotated injection of source chunks.
  • C Support:
    • Fast Tokenization: Uses robust regular expression tokenizers.
    • Comment Obfuscation: Obfuscates // and /* */ comment content.
    • Compilation Safety: Preserves keywords, preprocessor directives, and string literals (ensuring safety).
    • Lossless Restoration: Achieves lossless consistency with the original file via distributed annotated injection of source chunks.

Installation

pip install mistode

Usage

Simple Usage (Default Settings)

# Obfuscate (generates input.obf.py and input.map.json)
mistode o input.py

# Restore (automatically detects input.map.json if naming matches)
# Generates input.res.py
mistode r input.obf.py

Full Usage

# Obfuscate with explicit options
mistode obfuscate input.py --out output.py --key mapping.json

# Restore with explicit options
mistode restore output.py --out restored.py --key mapping.json

Configuration File

You can set default options in pyproject.toml to avoid repeating the same arguments:

[tool.mistode]
style = "similar"    # Default obfuscation style ("similar" or "random")
length = 16          # Default token length (8-32)
stats = true         # Always show statistics
# seed = 42          # Optional: set a default seed for reproducibility

How it works:

  • Mistode automatically searches for pyproject.toml in the current directory and parent directories
  • If found, settings from [tool.mistode] are used as defaults
  • Command-line arguments always override configuration file settings

Example:

# With the config above, these are equivalent:
mistode o input.py
mistode o input.py --style similar --length 16 --stats

# Override config with command-line args:
mistode o input.py --style random --length 20

For a complete guide, see examples/CONFIG_GUIDE.md.

Advanced Features

Smart Identifier Recognition for Python Obfuscation

Mistode intelligently identifies and avoids obfuscating the following types of identifiers:

  • Built-in Modules: re, os, sys, json, math, etc.
  • Built-in Functions: print, len, range, str, int, etc.
  • Imported Functions: Functions imported via import and from ... import statements.
  • Built-in Methods: re.sub, str.strip, list.append, etc.

Example

Original Code:

import re
from openpyxl.utils import get_column_letter, column_index_from_string

def shift_column_letter(base_column, offset):
    """Calculate the shifted column letter based on the given column letter and offset"""
    base_idx = column_index_from_string(base_column)
    target_idx = base_idx + offset
    return get_column_letter(target_idx)

Obfuscated Code:

import re
from openpyxl.utils import get_column_letter, column_index_from_string

#@mistode:chunk:eJzjSklNU8hIzcnJ11BQyEvMTVVQ0LTiU...
def Oo0iIlL1b6B8Zz2Ss5(Oo0iIlL1b6B8Zz2Ss6, Oo0iIlL1b6B8Zz2Ss7):
    """Obfuscated docstring: e2d30883ac53"""
#@mistode:chunk:aVaCikKXmAdOkoVEO01SopaCoogFWAiQo...
    Oo0iIlL1b6B8Zz2Ss8 = column_index_from_string(Oo0iIlL1b6B8Zz2Ss6)
    Oo0iIlL1b6B8Zz2Ss9 = Oo0iIlL1b6B8Zz2Ss8 + Oo0iIlL1b6B8Zz2Ss7
    return get_column_letter(Oo0iIlL1b6B8Zz2Ss9)
#@mistode:metadata:eJxVk1Fr3DAMx79K...

Embedded Metadata & Distributed Source

Mistode uses a two-layer restoration mechanism:

  1. Distributed Source Chunks (#@mistode:chunk: or // @mistode:chunk:): The original source code is compressed, encoded, and injected as chunks before each line of the obfuscated code. Restoration prioritizes these chunks to reconstruct the original code, achieving lossless restoration (including all comments, empty lines, and formatting).
  2. Embedded Metadata (#@mistode:metadata:): Contains the identifier mapping table as a backup restoration method.

This means you can perfectly restore the original code even without keeping the key file.

# ... Obfuscated Code ...
#@mistode:chunk:eJzjSk... (Distributed Source Chunk)
# ...
#@mistode:metadata:eJxVk1... (Base64 Encoded Mapping)

If no key file is provided, the restore command automatically detects and uses this metadata.

Project Structure

mistode/
โ”œโ”€โ”€ src/
โ”‚   โ””โ”€โ”€ mistode/
โ”‚       โ”œโ”€โ”€ __init__.py      # Package Initialization
โ”‚       โ”œโ”€โ”€ cli.py           # Command Line Interface
โ”‚       โ”œโ”€โ”€ python.py        # Python Obfuscator (mistode.python)
โ”‚       โ”œโ”€โ”€ c.py             # C Obfuscator (mistode.c)
โ”‚       โ””โ”€โ”€ core.py          # Core Functionality (mistode.core)
โ”œโ”€โ”€ tests/                   # Tests Directory
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ test_python.py       # Python Obfuscator Tests
โ”‚   โ”œโ”€โ”€ test_c.py            # C Obfuscator Tests
โ”‚   โ”œโ”€โ”€ test_cli.py          # CLI Tests (Integration)
โ”‚   โ”œโ”€โ”€ test_cli_unit.py     # CLI Tests (Unit)
โ”‚   โ””โ”€โ”€ test_core.py         # Core Functionality Tests
โ”œโ”€โ”€ pyproject.toml           # Project Configuration
โ””โ”€โ”€ README.md                # Project Documentation

Development

Running Tests

python -m pytest tests/

Building the Package

pip install build
python -m build

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mistode-0.1.1.tar.gz (56.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mistode-0.1.1-py3-none-any.whl (35.1 kB view details)

Uploaded Python 3

File details

Details for the file mistode-0.1.1.tar.gz.

File metadata

  • Download URL: mistode-0.1.1.tar.gz
  • Upload date:
  • Size: 56.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for mistode-0.1.1.tar.gz
Algorithm Hash digest
SHA256 82d6bd52c732f879ec445a7e44fd675c53e5abb9d622957502517e76c2913e78
MD5 ce73a4b0c2c155788eb3dc3f9378ac29
BLAKE2b-256 68a790621cee29fe413bfe825e1051c75b280b67bc7376781fd9c9c28d0aefc3

See more details on using hashes here.

File details

Details for the file mistode-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: mistode-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 35.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for mistode-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 60f1d7709fee2e4dd0f03dcecda87830bdef249d34a50b537b5a8fea7bab43fd
MD5 9ed14053b5280905fb28cd279ac9d32b
BLAKE2b-256 4c6190c2d7bc445128820696ca8438a713daa30e06405a04f749298c64159f6e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page