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)
考查C語言基本功和細(xì)心程度榨咐。
先放源碼:
#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;
}
問題就出在 if(fd=open("/home/mistake/password",O_RDONLY,0400) < 0)
這里,由于fd=open()外沒有括號酸舍,而 < 的優(yōu)先級高于 = 宛裕,于是先執(zhí)行 open() 函數(shù)吁朦,再比較和0的大小,最后賦值給fd。
當(dāng) open 成功時(shí)返回一個(gè)正數(shù)怠惶,這里的判斷邏輯就變成了正數(shù) < 0耀态,返回false轮傍,也就是0。所以最后賦給 fd 的值為0首装,也就變成了stdin创夜。
連續(xù)輸入兩個(gè)10byte的密碼就行了,一個(gè)每一位和 1 亦或仙逻,比如可以輸入00000000001111111111....(慚愧挥下,筆者一開始還以為是棧溢出,沒有看到少了括號桨醋,也沒看到 "%10s" 棚瘟,陰差陽錯(cuò)地輸入了這條字串還給我跑出了flag。直到去琢磨了一下“operator priority”和“do not bruteforce”喜最,仔細(xì)看了好久才發(fā)現(xiàn) - - )
結(jié)果如下:
mistake@ubuntu:~$ ./mistake
do not bruteforce...
00000000001111111111
input password : Password OK
Mommy, the operator priority always confuses me :(