Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it easy to fully decompile python compiled(*.pyc) files?

I wanted to know that how much easy is to decompile python byte code. I have made an application in python whose source I want to be secure. I am using py2exe which basically relies on python compiled files.

Does that secure the code?

like image 914
Shubham Avatar asked Aug 12 '10 02:08

Shubham


People also ask

Can Python be decompiled?

You can get your code back including variable names and doc strings, but without the comments. Some code may not successfully decompile, particularly with unusual control flow, or more recent Python 3. x versions. This is due to bugs in these decompilers where Python has changed its bytecode over time.

Is PYC faster than py?

Loading code from serialized . pyc files is faster than parsing the . py file using ANTLR. Creating the abstract syntax tree (AST) for a Python source has two phases.

What is the difference between PYC and py?

Your answer. py files contain the source code of a program. Whereas, . pyc file contains the bytecode of your program.


3 Answers

Depends on your definition of "fully" (in "fully decompile")...;-). You won't easily get back the original Python source -- but getting the bytecode is easy, and standard library module dis exists exactly to make bytecode easily readable (though it's still bytecode, not full Python source code;-).

like image 173
Alex Martelli Avatar answered Oct 06 '22 20:10

Alex Martelli


Compiling .pyc does not secure the code. They are easily read. See How do I protect Python code?

like image 23
Ned Batchelder Avatar answered Oct 06 '22 20:10

Ned Batchelder


If you search online you can find decompilers for Python bytecode: there's a free version for downloading but which only handles bytecode up to Python 2.3, and an online service which will decompile up to version 2.6.

There don't appear to be any decompilers yet for more recent versions of Python bytecode, but that's almost certainly just because nobody has felt the need to write one rather than any fundamental difficulty with the bytecode itself.

Some people have tried to protect Python bytecode by modifying the interpreter: there's no particular reason why you can't compile your own interpreter with the different values used for the bytecode: that will prevent simple examination of the code with import dis, but won't stand up long to any determined attack and it all costs money that code be better put into improving the program itself.

In short, if you want to protect your program then use the law to do it: use an appropriate software license and prosecute those who ignore it. Code is expensive to write, but the end result is rarely the valuable part of a software package: data is much more valuable.

like image 1
Duncan Avatar answered Oct 06 '22 22:10

Duncan