Package: regexploit
RegexploitFind regexes which are vulnerable to Regular Expression Denial of Service (ReDoS).Many default regular expression parsers have unbounded worst-case complexity. Regex matching may be quick when presented with a matching input string. However, certain non-matching input strings can make the regular expression matcher go into crazy backtracking loops and take ages to process. This can cause denial of service, as the CPU will be stuck trying to match the regex.This tool is designed to:find regular expressions which are vulnerable to ReDoSgive an example malicious string which will cause catastrophic backtrackingWorst-case complexityThis reflects the complexity of the regular expression matcher's backtracking procedure with respect to the length of the entered string.Cubic complexity here means that if the vulnerable part of the string is doubled in length, the execution time should be about 8 times longer (2^3).
For exponential ReDoS with starred stars e.g.(a*)*$a fudge factor is used and the complexity will be greater than 10.For explotability, cubic complexity or higher is typically required unless truly giant strings are allowed as input.ExampleRunregexploitand enter the regular expressionv\w*_\w*_\w*$at the command line.$ regexploit
v\w*_\w*_\w*$
Pattern: v\w*_\w*_\w*$
---
Worst-case complexity: 3 ⭐⭐⭐ (cubic)
Repeated character: [5f:_]
Final character to cause backtracking: [^WORD]
Example: 'v' + '_' * 3456 + '!'The part\w*_\w*_\w*contains three overlapping repeating groups (\w matches letters, digitsand underscores). As showed in the lineRepeated character: [5f:_], a long string of_(0x5f) will match this section in many different ways. The worst-case complexity is 3 as there are 3 infinitely repeating groups. An example to cause ReDoS is given: it consists of the required prefixv, a long string of_and then a!(non-word character) to cause backtracking. Not all ReDoSes require a particular character at the end, but in this case, a long string of_will match the regex successfully and won't backtrack. The lineFinal character to cause backtracking: [^WORD]shows that a non-matching character (not a word character) is required at the end to prevent matching and cause ReDoS.As another example, install a module version vulnerable to ReDoS such aspip install ua-parser==0.9.0.
To scan the installed python modules runregexploit-python-env.Importing ua_parser.user_agent_parser
Vulnerable regex in /somewhere/.env/lib/python3.9/site-packages/ua_parser/user_agent_parser.py #183
Pattern: \bSmartWatch *\( *([^;]+) *; *([^;]+) *;
Context: self.user_agent_re = re.compile(self.pattern)
---
Worst-case complexity: 3 ⭐⭐⭐
Repeated character: [20]
Example: 'SmartWatch(' + ' ' * 3456

Worst-case complexity: 3 ⭐⭐⭐
Repeated character: [20]
Example: 'SmartWatch(0;' + ' ' * 3456

Vulnerable regex in /somewhere/.env/lib/python3.9/site-packages/ua_parser/user_agent_parser.py #183
Pattern: ; *([^;/]+) Build[/ ]Huawei(MT1-U06|[A-Z]+\d+[^\);]+)[^\);]*\)
Context: self.user_agent_re = re.compile(self.pattern)
---
Worst-case complexity: 3 ⭐⭐⭐
Repeated character: [[0-9]]
Example: ';0 Build/HuaweiA' + '0' * 3456
...For each vulnerable regular expression it prints one or more malicious string to trigger ReDoS. Setting your user agent to;0 Build/HuaweiA000000000000000...and browsing a website using an old version of ua-parser may cause the server to take a long time to process your request, probably ending in status 502.InstallationPython 3.8+ is required. To extract regexes from JavaScript / TypeScript code, NodeJS 12+ is also required.Optionally make a virtual environmentpython3-mvenv.envsource.env/bin/activateNow actually install with pippip install regexploitUsageRegexploit with a list of regexesEnter regular expressions via stdin (one per line) intoregexploit.regexploitor via a filecatmyregexes.txt|regexploitExtract regexes automaticallyThere is built-in support for parsing regexes out of Python, JavaScript, TypeScript, C#, YAML and JSON.Python codeParses Python code (without executing it) via the AST to find regexes. The regexes are then analysed for ReDoS.regexploit-pymy-project/
regexploit-py"my-project/**/*.py"--globJavascript / TypescriptThis will use the bundled NodeJS package inregexploit/bin/javascriptwhich parses your JavaScript as an AST witheslintand prints out all regexes.Those regexes are fed into the python ReDoS finder.regexploit-jsmy-module/my-file.jsanother/file.jssome/folder/
regexploit-js"my-project/node_modules/**/*.js"--globN.B. there are differences between javascript and python regex parsing so there may be some errors. I'mnot sure I wantto write a JS regex AST!Python importsSearch for regexes in all the python modules currently installed in your path / env. This means you canpip installwhatever modules you are interested in and they will be analysed. Cpython code is included.regexploit-python-envN.B. this doesn't parse the python code to an AST and will only find regexes compiled automatically on module import. Modules are actually imported,so code in the modules will be executed. This is helpful for finding regexes which are built up from smaller strings on load e.g.CVE-2021-25292 in PillowJSON / YAMLYaml support requires pyyaml, which can be installed withpip install regexploit[yaml].regexploit-json*.json
regexploit-yaml*.yamlC# (.NET)regexploit-csharpsomething.cs:trophy: Bugs reported :trophy:CVE-2020-5243: uap-coreaffecting uap-python,uap-ruby, etc. (User-Agent header parsing)CVE-2020-8492: cpython's urllib.request(WWW-Authenticate header parsing)CVE-2021-21236: CairoSVG(SVG parsing)CVE-2021-21240: httplib2(WWW-Authenticate header parsing)CVE-2021-25292: python-pillow(PDF parsing)CVE-2021-26813: python-markdown2(Markdown parsing)CVE-2021-27290: npm/ssri(SRI parsing)CVE-2021-27291: pygmentslexers for ADL, CADL, Ceylon, Evoque, Factor, Logos, Matlab, Octave, ODIN, Scilab & Varnish VCL (Syntax highlighting)CVE-2021-27292: ua-parser-js(User-Agent header parsing)CVE-2021-27293: RestSharp(JSON deserialisation in a .NET C# package)bpo-38804: cpython's http.cookiejar(Set-Cookie header parsing)SimpleCrawler (archived)(HTML parsing)CVE-2021-28092: to be releasedPlus unpublished bugs in a handful of pypi, npm, ruby and nuget packagesCreditsThis tool has been created by Ben Caller ofDoyensec LLCduring research time.