ㅋㅋ 이 문제 개웃김 ㅋㅋ
이게 실제 현장에 있었던 취약점이라는게 놀랍다 ㄷㄷ;
#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를 읽기 전용으로 열고 내가 입력해준 값의 베타논리합을 구한 뒤 둘을 비교해서 맞으면 flag를 출력해준다.
여기서 눈여겨봐야할 점은
if(fd=open("/home/mistake/password",O_RDONLY,0400) < 0) 이 비교문이다.
프로그래머 입장에서 보면 이 문장의 뜻은 'fd에 파일 서술자를 집어넣고 해당 파일 서술자가 0보다 작으면' 이라는 의미에서 작성했을 것이다.
하지만 c언어의 할당 연산자( = ) 는 생각보다 우선순위가 매우 낮다.
즉, 프로그래머가 생각한건 if( (fd=open("/home/mistake/password",O_RDONLY,0400)) < 0) 이거지만
운영체제가 처리하는건 if( fd= (open("/home/mistake/password",O_RDONLY,0400) < 0) ) 이게 되는거다
password파일을 정상적으로 열면 open 함수의 반환값은 0이상의 값이 되므로 결과적으로 fd에는 false 즉, 0이 들어간다.
if(!(len=read(fd,pw_buf,PW_LEN) > 0)) 그니까 여기서 여는 fd는 password파일이 아니라 0 즉, 표준입력스트림을 열어버린다.
걍 아무값이나 10개 입력하고 그 값에 xor 1한값 10개 넣어주면 된다.
끝!
'write-up > pwnable.kr' 카테고리의 다른 글
[toddler's bottle] cmd1 풀이 (0) | 2018.12.03 |
---|---|
[toddler's bottle] input (0) | 2018.11.30 |
[toddler's bottle] random (0) | 2018.11.30 |
[toddler's bottle] passcode (0) | 2018.11.30 |
[toddler's bottle] flag (0) | 2018.11.30 |