23

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

提前致谢

4

2 回答 2

17

我假设 .pyd/.dll 文件是在 Cython 中创建的,而不是 Python?

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

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

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

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

于 2016-02-25T11:00:07.627 回答
2

我不同意接受的答案,似乎是的,源代码的内容即使在.pyd.

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

1)创建这个文件:

当error.pyx发生什么

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

2)编译它python setup.py build

安装程序.py

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

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

测试error.py时会发生什么

import whathappenswhenerror

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

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

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

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

于 2016-12-10T11:53:09.317 回答