使用python执行汇编代码

发布于 2024-11-08 03:20:24 字数 174 浏览 2 评论 0原文

我想在 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 技术交流群。

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

发布评论

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

评论(7

倒带 2024-11-15 03:20:24

作为一个具体示例,以下是如何调用一个函数,该函数将接受一个 int 并返回加一的值。

为了获取设置了可执行标志的内存,使用了mmap模块。

要调用该函数,需要使用ctypes模块。

为了将机器代码放入内存,有 x86-64 机器代码的硬编码字节串。

代码将打印 43。

实际上,我会在 C 共享对象库中编写代码,并在 C 中使用内联汇编。然后,我会使用 cffi 加载并运行该库。这个例子的优点是它是独立的,只需要标准的Python库。

import ctypes
import mmap

buf = mmap.mmap(-1, mmap.PAGESIZE, prot=mmap.PROT_READ | mmap.PROT_WRITE | mmap.PROT_EXEC)

ftype = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int)
fpointer = ctypes.c_void_p.from_buffer(buf)

f = ftype(ctypes.addressof(fpointer))

buf.write(
    b'\x8b\xc7'  # mov eax, edi
    b'\x83\xc0\x01'  # add eax, 1
    b'\xc3'  # ret
)

r = f(42)
print(r)

del fpointer
buf.close()

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.

import ctypes
import mmap

buf = mmap.mmap(-1, mmap.PAGESIZE, prot=mmap.PROT_READ | mmap.PROT_WRITE | mmap.PROT_EXEC)

ftype = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int)
fpointer = ctypes.c_void_p.from_buffer(buf)

f = ftype(ctypes.addressof(fpointer))

buf.write(
    b'\x8b\xc7'  # mov eax, edi
    b'\x83\xc0\x01'  # add eax, 1
    b'\xc3'  # ret
)

r = f(42)
print(r)

del fpointer
buf.close()
盛夏已如深秋| 2024-11-15 03:20:24

您可以将程序集直接嵌入到 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.

紫竹語嫣☆ 2024-11-15 03:20:24

实现此目的的一种方法是为 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.

盗琴音 2024-11-15 03:20:24

您可以使用 ASM 编写自己的 DLL,并从 Python 中调用其函数。

You can write your own DLL using ASM and call its functions from within Python.

坏尐絯℡ 2024-11-15 03:20:24

理论上,您可以:

  1. 用 C 编写一个简单的函数来调用程序
  2. 集 使用 CythonPyrex 从Python调用该函数

我不得不承认我没有使用过Pyrex或 Cython,但它们可能能够满足您的需求,而无需费力编写完整的 C 扩展。

In theory, you could:

  1. Write a simple function in C that calls the assembly
  2. Use Cython or Pyrex to call that function from Python

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.

无名指的心愿 2024-11-15 03:20:24

Python 不支持这种类型的低级硬件交互。

Python does not support this type of low level hardware interaction.

甜中书 2024-11-15 03:20:24

我很有可能用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/

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