是否可以将已编译的 .pyc 文件反编译为 .py 文件?

发布于 2024-10-21 12:47:02 字数 38 浏览 5 评论 0原文

是否可以从 .py 文件生成的 .pyc 文件中获取一些信息?

Is it possible to get some information out of the .pyc file that is generated from a .py file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

影子是时光的心 2024-10-28 12:47:02

简介

Python 反编译是一个难题,特别是对于最近的 3.x 版本。这就是为什么有各种各样的工具,但都有局限性和错误。

然而,Python 2.7 和更早的 3.x 版本应该可以很好地工作,即使部分反编译也比丢失所有内容要好。

尝试工具

Uncompyle6 对于 Python 3.8 及以下版本有效,但存在一些错误,并且适用于 2.7 。

  • 首先推荐选项,因为它的目标是统一早期的分叉并专注于自动化单元测试。
  • uncompyle6 GitHub 页面有更多详细信息。
  • 最适合早期的 3.x 版本,而不是 3.7+ 的最佳选择

如果这不起作用,最好接下来尝试这个 - 特别是对于 Python 3.7+:

  • decompyle3 是 Uncompyle6 的一个分支,来自同一作者,应该在 3.7 和 3.8 上工作得更好。
  • 注意:decompyle3 软件包来自 rocky/decompile3 存储库 - 拼写不同,但内容相同

如果您仍然遇到问题,请检查 uncompyle6decompyle3 自述文件链接到可能更适合您的代码的其他工具。

限制

您可以取回代码,包括变量名称和文档字符串,但不带注释。

某些代码可能无法成功反编译,特别是使用不寻常的控制流或更新的 Python 3.x 版本时。这是由于这些反编译器中的错误导致 Python 随着时间的推移更改了其字节码。

支持最新的 Python 版本

uncompyle6decompyle3 均不支持 Python 3.9 或更高版本,并且对 3.7 或更高版本的支持有限。

Python 中的新优化使反编译变得更加困难,并且这两个项目都缺乏代码贡献和赞助。

您可以提供帮助:

  • 在检查类似问题后,针对这些存在 bug 的项目提出 GitHub 问题 - 两者都在一系列 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.

  • Recommended option to start with as it's aiming to unify earlier forks and focusing on automated unit testing.
  • The uncompyle6 GitHub page has more details.
  • Works best for earlier 3.x versions, not best choice for 3.7+

If that doesn't work, it's probably best to try this next - particularly for Python 3.7+:

  • decompyle3 is a fork of Uncompyle6, from same author, that should work better for 3.7 and 3.8.
  • Note: this decompyle3 package is from the rocky/decompile3 repo - different spelling but same thing

If you still have problems, check the uncompyle6 and decompyle3 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 or decompyle3 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:

  • Raise GitHub issues for these projects with bugs, after checking for similar issues - both run unit test suites on a range of Python versions.
  • Sponsor these projects, particularly if they helped you

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.

暖树树初阳… 2024-10-28 12:47:02

您可以尝试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.

辞别 2024-10-28 12:47:02

我使用 uncompyle6 反编译(甚至支持最新的 Python 3.8.0):

uncompyle6 utils。 cpython-38.pyc > > utils.py

以及原始 python 和反编译的 python 比较如下所示:

pyc uncompile utils

所以你可以看到,几乎一样,反编译效果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:

pyc uncompile utils

so you can see, ALMOST same, decompile effect is VERY GOOD.

绻影浮沉 2024-10-28 12:47:02

是的,您可以通过 pypi 上的 unpyclib 获取它。

$ pip install unpyclib

您可以反编译 .pyc 文件

$ python -m unpyclib.application -Dq path/to/file.pyc

Yes, you can get it with unpyclib that can be found on pypi.

$ pip install unpyclib

Than you can decompile your .pyc file

$ python -m unpyclib.application -Dq path/to/file.pyc
梦言归人 2024-10-28 12:47:02

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

提笔书几行 2024-10-28 12:47:02

我已经研究了几个小时,终于找到了使用 Decompyle++ 的解决方案:

  • 访问 https://cmake.org/download/ 并安装 CMake。
  • 访问 https://github.com/zrax/pycdc 并获取此存储库的副本:pycdc-master 。
  • 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++:

  • visit https://cmake.org/download/ and install CMake.
  • visit https://github.com/zrax/pycdc and grab a copy of this repo: pycdc-master.
  • add C:\Program Files\CMake\bin to your system environment variables under PATH.

I suggest putting your pycdc-master folder into another folder, like anotherFolder.

Now you can run these commands in the command line:

  • cd anotherFolder to go into the folder that has pycdc-master in it.
  • cmake pycdc-master
  • cd ../ to go up one directory,
  • then: cmake --build anotherFolder

pycdc.exe will then be in anotherFolder\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.

葬花如无物 2024-10-28 12:47:02

是的,这是可能的。

有一个完美的开源 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.

故笙诉离歌 2024-10-28 12:47:02

安装使用
pip install pycompyle6

pycompyle6 文件名.pyc

Install using
pip install pycompyle6

pycompyle6 filename.pyc

梦归所梦 2024-10-28 12:47:02

如果您需要反编译 pyc 但安装了 python 3.9,您可以强制 uncompyle6 运行。它并不完美,但确实有效。只需编辑 site-packages\uncompyle6\bin\uncompile.py

def main_bin():
if not (sys.version_info[0:2] in ((2, 6), (2, 7), (3, 0),
                                  (3, 1), (3, 2), (3, 3),
                                  (3, 4), (3, 5), (3, 6),
                                  (3, 7), (3, 8), (3, 9)

只需以与其他版本相同的格式添加您已安装的版本并保存即可。它至少会运行。

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

def main_bin():
if not (sys.version_info[0:2] in ((2, 6), (2, 7), (3, 0),
                                  (3, 1), (3, 2), (3, 3),
                                  (3, 4), (3, 5), (3, 6),
                                  (3, 7), (3, 8), (3, 9)

Just add the version you have installed in the same format as the others and save. It will at least run.

洋洋洒洒 2024-10-28 12:47:02

标题:从 PyInstaller 生成的可执行文件中提取文件

问题:

如何从 PyInstaller 创建的可执行文件中提取文件?

解决方案:

  1. 克隆 pyinstxtractor-ng 存储库:< /p>

    git 克隆 https://github.com/pyinstxtractor/pyinstxtractor-ng.git
    
  2. 提取可执行文件:

    cd pyinstxtractor-ng
    python pyinstxtractor-ng.py“
  3. 安装依赖项:

    sudo yum install cmake make clang
    
  4. 克隆pycdc存储库:

    git 克隆 https://github.com/zrax/pycdc.git
    
  5. 构建并编译:

    cd pycdc
    cmake CMakeLists.txt
    制作
    
  6. 反编译字节码:

    ./pycdc“

通过执行以下步骤,您可以从 PyInstaller 生成的可执行文件中提取并反编译文件。


Title: Extracting Files from PyInstaller-Generated Executable

Problem:

How can I extract files from an executable created by PyInstaller?

Solution:

  1. Clone the pyinstxtractor-ng repository:

    git clone https://github.com/pyinstxtractor/pyinstxtractor-ng.git
    
  2. Extract the executable:

    cd pyinstxtractor-ng
    python pyinstxtractor-ng.py "<path_to_/dist/app>"
    
  3. Install dependencies:

    sudo yum install cmake make clang
    
  4. Clone the pycdc repository:

    git clone https://github.com/zrax/pycdc.git
    
  5. Build and compile:

    cd pycdc
    cmake CMakeLists.txt
    make
    
  6. Decompile the bytecode:

    ./pycdc "<path_to_/app_extracted/app.pyc>"
    

By following these steps, you can extract and decompile files from a PyInstaller-generated executable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文