zoukankan      html  css  js  c++  java
  • Pwnable-mistake

    先看看c的源码

    #include <stdio.h>
    #include <fcntl.h>
    
    #define PW_LEN 10
    #define XORKEY 1
    
    void xor(char* s, int len){
        int i;
        for(i=0; i<len; i++){
            s[i] ^= XORKEY;
        }
    }
    
    int main(int argc, char* argv[]){
        
        int fd;
        if(fd=open("/home/mistake/password",O_RDONLY,0400) < 0){
            printf("can't open password %d
    ", fd);
            return 0;
        }
    
        printf("do not bruteforce...
    ");
        sleep(time(0)%20);
    
        char pw_buf[PW_LEN+1];
        int len;
        if(!(len=read(fd,pw_buf,PW_LEN) > 0)){
            printf("read error
    ");
            close(fd);
            return 0;        
        }
    
        char pw_buf2[PW_LEN+1];
        printf("input password : ");
        scanf("%10s", pw_buf2);
    
        // xor your input
        xor(pw_buf2, 10);
    
        if(!strncmp(pw_buf, pw_buf2, PW_LEN)){
            printf("Password OK
    ");
            system("/bin/cat flag
    ");
        }
        else{
            printf("Wrong Password
    ");
        }
    
        close(fd);
        return 0;
    }

    让pw_buf 和 ^1过的pw_buf2相等就可以cat flag了

    题目给了提示,operator priority,运算符优先,即<会优先于=,所以会先打开password,里面的内容大于0,所以获得的flase  即fd=0

    if(fd=open("/home/mistake/password",O_RDONLY,0400) < 0)

    因为fd=0,是标准输入,所以我们可以进行输入

      if(!(len=read(fd,pw_buf,PW_LEN) > 0)){
            printf("read error
    ");
            close(fd);
            return 0;  

    那我们就可以输入pw_buf,之后在pw_buf2的位置输入一个10字节的数字并且异或1的值等于pw_buf,这样他们就相等,也就获得flag

    这里我们让pw_buf等于0000000000,然后让pw_buf2等于1111111111  ,因为1111111111 ^1111111111=0

    满足上面的if判断式

    Mommy, the operator priority always confuses me :(

  • 相关阅读:
    Git配置
    第一次作业
    第二次作业
    python目前最好用的IDE——pycharm
    九九乘法表
    python语言的优点和缺点
    Python高效编程的19个技巧
    Python中 filter | map | reduce | lambda的用法
    Python 代码优化常见技巧
    求逆序对
  • 原文地址:https://www.cnblogs.com/gaonuoqi/p/11752441.html
Copyright © 2011-2022 走看看