서브웨이

pwnable.kr lotto 풀이 본문

Pwnable/pwnable.kr

pwnable.kr lotto 풀이

샌드위치메이커 2020. 8. 21. 15:27

프로그래밍을 공부해본 사람이라면 누구나 한번쯤은 만들어 봤을법한 로또 프로그램에 관한 문제인가보네요.

접속해봅시다.

이러한 파일들이 있습니다.

 

일단 소스코드 먼저 봅시다.

#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함수 부분만 집중해서 봅시다.

// 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++;
			}
		}
	}

다른 부분은 큰 문제가 안보이는데 이 부분은 뭔가 이상합니다.

당첨번호 하나와 입력한 숫자 하나가 같은 것이 확인이 되었으면 안쪽 for문은 빠져나와야 할것같은데 그런 조치가 안되어있습니다.

이렇게 되면 당첨번호가 1, 2, 3, 4, 5, 6이라고 했을 때, 2를 6번 입력해도 match가 6이 되어버립니다.

그럼 간단히 45 이내의 값을 갖는 아스키코드 문자를 아무거나 입력해줍니다.

전 돈이 좋으니 $를 여섯번 입력하겠습니다.

하나만 맞추는건데도 13번을 시도했습니다. 이래서 로또는 로또인가봅니다.

가끔 기부한다는 생각으로만 로또를 사야겠네요.

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

pwnable.kr cmd2 풀이  (0) 2020.08.24
pwnable.kr cmd1 풀이  (0) 2020.08.24
pwnable.kr blackjack 풀이  (0) 2020.08.19
pwnable.kr coin1 풀이  (0) 2020.08.18
pwnable.kr shellshock 풀이  (0) 2020.08.17
Comments