可以从脚本执行Python字节码吗? [英] Possible to execute Python bytecode from a script?
问题描述
假设我有一个正在运行的CPython会话,
Say I have a running CPython session,
是否可以运行数据(字节
)直接从 pyc
文件中删除?
(不必在磁盘上存储数据,也不必编写临时pyc文件)
Is there a way to run the data (bytes
) from a pyc
file directly?
(without having the data on-disk necessarily, and without having to write a temporary pyc file)
示例脚本显示一个简单的用例:
Example script to show a simple use-case:
if foo:
# Intentionally ambiguous, since the data source
# is a detail and answers shouldn't depend this detail.
data = read_data_from_somewhere()
else:
data = open("bar.pyc", 'rb').read()
assert(type(data) is bytes)
code = bytes_to_code(data)
# call a method from the loaded code
code.call_function()
精确使用并不重要,但可以动态生成代码并通过网络复制到execute是一个用例(出于考虑这个问题的目的)。
Exact use isn't important, but generating code dynamically and copying over a network to execute is one use-case (for the purpose of thinking about this question).
以下是一些示例用例,这让我很好奇如何做到:
Here are some example use-cases, which made me curious to know how this can be done:
- 检查Python脚本中是否存在恶意代码。
单个命令可以访问隐藏在二进制数据中的大量代码,该命令将是什么样? - 动态生成代码并将其缓存以供重用(不一定在磁盘,例如可以使用数据库。)
- 能够将预编译的字节码发送到进程,控制应用程序w hich为例如嵌入Python。
- Checking Python scripts for malicious code.
If a single command can access a larger body of code hidden in binary data, what would that command look like? - Dynamically generate code and cache it for re-use (not necessarily on disk, could use a data-base for example).
- Ability to send pre-compiled byte-code to a process, control an application which embeds Python for eg.
推荐答案
有没有一种方法可以直接从pyc文件运行数据?
Is there a way to run the data from a pyc file directly?
可以使用元帅
import marshal
bytes = marshal.dumps(eggs)
字节可以转换回代码对象
the bytes can be converted back to a code object
eggs = marshal.loads(bytes)
exec(eggs)
一个 pyc
文件是带有标头的封送代码对象
A pyc
file is a marshaled code object with a header
对于Python3,标头为需要跳过12个字节,其余数据可以通过 marshal.loads
读取。
For Python3, the header is 12 bytes which need to be skipped, the remaining data can be read via marshal.loads
.
请参见内德·巴切尔德的博客文章:
在简单级别上,.pyc文件是二进制文件仅包含三个内容:
At the simple level, a .pyc file is a binary file containing only three things:
- 一个四字节的幻数,
- 一个四字节的修改时间戳记和
- 编组代码对象。
注意,该链接引用了Python2,但在Python3中几乎相同, pyc
标头大小仅为12个字节,而不是8个字节
Note, the link references Python2, but its almost the same in Python3, the pyc
header size is just 12 instead of 8 bytes.
这篇关于可以从脚本执行Python字节码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!