【Writeup】Pwnable.kr 0x09 mistake

0x09 mistake

题目描述

We all make mistakes, let's move on.
(don't take this too seriously, no fancy hacking skill is required at all)

This task is based on real event
Thanks to dhmonkey

hint : operator priority

ssh mistake@pwnable.kr -p2222 (pw:guest)

题目代码

#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\n", fd);
        return 0;
    }

    printf("do not bruteforce...\n");
    sleep(time(0)%20);

    char pw_buf[PW_LEN+1];
    int len;
    if(!(len=read(fd,pw_buf,PW_LEN) > 0)){
        printf("read error\n");
        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\n");
        system("/bin/cat flag\n");
    }
    else{
        printf("Wrong Password\n");
    }

    close(fd);
    return 0;
}

题目分析

首先,我们分析一下这段代码,代码的本意是读取password里的密码,然后该密码与1进行按位异或,得到结果和我们输入的password进行比较,如果前10位相等,那么就输出flag。这道题,很有意思,因为我们不知道password文件里的内容是什么,因此就要向如何绕过那里。看一下题目描述,给了hint,是 operator priority,运算符优先级,重新阅读代码,看到这里

if(fd=open("/home/mistake/password",O_RDONLY,0400) < 0){
        printf("can't open password %d\n", fd);
        return 0;
    }

if判断里,fd=open("/home/mistake/password",O_RDONLY,0400) < 0,“<”的优先级要大于"=",因此,会优先执行

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

这里是的意思是用只读的方式打开password文件,在这里肯定能够打开的,所以0<0为否,返回0,即fd=0(0是stdin的文件描述符),那么这道题就可以转传成我们输入一个password然后与1异或,接着再输入password,使得与刚才的那个结果相同就好了。


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,272评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,678评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 34,806评论 18 399
  • 这不算是一篇文章,主要是我想记录一下昨天发生的一件不太如愿的事。 昨天晚上20:30-21:30我终于正式推出“人...
    Tom教练阅读 500评论 28 7
  • 获取视频指定时间第一帧图片 内存缓存图片,五分钟后滑动cell会重新下载图片! 支持M3u8、rtmp、rtsp流...
    丶淡茗阅读 1,416评论 0 0

友情链接更多精彩内容