Linux 下GCC的編譯可分為4個步驟:預處理 --> 編譯 --> 匯編 -->鏈接
這里講一講劫持GCC編譯的鏈接程序,這里我們需要了解一下動態鏈接的一個環境變數,LD_PRELOAD;
LD_PRELOAD 環境變數可以定義在程式運行前優先加載的元件,這使得我們可以有選擇性地加載不同元件中的相同函式,即通過設定該變數,在主程式和其元件中間加載別的元件,甚至覆寫原本的庫,這就有可能出現劫持程式執行的安全問題,
“ldpreload.c”
#include<stdio.h>
#include<string.h>
void main() {
char passwd[] = "password";
char str[128];
scanf("%s", &str);
if (!strcmp(passwd, str)) {
printf("the password is correct\n");
return;
}
printf("the password is invalid\n");
}
程式很簡單,就不多解釋了,根據對環境變數LD_PRELOAD的解釋,我們要構造一個惡意的元件來多載 strcmp() 函式,編譯為元件,并設定 LD_PRELOAD 環境變數:
“hook.c”
#include<stdio.h>
#include<stdio.h>
int strcmp(const char *s1, const char *s2) {
printf("LD_PRELOAD hooked\n");
return 0;
}
//編譯hook so庫
curits@curits-virtual-machine:~/Desktop/ld_preload$ gcc -shared -o hook.so hook.c
curits@curits-virtual-machine:~/Desktop/ld_preload$ ls
hook.c hook.so ldpreload.c
//正常執行ldpreload程式
curits@curits-virtual-machine:~/Desktop/ld_preload$ gcc ldpreload.c
curits@curits-virtual-machine:~/Desktop/ld_preload$ ./a.out
124
the password is invalid
//鏈接時替換strcmp結果
curits@curits-virtual-machine:~/Desktop/ld_preload$ export LD_PRELOAD="./hook.so"
curits@curits-virtual-machine:~/Desktop/ld_preload$ ./a.out
123
LD_PRELOAD hooked
the password is correct
//移除hook
curits@curits-virtual-machine:~/Desktop/ld_preload$ ./a.out
ERROR: ld.so: object 'NULL' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored.
123
the password is invalid
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/195820.html
標籤:其他
上一篇:生物探針技術與使用現狀
