【问题标题】:Ptrace failing to attachPtrace 无法附加
【发布时间】:2013-12-16 00:42:56
【问题描述】:

我以./main & 身份运行我的进程

它给了我一个如下所示的地址:[1] 4257

然后在一个新终端上我这样做:./tracer 4257

这行代码返回-1

ptrace(PTRACE_ATTACH, traced_process, NULL, NULL);

main.c

int main()
{
    int i;
    for(i = 0; i < 10; i++)
    {
        printf("Hello World\n");
        sleep(5);
    }

    return 0;
}

tracer.c

#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/user.h>   // For user_regs_struct

int main(int argc, char *argv[])
{  
    pid_t traced_process;
   struct user_regs_struct regs;

   if(argc != 2) 
   {
        printf("Usage: %s <pid to be traced>\n", argv[0], argv[1]);
        exit(1);
   }

   traced_process = atoi(argv[1]);

   long t = 0;
   t = ptrace(PTRACE_ATTACH, traced_process, NULL, NULL);

   if(t < 0)
    printf("-1\n");

   wait(NULL);

   ptrace(PTRACE_GETREGS, traced_process, NULL, &regs);
   long ins = ptrace(PTRACE_PEEKTEXT, traced_process, regs.eip, NULL);

   if(ins < 0)
    printf("-1\n");

   printf("EIP: %lx Instruction executed: %lx\n", regs.eip, ins);

   ptrace(PTRACE_DETACH, traced_process, NULL, NULL);

   return 0;
}

我该如何解决这个问题?

【问题讨论】:

    标签: c ubuntu ptrace


    【解决方案1】:

    如果没有直接的父子进程关系(或者您不是 root),Ubuntu 会限制其他程序通过 ptrace 附加的能力。

    看看https://wiki.ubuntu.com/Security/Features#ptrace

    基本上你需要允许跟踪,或者通过echo 0 &gt; /proc/sys/kernel/yama/ptrace_scope禁用系统范围的保护

    【讨论】:

    • 我不得不这样做 echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope 但现在它在这条线上返回 -1 ptrace(PTRACE_GETREGS, traced_process, NULL, &amp;regs);
    • 抱歉,不知道这个问题 :((但是 ptrace 命令设置了 errno,所以你应该可以从那里学到更多东西)
    • 我会想办法解决的。谢谢你,你帮了我很多!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 2016-10-01
    • 2014-02-15
    • 1970-01-01
    相关资源
    最近更新 更多