문제
Mommy! I made a lotto program for my homework. do you want to play?
ssh lotto@pwnable.kr -p2222 (pw:guest)
풀이
```c
#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;
}
```
6개의 랜덤한 숫자를 맞추는 문제입니다. 한 개를 맞출 때 마다 ```match++```로 카운트 하며 ```match```가 6 일때 플래그를 출력합니다. 카운트 하는 동안 랜덤 값을 기준으로 입력 받은 값을 단순 반복하여 비교합니다. 여기서 코드 상으로 취약점이 발생하게 됩니다. 입력한 값이 동일한 값으로 6 개가 들어간다면 하나의 숫자만 매칭이 되어도 6이 됩니다. char형 변수로 입력을 받기 때문에 decimal 값으로 입력이 들어갈 것입니다. 1 부터 45 사이의 ASCII CODE를 참고하여 33 부터 45 사이의 숫자를 갖는 문자 6개를 대입하면 플래그를 출력할 수 있습니다.
```
- Select Menu -
1. Play Lotto
2. Help
3. Exit
1 Submit your 6 lotto bytes : ######
Lotto Start!
sorry mom... I FORGOT to check duplicate numbers... :(
- Select Menu -
1. Play Lotto
2. Help
3. Exit
```
'Wargame & CTF > pwnableKR' 카테고리의 다른 글
Toddler(cmd2) (0) | 2023.11.27 |
---|---|
Toddler(cmd1) (0) | 2023.11.27 |
Toddler(blackjack) (0) | 2023.11.27 |
Toddler(coin1) (0) | 2023.11.27 |
Toddler(shellshock) (0) | 2023.11.26 |