Mommy! I made a lotto program for my homework.
do you want to play?
ssh lotto@pwnable.kr -p2222 (pw:guest)
#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;
}
for(i=0; i<6; i++){
for(j=0; j<6; j++){
if(lotto[i] == submit[j]){
match++;
}
}
}
이코드를 잘보면 lotto에 들어있는 6가지 수 중에서 submit한 문자가 하나가 잇으면 match가 6이 된다.
1글자만 맞추면 된다는 것이다.
그래서 111111만 20번은 넣어본것 같다.
for(i=0; i<6; i++){
lotto[i] = (lotto[i] % 45) + 1; // 1 ~ 45
}
33부터 우리가 char형으로 입력할 수 있는 값이다. '!'=33 """=34 '#'=35... '-'=45 .... '0'=48->4 '1'=49->5...
만약 submit으로 1 1 1 1 1 1 을 넣었다면 1=49 이므로 lotto와 맞을 수가 없다
따라서 아스키가 45보다 작은 문자를 넣어줘야한다
######을 넣어봤다.
성공!!
'war game > pwnable.kr' 카테고리의 다른 글
pwnable.kr cmd2 9pt (0) | 2016.10.12 |
---|---|
pwnable.kr cmd1 1pt (0) | 2016.10.11 |
pwnable.kr blackjack 1pt (0) | 2016.10.11 |
pwnable.kr coin1 6pt (0) | 2016.10.11 |
pwnable.kr shellshock 1pt (0) | 2016.10.11 |