免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4745 | 回复: 3
打印 上一主题 下一主题

[函数] 劫持函数调用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-11 23:08 |只看该作者 |倒序浏览
看到有个帖子讨论劫持fork的问题,godbach的内核系统调用的问题,其实我以前分析过adore-ng的源码,在CU也可以收到!可以很好的劫持系统调用,说到这里就想能不能劫持诸如GLIBC的函数呢....

我们先从下面一段例程:

  1. /* 文件名:verifypasswd.c */
  2. /* 这是一段判断用户口令的程序,其中使用到了标准C函数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. }
复制代码


在上面这段程序中,我们使用了strcmp函数来判断两个字符串是否相等。下面,我们使用一个动态函数库来重载strcmp函数:

  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. }
复制代码


编译程序:
$ gcc -o verifypasswd verifypasswd.c
$ gcc -shared -o hack.so hack.c


测试一下程序:(得到正确结果)
$ ./verifypasswd asdf
Invalid Password!

设置LD_PRELOAD变量:(使我们重写过的strcmp函数的hack.so成为优先载入链接库)
         $ export LD_PRELOAD="./hack.so"

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

被劫持了,但是这种太过于恶劣,如果我们还是先正常的strcmp呢

我们可以看到,1)我们的hack.so中的strcmp被调用了。
              2)主程序中运行结果被影响了。如果这是一个系统登录程序,那么这也就意味着我们用任意口令都可以进入系统了。

被劫持了,但是这种太过于恶劣,如果我们还是想获得用户输入之后正常的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. }
复制代码

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



最后是我自己的测试结果:

  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=""//大家可以尝试下不加这句的结果^_^
  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=""
复制代码

评分

参与人数 1可用积分 +10 收起 理由
prolj + 10 没权限精你,有权限手你。

查看全部评分

论坛徽章:
0
2 [报告]
发表于 2009-12-12 00:18 |只看该作者
你说的是这个?
http://www.embedded-bits.co.uk/?p=9

论坛徽章:
0
3 [报告]
发表于 2009-12-12 11:46 |只看该作者
不明白为什么会调用hack.so的strcmp  

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

论坛徽章:
0
4 [报告]
发表于 2009-12-12 19:10 |只看该作者
因为设置了LD_PRELOAD环境变量
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP