Self-Improvement

[pwnable.kr] mistake 풀이 (연산자 순위) 본문

리버싱 기초/pwnable.kr

[pwnable.kr] mistake 풀이 (연산자 순위)

JoGeun 2020. 6. 23. 17:18

 

소스코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#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;
}
cs

소스코드는 간단히 password 파일에서 읽어와 pw_buf에 저장하고 scanf() 함수로 입력 값을 pw_buf2에 저장하여 둘이 비교하게 된다.

 

코드에서 open()함수로 password를 0400권한으로 readonly로 열고 있으며 반환 값은 1이 된다.

여기서 "="와 "<"의 우선순위로 따지면 "<"이 더 높기 때문에 "<"을 먼저 수행하게 되어 1<0으로 False(0)가 되어진다.

False는 fd에 대입되어짐으로 fd는 결국 0이 되어진다!

 

그 이후의 read()함수에서 fd가 0이기 때문에 pw_buf는 결국 입력값에 의해 결정되어 진다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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;        
    }
cs

 

결론적으론 pw_buf, pw_buf2 둘다 입력값에 의해 결정되며 pw_buf2는 xor 연산을 수행하게 된다.

pw_buf에 1111111111
pw_buf2에 0000000000
을 수행하면 풀어진다.