Share your repls and programming experiences

← Back to all posts
Python Obfuscator
JoeyLent (20)

Introduction

I've always been fascinated with obfuscation. To me, the fact that you can translate your code into an unintelligible mess but still be able to run it properly seems extraordinary.

Although I won't be obfuscating my programs, it's still nice to know that you can do so nevertheless. However, the issue to all this is that I code in Python. If you take a quick look online, there aren't any methods for obfuscating your Python code that don't include renaming variables, minifying your code, inserting dead code, etc... Most of these methods preserve the original structure of the program and aren't like the obfuscators in other languages. Take a quick search for "JavaScript obfuscator" and you'll find your search page filled with lots of different obfuscators. The same doesn't happen with Python. The best Python obfuscator I could find, is Oxyry, but nothing else.

After taking some inspiration from this absolute monstrosity, and after lots of head scratching, I was finally able to obfuscate my Python code for the first time ever. Of course, I had to do this manually, and that took a lot of time and effort. Out of boredom, I decided to make the dumbest thing ever and automate this.

I eventually managed to pull it off. Although not being fully polished, I feel contempt with this result.

Just to provide some insight, the name BFuscator came from combining the words "Bored" and "Obfuscator". Also, I have successfully obfuscated the obfuscator, however I had to manually minify the file for it to work.

Usage

The obfuscator won't work if you just press run, but will instead show the message below.

usage: python bfuscate.py -f <input file> -o (output file; defaults to <file>_BFUSCATE.py)
outputted code must be run as a file

From here, you have two choices. The first choice if forking the project and working from there, and the second choice is working from the repl terminal, supposing you know how to use UNIX.

After you get your input file ready, you first need to minify it a bit. This step will be automated at a later date. First begin by removing all comments, and all extra newlines. This will help reduce the obfuscated file's size and also prevent a possible bug I've seen once or twice.

Once this is done, you can now run the command shown in the default message to obfuscate your code. If an output directory isn't specified, and supposing the file name is sample, the output file will be named sample_BFUSCATE.py. As it states, you cannot run the obfuscated code in a shell. This is because the program reads its own file to find the code after the #. This cannot happen in a shell. You could change this by wrapping the code after the # with an exec(eval(...)), but I didn't include this in the final result because I felt that doing so would ruin the effect.

Happy coding!

https://repl.it/@JoeyLent/BFuscate