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進行按位異或赂苗,得到結(jié)果和我們輸入的password進行比較盏触,如果前10位相等渗蟹,那么就輸出flag。這道題赞辩,很有意思雌芽,因為我們不知道password文件里的內(nèi)容是什么,因此就要向如何繞過那里辨嗽∈缆洌看一下題目描述,給了hint糟需,是 operator priority屉佳,運算符優(yōu)先級,重新閱讀代碼,看到這里
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武花,“<”的優(yōu)先級要大于"=",因此杈帐,會優(yōu)先執(zhí)行
open("/home/mistake/password",O_RDONLY,0400) < 0
這里是的意思是用只讀的方式打開password文件体箕,在這里肯定能夠打開的,所以0<0為否挑童,返回0累铅,即fd=0(0是stdin的文件描述符),那么這道題就可以轉(zhuǎn)傳成我們輸入一個password然后與1異或站叼,接著再輸入password娃兽,使得與剛才的那個結(jié)果相同就好了。