免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6434 | 回复: 3

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

论坛徽章:
0
发表于 2009-12-11 23:11 |显示全部楼层
看到有个帖子讨论劫持fork的问题,godbach的内核系统调用的问题,其实我以前分析过adore-ng的源码很好的诠释了以VFS机制来劫持内核系统调用,在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=""
复制代码


这里主要以LD_PRELOAD环境变量的方法,应该还有别的方法,欢迎大家拍砖!

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
发表于 2009-12-12 09:37 |显示全部楼层
很好的实例

你这里是临时的修改环境变量吗。否则,容易被用户发现吧。

论坛徽章:
0
发表于 2009-12-12 10:45 |显示全部楼层
恩 是的 测试嘛!!!仅仅说明一种方法,肯定是不完善的^_^....
周末godbach兄也上CU工作

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
发表于 2009-12-12 10:56 |显示全部楼层
原帖由 ubuntuer 于 2009-12-12 10:45 发表
恩 是的 测试嘛!!!仅仅说明一种方法,肯定是不完善的^_^....
周末godbach兄也上CU工作


起来没啥事,就上CU了。呵呵
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP