39

Do you know of any tool that could assist me in obfuscating python code?

David Segonds
  • 83,345
  • 10
  • 45
  • 66

7 Answers7

15

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?

Andrew Dalke
  • 14,889
  • 4
  • 39
  • 54
  • 6
    +1 for Cython, which I have recently used and found it to be pretty good at what it does. –  Feb 23 '09 at 11:16
  • what's the simplest solution to use, if I just don't want easy `decompyle` possible ? – Basj Dec 06 '13 at 21:23
12

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

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

Nick Fortescue
  • 43,045
  • 26
  • 106
  • 134
9

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/

palaѕн
  • 72,112
  • 17
  • 116
  • 136
Adir Iakya
  • 91
  • 1
  • 2
5

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.

Greg Ball
  • 3,671
  • 3
  • 22
  • 15
4

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.

Cerin
  • 60,957
  • 96
  • 316
  • 522
2

Your best bet is to compile it using Shed Skin, an experimental Python-to-C++ compiler.

user7305
  • 5,741
  • 9
  • 28
  • 23
  • There's also Pypy, but it's undocumented and very limited in the code it accepts. – Antimony Apr 28 '13 at 04:37
  • @Antimony: Are you talking about RPython? PyPy (which uses RPython) is a full implementation of Python, and not at all limited in the code it accepts. – John Y Aug 09 '13 at 17:01
  • @John Yes I'm talking about RPython. It's part of the PyPy project. – Antimony Aug 10 '13 at 00:17
  • RPython is the language portions of Pypy are written in. It's a restricted subset of Pypy. However, Pypy implements a pretty complete Python interpreter. – dstromberg Apr 11 '14 at 23:03
0

Although it doesn't do obfuscation, this Python recipe works very well for minimizing the size of Python code, including stripping out comments.

Isaac
  • 215
  • 2
  • 8