使用 ptrace 解析 Call 和 Ret。

2024-01-27

我尝试使用 ptrace 解析可执行文件中的所有 Calls 和 Rets。 符合x64操作码 http://ref.x86asm.net/coder64.html,我找到了操作码呼叫:0xe8并为重试:0xc3、0xc2、0xca、0xcb.

自从我解析它们后,我发现重击次数多于调用次数。

有我跟踪的程序:

void func()                                                                                    
{                                                                                              
  write(1, "i", 1);                                                                            
}                                                                                              

int main(int ac)                                                                               
{                                                                                              
  func();                                                                                      
  return(0);                                                                                   
}

这是我的追踪器:

int                     tracer(t_info *info)                                                   
{                                                                                              
  int                   status;                                                                
  long                  ptr;                                                                   
  int                   ret = 0;                                                                                 
  int                   call = 0;                                                                                


  waitpid(info->pid, &status, 0);                                                              
  while (WIFSTOPPED(status))                                                                   
    {                                                                                          
      ptrace(PTRACE_GETREGS, info->pid, NULL, info->regs);                                     
      ptr = ptrace(PTRACE_PEEKDATA, info->pid, info->regs->rip);                               
      if (((ptr & 0x000000ff) == 0xe8)) // Opcode for call                                                              
        {                                                                                      
          call++;                                                                              
        }                                                                                      
      else if (((ptr & 0x000000ff) == 0xc3) // Opcodes for rets                                
               || ((ptr & 0x000000ff) == 0xc2)                                                 
               || ((ptr & 0x000000ff) == 0xca)                                                 
               || ((ptr & 0x000000ff) == 0xcb))                                                
        {                                                                                      
          ret++;                                                                               
        }                                                                                      
      ptrace(PTRACE_SINGLESTEP, info->pid, 0, 0);                                              
      waitpid(info->pid, &status, 0);                                                          
    }                                                                                          
  printf("Calls: %i\nRets: %i\nDiff: %i\n", call, ret, call - ret);                                             
  return (0);                                                                                  
}

这是我的输出:

Calls: 656
Rets: 666
Diff: -10

为什么数量不一样rets and calls? 我错过了一些操作码吗? 有没有不返回的函数?


例如,您错过了间接调用,例如

callq *(<expr>)

使用其他操作码。 Libc 标准初始化例程利用了这些。根据表达式,可能有多种操作码,两个示例:

ff d0                   callq  *%rax
41 ff 14 dc             callq  *(%r12,%rbx,8)

想要全部获得可能并不容易。也许使用 libbfd 和 libopcodes 等库来解码指令会更容易、更清晰

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 ptrace 解析 Call 和 Ret。 的相关文章

随机推荐