[轉載請註明出處] http://kezeodsnx.pixnet.net/blog
作者: kezeodsnx
介紹
WiKi上DLL injection的解釋為在另一個process的address space裡執行其程式碼。可能的用途為用來影響本來程式碼的行為,而這個改變並不是原作者所預期的。方法是強迫對該程式載入其他的shared library。結果呢,本來程式是呼叫其”原本的”某一function,但最後卻呼叫了”intercepted”的function。
LD_PRELOAD可以做到這件事,常常用來在debian打包的工具fakeroot就是用了LD_PRELOAD。
試用
LD_PRELOAD也會把其所指定的shared library預載到系統,有firefox啟動太慢的問題嗎?或許這是一個普通解。測試一下:
/home/user> ls -alh /opt/firefox/| grep libxul
-rw-r--r-- 1 root root 14M 2009-05-06 16:09 libxul.so
這是firefox啟動時會需要的library,而且有14M之大,利用LD_PRELOAD來預載看看
/home/user> export LD_PRELOAD="libxul.so"
/home/user> free -m
ERROR: ld.so: object 'libxul.so' from LD_PRELOAD cannot be preloaded: ignored.
total used free shared buffers cached
Mem: 330 157 172 0 8 94
-/+ buffers/cache: 55 275
Swap: 63 0 63
原來是libxul所在的位置錯誤,設置LD_LIBRARY_PATH:
/home/user> export LD_LIBRARY_PATH="/opt/firefox"
/home/user> free -m
total used free shared buffers cached
Mem: 330 173 157 0 8 108
-/+ buffers/cache: 56 274
Swap: 63 0 63
果然cache有變大了,而且正好是14M。趕快來測一下firefox啟動,真的有快一些喔!理論上,如果能把所有firefox所需要的library都load起來,就能更省些時間。不過要load哪些,目前還沒有研究。
再來試一下injection:
原本firefox所需的shared library:
/home/user> ldd /opt/firefox/firefox-bin
libpthread.so.0 => /lib/libpthread.so.0 (0x40036000)
libjemalloc.so => not found
libxul.so => not found
libmozjs.so => not found
libxpcom.so => not found
隨便找一個library插插看:
asus-1517850521:/usr/lib# export LD_PRELOAD="/usr/lib/libXv.so.1.0.0"
asus-1517850521:/usr/lib# ldd /opt/firefox/firefox-bin
/usr/lib/libXv.so.1.0.0 (0x40026000)
libpthread.so.0 => /lib/libpthread.so.0 (0x40042000)
範例
偷你的密碼
簡單的密碼核對:
#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[])
{
char* pwd="tooeasy";
if(argc!=2)
{
printf("enter password:\n");
return 1;
}
if( ! strcmp(pwd,argv[1]))
printf("Correct\n");
else
printf("Incorrect\n");
return 0;
}
intercept 其中的strcmp:
#include <stdio.h>
int strcmp(char* s1, char *s2)
{
printf("Your password is %s\n",s1);
return 0;
}
並compile成shared library:
user@user-ubuntu:~/$gcc -shared -o intercept.so intercept.c
利用LD_PRELOAD來強制load此intercept.so
user@user-ubuntu:~/$ export LD_PRELOAD="/home/user/intercept.so"
結果:
user@user-ubuntu:~/$ ./password whatever
Your password is tooeasy
Correct
那有什麼預防方式嗎?目前只有workaround:
1. setuid和setgid
2.build 成static
3.禁用
=========以下為廢言============
小沈:再來
4.不要寫程式
小沈:再來
5.要寫不要用任何function
小沈:再來
6.寫完不要compile
小沈:再來
7.compile完,chmod 444
小沈:再來
爆了
=========以上為廢言============
後記
LD_PRELOAD也會忽略setuid/setgid,與LD_LIBRARY_PATH原因一樣。
留言列表