前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Apache CGI模式下修改.htaccess导致服务器500的解决办法

Apache CGI模式下修改.htaccess导致服务器500的解决办法

作者头像
xuing
发布2019-10-19 09:34:13
1.6K0
发布2019-10-19 09:34:13
举报

通过向.htaccess文件添加AddType或AddHandler使非php文件被解析

介绍

除了正常的用途外,在渗透中的Apache环境下,可以用.htaccess来重写Apache配置以绕过上传,或者留后门什么的。

条件

一般网上复现给出的条件是

  1. httpd.conf中 AllowOverride All (文件中可能有多处,仅改DocumentRoot指向我们文件的即可)
  2. 开启mod_rewrite.so模块,这个一般是默认开启的。 以上两处都是服务端配置,实际上实战场景,我们也改不了。但本地复现的时候可能会遇到这个问题。 接下来给出的.htaccess写法都类似于
代码语言:javascript
复制
AddType application/x-httpd-php .jpg

亦或者是

代码语言:javascript
复制
  <FilesMatch "test.jpg">
    SetHandler application/x-httpd-php
  </FilesMatch>

但这两种写法其实都没有本质的区别。 在Apache模块DLL模式下,应该是能成功的。 但是如果你使用的是PHPStudy在windows下面,可能会有复现失败的情况。这是因为PHPStudy在大部分情况(我遇到的)下,使用的是CGI/FastCGI模式。

解决方法

以后缀名abc为例,路径自行进行替换。(经测试更改.htaccess是可以不重启服务器的)

代码语言:javascript
复制
AddHandler fcgid-script .abc
FcgidWrapper "D:/phpstudy_pro/Extensions/php/php7.3.4nts/php-cgi.exe" .abc

这里将AddHandler替换为SetHandler也是可以的。

这个写法,参考了vhosts.conf或者Apache2.4.39\conf\vhosts\localhost_80.conf这里。 也就给我们拓宽了思路,如果我们自己的.htaccess失效。可以参考默认生成的配置文件是如何对php进行解析的。

代码语言:javascript
复制
<VirtualHost _default_:80>
    DocumentRoot "D:/phpstudy_pro/WWW/localhost"
    FcgidInitialEnv PHPRC "D:/phpstudy_pro/Extensions/php/php7.3.4nts"
    AddHandler fcgid-script .php
    FcgidWrapper "D:/phpstudy_pro/Extensions/php/php7.3.4nts/php-cgi.exe" .php
    ErrorLog "D:/phpstudy_pro/Extensions/Apache2.4.39/logs/localhost_error.log"
    CustomLog "D:/phpstudy_pro/Extensions/Apache2.4.39/logs/localhost_acess.log" common
  <Directory "D:/phpstudy_pro/WWW/localhost">
      Options FollowSymLinks ExecCGI
      AllowOverride All
      Order allow,deny
      Allow from all
      Require all granted
      DirectoryIndex index.php index.html
  </Directory>
  ErrorDocument 400 /error/400.html
  ErrorDocument 403 /error/403.html
  ErrorDocument 404 /error/404.html
  ErrorDocument 500 /error/500.html
  ErrorDocument 501 /error/501.html
  ErrorDocument 502 /error/502.html
  ErrorDocument 503 /error/503.html
  ErrorDocument 504 /error/504.html
  ErrorDocument 505 /error/505.html
  ErrorDocument 506 /error/506.html
  ErrorDocument 507 /error/507.html
  ErrorDocument 510 /error/510.html
</VirtualHost>

.htaccess中添加php_value auto_append_file导致500错误

介绍

.htaccess中除了上面的添加文件的php解析以外。还可以通过添加auto_append_fileauto_prepend_file向所有php文件中的开头或最后插入指定的文件内容。可以用来制作后门。具体可自行百度。

复现方式

网上给出的方法一般是在.htaccess中添加如下代码

代码语言:javascript
复制
php_value auto_prepend_file "/home/fdipzone/header.php"
php_value auto_append_file "/home/fdipzone/footer.php"

但是如果运行在CGI模式下,这个php_value是不能被识别的,会导致服务器500错误。

解决方案

1. 通过.user.ini来实现

可以通过 .user.ini 来设置auto_append_file、auto_prepend_file。来添加后门。 自 PHP 5.3.0 起,PHP 支持基于每个目录的 .htaccess 风格的 INI 文件。此类文件仅被 CGI/FastCGI SAPI 处理 在目录下创建.user.ini文件。内容如下:

代码语言:javascript
复制
auto_prepend_file = 123.gif

123.gif自行替换为需要引入的文件。可以是.user.in的相对路径。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.10.17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 通过向.htaccess文件添加AddType或AddHandler使非php文件被解析
    • 介绍
      • 条件
        • 解决方法
        • .htaccess中添加php_value auto_append_file导致500错误
          • 介绍
            • 复现方式
              • 解决方案
                • 1. 通过.user.ini来实现
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档