【发布时间】:2018-11-08 17:59:23
【问题描述】:
我想从正在运行的进程中获取指令并使用 ptrace 进行更改。当变量 instr(包含当前指令 - PTRACE_PEEKDATA)未签名时,一切正常,但是当我将其更改为 long int 时,出现错误(内存转储)。 ptrace(PTRACE_PEEKDATA, ...) 返回 long int 所以这应该不是问题。我在 Ubuntu 上工作。
我在哪里做错了?我是新手,所以这可能会很愚蠢。
我的代码:
#include <stdio.h>
#include <sys/ptrace.h>
#include <sys/user.h>
#include <sys/types.h>
#include <stdlib.h>
#include <wait.h>
int main()
{
int status;
char *pid_char;
pid_t PID;
struct user_regs_struct reg; /* register */
long int instr;
unsigned changedInstr;
printf("Tracee PID: ");
scanf("%s", pid_char);
PID = atoi(pid_char);
printf("\n");
/* PTRACE STARTS */
ptrace(PTRACE_ATTACH, PID, NULL, NULL);
waitpid(PID, &status, 0);
ptrace(PTRACE_GETREGS, PID, NULL, ®);
instr = ptrace(PTRACE_PEEKDATA, PID, reg.rip, NULL);
printf("Current Instruction: %llx\n", instr);
scanf("%u", &changedInstr);
ptrace(PTRACE_POKEDATA, PID, reg.rip, &changedInstr);
ptrace(PTRACE_DETACH, PID, NULL, NULL);
return 0;
}
【问题讨论】:
-
On error, all requests return -1, and errno is set appropriately.你检查errno了吗?你把instr从unsigned long改了?这是唯一的变化吗?我怎样才能用一些程序重现它?