[natas]level 0 ~ level 09 > 10

domain

natas level 0부터 차례대로 풀어보자.

00

0001

<html>
<head>
<!-- This stuff in the header has nothing to do with the level -->
<link rel="stylesheet" type="text/css" href="http://natas.labs.overthewire.org/css/level.css">
<link rel="stylesheet" href="http://natas.labs.overthewire.org/css/jquery-ui.css" />
<link rel="stylesheet" href="http://natas.labs.overthewire.org/css/wechall.css" />
<script src="http://natas.labs.overthewire.org/js/jquery-1.9.1.js"></script>
<script src="http://natas.labs.overthewire.org/js/jquery-ui.js"></script>
<script src=http://natas.labs.overthewire.org/js/wechall-data.js></script><script src="http://natas.labs.overthewire.org/js/wechall.js"></script>
<script>var wechallinfo = { "level": "natas0", "pass": "natas0" };</script></head>
<body>
<h1>natas0</h1>
<div id="content">
You can find the password for the next level on this page.

<!--The password for natas1 is gtVrDuiDfck831PqWsLEZy5gyDz1clto -->
</div>
</body>
</html>

기본은 소스 코드 확인.
소스 코드를 보면(ctrl+u 또는 F12) password를 확인할 수 있다.
(이후 올리는 코드에서는 header 생략)

00 > 01

0101

<body oncontextmenu="javascript:alert('right clicking has been blocked!');return false;">
<h1>natas1</h1>
<div id="content">
You can find the password for the
next level on this page, but rightclicking has been blocked!

<!--The password for natas2 is ZluruAthQk7Q2MqmDeTiUij2ZvWy2mBi -->
</div>
</body>

문제를 확인하니 우클릭이 방지되었다. 하지만 소스코드 확인하는 것은 어렵지 않다. 간단하게 password 확인!

심심하니 공부를 더 해보자.


body의 attribute를 보면 oncontextmenu가 return false 되어 있다. oncontextmenu는 사용자가 우클릭하여 context menu를 열 때 발생하는 event이다. 이 event에 false 값을 주면 context menu를 열 수 없다.

참고 oncontextmenu

01 > 02

0201

<body>
<h1>natas2</h1>
<div id="content">
There is nothing on this page
<img src="files/pixel.png">
</div>
</body>

src가 “files/pixel.png”인 이미지가 있다. directory listing 취약점이 여기서 발생한다.


0202
0203

directory listing 취약점이란 file, img의 url을 통해 파일 구조를 파악할 수 있는 취약점이다. 실제 url을 통해 files로 이동하면 하위에 users.txt 파일을 있음을 확인할 수 있다. users.txt로 이동하여 password를 찾을 수 있다.

02 > 03

0301

<body>
<h1>natas3</h1>
<div id="content">
There is nothing on this page
<!-- No more information leaks!! Not even Google will find it this time... -->
</div>
</body>

구글은 찾아낼 수 없을 것이라고 힌트를 준다.

잘 모르겠으니 “구글 검색 차단”으로 검색해보니 robots.txt라는 것이 있다.


0302
0303

구글의 웹크롤링을 막기 위해 robots.txt을 작성한다고 한다. robots.txt로 가서 확인해보니 s3cr3t 디렉토리의 검색을 차단함을 확인할 수 있다. s3cr3t 디렉토리에는 user.txt가 있었고 이 안에서 password를 확인할 수 있었다.

03 > 04

0401

방문경로를 변조하는 문제같다. 보통 접속한 사이트와 같은 정보는 header에서 다루므로 개발자 도구를 이용해 확인해보자.

0402
0403

header에 referrer이라는 값이 접속지의 url과 동일하다. referrer을 조작하면 될 것 같다. burp suite로 referrer을 문제가 원하는 대로 변조했더니 password를 확인할 수 있었다.


referrer은 하이퍼링크로 접속 시 남는 흔적을 말한다. referrer을 검증하는 방식으로 xss를 막는다고 한다.

참고 referrer

04 > 05

0501
0502

취약점이 보이지 않아서 개발자 도구를 이용해 훑어보는 도중 cookie에 loggedin라는 쿠키가 보였다. console 창에서 cookie의 값을 변경했더니 password를 확인할 수 있었다.

05 > 06

0601

<body>
<h1>natas6</h1>
<div id="content">

<?

include "includes/secret.inc";

    if(array_key_exists("submit", $_POST)) {
        if($secret == $_POST['secret']) {
        print "Access granted. The password for natas7 is <censored>";
    } else {
        print "Wrong secret";
    }
    }
?>

<form method=post>
Input secret: <input name=secret><br>
<input type=submit name=submit>
</form>

<div id="viewsource"><a href="index-source.html">View sourcecode</a></div>
</div>
</body>

view sourcecode를 이용해서 보면 본래는 볼 수 없는 php 코드를 일부 확인할 수 있다. input 창에 입력한 값이 secret.inc의 $secret값과 동일하면 된다.

0602

처음에는 XSS 문제인가 싶었는데 includes/secret.inc으로 이동하니 $secret 값을 알 수 있었다. 이 값을 input에 입력하면 문제 해결.

06 > 07

0701

<body>
<h1>natas7</h1>
<div id="content">

<a href="index.php?page=home">Home</a>
<a href="index.php?page=about">About</a>
<br>
<br>
this is the front page

<!-- hint: password for webuser natas8 is in /etc/natas_webpass/natas8 -->
</div>
</body>

source code를 보니까 /etc/natas_webpass/natas8로 가면 된다는 hint를 봐서 home, about처럼 get방식으로 페이지를 이동했다. 그랬더니 password 발견

07 > 08

0801

<body>
<h1>natas8</h1>
<div id="content">

<?

$encodedSecret = "3d3d516343746d4d6d6c315669563362";

function encodeSecret($secret) {
    return bin2hex(strrev(base64_encode($secret)));
}

if(array_key_exists("submit", $_POST)) {
    if(encodeSecret($_POST['secret']) == $encodedSecret) {
    print "Access granted. The password for natas9 is <censored>";
    } else {
    print "Wrong secret";
    }
}
?>

<form method=post>
Input secret: <input name=secret><br>
<input type=submit name=submit>
</form>

<div id="viewsource"><a href="index-source.html">View sourcecode</a></div>
</div>
</body>

이번에는 비슷하지만 $encodedSecret 값이 주어져 있고 내가 입력하는 값은 몇 번의 특정한 encoding 방식을 거친다. 따라서 $encodeSecret을 특정 방식의 decoding 과정을 통해 내가 입력할 값을 찾으면 될 것 같다.

여기서 좀 실수를 했는데 이름만 보고 bin2hex 함수가 2진수에서 16진수로 바꿔주는 함수인줄 알았다. 알고보니 string에서 hex로 변환하는 함수. 이를 주의해서 $encodeSecret를 hex에서 string으로 바꾸고, 뒤집고, base64 방식으로 decode하면 된다.

0802

참고 hex2bin site

08 > 09

0901

<body>
<h1>natas9</h1>
<div id="content">
<form>
Find words containing: <input name=needle><input type=submit name=submit value=Search><br><br>
</form>


Output:
<pre>
<?
$key = "";

if(array_key_exists("needle", $_REQUEST)) {
// GET이든 POST방식이든 name이 needle인 값이 있으면
    $key = $_REQUEST["needle"];
}

if($key != "") {
    passthru("grep -i $key dictionary.txt");
}
?>
</pre>

<div id="viewsource"><a href="index-source.html">View sourcecode</a></div>
</div>
</body>

소스 코드를 보면 검색한 문자열을 dictionary.txt에서 찾아주는 모양이다. 현재 디렉토리 내에 dictionary.txt가 있다는 의미이므로 dictionary.txt로 이동해서 살펴보았다.

0902

말그대로 dictionary일 뿐 password의 단서는 없었다.

eval과 같은 부류의 pasthru에도 취약점이 있기 마련. 이것저것 실험해보다 ; 뒤에 다른 linux 명령어가 실행됨을 확인했다.

0903

;ls -l . 명령어를 통해 확인해보니 현재 디렉토리에는 password가 없었다.

0904
0905

한참 고민하다 보니 기억났는데 이런 문제는 url을 딱히 알려주지 않고 전에 문제에서 찾게 만든다. 06 > 07 문제에서 password가 있는 directory 위치가 나와 있음을 알 수 있었다.

;cat /etc/natas_webpass/natas10

natas10의 password를 확인할 수 있었다.

참고
$_ REQUEST
passthru

09 > 10

1001

<body>
<h1>natas10</h1>
<div id="content">

For security reasons, we now filter on certain characters<br/><br/>
<form>
Find words containing: <input name=needle><input type=submit name=submit value=Search><br><br>
</form>


Output:
<pre>
<?
$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");
    }
}
?>
</pre>

<div id="viewsource"><a href="index-source.html">View sourcecode</a></div>
</div>
</body>

바로 전 문제와 동일한데 필터링이 추가되었다. 다중 명령어를 실행하는 ;과 &&, or을 모두 필터링했다. 이 잔인한 놈….ㅂㄷㅂㄷ

[a-z] /etc/natas_webpass/natas11

그래서 grep으로 파일안에 있는 password를 출력하도록 했다.

참고 다중명령어