Do you know of any tool that could assist me in obfuscating python code?
-
1Duplicate: stackoverflow.com/questions/261638/how-do-i-protect-python-codeS.Lott– S.Lott2009-02-23 11:02:50 +00:00Commented Feb 23, 2009 at 11:02
-
And more general discussion: stackoverflow.com/questions/164137/…tzot– tzot2009-02-23 16:38:00 +00:00Commented Feb 23, 2009 at 16:38
-
github.com/chris-rands/emojifyChris_Rands– Chris_Rands2019-10-26 21:05:27 +00:00Commented Oct 26, 2019 at 21:05
7 Answers
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?
2 Comments
decompyle possible ?1 Comment
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/
Comments
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.
Comments
Your best bet is to compile it using Shed Skin, an experimental Python-to-C++ compiler.
4 Comments
Although it doesn't do obfuscation, this Python recipe works very well for minimizing the size of Python code, including stripping out comments.