0

我有以下代码:

void
attach_to_pid (int pid, char *username, int pts)
{
  int sys_call_nr = 0;
  struct user_regs_struct regs;
  ptrace (PTRACE_ATTACH, pid, 0, 0);
  waitpid (pid, 0, WCONTINUED);
  ptrace (PTRACE_SETOPTIONS, pid, 0,
          PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEEXIT);
  while (true)
    {
      ptrace (PTRACE_SYSCALL, pid, 0, 0);
      int status;
      waitpid (pid, &status, WCONTINUED);
      if (status == (SIGTRAP | PTRACE_EVENT_EXIT << 8))
        break;
      ptrace (PTRACE_GETREGS, pid, 0, &regs);
#ifdef __i386__
      sys_call_nr = regs.eax;
#elif __x86_64__
      sys_call_nr = regs.rax;
#else
#error "Unsupported architecture."
#endif
      if (sys_call_nr == __NR_write)
        {
          printf ("read!");
        }
      ptrace (PTRACE_SYSCALL, pid, 0, 0);
      waitpid (pid, &status, WCONTINUED);
ptrace(PTRACE_GETREGS,pid,0,&regs);
printf("%d = %d\n",sys_call_nr,regs.eax);
//ptrace(PTRACE_CONT, pid, 0 , 0);
    }
  ptrace (PTRACE_DETACH, pid, 0, 0);
}

我得到了奇怪的结果,如下所示:

-514 = -38
-514 = -38
1 = -38
0 = -38
...

通常,当使用stracesshd 会话进行跟踪时,我总是在写入 shell 时获得对 read 和 write 系统调用的调用。但是有了那个函数,我没有得到那个(我猜是假的)系统调用,只有(如你所见):1、0,等等......

有谁能够帮我?谢谢。

4

2 回答 2

1

这是我的解决方案:

  struct user_regs_struct regs /*_on_entry, regs_on_exit*/ ;
  ptrace (PTRACE_ATTACH, pid, 0, 0);
  waitpid (pid, 0, WCONTINUED);
  while (true)
    {
      ptrace (PTRACE_SYSCALL, pid, 0, 0);
      int status;
      wait (&status);
#ifdef __i386__
      int eax = ptrace (PTRACE_PEEKUSER, pid, ORIG_EAX * 4, 0);
      ptrace (PTRACE_GETREGS, pid, 0, &regs);
      ptrace (PTRACE_SYSCALL, pid, 0, 0);
      waitpid (pid, &status, WCONTINUED);
      if (eax == __NR_read && regs.ebx == 10)
        {
          int *buffer = (int *) malloc (regs.eax + 3);
          int i = 0;
          for (int j = 0; i < regs.eax; i += 4, j++)
            {
              buffer[j] = ptrace (PTRACE_PEEKTEXT, pid, regs.ecx + i, 0);
            }
          if (regs.edx % 4)     // rest of chunk.
            {
              buffer[i] =
                ptrace (PTRACE_PEEKUSER, pid, regs.ecx + 2 * i - regs.eax, 0);
            }
          write (1, buffer, regs.eax);
          free (buffer);
        }
#elif __x86_64__
#else
#error "Unsupported architecture."
#endif
    }

感谢您的回复!

于 2012-03-26T21:46:02.887 回答
1

甚至我也在同一个问题上苦苦挣扎。而您的问题与此完全相同。 那里的答案得到了更精美的解释。这是我的版本:
您的程序需要区分系统调用入口和系统调用出口。
为此维护一个变量。看看这段代码。这里的变量in_syscall也是一样的。
希望这对您有所帮助。
下次在发布问题之前在这里进行基础研究(重新搜索)。也为您节省了很多时间;)也是我们的 :D

于 2012-03-20T18:53:44.517 回答