我正在尝试在Python 3中实现一个调试器。主要思想非常简单:用ctypes包装系统调用“process_vm_readv”然后在其他进程上调用它。
我还创建了一个小的虚拟C ++程序,供我使用此工具进行调试。以下是两者的来源:
#!/usr/bin/python3
import typing
import ctypes
import os
libc = ctypes.cdll.LoadLibrary("libc.so.6")
def _error_checker(result, function, arguments):
if result == -1:
errno = ctypes.get_errno()
raise OSError(errno, os.strerror(errno))
class IOBuffer(ctypes.Structure): # iovec struct
_fields_ = [("base", ctypes.c_void_p),
("size", ctypes.c_size_t)]
_read_process_memory = libc.process_vm_readv
_read_process_memory.restype = ctypes.c_ssize_t
_read_process_memory.errcheck = _error_checker
_read_process_memory.args = [ctypes.c_ulong, ctypes.POINTER(IOBuffer),
ctypes.c_ulong, ctypes.POINTER(IOBuffer),
ctypes.c_ulong, ctypes.c_ulong]
def read_process_memory(pid: int, base: int, size: int) -> typing.Tuple[int, bytes]:
buffer = (ctypes.c_char * size)()
local = IOBuffer(ctypes.addressof(buffer), size)
remote = IOBuffer(base, size)
return _read_process_memory(pid, local, 1, remote, 1, 0), buffer.raw
#include <iostream>
#include <stdio.h>
using namespace std;
int main(void){
int a = 99;
int c;
while((c = getchar()) != EOF)
cout << "int a=" << a << ";\t&a=" << &a << endl;
return 0;
}
我的问题在于,每当我用我的虚拟程序的pid调用“read_process_memory”时,它提供给我的内存地址和数字4(int
的大小)作为参数 - 这应该有效 - 包装的系统调用返回-1(错误)。当发生这种情况时,errcheck
报告该操作的errno,该操作总是为零。 “错误成功”。由于这个无用的错误消息,我不知道如何解决这个问题。你们对如何解决这个问题有什么想法吗?
答案 0 :(得分:1)
由于这个无用的错误消息,我不知道如何解决此问题
您可以始终找出内核使用strace返回的真实错误。这样的事情应该有效:
strace -e process_vm_readv python test.py
答案 1 :(得分:1)
根据Employed Russian's answer的建议,我在命令行中添加了strace -e process_vm_readv
调试工具。它给了我以下错误:
process_vm_readv(3464,
[{iov_base=NULL, iov_len=0},
{iov_base=NULL, iov_len=0},
{iov_base=0x7f9d39c0c4f8, iov_len=140313255527400},
{iov_base=0xfffffffffffffffa, iov_len=4}],
4, 0x1, 140726046508276, 4)
= -1 EINVAL (Invalid argument)
稍微调整代码后,我将此错误消除了,将read_process_memory
函数更改为:
def read_process_memory(pid: int, base: int, size: int) -> typing.Tuple[int, bytes]:
buffer = (ctypes.c_char * size)()
local = (IOBuffer * 1)()
local[0].base, local[0].size = ctypes.addressof(buffer), size
remote = (IOBuffer * 1)()
remote[0].base, remote[0].size = base, size
return _read_process_memory(pid, local, 1, remote, 1, 0), buffer.raw
我现在唯一的问题是strace向我扔了以下错误:
process_vm_readv(3464,
[{iov_base=0x7fedc6f64450, iov_len=4}], 1,
[{iov_base=0x7ffd560344f4, iov_len=4}], 1, 0)
= -1 EPERM (Operation not permitted)
我以root身份运行它来响应,解决了最后一个问题。
编辑:正如Mark Tolonen所建议的,不是在read_process_memory
中声明两个大小为1的数组,而是使用ctypes.byref
将这两个结构传递给包装函数,如下所示:
def read_process_memory(pid: int, base: int, size: int) -> typing.Tuple[int, bytes]:
buffer = (ctypes.c_char * size)()
local = IOBuffer(ctypes.addressof(buffer), size)
remote = IOBuffer(base, size)
return _read_process_memory(pid, ctypes.byref(local), 1, ctypes.byref(remote), 1, 0), buffer.raw