slide-image

pwnable.kr

[Toddler's Bottle]

# lotto

 

 

lotto를 풀어봤다.

 

소스코드

...더보기
#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;
}

 

사실 문제가 되는 부분은 여기인데, lotto[i]와 submit[j]가 같으면 match를 증가시키는데, 개발자의 실수로 i와 j를 합해서 36번의 루프를 돌게된다. 그렇다는 건, lotto[i]와 submit[j]가 한 번이라도 같으면 한 번에 6번의 match값이 증가할 것이라는 점이다. 

        // 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");
        }

 

따라서 아무 문자나 아스키 코드 범위를 만족하는 값을 연속하여 6개 넣었을 때, 운이 좋으면 맞출 수 있게 된다. 

다음은 내가 !를 6개 넣었을 때의 모습이다.

sorry mom... I FORGOT to check duplicate numbers... :(

 

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

[pwnable.kr] TB #random  (0) 2019.07.13
[pwnable.kr] TB #cmd1  (0) 2019.05.16
[pwnable.kr] TB #mistake  (0) 2019.05.14
[pwnable.kr] TB #bof  (0) 2019.03.20
[pwnable.kr] TB #col  (0) 2019.03.18