Is it possible to decompile a .dll / .pyd file to extract Python source code?

Is there a way to decompile the dll and / or .pyd file to extract the source code written in Python?

Thank you in advance

+5
source share
2 answers

I assume that the .pyd / .dll files were created in Cython, not Python?

In any case, as a rule, this is impossible if there is no decompiler designed specifically for the language from which the file was originally compiled. And although I know about C, C ++, Delphi, .NET, and some other decompilers, I have not heard about the Cython decompiler.

Of course, what Cython does is first convert your Python [esque] code to C code, which means that you might be lucky to find the C decompiler and then write the Python source code based on the decompiled C code. At least so you will be dealing with translation from one (relatively) high-level language to another.

In the worst case, you will have to use a disassembler. However, recreating the Python code from the output of the disassembler will not be easy (quite similar to predicting the biological functions of the brain from the chemical formulas of the proteins that make up its cells).

You can look at this question for ideas and suggestions regarding various decompilers and disassemblers and continue your research from there.

+1
source

I do not agree with the accepted answer, it seems that yes, the contents of the source code are available even in .pyd .

Let's see, for example, what happens if an error arrives:

1) Create this file:

whathappenswhenerror.pyx

 A = 6 print 'hello' print A print 1/0 # this will generate an error 

2) Compile it with python setup.py build :

setup.py

 from distutils.core import setup from Cython.Build import cythonize setup(ext_modules = cythonize("whathappenswhenerror.pyx"), include_dirs=[]) 

3) Now import the .pyd file into the standard python file:

testwhathappenswhenerror.py

 import whathappenswhenerror 

4) Let run it with python testwhathappenswhenerror.py . Here is the result:

 hello 6 Traceback (most recent call last): File "D:\testwhathappenswhenerror.py", line 1, in <module> import whathappenswhenerror File "whathappenswhenerror.pyx", line 4, in init whathappenswhenerror (whathappenswhenerror.c:824) print 1/0 # this will generate an error ZeroDivisionError: integer division or modulo by zero 

As you can see, the line of code print 1/0 # this will generate an error , which was in the .pyx source code, is displayed! Even a comment is displayed!

4 bis) If I delete (or move elsewhere) the source .pyx file before step 3), then the source code print 1/0 # this will generate an error no longer displayed:

 hello 6 Traceback (most recent call last): File "D:\testwhathappenswhenerror.py", line 1, in <module> import whathappenswhenerror File "whathappenswhenerror.pyx", line 4, in init whathappenswhenerror (whathappenswhenerror.c:824) ZeroDivisionError: integer division or modulo by zero 

But does this mean that it is not included in .pyd? I'm not sure.

+2
source

Source: https://habr.com/ru/post/1243745/


All Articles