【问题标题】:How to modify EIP's tracee forked procee?如何修改 EIP 的 tracee fork procee?
【发布时间】:2016-08-05 19:10:45
【问题描述】:

我正在开发一个包含 ptrace 的 Linux 应用程序,以观察由 fork() 系统调用创建的另一个进程。

严格来说:我想在分叉进程(智利进程或“tracee”)中实现故障注入。

如下图所示:

跟踪器通过使用 PTRACE_GETREGS 请求从被跟踪者获取 regs (struct_user_regs) 结构。之后,tracer修改tracee的EIP值(当内核切换到tracee时,命令执行会违反所谓的控制流错误CFE)。然后 PTRAC E_CONT 请求将发送给 tracee 以继续执行。

很遗憾,修改EPI的tracee后,由于(segmentation fault),tracee没有继续执行。 如何为被跟踪 EIP 提供另一个合适的值?

这里是代码

#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include<sys/user.h>
#include<sys/reg.h>
#include<stdlib.h>
#include<stdio.h>
#include <asm/ptrace-abi.h>

 int main()

 { 

  pid_t child;
  int status;
  int sum=0;
  struct user_regs_struct regs;


    child = fork();
    if(child == 0) {
        ptrace(PTRACE_TRACEME, 0, NULL, NULL);

         printf("hello world 1\n");
         printf("hello world 2\n");
         raise (SIGINT); // just to move control to the tracer 
         printf("hello world 3\n");
         printf("hello world 4\n");
         printf("hello world 5\n");

         exit(EXIT_SUCCESS);
     }
    else {

          wait(NULL);
          ptrace(PTRACE_GETREGS, child,NULL, &regs);
          printf("\n EIP @  0x %#lx\n",regs.eip);
          //get the tracee EIP
          long int new_eip=ptrace(PTRACE_PEEKTEXT, child,regs.eip,NULL);
          //chabge EIP and poke it again
          new_eip += ???; // make change that let to jump to another tracee instruction address (say to print hello world 5)
          ptrace(PTRACE_POKETEXT, child,regs.eip,new_eip);
          ptrace(PTRACE_CONT, child, NULL, NULL);

          }

    return 0;
}

有什么想法吗? 感谢您的所有帮助。

【问题讨论】:

  • 如果您希望任何人知道您做错了什么,您需要展示您的代码。
  • @Barmar,代码已添加:)
  • 使用gdb或Qt调试器之类的调试器,无法调试tracee。

标签: linux linux-kernel signals ptrace


【解决方案1】:

您不是在修改 EIP,而是在 EIP 的指令值中添加了一些内容,并且可能导致地址引用错误。要更改EIP,请使用PTRACE_SETREGS

      wait(NULL);
      ptrace(PTRACE_GETREGS, child,NULL, &regs);
      printf("\n EIP @  0x %#lx\n",regs.eip);
      regs.eip += ???;
      ptrace(PTRACE_SETREGS, child, NULL, &regs);
      ptrace(PTRACE_CONT, child, NULL, NULL);

【讨论】:

  • 是的,你是对的。但是将它添加到 EIP 以指向下一条或任何指令的合适值是什么。我试过了: unsigned instruction_=ptrace(PTRACE_PEEKTEXT,child,regs.eip.NULL);然后,regs.eip+=sizeof(instruction);我也将指令_声明为长整数,但它也不起作用。
  • 在一条指令中添加字节数。这取决于 CPU 架构,甚至取决于当前 EIP 上的指令是什么,如果指令具有不同的长度。
【解决方案2】:

是的,它完全是特定于平台的。我阅读了很多描述 ptrace 系统调用的材料,以及我们如何使用它来捕获在 ptrace 向暂停的跟踪对象发送一些请求(如 PTRACE_CONT)后将要执行的下一条指令的 EIP。 我总是看到 EIP 值是这样的:

 80484a6:
 80484a7:   
 80484ac:   
 80484b2:
 80484b4:
 80484b6:
 80484b8:

我测试了一个简单的代码(只打印 EIP 的值和相应的执行指令)。 但结果如下所示:

EIP: b773cbe0 Instruction executed: c3595a5d
EIP: b773cbe1 Instruction executed: ccc3595a
EIP: b773cbe0 Instruction executed: c3595a5d
hello world 5
EIP: b773cbe0 Instruction executed: c3595a5d
EIP: b773cbe1 Instruction executed: ccc3595a
EIP: b773cbe0 Instruction executed: c3595a5d
hello world 6
EIP: b773cbe0 Instruction executed: c3595a5d
EIP: b773cbe1 Instruction executed: ccc3595a
EIP: b773cbe0 Instruction executed: c3595a5d

这是什么 (b773cbe0) ??!!! 我移至 gdb 并使用此命令行 (objdump -d a.out) 查看了转储文件 结果与之前的结果有些不同

 8048864:   8b 6c 24 20             mov    0x20(%esp),%ebp
 8048868:   8d b3 0c ff ff ff       lea    -0xf4(%ebx),%esi
 804886e:   e8 41 fb ff ff          call   80483b4 <_init>
 8048873:   8d 83 08 ff ff ff       lea    -0xf8(%ebx),%eax
 8048879:   29 c6                   sub    %eax,%esi
 804887b:   c1 fe 02                sar    $0x2,%esi
 804887e:   85 f6                   test   %esi,%esi
 8048880:   74 23                   je     80488a5 <__libc_csu_init+0x55>
 8048882:   8d b6 00 00 00 00       lea    0x0(%esi),%esi
 8048888:   83 ec 04                sub    $0x4,%esp
 804888b:   ff 74 24 2c             pushl  0x2c(%esp)
 804888f:   ff 74 24 2c             pushl  0x2c(%esp)
 8048893:   55                      push   %ebp
 8048894:   ff 94 bb 08 ff ff ff    call   *-0xf8(%ebx,%edi,4)
 804889b:   83 c7 01                add    $0x1,%edi
 804889e:   83 c4 10                add    $0x10,%esp
 80488a1:   39 f7                   cmp    %esi,%edi
 80488a3:   75 e3                   jne    8048888 <__libc_csu_init+0x38>
 80488a5:   83 c4 0c                add    $0xc,%esp
 80488a8:   5b                      pop    %ebx
 80488a9:   5e                      pop    %esi
 80488aa:   5f                      pop    %edi
 80488ab:   5d                      pop    %ebp
 80488ac:   c3                      ret    
 80488ad:   8d 76 00                lea    0x0(%esi),%esi

我真的很困惑。

【讨论】:

  • @Barmar,这是什么 (b773cbe0) ?为什么会有不同的结果?
猜你喜欢
  • 2014-05-01
  • 2011-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多