-
fd (file descripter)Write-Up/pwnable.kr 2019. 8. 20. 00:10
ssh fd@pwnable.kr -p2222
3개의 파일이 존재한다.
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"); system("/bin/cat flag"); exit(0); } printf("learn about Linux file IO\n"); return 0; }
atoi(argv[1]) - 0x1234(4660); // atoi는 문자열을 정수로 변환해주는 거고 이 변환된 수에서 0x1234를 빼주는 것이다.
len = read(fd,buf,32); // read(a,b,c) 일 때 a는 파일 디스크립터 값, b는 읽은 데이터를 저장할 버퍼, c는 얼만큼 읽을 지
즉, fd를 32만큼읽어서 buf에 데이터를 저장한다.
if(!strcmp("LETMEWIN\n", buf)){ // buf와 "LETMEWIN"문자열을 비교해서 같으면 아래 문자열을 반환해주고, flag를 준다.
printf("good job :)\n");
system("/bin/cat flag");
exit(0);}// file descripter는 다음과 같이 반환 값을 가진다.
file descripter 목적 POSIX name stdio stream 0 표준 입력 STDIN_FILENO stdin 1 표준 출력 STDOUT_FILENO stdout 2 표준 에러 STDERR_FILENO stderr // strcmp앞에 !가 붙는 이유는 if문은 0을 거짓으로 보기 때문이다.