How to protect python code ?

Chris Reedy creedy at mitretek.org
Tue Apr 8 11:13:17 EDT 2003


msw wrote:
> [snip] 
> 
> The software I write falls under the category of those that need to have 
> the source code hidden from customers - for many reasons, not least of 
> which is "because the marketing department says so".  Our customers are 
> unlikely to go after "movl $0x0007, %ax" with a hex editor, but are 
> almost certain to mess with code that looks like "licenseCount = 5".
> 
Given that no source code obfuscation system can provide absolute 
protection, and given that your objective is to keep the user from 
messing with the code, I suggest the following program:

1. Change the name of all local variables to _L1, _L2, etc.

2. Change the name of all parameters, including keywords (that may get a 
little tricky) for functions and methods that are not part of the 
external interface to _P1, _P2, etc.

3. Change the name of all functions and methods that are not part of the 
  external interface to _F1, _F2, etc.

4. Change the name of all classes that are not part of the external 
interface to _C1, etc.

5. Change the name of all module globals that are not part of the 
external interface and not already covered to _G1, etc.

6. Change the name of all packages and modules that are not part of the 
external interface to M1, M2, etc.

7. Remove all comments.

8. Remove all testing code.

9. Remove documentation strings for objects that aren't part of the 
external interface.

10. And anything else I forgot ...

At this point your code is sufficiently obfuscated that no one in their 
right mind would want to touch it. More to the point, anyone that is 
willing to devote that time and effort to understanding this code is 
probably going to penetrate any obfuscation scheme you care to design. 
That is, this will achieve your goal, you will keep the casual user from 
attempting to modify the code.

   Chris

-- 
This is informal and not an official Mitretek Systems position.
Dr. Christopher L. Reedy, Senior Principal Software Engineer
Mitretek Systems, 3150 Fairview Park Drive South, Falls Church, VA 22042
Email: creedy at mitretek.org  Phone: (703) 610-1615  FAX: (703) 610-2203





More information about the Python-list mailing list