是否可以反编译 .dll/.pyd 文件以提取 Python 源代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35601229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:39:44  来源:igfitidea点击:

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

pythondlldecompilingextractionpyd

提问by Youssef Imam

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

是否有任何方法可以反编译 dll 和/或 .pyd 文件以提取用 Python 编写的源代码?

Thanks in advance

提前致谢

采纳答案by Lav

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

我假设 .pyd/.dll 文件是在 Cython 中创建的,而不是 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.

无论如何,通常这是不可能的,除非有专门为文件最初编译的语言设计的反编译器。虽然我了解 C、C++、Delphi、.NET 和其他一些反编译器,但我还没有听说过 Cython 反编译器。

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.

当然,Cython 所做的是首先将您的 Python[esque] 代码转换为 C 代码,这意味着您可能会更幸运地找到 C 反编译器,然后根据反编译的 C 代码预测原始 Python 代码。至少,通过这种方式,您将处理从一种(相对)高级语言到另一种高级语言的翻译。

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).

在最坏的情况下,您将不得不使用反汇编程序。然而,从反汇编器的输出中重新创建 Python 代码并不容易(这与根据构成大脑细胞的蛋白质的化学式来预测大脑的生物学功能非常相似)。

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

你可以看看这个问题上的想法和关于各种反编译和反汇编的建议,并从那里继续你的调查。

回答by Basj

I don't agree with the accepted answer, it seems that yes, the content of the source code is accessible even in a .pyd.

我不同意接受的答案,似乎是的,即使在.pyd.

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

例如,让我们看看如果出现错误会发生什么:

1) Create this file:

1)创建这个文件:

whathappenswhenerror.pyx

whathappenswhenerror.pyx

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

2) Compile it with python setup.py build:

2)编译它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:

3) 现在在标准 python 文件中导入 .pyd 文件:

testwhathappenswhenerror.py

testwhathappenswhenerror.py

import whathappenswhenerror

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

4) 让我们用python testwhathappenswhenerror.py. 这是输出:

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 errorthat was in the .pyxsource code is displayed! Even the comment is displayed!

如您所见,显示print 1/0 # this will generate an error.pyx源代码中的代码行!连评论都显示出来了!

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 erroris no longer displayed:

4 之二)如果我在第 3 步之前删除(或移动到其他地方)原始 .pyx 文件,则print 1/0 # this will generate an error不再显示原始代码:

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.

但这是否意味着它不包含在 .pyd 中?我不知道。