Regexploit : Find Regular Expressions Which Are Vulnerable To ReDoS (Regular Expression Denial Of Service)

Regexploit a tool to Find regexes which are vulnerable to Regular Expression Denial of Service (ReDoS).

More info on the Doyensec blog

Regexploit: DoS-able Regular Expressions

When thinking of Denial of Service (DoS), we often focus on Distributed Denial of Service (DDoS) where millions of zombie machines overload a service by launching a tsunami of data. However, by abusing the algorithms a web application uses, an attacker can bring a server to its knees with as little as a single request. Doing that requires finding algorithms which have terrible performance under certain conditions, and then triggering those conditions. One widespread and frequently vulnerable area is in the misuse of regular expressions (regexes).

Regular expressions are used for all manner of text-processing tasks. They may seem to run fine, but if a regex is vulnerable to Regular Expression Denial of Service (ReDoS), it may be possible to craft input which causes the CPU to run at 100% for years.

In this blog post, we’re releasing a new tool to analyse regular expressions and hunt for ReDoS vulnerabilities. Our heuristic has been proven to be extremely effective, as demonstrated by many vulnerabilities discovered across popular NPM, Python and Ruby dependencies.

Check your regexes with Regexploit

@doyensec/regexploit – pip install regexploit and find some bugs.

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 ReDoS
  • give an example malicious string which will cause catastrophic backtracking

Worst-Case Complexity

This 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.

Example

Run regexploit and enter the regular expression v\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, digits and underscores). As showed in the line Repeated 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 prefix v, 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 line Final 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 as pip install ua-parser==0.9.0. To scan the installed python modules run regexploit-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.

Installation

Python 3.8+ is required. To extract regexes from JavaScript / TypeScript code, NodeJS 12+ is also required.

Optionally make a virtual environment

python3 -m venv .env
source .env/bin/activate

Now actually install with pip

pip install regexploit

Usage

Regexploit with a list of regexes

Enter regular expressions via stdin (one per line) into regexploit.

regexploit

or via a file

cat myregexes.txt | regexploit

Extract regexes automatically

There is built-in support for parsing regexes out of Python, JavaScript, TypeScript, C#, YAML and JSON.

Python code

Parses Python code (without executing it) via the AST to find regexes. The regexes are then analysed for ReDoS.

regexploit-py my-project/
regexploit-py “my-project/*/.py” –glob

Javascript / Typescript

This will use the bundled NodeJS package in regexploit/bin/javascript which parses your JavaScript as an AST with eslint and prints out all regexes.

Those regexes are fed into the python ReDoS finder.

regexploit-js my-module/my-file.js another/file.js some/folder/
regexploit-js “my-project/node_modules/*/.js” –glob

N.B. there are differences between javascript and python regex parsing so there may be some errors. I’m not sure I want to write a JS regex AST!

Python imports

Search for regexes in all the python modules currently installed in your path / env. This means you can pip install whatever modules you are interested in and they will be analysed. Cpython code is included.

regexploit-python-env

N.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 Pillow

JSON / YAML

Yaml support requires pyyaml, which can be installed with pip install regexploit[yaml].

regexploit-json *.json
regexploit-yaml *.yaml

C# (.NET)

regexploit-csharp something.cs

R K

Recent Posts

Kali Linux 2024.4 Released, What’s New?

Kali Linux 2024.4, the final release of 2024, brings a wide range of updates and…

8 hours ago

Lifetime-Amsi-EtwPatch : Disabling PowerShell’s AMSI And ETW Protections

This Go program applies a lifetime patch to PowerShell to disable ETW (Event Tracing for…

8 hours ago

GPOHunter – Active Directory Group Policy Security Analyzer

GPOHunter is a comprehensive tool designed to analyze and identify security misconfigurations in Active Directory…

2 days ago

2024 MITRE ATT&CK Evaluation Results – Cynet Became a Leader With 100% Detection & Protection

Across small-to-medium enterprises (SMEs) and managed service providers (MSPs), the top priority for cybersecurity leaders…

5 days ago

SecHub : Streamlining Security Across Software Development Lifecycles

The free and open-source security platform SecHub, provides a central API to test software with…

1 week ago

Hawker : The Comprehensive OSINT Toolkit For Cybersecurity Professionals

Don't worry if there are any bugs in the tool, we will try to fix…

1 week ago