我正在学习 ptrace 的用法。
我尝试了以下简单示例,但引发了 I/O 错误。
我想从其他进程覆盖“Hello, world”(打印字符串)。
目标程序打印“Hello, world”字符串直到停止,然后我想从另一个使用 ptrace 的程序更改此打印字符串。
然而,ptrace 程序未能通过 I/O 错误 POKEDATA。
我不知道为什么它失败了。
我试过:
x86_64 Linux 5.11。
AMD 锐龙 7 4750U
ptrace 代码:(此 ptrace 代码来自http://0xcc.net/blog/archives/000077.html(日语))
#include <assert.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
int
main (int argc, char **argv)
{
assert(argc == 4);
pid_t pid = atoi(argv[1]);
void *addr = (void *)strtol(argv[2], NULL, 0);
void *word = (void *)strtol(argv[3], NULL, 0);
assert(ptrace(PTRACE_ATTACH, pid, NULL, NULL) == 0);
wait(NULL);
if (ptrace(PTRACE_POKEDATA, pid, addr, word) != 0) {
printf("Error: %s\n", strerror(errno));
return 0;
}
assert(ptrace(PTRACE_DETACH, pid, NULL, NULL) == 0);
return 0;
}
目标代码:
#include <stdio.h>
#include <stdbool.h>
int main() {
while (true) {
printf("%s\n", "Hello, world");
}
}
目标二进制文件中目标字符串的地址(在我的环境中):
Contents of section .rodata:
2000 01000200 48656c6c 6f2c2077 6f726c64 ....Hello, world
执行(外壳):
./ptrace target_process_id 0x2004 0x60616263 //try to replace from Hell to abcdl