Chinaunix首页 | 论坛 | 博客
  • 博客访问: 39348
  • 博文数量: 10
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 90
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-05 14:37
文章分类

全部博文(10)

文章存档

2015年(1)

2014年(1)

2013年(8)

我的朋友

分类: LINUX

2013-11-19 09:34:01

你想过怎么实现对系统调用的拦截吗?你尝试过通过改变系统调用的参数来愚弄你的系统kernel吗?你想过调试器是如何使运行中的进程暂停并且控制它吗?

你可能会开始考虑怎么使用复杂的kernel编程来达到目的,那么,你错了。实际上Linux提供了一种优雅的机制来完成这些:ptrace系统函数。 ptrace提供了一种使父进程得以监视和控制其它进程的方式,它还能够改变子进程中的寄存器和内核映像,因而可以实现断点调试和系统调用的跟踪。

使用ptrace,你可以在用户层拦截和修改系统调用(sys call)
在这篇文章中,我们将学习如何拦截一个系统调用,然后修改它的参数。在本文的第二部分我们将学习更先进的技术:设置断点,插入代码到一个正在运行的程序中;我们将潜入到机器内部,偷窥和纂改进程的寄存器和数据段。

基本知识


操作系统提供了一种标准的服务来让程序员实现对底层硬件和服务的控制(比如文件系统),叫做系统调用(system calls)。当一个程序需要作系统调用的时候,它将相关参数放进系统调用相关的寄存器,然后调用软中断0x80,这个中断就像一个让程序得以接触到内核模式的窗口,程序将参数和系统调用号交给内核,内核来完成系统调用的执行。
在i386体系中(本文中所有的代码都是面向i386体系),系统调用号将放入%eax,它的参数则依次放入%ebx, %ecx, %edx, %esi 和 %edi。 比如,在以下的调用。
       Write(2, “Hello”, 5)
的汇编形式大概是这样的
    movl   $4, %eax
    movl   $2, %ebx
    movl   $hello, %ecx
    movl   $5, %edx
    int    $0x80
这里的$hello指向的是标准字符串”Hello”。

那么,ptrace会在什么时候出现呢?在执行系统调用之前,内核会先检查当前进程是否处于被“跟踪”(traced)的状态。如果是的话,内核暂停当前进程并将控制权交给跟踪进程,使跟踪进程得以察看或者修改被跟踪进程的寄存器。

让我们来看一个例子,演示这个跟踪程序的过程:

[html] view plaincopy
  1. #include <sys/ptrace.h>  
  2. #include <sys/types.h>  
  3. #include <sys/wait.h>  
  4. #include <unistd.h>  
  5. #include <linux/user.h>   /* For constants   
  6.                                    ORIG_EAX etc */  
  7. int main()  
  8. {  
  9.    pid_t child;  
  10.     long orig_eax;  
  11.     child = fork();  
  12.     if(child == 0) {  
  13.         ptrace(PTRACE_TRACEME, 0, NULL, NULL);  
  14.         execl("/bin/ls", "ls", NULL);  
  15.     }  
  16.     else {  
  17.         wait(NULL);  
  18.         orig_eax = ptrace(PTRACE_PEEKUSER,   
  19.                           child, 4 * ORIG_EAX,   
  20.                           NULL);  
  21.         printf("The child made a "  
  22.                "system call %ld ", orig_eax);  
  23.         ptrace(PTRACE_CONT, child, NULL, NULL);  
  24.     }  
  25.     return 0;  
  26. }  

运行这个程序,将会在输出ls命令的结果的同时,输出:
The child made a system call 11
说明:11是execve的系统调用号,这是该程序调用的第一个系统调用。
想知道系统调用号的详细内容,察看 /usr/include/asm/unistd.h。

在以上的示例中,父进程fork出了一个子进程,然后跟踪它。在调用exec函数之前,子进程用PTRACE_TRACEME作为第一个参数调用了ptrace函数,它告诉内核:让别人跟踪我吧!然后,在子进程调用了execve()之后,它将控制权交还给父进程。当时父进程正使用wait()函数来等待来自内核的通知,现在它得到了通知,于是它可以开始察看子进程都作了些什么,比如看看寄存器的值之类。


ptrace函数的参数

 Ptrace有四个参数

long ptrace(enum __ptrace_request request,
            pid_t pid,
            void *addr,
            void *data);

第一个参数决定了ptrace的行为与其它参数的使用方法,可取的值有:

出现系统调用之后,内核会将eax中的值(此时存的是系统调用号)保存起来,我们可以使用PTRACE_PEEKUSER作为ptrace的第一个参数来读到这个值。
我们察看完系统调用的信息后,可以使用PTRACE_CONT作为ptrace的第一个参数,调用ptrace使子进程继续系统调用的过程。

第一个参数决定了ptrace的行为与其它参数的使用方法,可取的值有:
PTRACE_ME
PTRACE_PEEKTEXT
PTRACE_PEEKDATA
PTRACE_PEEKUSER
PTRACE_POKETEXT
PTRACE_POKEDATA
PTRACE_POKEUSER
PTRACE_GETREGS
PTRACE_GETFPREGS,
PTRACE_SETREGS
PTRACE_SETFPREGS
PTRACE_CONT
PTRACE_SYSCALL,
PTRACE_SINGLESTEP
PTRACE_DETACH

在下文中将对这些常量的用法进行说明。

读取系统调用的参数
通过将PTRACE_PEEKUSER作为ptrace 的第一个参数进行调用,可以取得与子进程相关的寄存器值。

先看下面这个例子

 

  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5. #include   
  6. #include    /* For SYS_write etc */  
  7.   
  8. int main()  
  9. {     
  10.     pid_t child;  
  11.     long orig_eax, eax;  
  12.     long params[3];  
  13.     int status;  
  14.     int insyscall = 0;  
  15.     child = fork();  
  16.     if(child == 0) {  
  17.         ptrace(PTRACE_TRACEME, 0, NULL, NULL);  
  18.         execl("/bin/ls""ls", NULL);  
  19.     }  
  20.     else {  
  21.        while(1) {  
  22.           wait(&status);  
  23.           if(WIFEXITED(status))  
  24.               break;  
  25.           orig_eax = ptrace(PTRACE_PEEKUSER,   
  26.                      child, 4 * ORIG_EAX, NULL);  
  27.           if(orig_eax == SYS_write) {  
  28.              if(insyscall == 0) {      
  29.                 /* Syscall entry */  
  30.                 insyscall = 1;  
  31.                 params[0] = ptrace(PTRACE_PEEKUSER,  
  32.                                    child, 4 * EBX,   
  33.                                    NULL);  
  34.                 params[1] = ptrace(PTRACE_PEEKUSER,  
  35.                                    child, 4 * ECX,   
  36.                                    NULL);  
  37.                 params[2] = ptrace(PTRACE_PEEKUSER,  
  38.                                    child, 4 * EDX,   
  39.                                    NULL);  
  40.                 printf("Write called with "  
  41.                        "%ld, %ld, %ld ",  
  42.                        params[0], params[1],  
  43.                        params[2]);  
  44.                 }  
  45.           else { /* Syscall exit */  
  46.                 eax = ptrace(PTRACE_PEEKUSER,   
  47.                              child, 4 * EAX, NULL);  
  48.                     printf("Write returned "  
  49.                            "with %ld ", eax);  
  50.                     insyscall = 0;  
  51.                 }  
  52.             }  
  53.             ptrace(PTRACE_SYSCALL,   
  54.                    child, NULL, NULL);  
  55.         }  
  56.     }  
  57.     return 0;  
  58. }  
这个程序的输出是这样的

ppadala@linux:~/ptrace > ls
a.out        dummy.s      ptrace.txt   
libgpm.html  registers.c  syscallparams.c
dummy        ptrace.html  simple.c
ppadala@linux:~/ptrace > ./a.out
Write called with 1, 1075154944, 48
a.out        dummy.s      ptrace.txt
Write returned with 48
Write called with 1, 1075154944, 59
libgpm.html  registers.c  syscallparams.c
Write returned with 59
Write called with 1, 1075154944, 30
dummy        ptrace.html  simple.c
Write returned with 30

以上的例子中我们跟踪了write系统调用,而ls命令的执行将产生三个write系统调用。使用PTRACE_SYSCALL作为ptrace的第一个参数,使内核在子进程做出系统调用或者准备退出的时候暂停它。这种行为与使用PTRACE_CONT,然后在下一个系统调用/进程退出时暂停它是等价的。

在前一个例子中,我们用PTRACE_PEEKUSER来察看write系统调用的参数。系统调用的返回值会被放入%eax。

wait函数使用status变量来检查子进程是否已退出。它是用来判断子进程是被ptrace暂停掉还是已经运行结束并退出。有一组宏可以通过status的值来判断进程的状态,比如WIFEXITED等,详情可以察看wait(2) man。




读取寄存器的值

如果你想在系统调用或者进程终止的时候读取它的寄存器,使用前面那个例子的方法是可以的,但是这是笨拙的方法。使用PRACE_GETREGS作为ptrace的第一个参数来调用,可以只需一次函数调用就取得所有的相关寄存器值。

获得寄存器值得例子如下:

  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5. #include   
  6. #include   
  7.   
  8. int main()  
  9. {     
  10.     pid_t child;  
  11.     long orig_eax, eax;  
  12.     long params[3];  
  13.     int status;  
  14.     int insyscall = 0;  
  15.     struct user_regs_struct regs;  
  16.     child = fork();  
  17.     if(child == 0) {  
  18.         ptrace(PTRACE_TRACEME, 0, NULL, NULL);  
  19.         execl("/bin/ls""ls", NULL);  
  20.     }  
  21.     else {  
  22.        while(1) {  
  23.           wait(&status);  
  24.           if(WIFEXITED(status))  
  25.               break;  
  26.           orig_eax = ptrace(PTRACE_PEEKUSER,   
  27.                             child, 4 * ORIG_EAX,   
  28.                             NULL);  
  29.           if(orig_eax == SYS_write) {  
  30.               if(insyscall == 0) {  
  31.                  /* Syscall entry */  
  32.                  insyscall = 1;  
  33.                  ptrace(PTRACE_GETREGS, child,   
  34.                         NULL, ?s);  
  35.                  printf("Write called with "  
  36.                         "%ld, %ld, %ld ",  
  37.                         regs.ebx, regs.ecx,   
  38.                         regs.edx);  
  39.              }  
  40.              else { /* Syscall exit */  
  41.                  eax = ptrace(PTRACE_PEEKUSER,   
  42.                               child, 4 * EAX,   
  43.                               NULL);  
  44.                  printf("Write returned "  
  45.                         "with %ld ", eax);  
  46.                  insyscall = 0;  
  47.              }  
  48.           }  
  49.           ptrace(PTRACE_SYSCALL, child,  
  50.                  NULL, NULL);  
  51.        }  
  52.    }  
  53.    return 0;  
  54. }  
这段代码与前面的例子是比较相似的,不同的是它使用了PTRACE_GETREGS。 其中的user_regs_struct结构是在中定义的。


单步
ptrace提供了对子进程进行单步的功能。 ptrace(PTRACE_SINGLESTEP, …) 会使内核在子进程的每一条指令执行前先将其阻塞,然后将控制权交给父进程。下面的例子可以查出子进程当前将要执行的指令。为了便于理解,我用汇编写了这个受控程序,而不是让你为c的库函数到底会作那些系统调用而头痛。

以下是被控程序的代码 dummy1.s,使用gcc  –o dummy1 dummy1.s来编译
.data
hello:
    .string "hello world/n"
.globl  main
main:
    movl    $4, %eax
    movl    $2, %ebx
    movl    $hello, %ecx
    movl    $12, %edx
    int     $0x80
    movl    $1, %eax
    xorl    %ebx, %ebx
    int     $0x80
    ret
 

以下的程序则用来完成单步:


  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5. #include    
  6. #include   
  7. int main()  
  8. {  
  9.     pid_t child;  
  10.     const int long_size = sizeof(long);  
  11.     child = fork();  
  12.     if(child == 0) {  
  13.         ptrace(PTRACE_TRACEME, 0, NULL, NULL);  
  14.         execl("./dummy1""dummy1", NULL);  
  15.     }  
  16.     else {  
  17.         int status;  
  18.         union u {  
  19.             long val;  
  20.             char chars[long_size];  
  21.         }data;  
  22.         struct user_regs_struct regs;  
  23.         int start = 0;  
  24.         long ins;  
  25.         while(1) {  
  26.             wait(&status);  
  27.             if(WIFEXITED(status))  
  28.                 break;  
  29.             ptrace(PTRACE_GETREGS,   
  30.                    child, NULL, ?s);  
  31.             if(start == 1) {  
  32.                 ins = ptrace(PTRACE_PEEKTEXT,   
  33.                              child, regs.eip,   
  34.                              NULL);  
  35.                 printf("EIP: %lx Instruction "  
  36.                        "executed: %lx ",   
  37.                        regs.eip, ins);  
  38.             }  
  39.             if(regs.orig_eax == SYS_write) {  
  40.                 start = 1;  
  41.                 ptrace(PTRACE_SINGLESTEP, child,   
  42.                        NULL, NULL);  
  43.             }  
  44.             else  
  45.                 ptrace(PTRACE_SYSCALL, child,   
  46.                        NULL, NULL);  
  47.         }  
  48.     }  
  49.     return 0;  
  50. }  

程序的输出是这样的:

你可能需要察看Intel的用户手册来了解这些指令代码的意思。

更复杂的单步,比如设置断点,则需要很仔细的设计和更复杂的代码才可以实现。


 
在第一部分中我们已经看到ptrace怎么获取子进程的系统调用以及改变系统调用的参数。在这篇文章中,我们将要研究如何在子进程中设置断点和往运行中的程序里插入代码。实际上调试器就是用这种方法来设置断点和执行调试句柄。与前面一样,这里的所有代码都是针对i386平台的。




附着在进程上


在第一部分钟,我们使用ptrace(PTRACE_TRACEME, …)来跟踪一个子进程,如果你只是想要看进程是怎么进行系统调用和跟踪程序的,这个做法是不错的。但如果你要对运行中的进程进行调试,则需要使用 ptrace( PTRACE_ATTACH, ….)


当 ptrace( PTRACE_ATTACH, …)在被调用的时候传入了子进程的pid时, 它大体是与ptrace( PTRACE_TRACEME, …)的行为相同的,它会向子进程发送SIGSTOP信号,于是我们可以察看和修改子进程,然后使用 ptrace( PTRACE_DETACH, …)来使子进程继续运行下去。


下面是调试程序的一个简单例子。


  1. int main()  
  2. {     
  3.    int i;  
  4.     for(i = 0;i < 10; ++i) {  
  5.         printf("My counter: %d ", i);  
  6.         sleep(2);  
  7.     }  
  8.     return 0;  
  9. }  



将上面的代码保存为dummy2.c。按下面的方法编译运行:

gcc -o dummy2 dummy2.c
./dummy2 &
 
现在我们可以用下面的代码来附着到dummy2上。

  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5. #include    /* For user_regs_struct   
  6.                              etc. */  
  7. int main(int argc, char *argv[])  
  8. {     
  9.     pid_t traced_process;  
  10.     struct user_regs_struct regs;  
  11.     long ins;  
  12.     if(argc != 2) {  
  13.         printf("Usage: %s  ",  
  14.                argv[0], argv[1]);  
  15.         exit(1);  
  16.     }  
  17.     traced_process = atoi(argv[1]);  
  18.     ptrace(PTRACE_ATTACH, traced_process,   
  19.            NULL, NULL);  
  20.     wait(NULL);  
  21.     ptrace(PTRACE_GETREGS, traced_process,   
  22.            NULL, ?s);  
  23.     ins = ptrace(PTRACE_PEEKTEXT, traced_process,   
  24.                  regs.eip, NULL);  
  25.     printf("EIP: %lx Instruction executed: %lx ",   
  26.            regs.eip, ins);  
  27.     ptrace(PTRACE_DETACH, traced_process,   
  28.            NULL, NULL);  
  29.     return 0;  
  30. }  

上面的程序仅仅是附着在子进程上,等待它结束,并测量它的eip( 指令指针)然后释放子进程。


设置断点

调试器是怎么设置断点的呢?通常是将当前将要执行的指令替换成trap指令,于是被调试的程序就会在这里停滞,这时调试器就可以察看被调试程序的信息了。被调试程序恢复运行以后调试器会把原指令再放回来。这里是一个例子:

  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5. #include   
  6.   
  7. const int long_size = sizeof(long);  
  8.   
  9. void getdata(pid_t child, long addr,   
  10.              char *str, int len)  
  11. {     
  12.     char *laddr;  
  13.     int i, j;  
  14.     union u {  
  15.             long val;  
  16.             char chars[long_size];  
  17.     }data;  
  18.   
  19.     i = 0;  
  20.     j = len / long_size;  
  21.     laddr = str;  
  22.   
  23.     while(i < j) {  
  24.         data.val = ptrace(PTRACE_PEEKDATA, child,   
  25.                           addr + i * 4, NULL);  
  26.         memcpy(laddr, data.chars, long_size);  
  27.         ++i;  
  28.         laddr += long_size;  
  29.     }  
  30.     j = len % long_size;  
  31.     if(j != 0) {  
  32.         data.val = ptrace(PTRACE_PEEKDATA, child,   
  33.                           addr + i * 4, NULL);  
  34.         memcpy(laddr, data.chars, j);  
  35.     }  
  36.     str[len] = '';  
  37. }  
  38.   
  39. void putdata(pid_t child, long addr,   
  40.              char *str, int len)  
  41. {     
  42.     char *laddr;  
  43.     int i, j;  
  44.     union u {  
  45.             long val;  
  46.             char chars[long_size];  
  47.     }data;  
  48.   
  49.     i = 0;  
  50.     j = len / long_size;  
  51.     laddr = str;  
  52.     while(i < j) {  
  53.         memcpy(data.chars, laddr, long_size);  
  54.         ptrace(PTRACE_POKEDATA, child,   
  55.                addr + i * 4, data.val);  
  56.         ++i;  
  57.         laddr += long_size;  
  58.     }  
  59.     j = len % long_size;  
  60.     if(j != 0) {  
  61.         memcpy(data.chars, laddr, j);  
  62.         ptrace(PTRACE_POKEDATA, child,   
  63.                addr + i * 4, data.val);  
  64.     }  
  65. }  
  66.   
  67. int main(int argc, char *argv[])  
  68. {     
  69.     pid_t traced_process;  
  70.     struct user_regs_struct regs, newregs;  
  71.     long ins;  
  72.     /* int 0x80, int3 */  
  73.     char code[] = {0xcd,0x80,0xcc,0};  
  74.     char backup[4];  
  75.     if(argc != 2) {  
  76.         printf("Usage: %s  ",   
  77.                argv[0], argv[1]);  
  78.         exit(1);  
  79.     }  
  80.     traced_process = atoi(argv[1]);  
  81.     ptrace(PTRACE_ATTACH, traced_process,   
  82.            NULL, NULL);  
  83.     wait(NULL);  
  84.     ptrace(PTRACE_GETREGS, traced_process,   
  85.            NULL, ?s);  
  86.     /* Copy instructions into a backup variable */  
  87.     getdata(traced_process, regs.eip, backup, 3);  
  88.     /* Put the breakpoint */  
  89.     putdata(traced_process, regs.eip, code, 3);  
  90.     /* Let the process continue and execute  
  91.        the int 3 instruction */  
  92.     ptrace(PTRACE_CONT, traced_process, NULL, NULL);  
  93.     wait(NULL);  
  94.     printf("The process stopped, putting back "  
  95.            "the original instructions ");  
  96.     printf("Press  to continue ");  
  97.     getchar();  
  98.     putdata(traced_process, regs.eip, backup, 3);  
  99.     /* Setting the eip back to the original  
  100.        instruction to let the process continue */  
  101.     ptrace(PTRACE_SETREGS, traced_process,   
  102.            NULL, ?s);  
  103.     ptrace(PTRACE_DETACH, traced_process,   
  104.            NULL, NULL);  
  105.     return 0;  
  106.   
  107. }  

上面的程序将把三个byte的内容进行替换以执行trap指令,等被调试进程停滞以后,我们把原指令再替换回来并把eip修改为原来的值。下面的图中演示了指令的执行过程。


 

 

 

1. 进程停滞后

2. 替换入trap指令

 

 

3.断点成功,控制权交给了调试器

4. 继续运行,将原指令替换回来并将eip复原

 

ptrace的幕后工作

那么,在使用ptrace的时候,内核里发生了声么呢?这里有一段简要的说明:

当一个进程调用了 ptrace( PTRACE_TRACEME, …)之后,内核为该进程设置了一个标记,注明该进程将被跟踪。内核中的相关原代码如下:

Source: arch/i386/kernel/ptrace.c
if (request == PTRACE_TRACEME) {
    /* are we already being traced? */
    if (current->ptrace & PT_PTRACED)
        goto out;
    /* set the ptrace bit in the process flags. */
    current->ptrace |= PT_PTRACED;
    ret = 0;
    goto out;
}
一次系统调用完成之后,内核察看那个标记,然后执行trace系统调用(如果这个进程正处于被跟踪状态的话)。其汇编的细节可以在 arh/i386/kernel/entry.S中找到。

现在让我们来看看这个sys_trace()函数(位于 arch/i386/kernel/ptrace.c )。它停止子进程,然后发送一个信号给父进程,告诉它子进程已经停滞,这个信号会激活正处于等待状态的父进程,让父进程进行相关处理。父进程在完成相关操作以后就调用ptrace( PTRACE_CONT, …)或者 ptrace( PTRACE_SYSCALL, …), 这将唤醒子进程,内核此时所作的是调用一个叫wake_up_process() 的进程调度函数。其他的一些系统架构可能会通过发送SIGCHLD给子进程来达到这个目的。

小结:
ptrace函数可能会让人们觉得很奇特,因为它居然可以检测和修改一个运行中的程序。这种技术主要是在调试器和系统调用跟踪程序中使用。它使程序员可以在用户级别做更多有意思的事情。已经有过很多在用户级别下扩展操作系统得尝试,比如UFO,一个用户级别的文件系统扩展,它使用ptrace来实现一些安全机制。


 
作者:
Pradeep Padala,
p_padala@yahoo.com
~ppadala

阅读(4220) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~