ReDoS
Introduction
The Regular expression Denial of Service (ReDoS) [1] [2] is a Denial-of-service attack, that exploits the fact that most Regular expression implementations may reach extreme situations that cause them to work very slowly (exponentially related to input size). An attacker can then cause a program using a Regular expression to enter these extreme situations and then hang for a very long time. [3] [4]
Description
The problematic Regex naïve algorithm
The Regular expression naïve algorithm builds a Nondeterministic finite-state machine, which is a finite state machine where for each pair of state and input symbol there may be several possible next states. Then the engine starts to make transition until the end of the input. Since there may be several possible next states, a deterministic algorithms is used. This algorithm tries one by one all the possible paths (if needed) until a match is found (or all the paths are tried and fail).[5]
For example, the Regex ^(a+)+$ is represented by the following NFA - Regex NFA
For the input aaaaX there are 16 possible paths in the above graph. But for aaaaaaaaaaaaaaaaX there are 65536 possible paths, and the number is double for each additional a. This is an extreme case where the naïve algorithm is problematic, because it must pass on many many paths, and then fail.[6]
Notice, that not all algorithms are naïve, and actually Regex algorithms can be written in an efficient way. Unfortunately, most Regex engines today try to solve not only "pure" Regexes, but also "expanded" Regexes with "special additions", such as back-references that cannot be always be solved efficiently (see Patterns for non-regular languages in Regular expression for some more details). So even if the Regex is not "expanded", a naïve algorithm is used.
Evil Regexes
A Regex is called "evil" if it can stuck on crafted input.[7]
Evil Regex pattern contains:
- Grouping with repetition
- Inside the repeated group:
- Repetition
- Alternation with overlapping
Examples of Evil Patterns:
- (a+)+
- ([a-zA-Z]+)*
- (a|aa)+
- (a|a?)+
- (.*a){x} | for x > 10
All the above are susceptible to the input aaaaaaaaaaaaaaaaaaaaaaaa! (The minimum input length might change slightly, when using faster or slower machines).
"Weak" ReDoS
Although the main problem with ReDoS seems to be Evil Regexs, that cause the computer to hang for very short inputs, there is another type of ReDoS, which can be called "Weak" ReDoS. This ReDoS contains patterns that do not cause an exponential behavior, but for long enough inputs (a few hundreds of characters, usually) they make the Regex engine hang for a long time.
An example for such a pattern is
a*b?a*x
Using aaaaaa... (1000 characters) will hang most Regex engines. And this input is certainly a legitimate input length, especially when using files as an input.
This type of pattern might be more common, since at first sight it seems like there is no problem with the expression.
Attacks
The attacker might use the above knowledge to look for applications that use Regular Expressions, containing an Evil Regex, and send a well-crafted input, that will hang the system. Alternatively, if a Regex itself is affected by a user input, the attacker can inject an Evil Regex, and make the system vulnerable.
Risk Factors
The Web is Regex-Based. In every layer of the WEB there are Regular Expressions, that might contain an Evil Regex. An attacker can hang a WEB-browser (on a computer or potentially also on a mobile device), hang a Web Application Firewall (WAF), attack a database, and even stack a vulnerable WEB server.
For example, if a programmer uses a Regex to validate the client side of a system, and the Regex contains an Evil Regex, the attacker can assume the same vulnerable Regex is used in the server side, and send a well-crafted input, that stacks the WEB server.
Examples
Vulnerable Regex in online repositories
1. ReGexLib,id=1757 (email validation) - see bold part, which is an Evil Regex
^([a-zA-Z0-9])(([\-.]|[_]+)?([a-zA-Z0-9]+))*(@){1}[a-z0-9]+[.]{1}(([a-z]{2,3})|([a-z]{2,3}[.]{1}[a-z]{2,3}))$
Input:
aaaaaaaaaaaaaaaaaaaaaaaa!
2. OWASP Validation Regex Repository, Java Classname - see bold part, which is an Evil Regex
^(([a-z])+.)+[A-Z]([a-z])+$
Input:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!
Web application attack
- Open a JavaScript
- Find Evil Regex
- Craft a malicious input for the found Regex
- Submit a valid value via intercepting proxy
- Change the request to contain a malicious input
- You are done!
ReDoS via Regex Injection
The following example checks if the username is part of the password entered by the user.
String userName = textBox1.Text; String password = textBox2.Text; Regex testPassword = new Regex(userName); Match match = testPassword.Match(password); if (match.Success) { MessageBox.Show("Do not include name in password."); } else { MessageBox.Show("Good password."); }
If an attacker enters ^(([a-z])+.)+[A-Z]([a-z])+$ as a username and aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa! as a password, the program will hang.
References
- ^ OWASP (2010-02-10). "Regex Denial of Service". Retrieved 2010-04-16.
- ^
Checkmarx (2009-09-13). "Regular expression Denial of Service Revisited". Retrieved 2010-03-19.
{{cite web}}
: External link in
(help)|author=
- ^ RiverStar Software (2010-01-18). "Security Bulletin: Caution Using Regular Expressions". Retrieved 2010-04-16.
- ^
Ristic, Ivan (2010-03-15). ModSecurity Handbook. London, UK: Feisty Duck Ltd. p. 173. ISBN 978-1-907117-02-2.
{{cite book}}
: Check|authorlink=
value (help); External link in
(help)|authorlink=
and|publisher=
- ^ Crosby&Wallach, Usenix Security (2003). "Regular Expression Denial Of Service". Retrieved 2010-01-13.
- ^
OWASP NL (2009-12-10). "VAC Presentation - ReDoS, OWASP-NL Chapter meeting, Dec. 2009" (PDF). Retrieved 2010-03-22.
{{cite web}}
: External link in
(help)|author=
- ^ Jim Manico&Adar Weidman (2009-12-07). "OWASP Podcast 56 (ReDoS)". Retrieved 2010-04-02.
External links
- Examples of ReDoS in open source applications:
- Some benchmarks for ReDoS
- Achim Hoffman (2010). "ReDoS - benchmark for regular expression DoS in JavaScript". Retrieved 2010-04-19.
- Richard M. Smith (2010). "Regular expression denial of service (ReDoS) attack test results". Retrieved 2010-04-19.