• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            elva

            玩轉(zhuǎn)ptrace(一)

            by Pradeep Padala
            Created 2002-11-01 02:00
            翻譯: Magic.D E-mail: adamgic@163.com
            譯者序:在開發(fā)Hust Online Judge的過程中,查閱了不少資料,關(guān)于調(diào)試器技術(shù)的資料在網(wǎng)上是很少,即便是UNIX編程巨著《UNIX環(huán)境高級(jí)編程》中,相關(guān)內(nèi)容也不多,直到我在http://www.linuxjournal.com上找到這篇文章,如獲至寶,特翻譯之,作為鄙人翻譯技術(shù)文檔的第一次嘗試,必定會(huì)有不少蹩腳之處,各位就將就一下吧,歡迎大力拍磚。

            你想過怎么實(shí)現(xiàn)對(duì)系統(tǒng)調(diào)用的攔截嗎?你嘗試過通過改變系統(tǒng)調(diào)用的參數(shù)來愚弄你的系統(tǒng)kernel嗎?你想過調(diào)試器是如何使運(yùn)行中的進(jìn)程暫停并且控制它嗎?
            你 可能會(huì)開始考慮怎么使用復(fù)雜的kernel編程來達(dá)到目的,那么,你錯(cuò)了。實(shí)際上Linux提供了一種優(yōu)雅的機(jī)制來完成這些:ptrace系統(tǒng)函數(shù)。 ptrace提供了一種使父進(jìn)程得以監(jiān)視和控制其它進(jìn)程的方式,它還能夠改變子進(jìn)程中的寄存器和內(nèi)核映像,因而可以實(shí)現(xiàn)斷點(diǎn)調(diào)試和系統(tǒng)調(diào)用的跟蹤。
            使用ptrace,你可以在用戶層攔截和修改系統(tǒng)調(diào)用(sys call)
            在這篇文章中,我們將學(xué)習(xí)如何攔截一個(gè)系統(tǒng)調(diào)用,然后修改它的參數(shù)。在本文的第二部分我們將學(xué)習(xí)更先進(jìn)的技術(shù):設(shè)置斷點(diǎn),插入代碼到一個(gè)正在運(yùn)行的程序中;我們將潛入到機(jī)器內(nèi)部,偷窺和纂改進(jìn)程的寄存器和數(shù)據(jù)段。
             
            基本知識(shí)
            操 作系統(tǒng)提供了一種標(biāo)準(zhǔn)的服務(wù)來讓程序員實(shí)現(xiàn)對(duì)底層硬件和服務(wù)的控制(比如文件系統(tǒng)),叫做系統(tǒng)調(diào)用(system calls)。當(dāng)一個(gè)程序需要作系統(tǒng)調(diào)用的時(shí)候,它將相關(guān)參數(shù)放進(jìn)系統(tǒng)調(diào)用相關(guān)的寄存器,然后調(diào)用軟中斷0x80,這個(gè)中斷就像一個(gè)讓程序得以接觸到內(nèi)核 模式的窗口,程序?qū)?shù)和系統(tǒng)調(diào)用號(hào)交給內(nèi)核,內(nèi)核來完成系統(tǒng)調(diào)用的執(zhí)行。
             
            在i386體系中(本文中所有的代碼都是面向i386體系),系統(tǒng)調(diào)用號(hào)將放入%eax,它的參數(shù)則依次放入%ebx, %ecx, %edx, %esi 和 %edi。 比如,在以下的調(diào)用
             
                   Write(2, “Hello”, 5)
             
            的匯編形式大概是這樣的


                movl $4, %eax
                movl $2, %ebx
                movl $hello, %ecx
                movl $5, %edx
                int $0x80
             

            這里的$hello指向的是標(biāo)準(zhǔn)字符串”Hello”。
             
            那么,ptrace會(huì)在什么時(shí)候出現(xiàn)呢?在執(zhí)行系統(tǒng)調(diào)用之前,內(nèi)核會(huì)先檢查當(dāng)前進(jìn)程是否處于被“跟蹤”(traced)的狀態(tài)。如果是的話,內(nèi)核暫停當(dāng)前進(jìn)程并將控制權(quán)交給跟蹤進(jìn)程,使跟蹤進(jìn)程得以察看或者修改被跟蹤進(jìn)程的寄存器。
             
            讓我們來看一個(gè)例子,演示這個(gè)跟蹤程序的過程

             

            #include <sys/ptrace.h>
             #include <sys/types.h>
             #include <sys/wait.h>
             #include <unistd.h>
              #include <linux/user.h> /* For constants
                                                ORIG_EAX etc */

             int main()
              {
                pid_t child;
                 long orig_eax;
                 child = fork();
                  if(child == 0) {
                     ptrace(PTRACE_TRACEME, 0, NULL, NULL);
                     execl("/bin/ls", "ls", NULL);
                 }
                  else {
                     wait(NULL);
                     orig_eax = ptrace(PTRACE_PEEKUSER,
                                       child, 4 * ORIG_EAX,
                                       NULL);
                     printf("The child made a "
                            "system call %ld ", orig_eax);
                     ptrace(PTRACE_CONT, child, NULL, NULL);
                 }
                 return 0;
             }

            運(yùn)行這個(gè)程序,將會(huì)在輸出ls命令的結(jié)果的同時(shí),輸出:
             
            The child made a system call 11
             
            說明:11是execve的系統(tǒng)調(diào)用號(hào),這是該程序調(diào)用的第一個(gè)系統(tǒng)調(diào)用。
            想知道系統(tǒng)調(diào)用號(hào)的詳細(xì)內(nèi)容,察看 /usr/include/asm/unistd.h。
             
            在 以上的示例中,父進(jìn)程fork出了一個(gè)子進(jìn)程,然后跟蹤它。在調(diào)用exec函數(shù)之前,子進(jìn)程用PTRACE_TRACEME作為第一個(gè)參數(shù)調(diào)用了 ptrace函數(shù),它告訴內(nèi)核:讓別人跟蹤我吧!然后,在子進(jìn)程調(diào)用了execve()之后,它將控制權(quán)交還給父進(jìn)程。當(dāng)時(shí)父進(jìn)程正使用wait()函數(shù) 來等待來自內(nèi)核的通知,現(xiàn)在它得到了通知,于是它可以開始察看子進(jìn)程都作了些什么,比如看看寄存器的值之類。
             
            出現(xiàn)系統(tǒng)調(diào)用之后,內(nèi)核會(huì)將eax中的值(此時(shí)存的是系統(tǒng)調(diào)用號(hào))保存起來,我們可以使用PTRACE_PEEKUSER作為ptrace的第一個(gè)參數(shù)來讀到這個(gè)值。
            我們察看完系統(tǒng)調(diào)用的信息后,可以使用PTRACE_CONT作為ptrace的第一個(gè)參數(shù),調(diào)用ptrace使子進(jìn)程繼續(xù)系統(tǒng)調(diào)用的過程。
             
            ptrace函數(shù)的參數(shù)
             
            Ptrace有四個(gè)參數(shù)
            long ptrace(enum __ptrace_request request,
                        pid_t pid,
                        void *addr,
                        void *data);
             
            第一個(gè)參數(shù)決定了ptrace的行為與其它參數(shù)的使用方法,可取的值有:
            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
            在下文中將對(duì)這些常量的用法進(jìn)行說明。
             
            讀取系統(tǒng)調(diào)用的參數(shù)
             
            通過將PTRACE_PEEKUSER作為ptrace 的第一個(gè)參數(shù)進(jìn)行調(diào)用,可以取得與子進(jìn)程相關(guān)的寄存器值。
             
            先看下面這個(gè)例子

            #include <sys/ptrace.h>
             #include <sys/types.h>
             #include <sys/wait.h>
             #include <unistd.h>
             #include <linux/user.h>
              #include <sys/syscall.h> /* For SYS_write etc */
             
             int main()
              {
                 pid_t child;
                 long orig_eax, eax;
                 long params[3];
                 int status;
                 int insyscall = 0;
                 child = fork();
                  if(child == 0) {
                     ptrace(PTRACE_TRACEME, 0, NULL, NULL);
                     execl("/bin/ls", "ls", NULL);
                 }
                  else {
                     while(1) {
                       wait(&status);
                       if(WIFEXITED(status))
                           break;
                       orig_eax = ptrace(PTRACE_PEEKUSER,
                                  child, 4 * ORIG_EAX, NULL);
                        if(orig_eax == SYS_write) {
                           if(insyscall == 0) {
                              /* Syscall entry */
                             insyscall = 1;
                             params[0] = ptrace(PTRACE_PEEKUSER,
                                                child, 4 * EBX,
                                                NULL);
                             params[1] = ptrace(PTRACE_PEEKUSER,
                                                child, 4 * ECX,
                                                NULL);
                             params[2] = ptrace(PTRACE_PEEKUSER,
                                                child, 4 * EDX,
                                                NULL);
                             printf("Write called with "
                                    "%ld, %ld, %ld ",
                                    params[0], params[1],
                                    params[2]);
                             }
                        else { /* Syscall exit */
                             eax = ptrace(PTRACE_PEEKUSER,
                                          child, 4 * EAX, NULL);
                                 printf("Write returned "
                                        "with %ld ", eax);
                                 insyscall = 0;
                             }
                         }
                         ptrace(PTRACE_SYSCALL,
                                child, NULL, NULL);
                     }
                 }
                 return 0;
             }

            這個(gè)程序的輸出是這樣的
             
            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系統(tǒng)調(diào)用,而ls命令的執(zhí)行將產(chǎn)生三個(gè)write系統(tǒng)調(diào)用。使用PTRACE_SYSCALL作為ptrace的 第一個(gè)參數(shù),使內(nèi)核在子進(jìn)程做出系統(tǒng)調(diào)用或者準(zhǔn)備退出的時(shí)候暫停它。這種行為與使用PTRACE_CONT,然后在下一個(gè)系統(tǒng)調(diào)用/進(jìn)程退出時(shí)暫停它是等 價(jià)的。
             
            在前一個(gè)例子中,我們用PTRACE_PEEKUSER來察看write系統(tǒng)調(diào)用的參數(shù)。系統(tǒng)調(diào)用的返回值會(huì)被放入%eax。
             
            wait函數(shù)使用status變量來檢查子進(jìn)程是否已退出。它是用來判斷子進(jìn)程是被ptrace暫停掉還是已經(jīng)運(yùn)行結(jié)束并退出。有一組宏可以通過status的值來判斷進(jìn)程的狀態(tài),比如WIFEXITED等,詳情可以察看wait(2) man。
             
            讀取寄存器的值
             
            如果你想在系統(tǒng)調(diào)用或者進(jìn)程終止的時(shí)候讀取它的寄存器,使用前面那個(gè)例子的方法是可以的,但是這是笨拙的方法。使用PRACE_GETREGS作為ptrace的第一個(gè)參數(shù)來調(diào)用,可以只需一次函數(shù)調(diào)用就取得所有的相關(guān)寄存器值。
            獲得寄存器值得例子如下:

             

            #include <sys/ptrace.h>
             #include <sys/types.h>
             #include <sys/wait.h>
             #include <unistd.h>
             #include <linux/user.h>
             #include <sys/syscall.h>
             
             int main()
              {
                 pid_t child;
                 long orig_eax, eax;
                 long params[3];
                 int status;
                 int insyscall = 0;
                 struct user_regs_struct regs;
                 child = fork();
                  if(child == 0) {
                     ptrace(PTRACE_TRACEME, 0, NULL, NULL);
                     execl("/bin/ls", "ls", NULL);
                 }
                  else {
                     while(1) {
                       wait(&status);
                       if(WIFEXITED(status))
                           break;
                       orig_eax = ptrace(PTRACE_PEEKUSER,
                                         child, 4 * ORIG_EAX,
                                         NULL);
                        if(orig_eax == SYS_write) {
                            if(insyscall == 0) {
                               /* Syscall entry */
                              insyscall = 1;
                              ptrace(PTRACE_GETREGS, child,
                                     NULL, &regs);
                              printf("Write called with "
                                     "%ld, %ld, %ld ",
                                     regs.ebx, regs.ecx,
                                     regs.edx);
                          }
                           else { /* Syscall exit */
                              eax = ptrace(PTRACE_PEEKUSER,
                                           child, 4 * EAX,
                                           NULL);
                              printf("Write returned "
                                     "with %ld ", eax);
                              insyscall = 0;
                          }
                       }
                       ptrace(PTRACE_SYSCALL, child,
                              NULL, NULL);
                    }
                }
                return 0;
             }

            這段代碼與前面的例子是比較相似的,不同的是它使用了PTRACE_GETREGS。 其中的user_regs_struct結(jié)構(gòu)是在<linux/user.h>中定義的。
             
             
            來點(diǎn)好玩的
             
            現(xiàn)在該做點(diǎn)有意思的事情了,我們將要把傳給write系統(tǒng)調(diào)用的字符串給反轉(zhuǎn)。

             

            #include <sys/ptrace.h>
             #include <sys/types.h>
             #include <sys/wait.h>
             #include <unistd.h>
             #include <linux/user.h>
             #include <sys/syscall.h>
             
             const int long_size = sizeof(long);
             
             void reverse(char *str)
              {
                 int i, j;
                 char temp;
                 for(i = 0, j = strlen(str) - 2;
                      i <= j; ++i, --j) {
                     temp = str[i];
                     str[i] = str[j];
                     str[j] = temp;
                 }
             }
             
             void getdata(pid_t child, long addr,
                          char *str, int len)
              {
                 char *laddr;
                 int i, j;
                  union u {
                         long val;
                         char chars[long_size];
                 }data;
             
                 i = 0;
                 j = len / long_size;
                 laddr = str;
                  while(i < j) {
                     data.val = ptrace(PTRACE_PEEKDATA,
                                       child, addr + i * 4,
                                       NULL);
                     memcpy(laddr, data.chars, long_size);
                     ++i;
                     laddr += long_size;
                 }
                 j = len % long_size;
                  if(j != 0) {
                     data.val = ptrace(PTRACE_PEEKDATA,
                                       child, addr + i * 4,
                                       NULL);
                     memcpy(laddr, data.chars, j);
                 }
                 str[len] = '';
             }
             
             void putdata(pid_t child, long addr,
                          char *str, int len)
              {
                 char *laddr;
                 int i, j;
                  union u {
                         long val;
                         char chars[long_size];
                 }data;
             
                 i = 0;
                 j = len / long_size;
                 laddr = str;
                  while(i < j) {
                     memcpy(data.chars, laddr, long_size);
                     ptrace(PTRACE_POKEDATA, child,
                            addr + i * 4, data.val);
                     ++i;
                     laddr += long_size;
                 }
                 j = len % long_size;
                  if(j != 0) {
                     memcpy(data.chars, laddr, j);
                     ptrace(PTRACE_POKEDATA, child,
                            addr + i * 4, data.val);
                 }
             }
             
             int main()
              {
                pid_t child;
                child = fork();
                 if(child == 0) {
                   ptrace(PTRACE_TRACEME, 0, NULL, NULL);
                   execl("/bin/ls", "ls", NULL);
                }
                 else {
                   long orig_eax;
                   long params[3];
                   int status;
                   char *str, *laddr;
                   int toggle = 0;
                    while(1) {
                      wait(&status);
                      if(WIFEXITED(status))
                          break;
                      orig_eax = ptrace(PTRACE_PEEKUSER,
                                        child, 4 * ORIG_EAX,
                                        NULL);
                       if(orig_eax == SYS_write) {
                          if(toggle == 0) {
                            toggle = 1;
                            params[0] = ptrace(PTRACE_PEEKUSER,
                                               child, 4 * EBX,
                                               NULL);
                            params[1] = ptrace(PTRACE_PEEKUSER,
                                               child, 4 * ECX,
                                               NULL);
                            params[2] = ptrace(PTRACE_PEEKUSER,
                                               child, 4 * EDX,
                                               NULL);
                            str = (char *)calloc((params[2]+1)
                                              * sizeof(char));
                            getdata(child, params[1], str,
                                    params[2]);
                            reverse(str);
                            putdata(child, params[1], str,
                                    params[2]);
                         }
                          else {
                            toggle = 0;
                         }
                      }
                   ptrace(PTRACE_SYSCALL, child, NULL, NULL);
                   }
                }
                return 0;
             }
            輸出是這樣的:
             
            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
            txt.ecartp s.ymmud tuo.a
            c.sretsiger lmth.mpgbil c.llacys_egnahc
            c.elpmis lmth.ecartp ymmud

            這個(gè)例子中涵蓋了前面討論過的所有知識(shí)點(diǎn),當(dāng)然還有些新的內(nèi)容。這里我們用PTRACE_POKEDATA作為第一個(gè)參數(shù),以此來改變子進(jìn)程中的變量值。它以與PTRACE_PEEKDATA相似的方式工作,當(dāng)然,它不只是偷窺變量的值了,它可以修改它們。
             
            單步
             
            ptrace 提供了對(duì)子進(jìn)程進(jìn)行單步的功能。 ptrace(PTRACE_SINGLESTEP, …) 會(huì)使內(nèi)核在子進(jìn)程的每一條指令執(zhí)行前先將其阻塞,然后將控制權(quán)交給父進(jìn)程。下面的例子可以查出子進(jìn)程當(dāng)前將要執(zhí)行的指令。為了便于理解,我用匯編寫了這個(gè) 受控程序,而不是讓你為c的庫函數(shù)到底會(huì)作那些系統(tǒng)調(diào)用而頭痛。
             
            以下是被控程序的代碼 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

            以下的程序則用來完成單步:

             

            #include <sys/ptrace.h>
             #include <sys/types.h>
             #include <sys/wait.h>
             #include <unistd.h>
             #include <linux/user.h>
             #include <sys/syscall.h>
             int main()
              {
                 pid_t child;
                 const int long_size = sizeof(long);
                 child = fork();
                  if(child == 0) {
                     ptrace(PTRACE_TRACEME, 0, NULL, NULL);
                     execl("./dummy1", "dummy1", NULL);
                 }
                  else {
                     int status;
                      union u {
                         long val;
                         char chars[long_size];
                     }data;
                     struct user_regs_struct regs;
                     int start = 0;
                     long ins;
                      while(1) {
                         wait(&status);
                         if(WIFEXITED(status))
                             break;
                         ptrace(PTRACE_GETREGS,
                                child, NULL, &regs);
                          if(start == 1) {
                             ins = ptrace(PTRACE_PEEKTEXT,
                                          child, regs.eip,
                                          NULL);
                             printf("EIP: %lx Instruction "
                                    "executed: %lx ",
                                    regs.eip, ins);
                         }
                          if(regs.orig_eax == SYS_write) {
                             start = 1;
                             ptrace(PTRACE_SINGLESTEP, child,
                                    NULL, NULL);
                         }
                         else
                             ptrace(PTRACE_SYSCALL, child,
                                    NULL, NULL);
                     }
                 }
                 return 0;
             }

            程序的輸出是這樣的:
            你可能需要察看Intel的用戶手冊(cè)來了解這些指令代碼的意思。
            更復(fù)雜的單步,比如設(shè)置斷點(diǎn),則需要很仔細(xì)的設(shè)計(jì)和更復(fù)雜的代碼才可以實(shí)現(xiàn)。
             
             
            在第二部分,我們將會(huì)看到如何在程序中加入斷點(diǎn),以及將代碼插入到已經(jīng)在運(yùn)行的程序中

            posted on 2009-07-25 17:45 葉子 閱讀(1516) 評(píng)論(5)  編輯 收藏 引用 所屬分類: Unix

            Feedback

            # re: 玩轉(zhuǎn)ptrace(一)[未登錄] 2012-05-07 17:27 along

            good job, guys  回復(fù)  更多評(píng)論   

            # re: 玩轉(zhuǎn)ptrace(一) 2012-06-27 17:41 KIMBERLEYCRAWFORD

            Do you understand that this is correct time to receive the <a href="http://goodfinance-blog.com">loan</a>, which can help you.   回復(fù)  更多評(píng)論   

            一本色道久久99一综合| 日韩久久久久中文字幕人妻| 久久精品无码一区二区日韩AV| 亚洲国产天堂久久综合网站| 91久久香蕉国产熟女线看| 国内精品伊人久久久久影院对白| 欧美性猛交xxxx免费看久久久| 色播久久人人爽人人爽人人片aV | 久久这里只精品99re66| 久久精品免费全国观看国产| 77777亚洲午夜久久多人| 久久国产免费观看精品| 久久精品国产精品亚洲精品| 99久久这里只有精品| 久久精品国产久精国产一老狼| 久久久精品免费国产四虎| 国产Av激情久久无码天堂| 蜜臀久久99精品久久久久久 | 99久久免费国产精精品| 久久久青草青青国产亚洲免观| 久久AV高潮AV无码AV| 香蕉久久夜色精品国产小说| 香港aa三级久久三级老师2021国产三级精品三级在 | 久久国产精品99久久久久久老狼| 伊人情人综合成人久久网小说| 国内精品久久国产大陆| 精品久久久久久久久免费影院| 久久国产精品免费一区| 国产精品久久国产精麻豆99网站| 一本一道久久综合狠狠老| 午夜精品久久久内射近拍高清| 狠狠色丁香婷婷综合久久来来去 | 91精品国产91久久| 大伊人青草狠狠久久| 国产亚洲精品久久久久秋霞| 国内精品伊人久久久影院| 国产精品欧美亚洲韩国日本久久| 一本大道加勒比久久综合| 91精品国产91久久久久福利| 久久99国产乱子伦精品免费| 久久精品人人槡人妻人人玩AV |