小编Wal*_*ter的帖子

如何在Linux上调用Python中的内联机器代码?

我正在尝试从Linux上的纯Python代码调用内联机器代码.为此,我将代码嵌入到字节文字中

code = b"\x55\x89\xe5\x5d\xc3"
Run Code Online (Sandbox Code Playgroud)

然后调用mprotect()via ctypes以允许执行包含代码的页面.最后,我尝试ctypes用来调用代码.这是我的完整代码:

#!/usr/bin/python3

from ctypes import *

# Initialise ctypes prototype for mprotect().
# According to the manpage:
#     int mprotect(const void *addr, size_t len, int prot);
libc = CDLL("libc.so.6")
mprotect = libc.mprotect
mprotect.restype = c_int
mprotect.argtypes = [c_void_p, c_size_t, c_int]

# PROT_xxxx constants
# Output of gcc -E -dM -x c /usr/include/sys/mman.h | grep PROT_
#     #define PROT_NONE 0x0
#     #define PROT_READ 0x1
#     #define PROT_WRITE 0x2
#     #define PROT_EXEC …
Run Code Online (Sandbox Code Playgroud)

python linux ctypes mprotect

16
推荐指数
1
解决办法
1581
查看次数

标签 统计

ctypes ×1

linux ×1

mprotect ×1

python ×1