Featured image of post Bypassing a preg_match filter for command injection - PwnMe 2023

Bypassing a preg_match filter for command injection - PwnMe 2023

Bypassing a preg_match filter over shell_exec to read flag.txt via command injection. PwnMe 2023 web write-up.

Introduction

This is a WEB challenge from the PWNME CTF.

Context Explanation

Here, you can check the content present on the server.

Prompt

Find a way to abuse this functionality and read the contents of the flag.txt file.

Solution

Tree viewer

The homepage. The source immediately gives it away with <?= shell_exec('ls '.$parsed); ?>: control $parsed and you get command execution.

The input is filtered through preg_match_all, which blocks ; and |:

<?php
$parsed = isset($_POST['input']) ? $_POST['input'] : "/home/";
if($illegals){
    echo "Illegals chars found";
    $parsed = "/home/";
}

The filter forgets &&, which chains commands just as well. So && cat /home/flag.txt runs after the ls and leaks the file:

Tree viewer flag

Payload: && cat /home/flag.txt

Flag: PWNME{US3R_1nPUT2_1n_ShELL_Y3S_6x8c}

Tips & Tricks

  • && can be used to chain commands, similar to ; or |.
  • Check which characters are filtered.
  • Review the page source code.