是否可以将已编译的 .pyc 文件反编译为 .py 文件?
是否可以从 .py 文件生成的 .pyc 文件中获取一些信息?
Is it possible to get some information out of the .pyc file that is generated from a .py file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
简介
Python 反编译是一个难题,特别是对于最近的 3.x 版本。这就是为什么有各种各样的工具,但都有局限性和错误。
然而,Python 2.7 和更早的 3.x 版本应该可以很好地工作,即使部分反编译也比丢失所有内容要好。
尝试工具
Uncompyle6 对于 Python 3.8 及以下版本有效,但存在一些错误,并且适用于 2.7 。
如果这不起作用,最好接下来尝试这个 - 特别是对于 Python 3.7+:
decompyle3
软件包来自rocky/decompile3
存储库 - 拼写不同,但内容相同如果您仍然遇到问题,请检查
uncompyle6
和decompyle3
自述文件链接到可能更适合您的代码的其他工具。限制
您可以取回代码,包括变量名称和文档字符串,但不带注释。
某些代码可能无法成功反编译,特别是使用不寻常的控制流或更新的 Python 3.x 版本时。这是由于这些反编译器中的错误导致 Python 随着时间的推移更改了其字节码。
支持最新的 Python 版本
uncompyle6
或decompyle3
均不支持 Python 3.9 或更高版本,并且对 3.7 或更高版本的支持有限。Python 中的新优化使反编译变得更加困难,并且这两个项目都缺乏代码贡献和赞助。
您可以提供帮助:
防止将来丢失代码
频繁的 Git 提交或备份显然是一个好主意。
某些编辑器/IDE 具有帮助恢复已删除文件的功能,即使这些文件未提交到 Git。请参阅此答案,了解可能适用于您的编辑器或 IDE 的一些提示,包括 VS Code 和 JetBrains IDE,例如 PyCharm、GoLand和 IntelliJ。
Intro
Python decompilation is a hard problem, particularly for recent 3.x versions. That's why there are various tools, all with limitations and bugs.
However, Python 2.7 and earlier 3.x versions should work pretty well, and even partial decompilation is better than losing everything.
Tools to try
Uncompyle6 works, with some bugs, for Python up to 3.8, and works well for 2.7.
If that doesn't work, it's probably best to try this next - particularly for Python 3.7+:
decompyle3
package is from therocky/decompile3
repo - different spelling but same thingIf you still have problems, check the
uncompyle6
anddecompyle3
READMEs which link to other tools that may work better for your code.Limitations
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.
Supporting recent Python versions
Neither
uncompyle6
ordecompyle3
support Python 3.9 or higher, and support for 3.7 or higher is limited.New optimizations in Python are making decompilation harder, and both code contributions and sponsorship are lacking for both projects.
What you can do to help:
Preventing loss of code in future
Frequent Git commits or backups are clearly a good idea.
Some editors/IDEs have features to help recover deleted files even if not committed to Git. See this answer for some pointers that may work in your editor or IDE, including VS Code and JetBrains IDEs such as PyCharm, GoLand and IntelliJ.
您可以尝试Easy Python Decompiler。它基于 Decompyle++ 和 Uncompyle2。
它支持反编译python版本1.0-3.3
注:我是上述工具的作者。
You may try Easy Python Decompiler. It's based on Decompyle++ and Uncompyle2.
It's supports decompiling python versions 1.0-3.3
Note: I am the author of the above tool.
是。
我使用 uncompyle6 反编译(甚至支持最新的 Python 3.8.0):
uncompyle6 utils。 cpython-38.pyc > > utils.py
以及原始 python 和反编译的 python 比较如下所示:
所以你可以看到,几乎一样,反编译效果VERY GOOD。
Yes.
I use uncompyle6 decompile (even support latest Python 3.8.0):
uncompyle6 utils.cpython-38.pyc > utils.py
and the origin python and decompiled python comparing look like this:
so you can see, ALMOST same, decompile effect is VERY GOOD.
是的,您可以通过 pypi 上的
unpyclib
获取它。您可以反编译 .pyc 文件
Yes, you can get it with
unpyclib
that can be found on pypi.Than you can decompile your .pyc file
Decompyle++ (pycdc) 是唯一对我有用的:https://github.com/zrax/pycdc
在 反编译 Python 2.7 .pyc 中建议
Decompyle++ (pycdc) was the only one that worked for me: https://github.com/zrax/pycdc
was suggested in Decompile Python 2.7 .pyc
我已经研究了几个小时,终于找到了使用 Decompyle++ 的解决方案:
C:\Program Files\CMake\bin
添加到 PATH 下的系统环境变量中。我建议将
pycdc-master
文件夹放入另一个文件夹中,例如anotherFolder
。现在,您可以在命令行中运行这些命令:
cd anotherFolder
以进入其中包含pycdc-master
的文件夹。cmake pycdc-master
cd ../
转到一个目录,cmake --build anotherFolder
pycdc.exe 将位于
anotherFolder\Debug
中。在控制台中执行诸如 pycdc.exe onlyhopeofgettingmycodeback.pyc 之类的操作,它将打印出源代码。我有 Python 3.9.6 源代码,其他都不起作用。
I've been at this for a couple hours and finally have a solution using Decompyle++:
https://cmake.org/download/
and install CMake.https://github.com/zrax/pycdc
and grab a copy of this repo:pycdc-master
.C:\Program Files\CMake\bin
to your system environment variables under PATH.I suggest putting your
pycdc-master
folder into another folder, likeanotherFolder
.Now you can run these commands in the command line:
cd anotherFolder
to go into the folder that haspycdc-master
in it.cmake pycdc-master
cd ../
to go up one directory,cmake --build anotherFolder
pycdc.exe
will then be inanotherFolder\Debug
.Do something like
pycdc.exe onlyhopeofgettingmycodeback.pyc
in a console and it will print out the source code. I had Python 3.9.6 source code and nothing else was working.是的,这是可能的。
有一个完美的开源 Python (.PYC) 反编译器,名为 Decompyle++ https://github.com/zrax /pycdc/
Decompyle++ 旨在将编译后的 Python 字节码转换回有效且人类可读的 Python 源代码。
虽然其他项目已经取得了不同程度的成功,但 Decompyle++ 的独特之处在于它寻求支持任何版本的 Python 的字节码。
Yes, it is possible.
There is a perfect open-source Python (.PYC) decompiler, called Decompyle++ https://github.com/zrax/pycdc/
Decompyle++ aims to translate compiled Python byte-code back into valid and human-readable Python source code.
While other projects have achieved this with varied success, Decompyle++ is unique in that it seeks to support byte-code from any version of Python.
安装使用
pip install pycompyle6
pycompyle6 文件名.pyc
Install using
pip install pycompyle6
pycompyle6 filename.pyc
如果您需要反编译 pyc 但安装了 python 3.9,您可以强制 uncompyle6 运行。它并不完美,但确实有效。只需编辑 site-packages\uncompyle6\bin\uncompile.py
只需以与其他版本相同的格式添加您已安装的版本并保存即可。它至少会运行。
If you need to decompile a pyc but have python 3.9 installed you can force uncompyle6 to run. It's not perfect but it does work. Just edit site-packages\uncompyle6\bin\uncompile.py
Just add the version you have installed in the same format as the others and save. It will at least run.
标题:从 PyInstaller 生成的可执行文件中提取文件
问题:
如何从 PyInstaller 创建的可执行文件中提取文件?
解决方案:
克隆 pyinstxtractor-ng 存储库:< /p>
提取可执行文件:
安装依赖项:
克隆pycdc存储库:
构建并编译:
反编译字节码:
通过执行以下步骤,您可以从 PyInstaller 生成的可执行文件中提取并反编译文件。
Title: Extracting Files from PyInstaller-Generated Executable
Problem:
How can I extract files from an executable created by PyInstaller?
Solution:
Clone the pyinstxtractor-ng repository:
Extract the executable:
Install dependencies:
Clone the pycdc repository:
Build and compile:
Decompile the bytecode:
By following these steps, you can extract and decompile files from a PyInstaller-generated executable.