我只是通过调用测试读写内存ptrace()
,但是当我运行下面显示的代码时,我收到了这样的错误。
Processkey() : 0x80481240
readmem pid 3950
Original opcode : 0x4641682b
writemem pid 3950
readmem pid 3950
PEEKDATA error: No such file or directory
如您所见,readmem()
工作正常,但之后会readmem()
打印错误。
long readmem(int pid, unsigned long addr)
{
long ret=0;
printf("readmem pid %d\n", pid);
ret = ptrace(PTRACE_PEEKDATA, pid, (void *)(addr), 0);
if(ret<0)
{
error("PEEKDATA error");
}
return ret;
}
void writemem(int pid, unsigned long addr, long data)
{
long ret=0;
printf("writemem pid %d\n", pid);
ret = ptrace(PTRACE_POKEDATA, pid, (void *)addr, (void *)data);
if(ret<0)
{
error("POKEDATA error");
}
}
void detach(int pid)
{
long ret=0;
ret = ptrace(PTRACE_DETACH, pid, NULL, NULL);
if(ret<0)
{
error("detach() error");
}
}
void attach(int pid)
{
long ret=0;
ret = ptrace(PTRACE_ATTACH, pid, NULL, NULL);
if(ret<0)
{
error("ptrace() error");
}
ret = waitpid(pid, NULL, WUNTRACED);
if(ret<0)
{
error("waitpid ()");
}
}
int main(int argc, char **argv)
{
long ret = 0;
pid_t pid = 0;
REGS *regs;
unsigned long processkey_addr = 0;
int stat_value = 0;
long op = 0;
pid = atoi(argv[1]);
processkey_addr = 0x80481240;
printf("Processkey() : 0x%lx\n", processkey_addr);
attach(pid);
op = readmem(pid, processkey_addr);
printf("Original opcode : 0x%lx\n", op);
writemem(pid, processkey_addr, 0x41424344);
printf("Changed opcode : 0x%lx\n", readmem(pid, processkey_addr));
detach(pid);
}
但是——这让我发疯了——当我测试没有函数的代码(只是在 中内联代码main()
)时,整个代码运行良好!
有谁知道为什么会发生这种情况...?