简体   繁体   English

如何将马歇尔码 object 转换为 PYC 文件

[英]How to convert Marshall code object to PYC file

I was reversing some obfuscated python code and I found some marshal code object.我正在反转一些混淆的 python 代码,我发现了一些元帅代码 object。 The problem is that I don't find a way to get the source code, I tried dissasemble it but I don't understand much of it, also I've tried pycd or uncompyle6 but I need the PYC file so I was wondering, How do I convert the marshal code object to a PYC file?问题是我找不到获取源代码的方法,我尝试对其进行分解,但我不太了解它,我也尝试过pycd或 uncompyle6 但我需要 PYC 文件所以我想知道,如何将编组代码 object 转换为 PYC 文件?

This is the code example:这是代码示例:

import marshal

code = marshal.loads(b'\xe3\x00\x00\x00\x00......More Stuff')

# And now i want to convert the "code" variable to a PYC file, any ideas?

Thank you谢谢

The source of py_compile gave me an idea. py_compile的源码给了我一个思路。 You can mimic how the Python interpreter "compiles" the source code.您可以模仿 Python 解释器如何“编译”源代码。 However, I'm sure this is undocumented, but it works for me.但是,我确定这是无证的,但它对我有用。

# file test.py
import importlib, sys
# Replace this with your code
code = compile('print(123)', 'file.py', 'exec')
print('Filename:', code.co_filename)
# This is the trick
pyc_data = importlib._bootstrap_external._code_to_timestamp_pyc(code)
print(pyc_data)
with open('file.pyc', 'wb') as f:
    f.write(pyc_data)

Output: Output:

$ python3 test.py
Filename: file.py
bytearray(b'o\r\r\n\x00\x00\x00\x00 ... cut ... \x00\x00\x0c\x00')
$ python3 test.pyc
123

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM