Linux中基於ptrace的外掛程式設計

來源:互聯網
上載者:User

標籤:ptrace   外掛程式   

所謂的外掛程式就是在某個進程執行的過程中,其他進程可以動態修改進程中的資料或代碼,從而影響程式的執行路徑,並最終影響程式的運行結果。在windows上我們有系統庫函數writeprocess()可以直接完成該項功能。而在linux上沒有相同功能的函數可以使用,不過使用ptrace也可以完成類似的功能。

首先,Ptrace的使用形式如下:

#include <sys/ptrace.h>

int ptrace(int request, int pid, int addr, int data);

Ptrace 提供了一種父進程可以控制子進程運行,並可以檢查和改變它的核心image的方式。它主要用於實現斷點調試。一個被跟蹤的進程在運行中,發生一個訊號的時候進程被中止,並且通知其父進程。在進程中止的狀態下,進程的記憶體空間可以被讀寫。父進程還可以使子進程繼續執行,並選擇是否是否忽略引起中止的訊號。

#include <sys/ptrace.h>  

#include <sys/types.h>  

#include <sys/wait.h>  

#include <unistd.h>  

#include <sys/syscall.h>  

#include <sys/reg.h>

  

const int int_size = sizeof(int);  

  

void putdata(pid_t child, int addr,   //向進程號為child的進程的addr這個地址空間寫入str

             char *str, int len)  

{     

    char *laddr;  

    int i, j;  

    union u {  

            int val;  

            char chars[int_size];  

    }data;  

  

    i = 0;  

    j = len / int_size;  

    laddr = str;  

    while(i < j) {  

        memcpy(data.chars, laddr, int_size);  

        ptrace(PTRACE_POKEDATA, child,   //每次寫入長度為int_size的資料

               addr + i * 4, data.val);  

        ++i;  

        laddr += int_size;  

    }  

    j = len % int_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("./test", "test", NULL);  //由父進程啟動自進程

   }  

   else 

   {  

      int orig_eax;  

      int 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) {  //如果是寫系統調用,修改細節具體參見系統write調用

         

               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);  

               char* writedata =  "The winner of 100 billion dollars is:gagaga\n";

               putdata(child, params[1],writedata,   

                       params[2]);  

         }  

       ptrace(PTRACE_SYSCALL, child, NULL, NULL);  //設定子進程系統調用或者終止時暫停

      }  

   }  

   return 0;  

}  

 

 

子進程代碼:

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

 

int main()

{

srand((unsigned)(time(NULL)));

int i = 0;

for(i=0;i<120;i++)

{

int choose = rand()%4;

char* name="gagaga";

switch(choose)

{

case 0: name="hahaha";break;

case 1: name="papapa";break;

case 2: name="lalala";break;

}

printf("The winner of 100 billion dollars is:%s\n",name);

}

    return 0;

}

這樣在子進程系統調用write的時候,父進程就可以修改write的參數值,並進一步影響了資訊的輸出。

Linux中基於ptrace的外掛程式設計

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.