亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 4877 | 回復(fù): 3
打印 上一主題 下一主題

[函數(shù)] 劫持函數(shù)調(diào)用 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2009-12-11 23:08 |只看該作者 |倒序瀏覽
看到有個帖子討論劫持fork的問題,godbach的內(nèi)核系統(tǒng)調(diào)用的問題,其實我以前分析過adore-ng的源碼,在CU也可以收到!可以很好的劫持系統(tǒng)調(diào)用,說到這里就想能不能劫持諸如GLIBC的函數(shù)呢....

我們先從下面一段例程:

  1. /* 文件名:verifypasswd.c */
  2. /* 這是一段判斷用戶口令的程序,其中使用到了標準C函數(shù)strcmp*/

  3. #include <stdio.h>
  4. #include <string.h>


  5. int main(int argc, char **argv)
  6. {
  7. char passwd[] = "password";

  8. if (argc < 2) {
  9.         printf("usage: %s <password>\n", argv[0]);
  10.         return -1;
  11. }

  12. if (!strcmp(passwd, argv[1])) {
  13.         printf("Correct Password!\n");
  14.         return 1;
  15. }

  16. printf("Invalid Password!\n");
  17. return 0;
  18. }
復(fù)制代碼


在上面這段程序中,我們使用了strcmp函數(shù)來判斷兩個字符串是否相等。下面,我們使用一個動態(tài)函數(shù)庫來重載strcmp函數(shù):

  1. /* 文件名:hack1.c */
  2. #include <stdio.h>
  3. #include <string.h>

  4. int strcmp(const char *s1, const char *s2)
  5. {
  6.         printf("hack function invoked. s1=<%s> s2=<%s>\n", s1, s2);
  7.         return 0;
  8. }
復(fù)制代碼


編譯程序:
$ gcc -o verifypasswd verifypasswd.c
$ gcc -shared -o hack.so hack.c


測試一下程序:(得到正確結(jié)果)
$ ./verifypasswd asdf
Invalid Password!

設(shè)置LD_PRELOAD變量:(使我們重寫過的strcmp函數(shù)的hack.so成為優(yōu)先載入鏈接庫)
         $ export LD_PRELOAD="./hack.so"

再次運行程序:
$ ./verifypasswd  asdf
hack function invoked. s1=<password> s2=<asdf>
Correct Password!

被劫持了,但是這種太過于惡劣,如果我們還是先正常的strcmp呢

我們可以看到,1)我們的hack.so中的strcmp被調(diào)用了。
              2)主程序中運行結(jié)果被影響了。如果這是一個系統(tǒng)登錄程序,那么這也就意味著我們用任意口令都可以進入系統(tǒng)了。

被劫持了,但是這種太過于惡劣,如果我們還是想獲得用戶輸入之后正常的strcmp呢

  1. /* 文件名:hack2.c */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <dlfcn.h>

  5. int strcmp(const char *s1, const char *s2)
  6. {
  7.    int (*hac_strcmp)(const char*, const char*);

  8.    printf("hack function invoked. s1=<%s> s2=<%s>\n", s1, s2);

  9.   *(void **)(&hac_strcmp) = dlsym(RTLD_NEXT, "strcmp");
  10.   if(dlerror()) {
  11.     errno = EACCES;
  12.     return -1;
  13.   }

  14.    return (*hac_strcmp)(s1, s2);
  15. }
復(fù)制代碼

dlsym用法不是太懂的大家可以google之!



最后是我自己的測試結(jié)果:

  1. [root@nfs-server hack]# ls
  2. hack1.c  hack2.c  test.c
  3. [root@nfs-server hack]# gcc -Wall -o test test.c
  4. [root@nfs-server hack]# ./test aaa
  5. Invalid Password!
  6. [root@nfs-server hack]# gcc -fPIC -shared -o hack.so hack1.c
  7. [root@nfs-server hack]# export LD_PRELOAD="./hack.so"
  8. [root@nfs-server hack]# ./test aaa
  9. hack function invoked. s1=<password> s2=<aaa>
  10. Correct Password!
  11. [root@nfs-server hack]# export LD_PRELOAD=""//大家可以嘗試下不加這句的結(jié)果^_^
  12. [root@nfs-server hack]# gcc -fPIC -shared -o hack.so hack2.c -ldl
  13. [root@nfs-server hack]# ./test aaa
  14. Invalid Password!
  15. [root@nfs-server hack]# export LD_PRELOAD=""
  16. [root@nfs-server hack]# ./test aaa
  17. Invalid Password!
  18. [root@nfs-server hack]# export LD_PRELOAD="./hack.so"
  19. [root@nfs-server hack]# ./test aaa
  20. hack function invoked. s1=<password> s2=<aaa>
  21. Invalid Password!
  22. [root@nfs-server hack]# export LD_PRELOAD=""
復(fù)制代碼

評分

參與人數(shù) 1可用積分 +10 收起 理由
prolj + 10 沒權(quán)限精你,有權(quán)限手你。

查看全部評分

論壇徽章:
0
2 [報告]
發(fā)表于 2009-12-12 00:18 |只看該作者
你說的是這個?
http://www.embedded-bits.co.uk/?p=9

論壇徽章:
0
3 [報告]
發(fā)表于 2009-12-12 11:46 |只看該作者
不明白為什么會調(diào)用hack.so的strcmp  

為什么不是其它名字.so的strcmp

論壇徽章:
0
4 [報告]
發(fā)表于 2009-12-12 19:10 |只看該作者
因為設(shè)置了LD_PRELOAD環(huán)境變量
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP