Need help with regexp-security-cheatsheet?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.
464 Stars 70 Forks 65 Commits 0 Opened issues

Services available

Need anything else?

Regexp Security Cheatsheet

Research was done to find "weak places" in regular expressions of Web Application Firewalls (WAFs).
Repository contains SAST, which can help you to find security vulnerabilities in custom regular expressions in own projects.
Contribution is highly welcomed.

High severity issues:

|#| Requirement | Vulnerable regex example | Bypass example | |---|---|---|---| |1| Regexp should avoid using

) and
) symbols, which are metacharacters for start and end of a string. It is possible to bypass regex by inserting any symbol in front or after regexp. |
|2| Regexp should be case-insensitive:
. It is possible to bypass regex using upper or lower cases in words. Modsecurity transformation commands (which are applied on string before regex pattern is applied) can also be included in tests to cover more regexps. |
|3| In case modifier
is not (globally) specified, regexp should avoid using dot
symbol, which means every symbol except newline (
). It is possible to bypass regex using newline injection. |
|4| Regexp should not be vulnerable to ReDoS. OWASP ReDoS article 1. Find various evil patterns. 2. Generate evil string using e.g. “SDL Regex Fuzzer” |
|5| Number of repetitions of set or group
should be carefully used, as one can bypass such limitation by lowering or increasing specified numbers. |
aaaaaa (6 times)
|6| Nonstandard ranges (almost everything except a-z, 0-9, a-f, etc) |
[A-z] = [a-zA-Z] + [\]^_`
|7| Regexp should only use plus “
” metacharacter in places where it is necessary, as it means “one or more”. Alternative metacharacter star “
”, which means “zero or more” is generally preferred. |
|8| Usage of newline wildcards should be reasonable.
characters can often be bypassed by either substitution, or by using newline alternative
and others. Wildcard
has different meanings while using it in square brackets (“backspace”) and in plain regex (“word boundary”) - RegexLib |
? |9| Regexp should be applied to right scope of inputs:
Cookies names and values
Argument names and values
Header names and values
Files argument names and content
. Modsecurity:
grep -oP 'SecRule(.*?)"' -n
Other WAFs: manual observation. | Argument values | Cookie names and values |10| Regular expression writers should be careful while using only whitespace character (
) as separators. Rule can be bypassed e.g. with newline character, tabulation, by skipping whitespace, or alternatives. |
a not b
|11| Nonstandard combinations of operators |
|12| Special cases: whitespaces before operators |
(a \|b)c
|13| Usage of wrong syntax in POSIX character classes |
|14| Opposite usage of brackets [], () and {} |

Medium severity issues (non-expected behaviour: manual observation needed):

|#| Requirement | Vulnerable regex example | Bypass example | |---|---|---|---| |15| Check backlinks, and bear in mind that

can be backlink -OR- 0x09 |

|16| Unsafe usage of comments |
a(?#some comment about wildcards:\)(\w*)b
|17| Excessive usage of metacharacters in [] |

|18| Rarely used wildcards. All wildcards except popular: A,Z,b,r,n,t,wW,sS,dD,u,x  | 
\a = 0x07; \e = 0x1B; \R = \r\|\n\|\r\n; \xXX = 0xXX; \ddd = 0oddd; \cX, \x{XXXX}, \H, \V, \G

|19| Excessive escaping, e.g. escaping symbol which is not a wildcard | 

|20| Unsafe usage of recursion, IF statements, etc | 
, ... |

|21| Unsafe usage of ranges | 

Experimental rules (probably to be removed):

|#| Requirement | Vulnerable regex example | Bypass example | |---|---|---|---| |X| Greediness of regular expressions should be considered. Highlight of this topic is well done in Chapter 9 of Jan Goyvaert’s tutorial. While greediness itself does not create bypasses, bad implementation of regexp Greediness can raise False Positive rate. This can cause excessive log-file flooding, forcing vulnerable rule or even whole WAF to be switched off. | | |X| Best Practice from slides of Ivan Novikov: Modsecurity should avoid using t:base64Decode function (t:base64DecodeExt instead). |


Vladimir Ivanov @httpsonly

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.