- 从ida反编译结果来看,有两次输入,只要第一次的输入字符每一位异或1后与第二次输入的字符相等就会cat flag ,有点没明白考点是撒


- 事后看了师傅的wp才知道原来是源码出现了错误
#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;
}
-
由于fd=open()外没有括号,而<的优先级高于= ,于是先执行open()函数,再和0比较大小,最后赋值给fd。
-
当open成功时返回一个正数,这里的判断逻辑就变成了正数 < 0,返回false,也就是0。所以最后赋给 fd 的值为0,也就变成了stdin,所以就造成了ida反编译看有两次输入机会
-
参考文章:
网友评论