linux--函數劫持--基於LD_PRELOAD

來源:互聯網
上載者:User

標籤:linux   ld_preload   函數攔截   

        Recently i am facing a problem, how to differentiate a problem of library-function from application problems.for solving this problem, we need to know some knowledge about share-library and basics in Linux.For dynamic libraries, they are loaded to memory at program running. There are many benefits for this.On the other hand, we have a chance to do something. For example: replace the function, which will be loaded,as our function.Usually,this is maybe a very professional problem, at least for me.But thanks for some  masters, they have been provide a mature way to our goal.
       1). How to replace a function of shared libraries?
             the answer is LD_PRELOAD, this is a environment variable for GUN-Linker.It is used to indicated some pre-load shared libraries.This meaning that functions in            

       this libraries will get a higher priority than normal libraries.

    Normally, we use this technique just want to intercept some functions.So we can do some other thing( evil thing?)
above the original function.But
       1). How can i get the original function?
     some functions: dlopen(), dlsym(), dlclose() and dlerror().There have been many articles to explain them.
   
    In the next section, I will try to translate a nice article. The original article is there

        http://fluxius.handgrep.se/2011/10/31/the-magic-of-ld_preload-for-userland-rootkits/

 

這篇文章的目錄如下:
一.共用庫
二.簡單的LD_PRELOAD(相對於後面的被加工過的LD_PRELOAD)
 2.1. 製作和使用的共用庫
 2.2. dlsym()
 2.3. 使用限制

三.相關的隱匿技術

  3.1 Jynx-Kit

  3.2 檢查方法

 

下面是本文:

一.共用庫
        正如我們所知道的,動態庫的連結是在程式載入時實現.在我的電腦上這個功能是由ld-linux-x86-64.so.X實現,但對於其他機型也可能是ld-linux.so.X。如果有興趣,可以這樣驗證

fl[email protected]:~$ readelf -l /bin/ls [...]  INTERP         0x0000000000000248 0x00000000004purposes00248 0x0000000000400248                  0x000000000000001c 0x000000000000001c  R      1       [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2] [...]

(PS: 這裡的readelf 是一種輔助閱讀elf檔案的工具,對於需要動態載入的程式檔案一般會指定一個動態載入工具。這涉及到elf格式的解析,在下一篇文章中會嘗試對elf格式進行一些簡單分析。)

        相對於靜態編譯龐大的體積而言,動態編譯就小多了。對於其中的部分庫函數只保留了一個指向相關庫的指標,並沒有包含函數的實體。如果要查看一個程式包含了那些庫的調用,可以用"ldd"命令檢查,例如:

<a target=_blank href="mailto:[email protected]:~$">[email protected]:~$</a> ldd /bin/ls     linux-vdso.so.1 =>  (0x00007fff0bb9a000)     libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f7842edc000)     librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f7842cd4000)     libacl.so.1 => /lib/x86_64-linux-gnu/libacl.so.1 (0x00007f7842acb000)     libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7842737000)     libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f7842533000)     /lib64/ld-linux-x86-64.so.2 (0x00007f7843121000)     libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f7842314000)     libattr.so.1 => /lib/x86_64-linux-gnu/libattr.so.1 (0x00007f784210f000)

        我們看一個例子,(例子程式如下)

#include <stdio.h> main() {         printf("huhu la charrue"); }

         分別編譯成動態和靜態兩種

[email protected]:~$ gcc toto.c -o toto-dyn [email protected]:~$ gcc -static toto.c -o toto-stat [email protected]:~$ ls -l | grep "toto-" -rwxr-xr-x  1 fluxiux fluxiux     8426 2011-10-28 23:21 toto-dyn -rwxr-xr-x  1 fluxiux fluxiux   804327 2011-10-28 23:21 toto-stat

        我們看到"toto-stat"幾乎是"toto-dyn"的96倍,這是為什麼呢?

[email protected]:~$ ldd toto-stat     is not a dynamic executable

        (因為"toto-stat"是靜態唄)
        動態連結是一種非常棒的方法,給了我們很多好處,例如:
                ■update libraries and still support programs that want to use older, non-backward-compatible versions of those libraries,
                ■override specific libraries or even specific functions in a library when executing a particular program,
                ■do all this while programs are running using existing libraries.
       對於共用庫的命名,我們有一些習慣。如果一個庫的名字是"soname",那麼通常會加一個首碼"lib",一個尾碼".so",以及一個版本號碼。
       (PS:雖然說只是約定:但你不這麼做的話,你的庫是不能被正確識別出來的)
        現在我們可以來看一下LD_PRELOAD了,

二.簡單的LD_PRELOAD應用

        我們知道,庫檔案一般存放在"/lib"目錄下。所以如果想修改一個庫,最容易想到的辦法就是找到該庫的源碼,修改之後再重新編譯一遍。但除了這種方案,我們還有另一種很酷的方法,那就是使用Linux提供給我們的一個外部介面:LD_PRELOAD。(先看看下面這個吧)

         2.1. 製作和使用的共用庫
                 假如你希望重寫"printf"的行為,可以這樣做先編寫一個你的"printf"函數

        #define _GNU_SOURCE        #include <stdio.h>        int printf(const char *format, ...)        {              exit(153);        }

                 然後編譯把它編譯成一個共用庫。像這樣,

        [email protected]:~$ gcc -Wall -fPIC -c -o my_printf.o my_printf.c         my_printf.c: In function ‘printf’:        my_printf.c:6:2: warning: implicit declaration of function ‘exit’        my_printf.c:6:2: warning: incompatible implicit declaration of built-in function ‘exit’        [email protected]:~$ gcc -shared -fPIC -Wl,-soname -Wl,libmy_printf.so -o libmy_printf.so  my_printf.o

                然後我們修改一個環境變數後再運行自己的測試程式

        [email protected]:~$ export LD_PRELOAD=$PWD/libmy_printf.so        [email protected]:~$ ./toto-dyn

            你會看到"printf"的行為已經被改變了,它並沒有列印"huhu la charrue"。好吧,讓我們看看"ltrace"是怎麼說的

        [email protected]:~$ ltrace ./toto-dyn         __libc_start_main(0x4015f4, 1, 0x7fffa88d0908, 0x402530, 0x4025c0 <unfinished ...>        printf("huhu la charrue" <unfinished ...>        +++ exited (status 153) +++

           有趣吧,我們的"printf"在系統的"printf"之前被調用了。那麼現在來看一個新的問題,如果我們的目標僅僅是簡單的修改一下"printf"的行為,但不破壞原有的功能。那要怎麼辦呢,重寫整個函數?!!   那樣明顯是不合適的,為了處理這個問題,可以看下面的幾個函數。

          2.2. dlsym()
                    在庫"libdl"中有幾個有趣的函數
                    dlopen() : 載入一個庫
                    dlsym() : 擷取一個特定符號的指標
                    dlclose() : 卸載一個庫

 
                   在這裡,因為庫已經在程式裝載時就載入好了,我們只需要直接調用"dlsym"就可以了。我們給"dlsym"傳"RTLD_NEXT"參數,用來擷取指向原有的"printf"函數的指標。就像這樣

[...]typeof(printf) *old_printf;[...] //DO HERE SOMETHING VERY EVILold_printf = dlsym(RTLD_NEXT, "printf");[...]

                 然後我們需要對格式化字串進行一點特殊處理(對應一般的參數是不需要這麼麻煩的),處理完了之後就可以直接用了

        #include <stdio.h>        #include <dlfcn.h>        #include <stdlib.h>        #include <stdarg.h>        int printf(const char *format, ...)        {                  va_list list;                  char *parg;                  typeof(printf) *old_printf;                  // format variable arguments                  va_start(list, format);                  vasprintf(&parg, format, list);                  va_end(list);                  //DO HERE SOMETHING VERY EVIL                  // get a pointer to the function "printf"                  old_printf = dlsym(RTLD_NEXT, "printf");                  (*old_printf)("%s", parg); // and we call the function with previous arguments                  free(parg);        }

         再重新編譯一下

        [email protected]:~$ gcc -Wall -fPIC -c -o my_printf.o my_printf.c         my_printf.c: In function ‘printf’:        my_printf.c:21:1: warning: control reaches end of non-void function        [email protected]:~$ gcc -shared -fPIC -Wl,-soname -Wl libmy_printf.so -ldl -o libmy_printf.so my_printf.o 

       再試一次

        [email protected]:~$ export LD_PRELOAD=$PWD/libmy_printf.so        [email protected]:~$ ./toto-dyn         huhu la charrue

      就這樣一個調用"printf"的使用者不希望的事情就悄悄的發生了。但這種機制也有一些缺陷。

          2.3. 使用限制
                 這種方式雖然很酷,但卻有一些限制。比如對於靜態編譯的程式是無效的。因為靜態編譯的程式不需要串連動態庫的面的函數。而且,假如檔案的SUID或SGID位被置1,載入的時候會忽略LD_PRELOAD(這是ld的開發人員出於安全考慮做的)。

三.相關的隱匿技術

         3.1 Jynx-Kit

         大約在兩個周以前,我們介紹了一種新的隱匿技術。這玩意利用了一個自動啟動並執行shell指令碼,並且一直沒有被rkhunter 和chkrootkit檢查到。先讓我們來看看實際的代碼,在"ld_poison.c"中,有14個函數被劫持了

[...]    old_fxstat = dlsym(RTLD_NEXT, "__fxstat");    old_fxstat64 = dlsym(RTLD_NEXT, "__fxstat64");    old_lxstat = dlsym(RTLD_NEXT, "__lxstat");    old_lxstat64 = dlsym(RTLD_NEXT, "__lxstat64");    old_open = dlsym(RTLD_NEXT,"open");    old_rmdir = dlsym(RTLD_NEXT,"rmdir");    old_unlink = dlsym(RTLD_NEXT,"unlink");     old_unlinkat = dlsym(RTLD_NEXT,"unlinkat");    old_xstat = dlsym(RTLD_NEXT, "__xstat");    old_xstat64 = dlsym(RTLD_NEXT, "__xstat64");    old_fdopendir = dlsym(RTLD_NEXT, "fdopendir");    old_opendir = dlsym(RTLD_NEXT, "opendir");    old_readdir = dlsym(RTLD_NEXT, "readdir");    old_readdir64 = dlsym(RTLD_NEXT, "readdir64");[...]

         我們隨便調一個"open"函數來看看,在內部看到"__xstat"被調用了

[...]    struct stat s_fstat;[...]    old_xstat(_STAT_VER, pathname, &s_fstat);[...]

        接下來是一個檢查操作,檢查了檔案的組ID,路徑,以及檔案名稱。(我們要確保這個檔案不是"ld.so.preload",因為我們想隱藏它)。如果是我們想隱藏的檔案,就不將結果返回給使用者

[...]    if(s_fstat.st_gid == MAGIC_GID || (strstr(pathname, MAGIC_DIR) != NULL) || (strstr(pathname, CONFIG_FILE) != NULL)) {        errno = ENOENT;        return -1;    }[...]


        按照這種思路將上面的函數都處理一遍後,我們就可以向使用者(被攻擊者)隱藏一些檔案以及行為。但我們有什麼方法可以用來檢查嗎?繼續看下面

        3.2 檢查隱匿
        不論這種隱匿方法是不是足夠讓你眼前一亮,但它確實把rkhunter 和chkrootkit幹掉了。因為那兩種工具使用的是基於符號的檢查方式,這種方式確實不是最好的方法。

        來看下面這個例子:
                首先,如果我們在清空LD_PRELOAD變數後,對一個指定程式檔案產生一個校正檔案.就像這樣

        [email protected]:~$ sha1sum toto-dyn         a659c72ea5d29c9a6406f88f0ad2c1a5729b4cfa  toto-dyn        [email protected]:~$ sha1sum toto-dyn > toto-dyn.sha1

        然後再在設定了LD_PRELOAD後檢查這個檔案的校正和,就像這樣

        [email protected]:~$ export LD_PRELOAD=$PWD/libmy_printf.so        [email protected]:~$ sha1sum -c toto-dyn.sha1         toto-dyn: OK

         (貌似通過了對這個檔案的身分識別驗證)
         但這樣真的是正確的嗎?

         很顯然不是,因為我們實際上沒有修改過程式檔案,所以隨便什麼時候檔案的校正和都會是相同的。如果反隱匿工具是基於校正和工作,那麼無疑這是不行的。其他的檢查技術還有:檢查可疑檔案,符號,port binding detection等,但也失敗了,因為這種隱匿方法太靈活了,and in Jynx we have a sort of port knocking to open the remote shell for our host.

 
        好吧,對於這些東西我們還能做什麼呢?檢查LD_PRELOAD指向的所有庫,或"/etc/ld.so.preload"吧。我們知道"dlsym"經常被用來尋找原始函數,所有可以這樣

$ strace ./bin/ls[...]open("/home/fluxiux/blabla/Jynx-Kit/ld_poison.so", O_RDONLY) = 3read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\240\n\0\0\0\0\0\0"..., 832) = 832fstat(3, {st_mode=S_IFREG|0755, st_size=17641, ...}) = 0mmap(NULL, 2109656, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f5e1a586000mprotect(0x7f5e1a589000, 2093056, PROT_NONE) = 0mmap(0x7f5e1a788000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2000) = 0x7f5e1a788000close(3) [...]open("/lib/x86_64-linux-gnu/libdl.so.2", O_RDONLY) = 3[...]

         解析其中的"ld_poison.so"檔案,在裡面看到了很多的替換,它們都是有可能藏汙納垢的地方。看看這些二進位檔案的字串資訊,都很有可能給我們提供一些有趣的暗示。當然如果它們被巧妙的處理過了,那就沒辦法了。但再換個角度來思考,那個正常的程式檔案需要去掩飾自己的這些字串資訊呢。
 

[email protected]:~/blabla/Jynx-Kit$ strings ld_poison.so[...]libdl.so.2[...]dlsymfstat[...]lstat hooked.ld.so.preloadxochi <-- sounds familiar[...]/proc/%s <-- hmmm... strange![...]

          這種叫做Jynx-kit的隱匿方法證明了,想通過基於符號的檢查來應付上文的隱匿方法是不太現實的。而啟發學習法的檢查效果會不錯。

原文在這裡:http://fluxius.handgrep.se/2011/10/31/the-magic-of-ld_preload-for-userland-rootkits/

 



 


 

 

 


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.