0

我在一个用 python 编码的游戏中,我可以访问所有模块及其功能。

我不想获得一些较大函数的 .pyc 文件,以便我可以通过 dePython 或类似的东西来放置它。手动读取这些函数将是一个巨大的痛苦。

假设我有 Module.function,我该怎么做才能将该函数放入 .pyc 文件中?

谢谢!

4

2 回答 2

1

您可以导入模块以.pyc自动生成,或者如果您更喜欢以编程方式执行此操作,请使用py_compile模块:http ://docs.python.org/2/library/py_compile.html

于 2013-07-19T04:19:34.070 回答
1

xasmwrite_pycfile()中的函数可以编写一个 pycfile。它需要一个“asm”对象,但它基本上需要一个 Python 解释器版本和一个要编写的代码对象列表。它主要使用来自xdis的函数。这是一个修改后的版本,基本上展示了它是如何工作的:

import xdis
from xdis import PYTHON3
from xdis.magics import magics
from xdis.marsh import dumps
from struct import pack
import time


def write_pycfile(pyc_file, code_list, version=xdis.PYTHON_VERSION):
    if PYTHON3:
        file_mode = 'wb'
    else:
        file_mode = 'w'

    with open(pyc_file, file_mode) as fp:
        fp.write(magics[version])
        timestamp = int(time.time())
        fp.write(pack('I', timestamp))
        if version > 3.2:
            fp.write(pack('I', 0))
        for co in code_list:
            try:
                co_obj = dumps(co, python_version=str(version))
                if PYTHON3 and version < 3.0:
                    co_obj = str.encode(co_obj)
                    pass

                fp.write(co_obj)
            except:
                pass
            pass
    print("Wrote %s" % pyc_file)

write_pycfile("/tmp/test_pyc.pyc", [write_pycfile.__code__])

现在运行它并反汇编:

$ python /tmp/write-pyc.py 
Wrote /tmp/test_pyc.pyc

$ pydisasm /tmp/test_pyc.pyc
# pydisasm version 4.3.2
# Python bytecode 3.6 (3379)
# Disassembled from Python 3.8.2 (default, Mar 28 2020, 12:46:55) 
# [GCC 7.5.0]
# Timestamp in code: 1587126086 (2020-04-17 08:21:26)
# Method Name:       write_pycfile
# Filename:          /tmp/write-pyc.py
# Argument count:    3
# Kw-only arguments: 0
# Number of locals:  8
# Stack size:        18
# Flags:             0x00000043 (NOFREE | NEWLOCALS | OPTIMIZED)
# First Line:        9
# Constants:
#    0: None
...
# Names:
....    
10:           0 LOAD_GLOBAL               0 (PYTHON3)
              2 POP_JUMP_IF_FALSE        10 (to 10)
...

最后使用uncompyle6反编译:

$ uncompyle6 /tmp/test_pyc.pyc
# uncompyle6 version 3.6.5
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.8.1 (default, Jan 23 2020, 17:02:14) 
# [GCC 7.4.0]
# Embedded file name: /tmp/write-pyc.py
# Compiled at: 2020-04-17 08:21:26
if PYTHON3:
    file_mode = 'wb'
else:
    file_mode = 'w'
with open(pyc_file, file_mode) as (fp):
    fp.write(magics[version])
    timestamp = int(time.time())
    fp.write(pack('I', timestamp))
    if version > 3.2:
        fp.write(pack('I', 0))
    for co in code_list:
        try:
            co_obj = dumps(co, python_version=(str(version)))
            if PYTHON3:
                if version < 3.0:
                    co_obj = str.encode(co_obj)
            fp.write(co_obj)
        except:
            pass

print('Wrote %s' % pyc_file)
# okay decompiling /tmp/test_pyc.pyc
于 2020-04-17T12:34:04.813 回答