6

I would like to protect my python source code, I know there is no absolute protection possible, but still there should be some means to make it difficult enough to do or quite time-consuming. I want

1) to remove all documentation, comments automatically and

2) to systematically change the names of variables and functions within a module (obfuscation?), so that I can keep an external interface (with meaningful names) while the internal names of variables and functions are impossible to pronounce.

Perhaps the best solution, which would make 1) and 2) redundant, is the following:

3) Is there a simple way to compile python modules to .so libraries, with a clear interface and which can be used by other python modules? It would be similar as building C and C++ extensions with distutils, except that the source code is python itself rather than C/C++. The idea is to organize all "secret code" into modules, compile them, and then import them in the rest of the python code which is not considered secret.

Again, I am aware that everything can be reverse-engineered, I think in pragmatic terms, most of the average developers would not be able to reverse-engineer code and even if they would be able, ethical/legal/timing reasons would make them think twice if they really want to work on this.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Mannaggia
  • 4,559
  • 12
  • 34
  • 47
  • 3
    There's `Cython` which might do what you want for number 3, however, I imagine you'd need to maintain a `.so` for each system and each version of python, which could get pretty tedious... I'm not sure how much that would hide from python's introspection abilities. – mgilson Jul 11 '12 at 15:56
  • Is it possible to reuse the whole python code, which imports C++ .so libraries and uses also numpy, and compile this using Cython (without adding types or changing the initial code)? – Mannaggia Jul 11 '12 at 16:07
  • I've not really used Cython much so I can't say. I suspect the answer is yes, but I don't really know. Maybe somebody else can weigh in on this? – mgilson Jul 11 '12 at 16:16

1 Answers1

7

As mgilson mentioned in the comments, Cython is probably your best bet here. Generally speaking, you can use it to convert your pure-python source code into compiled extension modules. While the primary intent of Cython is for enhanced performance, there shouldn't be any barriers for using it for source-code protection. The extension modules it outputs aren't limited in any special ways so anything you were able to do from within Python before, you should be able to do from the Cython-generated extension modules. Cython does have a few known limitations in terms of supported features that may need to be worked around but, overall, it looks well suited to serving your purpose.

Rakis
  • 7,779
  • 24
  • 25
  • @Mannaggia - The Cython manual looks intimidating but it's actually a pretty easy read. Moreover, you should be able to skim most of it as you'll not be using any of the C-integration features. – Rakis Jul 13 '12 at 21:00
  • thanks, seems to work for one module, but I realized that the module I compile still import other python source modules, it there a way to build .so libraries in cython by systematically compiling all modules in a package, and making sure that each module imports only .so libraries rather than source python? – Mannaggia Jul 16 '12 at 14:52
  • 1
    I'm not aware of any automated method. But it would probably be pretty easy to run a script that converts all the .py files you care about into equivalent .pyx files and then run the cython translator to convert them all to .c files. – Rakis Jul 16 '12 at 18:04
  • yes, this is addressed [here](http://stackoverflow.com/questions/11507101/how-to-compile-and-link-multiple-python-modules-or-packages-using-cython) but it is not straightforward to keep all packages/namespaces when compiling, luckily somebody made a script for this – Mannaggia Jul 29 '12 at 15:40
  • 2
    @Mannaggia Try [Nuitka](http://nuitka.net), it can recursively convert your py modules to c++ source files and compile them into single so file (or a standalone executable if you wish). – Meow Jan 25 '15 at 16:07
  • @Mannaggia Please have a look at my related question https://stackoverflow.com/questions/50459248/linux-how-to-simulate-environment-is-which-python-is-not-installed – user13107 May 22 '18 at 03:19