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

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.0.tar.gz (49.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.0-py3-none-any.whl (33.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mistode-0.1.0.tar.gz
  • Upload date:
  • Size: 49.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.0.tar.gz
Algorithm Hash digest
SHA256 8df4621bfc361a50a5b2a551eb0b88e8dd696f02f1c37ec7aa68fcf3e020ca41
MD5 dc38300d7641af5b986d1ab75b0b7d84
BLAKE2b-256 8fd80cb439777b9352435e5958ebe4ccb9c967dd83054563c3efcfea0782993c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mistode-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 33.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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e0b923da2f4a25e0fa08f5def268bff2bd5d4ab4fef94e945bd132b2d49d19c6
MD5 d9b5e4a808f42c585010f09244853266
BLAKE2b-256 89632dad9257f796e4ac57cf20ab0c97c0f23c8e0178e2447ae3747dea45b212

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