Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to decompile a .dll/.pyd file to extract Python Source Code?

Tags:

Are there any ways to decompile a dll and/or a .pyd file in order to extract source code written in Python?

Thanks in advance

like image 790
Youssef Imam Avatar asked Feb 24 '16 11:02

Youssef Imam


People also ask

Can pyd file be decompiled?

pyd/. dll files were created in Cython, not Python? Anyway, generally it's not possible, unless there's a decompiler designed specifically for the language the file was originally compiled from.

Can I read a pyd file?

How to open PYD files. You need a suitable software like Python from Python Software Foundation to open a PYD file. Without proper software you will receive a Windows message "How do you want to open this file?" or "Windows cannot open this file" or a similar Mac/iPhone/Android alert.

What is pyd file in Python?

A . pyd file is a dynamic link library that contains a Python module, or set of modules, to be called by other Python code. To create a . pyd file, you need to create a module named, for example, example. pyd.

Can DLL be decompiled?

Such a DLL is compiled to machine language and can only be directly decompiled to assembly language. So, again, it depends on the language used. And the answer might be that it's just not possible to get anything resembling the original source code. Then, as stated, if it's Visual Basic, research p-code decompilers.


2 Answers

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

Anyway, generally it's not possible, unless there's a decompiler designed specifically for the language the file was originally compiled from. And while I know about C, C++, Delphi, .NET and some other decompilers, I've yet to hear about Cython decompiler.

Of course, what Cython does is convert your Python[esque] code into C code first, which means you might have more luck finding a C decompiler and then divining the original Python code based on the decompiled C code. At the very least, this way you'll be dealing with translation from one (relatively) high-level language to another.

Worst-case scenario, you'll have to use a disassembler. However, recreating Python code from disassembler's output isn't going to be easy (pretty similar to divining the biological functions of a brain from chemical formulas of proteins that make up it's cells).

You might look at this question on ideas and suggestions regarding various decompilers and disassemblers, and proceed your investigation from there.

like image 175
Lav Avatar answered Oct 05 '22 07:10

Lav


I don't agree with the accepted answer, it seems that yes, the content of the source code is accessible even in a .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 in a standard python file:

testwhathappenswhenerror.py

import whathappenswhenerror 

4) Let's run it with python testwhathappenswhenerror.py. Here is the output:

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 that was in the .pyx source code is displayed! Even the comment is displayed!

4 bis) If I delete (or move somewhere else) the original .pyx file before step 3), then the original code print 1/0 # this will generate an error is 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 it's not included in the .pyd? I'm not sure.

like image 40
Basj Avatar answered Oct 05 '22 06:10

Basj