OverTheWire - Natas - Level 10 → Level 11
Information
1.) you want to see a possible alternative solution or
2.) you are stuck and need a hint!
Login using given credentials.
URL: http://natas11.natas.labs.overthewire.org
Username: natas11
Password: U82qxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Relevant code from the site source code:
|
|
As mentioned on the level page, the cookies are protected with XOR encryption. Before starting with the challenge, let us take a look how XOR is working by using a simple example.
Preparation - XOR encryption
From Wikipedia :
With this logic, a string of text can be encrypted by applying the
bitwise XOR operator to every character using a given key.
To decrypt the output, merely reapplying the XOR function with the key will remove the cipher.
We’ll use the following variables for the example:
- A (Plaintext): thisisjustatexttotestXOR
- B (Key): 1234
- C (Encrypted Text): Result from A⊕B (A XOR B)
So, how is it calculated? This can be perfectly shown with the binary representation. To do so, the strings A and B need to be converted to binary:
|
|
We need to repeat B until it has the same length as A, which will make the XOR calculation easier to show. This will result in the following:
|
|
The next step is to perform the XOR operation bit by bit. A simple example, with the first (or last) two bytes (=16 bits).
|
|
From this example, it quickly becomes clear how the XOR operation works. A table-based representation of the XOR operation:
A | B | C |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Once completely executed, C looks as below:
|
|
The ASCII representation of C, after converting it from binary to ASCII, is the XOR-encoded string.
- A (Plaintext): thisisjustatexttotestXOR
- B (Key): 1234
- C (Encrypted Text): EZZGXAYABFR@TJG@^FVGEj|f
Now, what is the thing here? How can we use this to solve the challenge?
The answer to these questions is (from Wikipedia
):
If the content of any message can be guessed or otherwise known then the key can be revealed.
Using the example above, performing the XOR operation A⊕C should result in B:
|
|
The secret key B can be recovered.
The challenge
The following things can be read from the source code:
A cookie with the name
data
is set when the page is loaded- The default values for
data
are defined in the array$defaultdata
showpassword
→no
bgcolor
→#ffffff
- The default values for
However,
data
is not stored in plain text1 2 3
function saveData($d) { setcookie("data", base64_encode(xor_encrypt(json_encode($d)))); }
- First
data
(passed as$d
) is JSON encoded, then XOR encrypted and base64 encoded
- First
The goal of this level is to change the value of showpassword
to yes
. It is known from the previous chapter that the key ($key
in the source code) can be returned if you know the encrypted string and the unencrypted string.
In this case, we know both values, but we have to extract the exact strings to recover the secret key.
Extract the unencrypted string
The array
$defaultdata
is filled with the following values1
$defaultdata = array( "showpassword"=>"no", "bgcolor"=>"#ffffff");
Those values are used in two places, but the interesting part here is the function
saveData
1 2 3
function saveData($d) { setcookie("data", base64_encode(xor_encrypt(json_encode($d)))); }
The data is first JSON encoded and then used in the function
xor_encrypt
as text (variable$text
) which is then encryptedThis means, that the JSON encoded
$data
value is our unencrypted string
Extracting the important parts from the PHP source code results in the string: {"showpassword":"no","bgcolor":"#ffffff"}
|
|
Extract the encrypted string
The encrypted string is stored in the cookie named data
→ ClVLIh4ASCsCBE8lAxMacFMZV2hdVVotEhhUJQNVAmhSEV4sFxFeaAw=
The function
loadData
reveals, how the cookie has to be decoded1
$tempdata = json_decode(xor_encrypt(base64_decode($_COOKIE["data"])), true);
First the encrypted string must be base64 decoded and then XOR encrypted/decrypted
The following PHP code will return the secret key:
|
|
Put everything together
Using the secret key, it is now possible to change the value of showpassword
to yes
, XOR encrypt the modified array and base64 encode it.
Once this is done, the cookie value in the browser has to be updated. Refreshing the site will reveal the password for the next level.