本文主要研究父进程利用ptrace系统调用访问子进程的内存。
跟踪进程能够查看被跟踪进程的地址空间并且查看进程某个内存地址锁存储的变量的值。在这里我们需要使用linux内核的nm工具对程序进行分析从而获取到符号的地址信息。
本文分析的例子就是通过变量的地址修改变量的值。具体程序如下:
child4:
#include "ptrace.h"
void main(int argc,char *argv[])
{
pid_t traced_process;
traced_process=atoi(argv[1]);
ptrace(PTRACE_ATTACH,traced_process,NULL,NULL);
wait(NULL);
int data=ptrace(PTRACE_PEEKDATA,traced_process,0x080496f4,0);
printf("data=%d\n",data);
data=245;
ptrace(PTRACE_POKEDATA,traced_process,0x080496f4,data);
ptrace(PTRACE_DETACH,traced_process,0,0);
}
list9.c
#include "ptrace.h"
int i;
void main()
{
i=143;
printf("child 4 start ...\n");
while(i!=245);
printf("child 4 outside loop ...\n");
}
首先在一个中断后台运行child4.o
#./child4.o\ &
然后使用ps命令查看该进程的进程