C pts系统调用

C pts系统调用,c,system-calls,ptrace,C,System Calls,Ptrace,我有以下代码: 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_TRACES

我有以下代码:

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);
}
通常,当使用
strace
sshd会话进行strace时,我总是在写入shell时获得对读写系统调用的调用。但使用该函数,我并没有获得(我猜是假的)系统调用,只是(如您所见):1、0等等


有人能帮我吗?谢谢。

就连我也在同一个问题上苦苦挣扎。你的问题完全是重复的 那里的答案解释得更清楚。这是我的版本:
您的程序需要区分系统调用入口和系统调用出口
为此维护一个变量。看看这个。这里,syscall中的变量
也执行相同的操作
希望这对您有所帮助。
下次在病房发布问题之前,请在此进行基础研究(搜索)。也节省了很多时间;)n我们也是:D

这是我的解决方案:

  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
    }
struct user_regs_struct regs/*_on_entry,regs_on_exit*/;
ptrace(ptrace_-ATTACH,pid,0,0);
waitpid(pid,0,WCONTINUED);
while(true)
{
ptrace(ptrace_系统调用,pid,0,0);
智力状态;
等待(&状态);
#ifdef__i386__
int eax=ptrace(ptrace_PEEKUSER,pid,ORIG_eax*4,0);
ptrace(ptrace_GETREGS、pid、0和regs);
ptrace(ptrace_系统调用,pid,0,0);
waitpid(pid和状态,WCONTINUED);
if(eax==\uuu NR\u read&®s.ebx==10)
{
int*buffer=(int*)malloc(regs.eax+3);
int i=0;
对于(int j=0;i

谢谢你的回复

我猜,Linux内核是在退出时捕获sys调用,而不是在进入和退出时捕获sys调用?你在找函数名吗?@user1189104:想看看你发布的问题的答案吗??:P非常感谢你的回答,我以不同和相似的方式解决了我的问题,当我完成另一个问题时,我会发布我解决的代码,这不是我所希望的。谢谢。我下次会在那个论坛上搜索,别担心。谢谢听着,自从我问起这件事已经过去很久了。我终于可以解决这个问题了,但还是做了一些老把戏。由于ptrace手册不支持所有命名的选项,因此没有一种简单的方法可以做到这一点。我不记得我到底要做什么,但肯定不是手动编程中描述的正常情况。而且没有办法知道是捕获一个syscall\u入口还是一个syscall\u出口,所以。。。不过谢谢你。Ptrace现在仍然不是很强大。我很想看看你的代码是怎么做的。你对这个问题的处理也不一样。如果你不介意,你能和我们分享代码吗?你可以粘贴你的代码,这是一个sshd监视器。它还没有完成,因为我正试图为这个目标修补openssh。谢谢你的关注。希望这有帮助。没有评论吗?好吧,我想这件事很久以前就过去了,而且在这里也有负面影响。。不知道为什么,真的。格斯利兄弟。。因为一份无聊的工作而自命不凡(很久以前,我打开了一个候机楼:’(我去看看.)谢谢你的光临。。
  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
    }