Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Code Obfuscation [closed]

People also ask

Can Python code be obfuscated?

Encrypting some or all of a program's code is one obfuscation method. In Python, There are multiple packages available using which you can obfuscate your code base and secure your intellectual property.

How can I protect my Python code but still make it available to run?

Instead of converting some parts of the code to C, they hide the entire python code inside a protective C layer. Then, if they want a module importable by python, they write a thin python extension on top of the C. Open source is a much easier way of life.

Can Python code be encrypted?

Project description. SOURCEdefender is the easiest way to obfuscate Python code using AES-256 encryption. AES is a symmetric algorithm which uses the same key for both encryption and decryption (the security of an AES system increases exponentially with key length).


Your problem space is underspecified. Is this for a command-line app? Is this code supposed to be used as a library?

In addition to the two other answers, you could embed the code into a binary. When it starts, decode the code and eval the string. This works for a shared library extension as well. You could also do that with byte code, I think, but it wouldn't be as simple as calling Py_EvalCode.

py2exe or freeze are other solution, which convert the code into an executable. It just includes the code in the binary, and doesn't do any sort of serious obsfucation, but it's still harder than opening a .py file.

You could write the code in Cython, which is similar to Python and writes Python extension files in C, for use as a .so. That's perhaps the hardest of these to reverse engineer and still give you a high-level language for develoment.

They are all hackable, as are all solutions. How hard to you want it to be?


http://www.lysator.liu.se/~astrand/projects/pyobfuscate/

Or at http://freshmeat.net/projects/pyobfuscate/


I actually found a very nice project which basically converts a Python to C++ and create a binary, statically linked file.

Check this out: http://www.nuitka.net/


In many situations you can ship byte-compiled .pyc files instead of the .py source files. This gives you some level of obfuscation. As the pyobfuscate README suggests, this has limitations. But you may be able to combine the two approaches.


Python's standard library includes compileall.py. You can run this on a directory and it will generate .pyc files for all your source files. The .pyc files will only include bytecode and docstrings, and will strip out all comments. You could then copy this directory, and then run something like rm -rf $(find . -name .py) to remove the original source files.