본문 바로가기

System Hacking/pwnable.kr

[시스템] 파일 디스크립터(fd) 취약점

파일 디스크립터

: 유닉스 시스템에서 파일을 접근할 때 사용되는 0 이상의 정수값 = 파일들의 index

-> 보통 3부터 할당됨 (0, 1, 2는 각각 표준 입력, 표준 출력, 표준 오류가 기본적으로 할당됨)

 

파일 디스크립터(fd) 관련 함수

파일 열기: open()

파일 읽기(입력): read()

파일 쓰기(출력): write()

파일 닫기: close()

 

pwnable.kr: fd

ssh 접속 방식으로 접속하면 된다.

명령어는 ssh 아이디@IP주소 (포트번호) 이다.

 

이후 ls 명령어로 파일을 살펴본다 -l 옵션을 사용하면 관련 정보도 함께 볼 수 있다.

 

fd@ubuntu:~$ cat fd.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char buf[32];
int main(int argc, char* argv[], char* envp[]){
        if(argc<2){
                printf("pass argv[1] a number\n");
                return 0;
        }
        int fd = atoi( argv[1] ) - 0x1234;
        int len = 0;
        len = read(fd, buf, 32);
        if(!strcmp("LETMEWIN\n", buf)){
                printf("good job :)\n");
                setregid(getegid(), getegid());
                system("/bin/cat flag");
                exit(0);
        }
        printf("learn about Linux file IO\n");
        return 0;

}

atoi: char(문자열)을 int로 전환, 문자열은 0으로 변환하고, 문자열 내 숫자만 int로 변환함

0x1234의 10진수인  4660의 값을 인자로 집어넣은 후 LETMEWIN을 입력해주면 (strcmp 함수)

다음과 같이 결과가 출력된것을 확인할 수 있다.