본문 바로가기

리버싱

[pwnable.kr]lotto

일단 cmd창으로 들어간다.

cmd창에서 나와있는대로 명령어를 치고, yes를 눌러주면 된다.

ls 명령어를 통해 flag, lotto, lotto.c가 있음을 확인했다.

일단 다 실행해보도록 한다.

 

6자리의 로또 코드를 입력하라고 한다.

당연히 틀렸다고 나온다.

이제 소스코드를 확인한다.

이런 식으로 쳐서 코드를 확인해볼 수 있다.

너무 길어서 밑에처럼 정리해보도록 했다.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>

unsigned char submit[6];

void play(){

        int i;
        printf("Submit your 6 lotto bytes : ");
        fflush(stdout);

        int r;
        r = read(0, submit, 6);

        printf("Lotto Start!\n");
        //sleep(1);

        // generate lotto numbers
        int fd = open("/dev/urandom", O_RDONLY);
        if(fd==-1){
                printf("error. tell admin\n");
                exit(-1);
        }
        unsigned char lotto[6];
        if(read(fd, lotto, 6) != 6){
                printf("error2. tell admin\n");
                exit(-1);
        }
        for(i=0; i<6; i++){
                lotto[i] = (lotto[i] % 45) + 1;         // 1 ~ 45
        }
        close(fd);

        // calculate lotto score
        int match = 0, j = 0;
        for(i=0; i<6; i++){
                for(j=0; j<6; j++){
                        if(lotto[i] == submit[j]){
                                match++;
                        }
                }
        }

        // win!
        if(match == 6){
                system("/bin/cat flag");
        }
        else{
                printf("bad luck...\n");
        }

}

void help(){
        printf("- nLotto Rule -\n");
        printf("nlotto is consisted with 6 random natural numbers less than 46\n");
        printf("your goal is to match lotto numbers as many as you can\n");
        printf("if you win lottery for *1st place*, you will get reward\n");
        printf("for more details, follow the link below\n");
        printf("http://www.nlotto.co.kr/counsel.do?method=playerGuide#buying_guide01\n\n");
        printf("mathematical chance to win this game is known to be 1/8145060.\n");
}

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

        // menu
        unsigned int menu;

        while(1){

                printf("- Select Menu -\n");
                printf("1. Play Lotto\n");
                printf("2. Help\n");
                printf("3. Exit\n");

                scanf("%d", &menu);

                switch(menu){
                        case 1:
                                play();
                                break;
                        case 2:
                                help();
                                break;
                        case 3:
                                printf("bye\n");
                                return 0;
                        default:
                                printf("invalid menu\n");
                                break;
                }
        }
        return 0;
}

play()를 제외하고는 얻을 힌트가 없는 듯 하다.

소스코드를 확인한 결과 1~45의 범위 내에서 6개의 문자가 lotto[] 배열에 저장된다.

 

근데 이중 For문을 이용해 입력한 문자열과 lotto 번호가 일치하는지 확인하는 것을 확인할 수 있다.

보통 하나의 for문으로 match하는데 이 코드는 좀 다르다.

하나의 로또 번호라도 일치한다면, match 변수의 값이 6이 된다.

즉, 하나만 번호가 일치해도 당첨이 되어버리는 소스코드다.

 

그럼 1~45 범위 내의 문자를 입력해보면 된다.

나는 그냥 33에 해당하는 !로 시도했다.

ascii 1~45에 해당하는 걸 아무거나 누르면 된다.

난 한번만에 답이 나왔다

flag가 상당히 신기해서 내가 맞춘 게 맞나 했는데 맞았다

반응형

'리버싱' 카테고리의 다른 글

[CodeEngn]코드엔진 advance RCE L03  (0) 2020.08.19
[Reversing.kr]Easy Unpack  (0) 2020.08.16
[HackCTF]Reversing Me  (0) 2020.08.14
[codeengn]코드엔진 Advance RCE L02  (0) 2020.08.13
[codeengn]코드엔진 Advance RCE L01  (0) 2020.08.12