본문 바로가기

[+] Hacking/[-] Challenge Report

exploit-exercises Nebula - Level04

About

This level requires you to read the token file, but the code restricts the files that can be read. Find a way to bypass it :)

To do this level, log in as the level04 account with the password level04 . Files for this level can be found in /home/flag04.

Source code

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <stdio.h>
#include <fcntl.h>

int main(int argc, char **argv, char **envp)
{
char buf[1024];
int fd, rc;

if(argc == 1) {
printf("%s [file to read]\n", argv[0]);
exit(EXIT_FAILURE);
}

if(strstr(argv[1], "token") != NULL) {
printf("You may not access '%s'\n", argv[1]);
exit(EXIT_FAILURE);
}

fd = open(argv[1], O_RDONLY);
if(fd == -1) {
err(EXIT_FAILURE, "Unable to open %s", argv[1]);
}

rc = read(fd, buf, sizeof(buf));

if(rc == -1) {
err(EXIT_FAILURE, "Unable to read fd %d", fd);
}

write(1, buf, rc);
}


문제의 요점은 다음과 같다.

위 소스코드를 이용해 token 파일을 읽어라.


소스는 그렇게 길지 않다. 우리가 중요하게 봐야 할 부분은 다음과 같다.


if(strstr(argv[1], "token") != NULL) {
printf("You may not access '%s'\n", argv[1]);
exit(EXIT_FAILURE);
}


파일명에서 token 이란 글자가 있을 시 You may ~ 라는 문자열을 출력하며 프로그램이 종료 되어 버린다. 즉 목적인 token 파일을 읽을 수 없도록 해 놓은 것이다. 하지만 반대로 생각해보면 argv[1]에 들어가는 문자열에서 token이란 문자만 없으면 위 루틴은 pass 된다. 심볼릭링크를 이용하면 되는 것이다.


[그림 1 - 풀이 전체 과정]


argv[1] 에는 /tmp/shell 문자열이 들어가게 되고 해당 루틴을 pass하게 된다. 그 후 아래에 있는 open() 함수를 통해 파일을 열게 되는데 시스템은 최종적으로 /tmp/shell 이 가리키고 있는 token 파일을 열게 되어 파일 내용을 읽을 수 있는 것이다.