XOR ---Write up for natas 11

Problem: find the password of natas 12.
the url of this problem is http://natas11.natas.labs.overthewire.org/



Step 1: understand the source code.



<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": "natas11", "pass": "<censored>" };</script></head>
<?

$defaultdata = array( "showpassword"=>"no", "bgcolor"=>"#ffffff");

function xor_encrypt($in) {
    $key = '<censored>';
    $text = $in;
    $outText = '';


    // Iterate through each character
    for($i=0;$i<strlen($text);$i++) {
    $outText .= $text[$i] ^ $key[$i % strlen($key)];
    }

    return $outText;
}


function loadData($def) {
    global $_COOKIE;
    $mydata = $def;
    if(array_key_exists("data", $_COOKIE)) {
    $tempdata = json_decode(xor_encrypt(base64_decode($_COOKIE["data"])), true);
    if(is_array($tempdata) && array_key_exists("showpassword", $tempdata) && array_key_exists("bgcolor", $tempdata)) {
        if (preg_match('/^#(?:[a-f\d]{6})$/i', $tempdata['bgcolor'])) {
        $mydata['showpassword'] = $tempdata['showpassword'];
        $mydata['bgcolor'] = $tempdata['bgcolor'];
        }
    }
    }
    return $mydata;
}


function saveData($d) {
    setcookie("data", base64_encode(xor_encrypt(json_encode($d))));
}

$data = loadData($defaultdata);


if(array_key_exists("bgcolor",$_REQUEST)) {
    if (preg_match('/^#(?:[a-f\d]{6})$/i', $_REQUEST['bgcolor'])) {
        $data['bgcolor'] = $_REQUEST['bgcolor'];
    }
}
saveData($data);

?>

<h1>natas11</h1>
<div id="content">
<body style="background: <?=$data['bgcolor']?>;">
Cookies are protected with XOR encryption<br/><br/>

<?
if($data["showpassword"] == "yes") {
    print "The password for natas12 is <censored>
";
}

?>

<form>
Background color: <input name=bgcolor value="<?=$data['bgcolor']?>">
<input type=submit value="Set color">
</form>

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

At first, the default 'showpassword' is 'no' , and the bgcolor is '#ffffff'.  This is very important because it's the plain text of the cipher in the cookie 'data'. 

The most important thing is the cookie is encoded with json, next XOR, at last is base64.

If we set the 'showpassword' as 'yes', we will get the password of natas12.



Step 2: find the cookie of the page:
We check the cookie, the data type is what we need.
It's the cipher of '"showpassword"=>"no", "bgcolor"=>"#ffffff"' .


ClVLIh4ASCsCBE8lAxMacFMZV2hdVVotEhhUJQNVAmhSEV4sFxFeaAw%3D 






Step 3: first encrypt step --encrypt the plain text with json.
We know the plain text which is ""showpassword"=>"no", "bgcolor"=>"#ffffff"", then we are going to encrypt it with json.


<?php

// Declare an array
$value = array( "showpassword" => "no", "bgcolor" => "#ffffff");

// Use json_encode() function
$json = json_encode($value);

// Display the output
echo($json);

?>


Run the code, then we get the decrypted json word(It just changes the format)

{"showpassword":"no","bgcolor":"ffffff"}

Step 4: find the key of XOR.
XOR is an algorithm that only one of them is true, we can get true.

Original  0 0 1 1
Key 0 1 0 1
Cipher  0 1 1

We know that OTP is based on XOR which is not safe, because we can make cipher and plain text to do XOR algorithm to get the key, then we can decode all the cipher.


Original 0011
Cipher0101
key011

We make use of the 'cyberchef' to find the key.
In the input, we paste the encrypted json word.
Then we set the key as our cipher which is the 'data' cookie we copied before.







Then we get the key 'qw8J'.


Step 4: get the cookie what we need.
This time, we're going to set the 'showpasswd' as 'yes', and then encrypt it with json, XOR and base64.




Then we get the encrypted cookie: 
ClVLIh4ASCsCBE8lAxMacFMOXTlTWxooFhRXJh4FGnBTEV4sFxFeLFMK


Step 5: send the request to get the password.
Change the cookie part of the request with our cookie, send the request. 



The password of natas12 is EDXp0pS26wLKHZy1rDBPUZk0RKfLGIR3


Popular posts from this blog

Phonebook - Hack the box Write up -- Web LDAP injection

wafwaf -- Hack The Box -- Web SQL injection

Cheat sheet for security+