zoukankan      html  css  js  c++  java
  • XCTF-open-source

    下载附件拿到源码。

    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[]) {
        if (argc != 4) {
        	printf("what?
    ");
        	exit(1);
        }
    
        unsigned int first = atoi(argv[1]);
        if (first != 0xcafe) {
        	printf("you are wrong, sorry.
    ");
        	exit(2);
        }
        
        unsigned int second = atoi(argv[2]);
        if (second % 5 == 3 || second % 17 != 8) {
        	printf("ha, you won't get it!
    ");
        	exit(3);
        }
    
        if (strcmp("h4cky0u", argv[3])) {
        	printf("so close, dude!
    ");
        	exit(4);
        }
    
        printf("Brr wrrr grr
    ");
    
        unsigned int hash = first * 31337 + (second % 17) * 11 + strlen(argv[3]) - 1615810207;
    
        printf("Get your key: ");
        printf("%x
    ", hash);
        return 0;
    }
    

    这是段C语言的源码。拿到变量hash的值就是flag。一共四个if条件。
    第一个if:argc为表示传参的个数,这里判断是不是四个参数

    if (argc != 4) {
        	printf("what?
    ");
        	exit(1);
        }
    

    第二个if:atoi函数把字符串转成整型(int型)。oxcafe转成整型为:

    unsigned int first = atoi(argv[1]);
        if (first != 0xcafe) {
        	printf("you are wrong, sorry.
    ");
        	exit(2);
        }
    

    第三个if:||运算符只要前一部分成立就不会看后一部分,满足条件会输出“"ha, you won't get it!”,我们让second=25,不满足这个if条件。

    unsigned int second = atoi(argv[2]);
        if (second % 5 == 3 || second % 17 != 8) {
        	printf("ha, you won't get it!
    ");
        	exit(3);
        }
    

    第四个if:strcmp会根据 ASCII 编码依次比较 str1 和 str2 的每一个字符,直到出现不到的字符,或者到达字符串末尾(遇见)
    如果返回值 < 0,则表示 str1 小于 str2。
    如果返回值 > 0,则表示 str2 小于 str1。
    如果返回值 = 0,则表示 str1 等于 str2。
    让argv[3]='h4cky0u',则if判断为假,不进入if后的运算。

    if (strcmp("h4cky0u", argv[3])) {
        	printf("so close, dude!
    ");
        	exit(4);
        }
    

    最后求hash的值:
    first =0xcafe,
    second=25
    strlen(argv[3])=7

    unsigned int hash = 0xcafe * 31337 + (25% 17) * 11 + 7 - 1615810207;
    

    最后以16进制输出

     printf("%x
    ", hash);
    

    用python写解题脚本:

    a = int('0xcafe',16)   #把16进制的0xcafe转成int型
    hash = a * 31337 + (25% 17) * 11 + 7 - 1615810207;
    print(hex(hash))
    

    得到0xc0ffee,flag为c0ffee

  • 相关阅读:
    c# winform DataGridView导出数据到Excel中,可以导出当前页和全部数据
    水晶报表动态加载图片(签名)
    第1章 开启Threejs之旅(一)
    Python中@contextmanager的用法
    Systemd
    Python中with的用法
    systemctl的配置和使用
    /*CS5460_Note_1*/
    Just try the code
    MCP2515无BUG版本驱动(C文件)
  • 原文地址:https://www.cnblogs.com/HelloCTF/p/13111874.html
Copyright © 2011-2022 走看看