Apache CGI模式下修改.htaccess導(dǎo)致服務(wù)器500的解決辦法

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

介紹

除了正常的用途外,在滲透中的Apache環(huán)境下,可以用.htaccess來重寫Apache配置以繞過上傳,或者留后門什么的。

條件

一般網(wǎng)上復(fù)現(xiàn)給出的條件是

  1. httpd.conf中 AllowOverride All (文件中可能有多處,僅改DocumentRoot指向我們文件的即可)
  2. 開啟mod_rewrite.so模塊,這個一般是默認(rèn)開啟的。
    以上兩處都是服務(wù)端配置,實(shí)際上實(shí)戰(zhàn)場景,我們也改不了。但本地復(fù)現(xiàn)的時候可能會遇到這個問題。
    接下來給出的.htaccess寫法都類似于
AddType application/x-httpd-php .jpg

亦或者是

  <FilesMatch "test.jpg">
    SetHandler application/x-httpd-php
  </FilesMatch>

但這兩種寫法其實(shí)都沒有本質(zhì)的區(qū)別。
在Apache模塊DLL模式下,應(yīng)該是能成功的。
但是如果你使用的是PHPStudy在windows下面,可能會有復(fù)現(xiàn)失敗的情況。這是因?yàn)镻HPStudy在大部分情況(我遇到的)下,使用的是CGI/FastCGI模式。

解決方法

以后綴名abc為例,路徑自行進(jìn)行替換。(經(jīng)測試更改.htaccess是可以不重啟服務(wù)器的)

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失效??梢詤⒖寄J(rèn)生成的配置文件是如何對php進(jìn)行解析的。

<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導(dǎo)致500錯誤

介紹

.htaccess中除了上面的添加文件的php解析以外。還可以通過添加auto_append_fileauto_prepend_file向所有php文件中的開頭或最后插入指定的文件內(nèi)容。可以用來制作后門。具體可自行百度。

復(fù)現(xiàn)方式

網(wǎng)上給出的方法一般是在.htaccess中添加如下代碼

php_value auto_prepend_file "/home/fdipzone/header.php"
php_value auto_append_file "/home/fdipzone/footer.php"

但是如果運(yùn)行在CGI模式下,這個php_value是不能被識別的,會導(dǎo)致服務(wù)器500錯誤。

解決方案

1. 通過.user.ini來實(shí)現(xiàn)

可以通過 .user.ini 來設(shè)置auto_append_file、auto_prepend_file。來添加后門。
自 PHP 5.3.0 起,PHP 支持基于每個目錄的 .htaccess 風(fēng)格的 INI 文件。此類文件僅被 CGI/FastCGI SAPI 處理
在目錄下創(chuàng)建.user.ini文件。內(nèi)容如下:

auto_prepend_file = 123.gif

123.gif自行替換為需要引入的文件??梢允?code>.user.in的相對路徑。
其他還有

參考鏈接

Using .htaccess to make all .html pages to run as .php files?
Apache下PHP的幾種工作方式
Apache 中文文檔
Apache 2.4 官方文檔
第二部分的參考鏈接
Nginx 設(shè)置 PHP_VALUE 的靈異問題
Unable to set php_value in .htaccess file

我的博客即將同步至騰訊云+社區(qū),邀請大家一同入駐:https://cloud.tencent.com/developer/support-plan?invite_code=2cwj8l6bjwe84

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容