Wargame/pwnable.kr

[Pwnable.kr] lotto

핏디 2021. 8. 12. 16:36
SMALL

[문제]


[풀이]

 1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 #include <fcntl.h>
  5 
  6 unsigned char submit[6];
  7 
  8 void play(){
  9 
 10         int i;
 11         printf("Submit your 6 lotto bytes : ");
 12         fflush(stdout);
 13 
 14         int r;
 15         r = read(0, submit, 6);
 16 
 17         printf("Lotto Start!\n");
 18         //sleep(1);
 19 
 20         // generate lotto numbers
 21         int fd = open("/dev/urandom", O_RDONLY);
 22         if(fd==-1){
 23                 printf("error. tell admin\n");
 24                 exit(-1);
 25         }
 26         unsigned char lotto[6];
 27         if(read(fd, lotto, 6) != 6){
 28                 printf("error2. tell admin\n");
 29                 exit(-1);
 30         }
 31         for(i=0; i<6; i++){
 32                 lotto[i] = (lotto[i] % 45) + 1;         // 1 ~ 45
 33         }
 34         close(fd);
 35 
 36         // calculate lotto score
 37         int match = 0, j = 0;
 38         for(i=0; i<6; i++){
 39                 for(j=0; j<6; j++){
 40                         if(lotto[i] == submit[j]){
 41                                 match++;
 42                         }
 43                 }
 44         }
 45 
 46         // win!
 47         if(match == 6){
 48                 system("/bin/cat flag");
 49         }
 50         else{
 51                 printf("bad luck...\n");
 52         }
 53 
 54 }
 55 
 56 void help(){
 57         printf("- nLotto Rule -\n");
 58         printf("nlotto is consisted with 6 random natural numbers less than 46\n");
 59         printf("your goal is to match lotto numbers as many as you can\n");
 60         printf("if you win lottery for *1st place*, you will get reward\n");
 61         printf("for more details, follow the link below\n");
 62         printf("http://www.nlotto.co.kr/counsel.do?method=playerGuide#buying_guide01\n\n");
 63         printf("mathematical chance to win this game is known to be 1/8145060.\n");
 64 }
 65 
 66 int main(int argc, char* argv[]){
 67 
 68         // menu
 69         unsigned int menu;
 70 
 71         while(1){
 72 
 73                 printf("- Select Menu -\n");
 74                 printf("1. Play Lotto\n");
 75                 printf("2. Help\n");
 76                 printf("3. Exit\n");
 77 
 78                 scanf("%d", &menu);
 79 
 80                 switch(menu){
 81                         case 1:
 82                                 play();
 83                                 break;
 84                         case 2:
 85                                 help();
 86                                 break;
 87                         case 3:
 88                                 printf("bye\n");
 89                                 return 0;
 90                         default:
 91                                 printf("invalid menu\n");
 92                                 break;
 93                 }
 94         }
 95         return 0;
 96 }

 

소스코드의 주요 부분인 play함수를 분석해보면 다음과 같다.

 31         for(i=0; i<6; i++){
 32                 lotto[i] = (lotto[i] % 45) + 1;         //입력 값을 hex로 치환한 값이 1~45가 되어야 함
 33         }
 34         close(fd);
 35 
 36         // calculate lotto score
 37         int match = 0, j = 0;
 38         for(i=0; i<6; i++){        // for 문을 36번 돌림 -> 글자 중에 하나만 맞아도 lotto 당첨
 39                 for(j=0; j<6; j++){  
 40                         if(lotto[i] == submit[j]){
 41                                 match++;
 42                         }
 43                 }
 44         }

ascii - hex 표를 확인해보면 입력 값이 많지 않다. 그리고 당첨 확률을 더욱 높이기 위해 같은 문자만 6번 반복하여 입력해주면 flag를 구할 수 있다.

 

 

!를 예로 하여 돌려보면,

2번만에 flag를 출력할 수 있었다.

LIST

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

[Pwnable.kr] blackjack  (0) 2021.08.12
[Pwnable.kr] leg  (0) 2021.08.04
[Pwnable.kr] input2  (0) 2021.07.22
[Pwnable.kr] random  (0) 2021.07.22
[Pwnable.kr] passcode  (0) 2021.06.03