50 코인 cat: flag: No such file or directory 에러
❯ python PoC.py
[+] Starting local process './basic_exploitation_001': pid 104804
[*] Switching to interactive mode
cat: flag: No such file or directory
[*] Got EOF while reading in interactive
$ 
[*] Process './basic_exploitation_001' stopped with exit code -11 (SIGSEGV) (pid 104804)
[*] Got EOF while sending in interactive

이와 같이 제 로컬 PC에서 스택버퍼오버플로우를 이용해 read_flag 함수를 동작시켰습니다.
그러나, cat: flag: No such file or directory이라는 에러가 발생했습니다.

cat /flag
test

하지만 로컬PC에서 임의의 데이터를 삽입한 /flag는 존재하는데, 해당 파일을 못 읽어오는게 의문이 생깁니다.

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>


void alarm_handler() {
    puts("TIME OUT");
    exit(-1);
}


void initialize() {
    setvbuf(stdin, NULL, _IONBF, 0);
    setvbuf(stdout, NULL, _IONBF, 0);

    signal(SIGALRM, alarm_handler);
    alarm(30);
}


void read_flag() {
    system("cat /flag");
}

int main(int argc, char *argv[]) {

    char buf[0x80];

    initialize();
    
    gets(buf);

    read_flag();

    return 0;
}

그래서 로컬PC의 환경적 요인 때문에 안된다는 생각이 들어, 문제 코드의 main 함수에 read_flag 함수를 호출해봤습니다.

❯ gcc -g -fno-stack-protector -o test test.c
test.c: In functionmain’:
test.c:32:5: warning: implicit declaration of functiongets; did you mean ‘fgets’? [-Wimplicit-function-declaration]
   32 |     gets(buf);
      |     ^~~~
      |     fgets
/usr/bin/ld: /tmp/ccpgL4yb.o: in function `main':
/hack/wargame/lv1_basic_exploitation_001/test.c:32:(.text+0xd3): warning: the `gets' function is dangerous and should not be used.
❯ ./test

test

컴파일 후 동작을 시켜보면 /flag 파일을 잘 가져오는 것을 확인할 수 있습니다.

왜 문제에서 제공된 basic_exploitation_001에서만 임의의 생성한 /flag 파일을 못 가져오나요??

파일 권한 및 사용자 권한

❯ ls -al basic_exploitation_001 test
-rwxr-xr-x 1 root root  5912  213  2024 basic_exploitation_001
-rwxr-xr-x 1 root root 19504  228 03:06 test
❯ ls -al /flag
-rw-r--r-- 1 root root 5  228 01:17 /flag
❯ id
uid=0(root) gid=0(root) groups=0(root)
#pwnable
작성자 정보
답변 1
질문자가 채택한 답변입니다. 좋은 지식을 공유해줘서 고마워요!
avatar
bincat
세계수

basic_exploitation_001read_flag() 함수는 system("cat flag")를 호출합니다.
주어진 .c 파일에서는 system("cat /flag") 지만 실제 바이너리에서는 system("cat flag");를 호출하는 차이가 있기 때문에 로컬환경에서는 작동을 안하고 있었던 것 같습니다!
플래그 위치를 바이너리 위치와 동일한곳에 놓으시면 테스트 플래그가 정상적으로 출력될 것 같네요.

basic_exploitation_001.c의 코드가 실제 바이너리와 다른 것은 의도된 것은 아니고 착오가 있었던 것 같으니 제보하여 수정요청하면 될것같습니다!!

2025.02.28. 14:27
질문에 대한 답을 알고 계신가요?
지식을 나누고 포인트를 획득해보세요.
답변하고 포인트 받기