一、apache基本配置
- 以centos6.9为例,apache版本为2.2.15
[root@redwand conf]# httpd -v
Server version: Apache/2.2.15 (Unix)
Server built: Oct 19 2017 16:43:38
- 配置文件httpd.conf默认配置。
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
......
LoadModule rewrite_module modules/mod_rewrite.so #rewrite模块为开启状态
- 关于AllowOverride,httpd.conf配置文件的解释如下
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
- .htaccess文件作为局部变量作用文件成功作用的两个条件
1、Allow Override All
2、LoadModule rewrite_module modules/mod_rewrite.so #rewrite模块为开启状态
二、原理测试
- 当rewrite模块开启,配置文件httpd.conf如下时,apache服务器会将所有.jpg为后缀的文件作为php文件解析。
<Directory />
Options FollowSymLinks
AllowOverride All
AddType application/x-httpd-php .jpg #将.jpg后缀的文件作为PHP文件解析
</Directory>
# AddType allows you to add to or override the MIME configuration
# file mime.types for specific file types.
#
#AddType application/x-tar .tgz
- 当上传.htaccess文件到upload目录时,upload目录下的文件会按其配置生效解析
httpd.conf文件配置
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
.htaccess文件内容
AddType application/x-httpd-php .jpg
- 这样配置有一个问题,apache会将所有的.jpg后缀的文件当作php文件解析,这样会明显影响系统的功能,改进代码如下,这样系统就只对文件名包含“info.png”字符串的文件进行解析,例如aaainfo.png,aaainfo.pngxsdf,aaainfo.png.txt。
<FilesMatch "info.png">
setHandler application/x-httpd-php
</FilesMatch>
再次改进
<FilesMatch "^info.png$">
setHandler application/x-httpd-php
</FilesMatch>
三、.htaccess上传漏洞
漏洞形成条件
- apache服务器
- 能够上传.htaccess文件,一般为黑名单限制。
- AllowOverride All,默认配置为关闭None。
- LoadModule rewrite_module modules/mod_rewrite.so #模块为开启状态
- 上传目录具有可执行权限。