歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux資訊 >> 更多Linux >> 在linux下面如何截獲系統調用

在linux下面如何截獲系統調用

日期:2017/2/27 9:28:33   编辑:更多Linux
  在Linux下面如何截獲系統調用      使用Linux Kernel Module的一般目的就是擴展系統的功能,或者給某些特殊的設備提供驅動等等。其實利用Linux內核模塊我們還可以做一些比較“黑客”的事情,例如用來攔截系統調用,然後自己處理。嘿嘿,有意思的說。      下面給出一個簡單的例子,說明了其基本的工作過程。    #define MODULE  #define __KERNEL__  #include   #include   #include   #include   #include   #include   #include   #include   #include   extern void* sys_call_table[];  /*sys_call_table is eXPorted, so we can Access it*/  int (*orig_mkdir)(const char *path);  /*the original systemcall*/  int hacked_mkdir(const char *path)  {  return 0; /*everything is ok, but he new systemcall  does nothing*/  }  int init_module(void) /*module setup*/  {  orig_mkdir=sys_call_table[SYS_mkdir];  sys_call_table[SYS_mkdir]=hacked_mkdir;  return 0;  }  void cleanup_module(void) /*module shutdown*/  {  sys_call_table[SYS_mkdir]=orig_mkdir;  /*set mkdir syscall to the origal  one*/  }      大家看到前面的代碼了,非常簡單,我們就是替換了內核的系統調用數組中我們關心的指針的值,系統調用在內核中實際就是一個數組列表指針對應的函數列表。我們通過替換我們想“黑”的函數的指針,就可以達到我們特定的目的。這個例子中我們替換了“mkdir”這個函數。這樣,用戶的應用程序如果調用mkdir後,當內核響應的時候,實際上是調用我們“黑”了的函數,而我們實現的函數裡面是什麼都沒有干,所以這裡會導致用戶運行“mkdir”得不到結果。這個例子很簡單,但是我們可以看出,如果我們想截獲一個系統調用,那麼我們只需要做以下的事情:      1. 查找出感興趣的系統調用在系統內核數組中的入口位置。可以參看include/sys/ syscall.h文件。      2. 將內核中原來的調用函數對應的指針sys_call_table[X]保留下來。      3. 將我們新的偽造的系統函數指針給sys_call_table[X]。      對了,你需要截獲什麼函數?不妨試試看。




Copyright © Linux教程網 All Rights Reserved