|
1 |
| -# python-code-obfuscator |
| 1 | + |
| 2 | +# Python Code Obfuscator |
| 3 | + |
| 4 | +This is a Python script that obfuscates Python code by renaming variables and function names to randomized, hard-to-read names while preserving the functionality of the original code. |
| 5 | + |
| 6 | +## Features |
| 7 | + |
| 8 | +- **Customizable obfuscation**: Specify the length of obfuscated names. |
| 9 | +- **Automatic detection**: Detects and skips obfuscation for built-in names, imported modules, and essential functions. (almost always) |
| 10 | +- **Output to a new file**: Saves the obfuscated code in a separate file, leaving the original file untouched. |
| 11 | + |
| 12 | +## Requirements |
| 13 | + |
| 14 | +- Python 3.6 or higher. |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +Run the script from the command line and specify the input file, length of obfuscated names, and output file. |
| 19 | + |
| 20 | +### Command-Line Arguments |
| 21 | + |
| 22 | +| Argument | Description | Example | |
| 23 | +|----------|---------------------------------------------------------------|---------------------------------------| |
| 24 | +| `-i` / `--input` | Input Python file to obfuscate. | `-i input.py` | |
| 25 | +| `-l` / `--length` | Length of the obfuscated names (default: 16, must be > 3). | `-l 16` | |
| 26 | +| `-o` / `--output` | Output file to save the obfuscated Python code. | `-o obfuscated.py` | |
| 27 | + |
| 28 | +### Example Usage |
| 29 | + |
| 30 | +```bash |
| 31 | +python obfuscator.py -i example.py -l 16 -o obfuscated_example.py |
| 32 | +``` |
| 33 | + |
| 34 | +This will read the file `example.py`, obfuscate it with randomized names of length 16, and save the result in `obfuscated_example.py`. |
| 35 | + |
| 36 | +## How It Works |
| 37 | + |
| 38 | +1. **Parse the code**: The script uses the `ast` module to parse the input Python file and analyze its structure. |
| 39 | +2. **Collect names**: It identifies variable and function names, skipping: |
| 40 | + - Built-in Python names. |
| 41 | + - Imported module names and their functions/classes. |
| 42 | + - Names listed in the `NO_OBFUSCATE` set. |
| 43 | +3. **Generate random names**: Uses the `random` module to generate randomized names of the specified length. |
| 44 | +4. **Replace names**: Substitutes the original names with the randomized names while ensuring the code remains functional. |
| 45 | +5. **Save the output**: Writes the obfuscated code to the specified output file. |
| 46 | + |
| 47 | +## Limitations |
| 48 | + |
| 49 | +- The `NO_OBFUSCATE` set contains names that the script avoids obfuscating. It's there only because I couldn't fix the issues that occur without it. You will need to add keywords depending on your use case |
| 50 | +- Length of obfuscated names must be greater than 3 to avoid unexpected issues. |
| 51 | + |
| 52 | +## Example Input and Output |
| 53 | + |
| 54 | +### Input (`example.py`): |
| 55 | +```python |
| 56 | +def greet(name): |
| 57 | + print(f"Helo, {name}") |
| 58 | + |
| 59 | +greet("Alice") |
| 60 | +``` |
| 61 | + |
| 62 | +### Command: |
| 63 | +```bash |
| 64 | +python obfuscator.py -i example.py -l 16 -o obfuscated_example.py |
| 65 | +``` |
| 66 | + |
| 67 | +### Output (`obfuscated_example.py`): |
| 68 | +```python |
| 69 | +def xzhlrvioukiqjkyo(ahtjqcvqjdakllay): |
| 70 | + print(f"Helo, {ahtjqcvqjdakllay}") |
| 71 | + |
| 72 | +xzhlrvioukiqjkyo("Alice") |
| 73 | +``` |
| 74 | + |
| 75 | +## License |
| 76 | + |
| 77 | +This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. |
| 78 | + |
| 79 | +--- |
| 80 | + |
| 81 | +**Developed by stigsec** |
0 commit comments