#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;

}



힌트가 opration priority. 연산자 우선순위이다.

우선, 이 코드에서 사용된 연산자들을 한번 보자.


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

if(!(len=read(fd,pw_buf,PW_LEN) > 0))


그리고 C의 우선순위를 알아보자 위쪽이 우선순위가 더 높은 연산자이다



Symbol1연산 형식결합성
[ ] ( ) . –>후위 ++ 및 후위 ––왼쪽에서 오른쪽
전위 ++ 및 전위 –– sizeof & * + – ~ !단항오른쪽에서 왼쪽
형식 캐스팅단항오른쪽에서 왼쪽
* / %곱하기왼쪽에서 오른쪽
+ –더하기왼쪽에서 오른쪽
<< >>비트 시프트왼쪽에서 오른쪽
< > <= >=관계왼쪽에서 오른쪽
== !=같음왼쪽에서 오른쪽
&비트 AND왼쪽에서 오른쪽
^비트 제외 OR왼쪽에서 오른쪽
|비트 포함 OR왼쪽에서 오른쪽
&&논리 AND왼쪽에서 오른쪽
&#124;&#124;논리 OR왼쪽에서 오른쪽
? :조건식오른쪽에서 왼쪽
= *= /= %=

 += –= <<= >>=&=

 ^= |=
단순 및 복합 할당2오른쪽에서 왼쪽
,순차적 계산왼쪽에서 오른쪽

출처 : http://forum.falinux.com/zbxe/index.php?document_srl=408448&mid=C_LIB



부등호가 =보다 더 상위에 위치한다.


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


그렇다면, 이 식에서 = 보다 < 가 더 먼저 실행된다는 의미이고, 비교할 때 참이면 1, 거짓이면 0을 반환한다.


open 함수는 파일이 열리면 파일의 fd를 반환하고, 실패하면 음수를 반환하므로, 파일이 열리면 fd = 0  이 되고  안열리면 1이 된다.


그 후


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(!(len=read(fd,pw_buf,PW_LEN) > 0))


파일이 똑바로 열렸다면 fd = 0이므로 stdin을 가리킨다. 즉 입력을 10자 받게된다.

그 후 scanf로 10자를 받는다.

위에서 각 글자를 1과 XOR해서 반환하므로 적당한 값을 넣어주자.

가장 쉽게 1111111111 과 0000000000을 넣어주자.




'pwnable > Toddler's Bottle' 카테고리의 다른 글

[Toddle's Bottle] coin1  (0) 2018.05.29
[Toddler's Bottle] shellshock  (0) 2018.05.28
[Toddler's Bottle] leg  (0) 2018.05.28
[Toddler's Bottle] input  (0) 2018.05.28
[Toddler's Bottle] random  (0) 2018.05.21
블로그 이미지

천재보다는 범재

,