[Dreamhack] Rock Paper Scissors

pandas·2024년 10월 21일
0

Dreamhack

목록 보기
10/14

00. 문제 파일

pwnable + misc????

01. 바이너리 분석

도커 파일과 바이너리, C언어까지 준다..? 굉장히 친절하다

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>

int main(void)
{
    char c, t[4] = "RPS";
    int i, p, r;
    srand(time(NULL));
    setvbuf(stdin, NULL, _IONBF, 0);
    setvbuf(stdout, NULL, _IONBF, 0);
    setvbuf(stderr, NULL, _IONBF, 0);
    for(i = 1; i <= 10; i++)
    {
        printf("Round %d of 10\n", i);
        printf("Put your hand out(R, P, S): ");
        scanf("%c", &c);
        while(getchar() != '\n');
        switch(c)
        {
            case 'R':
                p = 0;
                break;
            case 'P':
                p = 1;
                break;
            case 'S':
                p = 2;
                break;
            default:
                printf("Nope!\n");
                return 0;
        }
        sleep(1);
        r = rand() % 3;
        printf("You: %c Computer: %c\n", t[p], t[r]);
        if((r - p + 1) % 3)
        {
            printf("Nope!\n");
            return 0;
        }
    }
    int fd = open("./flag", O_RDONLY);
    char flag[64] = { 0, };
    read(fd, flag, 64);
    printf("Flag is %s\n", flag);
    close(fd);
    return 0;
}

간단한 가위바위보 게임이다
10번 이기면 flag를 주는데 컴퓨터는 랜덤하게 선택한다
-> 취약점 (타임스탬프)

02. 공격 시나리오

from ctypes import *
libc = CDLL("/lib/x86_64-linux-gnu/libc.so.6")
libc.srand(libc.time(0))
libc.rand()

이게 Python에서 srand로 타임스탬프를 찍는 방법이다

c에서 시간기준으로 랜덤값을 생성하기에, 바이너리와 익스코드를 동시에 실행하면 random을 유출시킬 수 있다

03. Exploit

from pwn import *
from ctypes import *
import time

p = process("./chall")

libc = CDLL("/lib/x86_64-linux-gnu/libc.so.6")

libc.srand(libc.time(0))

def rps(k):
    if k==0:
        return "R"
    elif k==1:
        return "P"
    elif k==2:
        return "S"
libc.srand(libc.time(0))

for i in range(10):
    com = rps(libc.rand()%3)
    #print(f"COM : {com}")

    if com == "P":
        #print("USER : S")
        p.sendline(b"S")
    elif com == "S":
        #print("USER : R")
        p.sendline(b"R")
    else:
        #print("USER : P")
        p.sendline(b"P")
    time.sleep(1)

p.interactive()

야-압

profile
KDMHS 23 WP

0개의 댓글

관련 채용 정보