pwnable.kr [Toddler's Bottle] - lotto

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


规则是输入一个 6 字节的字符串,与程序随机生成的 6 字节字符串比较( /dev/urandom 文件是 Linux 系统生成的char型随机数据,从这里读数据相当于产生不为空的随机字符流),相同则成功。并且由 lotto[i] = (lotto[i] % 45) + 1; // 1 ~ 45 可知 lotto 中字符的 ASCII 码为 1 到 45 。
ASCII 码表中只有 DEC 33 开始才是可见字符,所以需要输入的字符为 ASCII DEC 33 到 45 。

明确了规则还不够,因为从正常流程下猜中的几率几乎为 0 。

继续看代码,发现一个问题:

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

这里的循环逻辑是,对于每 lotto[i] ,会和输入字串的每一个字符去比较,相等就使 match +1,而成功需要match = 6 。所以只要输入的字串为相同的6个字符,6个字符里有一个中了,那就中了 :P

又由于是随机产生的 lotto 串,这里只要重复尝试一个字串就行了:

……
Submit your 6 lotto bytes : ''''''
Lotto Start!
bad luck...
- 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... :(


至于需要尝试几次就完全看运气了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,905评论 18 399
  • 第5章 引用类型(返回首页) 本章内容 使用对象 创建并操作数组 理解基本的JavaScript类型 使用基本类型...
    大学一百阅读 8,455评论 0 4
  • http://python.jobbole.com/85231/ 关于专业技能写完项目接着写写一名3年工作经验的J...
    燕京博士阅读 12,222评论 1 118
  • 文/微尘 七夕这天不见了喜鹊 它们又在银河搭桥 这天,相爱的人 眼泪 忧伤化作 天边最美的彩虹 见,也凄凉 悲切 ...
    绵绵乡愁阅读 1,865评论 0 0
  • 世界上有一條很長很美的路 叫做夢想 還有一堵很高很硬的牆 叫做現實 翻越那堵牆 叫做堅持 推倒那堵牆 叫做突破 堅...
    Janeliqin阅读 968评论 0 0