使用python执行汇编代码
我想在 python 脚本中执行汇编代码。这可能吗?
在 C 语言中编程是这样的
static inline getesp(){
__asm__("mov %esp, %eax");
}
,但是用 Python 该怎么做呢?是否可以?
I want to execute assembly code inside a python script. Is that possible?
In C programming would be like this
static inline getesp(){
__asm__("mov %esp, %eax");
}
But how to do that with Python? Is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
作为一个具体示例,以下是如何调用一个函数,该函数将接受一个 int 并返回加一的值。
为了获取设置了可执行标志的内存,使用了
mmap
模块。要调用该函数,需要使用
ctypes
模块。为了将机器代码放入内存,有 x86-64 机器代码的硬编码字节串。
代码将打印 43。
实际上,我会在 C 共享对象库中编写代码,并在 C 中使用内联汇编。然后,我会使用
cffi
加载并运行该库。这个例子的优点是它是独立的,只需要标准的Python库。As a specific example, here is how to call a function which will take an int and return it incremented by one.
To obtain memory with the executable flag set,
mmap
module is used.To call the function,
ctypes
module is used.To put the machine code into memory, there is hardcoded byte string of x86-64 machine code.
The code will print 43.
In practice, I'd write the code in C shared object library and use inline assembly in C. I'd then use
cffi
to load and run the library. The advantage of this example is that it is self-contained and only needs the standard Python library.您可以将程序集直接嵌入到 Python 程序中:
这些工作通过编译程序集并将其加载到运行时的可执行内存。前三个项目在 Python 中实现 x86-64 或 x86 汇编器,而最后一个项目则调用外部编译器。
You can embed assembly directly inside your Python program:
These work by compiling the assembly and loading it into executable memory at runtime. The first three projects implement x86-64 or x86 assemblers in Python, whereas the last calls out to an external compiler.
实现此目的的一种方法是为 Python 编写一个 (C) 扩展。您可以查看此文档,了解如何执行此操作的完整详细信息。
开发基于 C 的 Python 扩展的另一种方法是使用 ctypes 模块直接与外部库交互。
无论如何,您都需要将一些 C 代码编译到库或扩展中,以及从 Python 调用它的方法。显然,对于您想要实现的目标来说,这可能不是最佳选择,但实际上公开一些功能并没有那么多工作。
One way you could do this would be to write a (C) extension for Python. You can take a look at this documentation for full details of how to do that.
Another way of developing C-based Python extensions would be to interface directly with an external library using the ctypes module.
In any case, you'd need some C code compiled into either a library or an extension and a way to call it from Python. Clearly for what you want to achieve this is probably not optimal but actually its not that much work to expose a few functions.
您可以使用 ASM 编写自己的 DLL,并从 Python 中调用其函数。
You can write your own DLL using ASM and call its functions from within Python.
理论上,您可以:
我不得不承认我没有使用过Pyrex或 Cython,但它们可能能够满足您的需求,而无需费力编写完整的 C 扩展。
In theory, you could:
I have to admit that I haven't used either Pyrex or Cython, but they might be able to do what you need without having to go to the trouble of writing a full blown C extension.
Python 不支持这种类型的低级硬件交互。
Python does not support this type of low level hardware interaction.
我很有可能用Python编写了一个小型汇编程序,我使用的一些库可能支持Ctypes,但我使用纯Python,大多数语言实际上在低级别进行接口,我们只是使用HLL Langage功能而不付费适当注意代码的处理方式,我还在 Visual Basic 中编写了一个使用 ASM x86 代码的小型 POC 图像编辑应用程序
我实际上不确定如何将其编辑成我想说的内容。除了可能读取 asm 代码并在脚本本身内部工作的函数之外。我确实相信我的想法被指出是错误的。 ASM 代码可以通过使用脚本函数来执行,该函数读取代码的该区域并编译它们。几乎就好像它是一个内置的即时汇编器。我试图帮助一个不太出色的演讲者(或者在这种情况下可能是作家),这里的页面也许能够更好地解释我想说的内容
http:// /code.activestate.com/recipes/579037-how-to-execute-x86-64-bit-assemble-code-directly-f/
It is highly possible I made a small assembler in python, some of the libraries I used may have supported Ctypes, but I used pure Python, Most languages do actually interface at a low level, We are just using the HLL Langage features and not paying appropriate attention to how the code is being processed, I have also written a small POC image editing app in visual basic that used ASM x86 Code
I am not actually sure on how to edit this into what I was trying to say. except maybe functions that would read the asm code and work from within the script itself. I do believe my thoughts were pointed out to be wrong. ASM code can be executed through the use of scripted functions that read that area of the code and compile them. almost as if it were a built in on the fly assembler. I try to help not so great a speaker though (or writter in this case as it may be) this page here maybe able to explain better on what I am trying to say
http://code.activestate.com/recipes/579037-how-to-execute-x86-64-bit-assembly-code-directly-f/