Magic Cars

Description

...Writing after ctf ended so no access to challenge descriptions...

http://52.59.124.14:10021/arrow-up-right

Sourcearrow-up-right

Analysis

Given source doesnt contain much, 99% is just frontend. What we are interested is index.php

PHP code inside index.php:

  <?php error_reporting(0); ?>
  <?php
    $files = $_FILES["fileToUpload"];
    $uploadOk = true;
    if ($files["name"] != "") {
        $target_dir = urldecode("images/" . $files["name"]);
        if (strpos($target_dir, "..") !== false) {
            $uploadOk = false;
        }
        if (filesize($files["tmp_name"]) > 1 * 1000) {
            $uploadOk = false;
            echo "too big!!!";
        }
        $extension = strtolower(pathinfo($target_dir, PATHINFO_EXTENSION));
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        $type = finfo_file($finfo, $files["tmp_name"]);
        finfo_close($finfo);
        if ($extension != "gif" || strpos($type, "image/gif") === false) {
            echo " Sorry, only gif files are accepted";
            $uploadOk = false;
        }
        $target_dir = strtok($target_dir, chr(0));
        if ($uploadOk && move_uploaded_file($files["tmp_name"], $target_dir)) {
            echo "<a href='$target_dir'>uploaded gif here go see it!</a>";
        }
    }
?>

We can upload files and view them. There's few restrictions:

  1. Filename cant contain .. (Prevents directory traversal)

  2. File size cant exceed 1000kb

  3. It has to be type of GIF

  4. Extension must be a GIF

Type checking is easy to bypass due to magic bytesarrow-up-right, tricky part is $extension.

I found the attack vector on one of the Hak5 Forumarrow-up-right It says is that if you do exploit.php%00.jpg pathinfo will parse extension as .jpg, but actual filename is exploit.php due to NULL byte.

Solution

I used weevelyarrow-up-right to generate PHP payload.

circle-info

Wikipedia says that gif magic bytes are GIF87a or GIF89a. I learned GIF8; from IppSecarrow-up-right and it works so... yeah.

Upload. Response: a href='images/agent.php'>uploaded gif here go see it!</a></body>

Connect to backdoor:

circle-check

Last updated