CVE-2024-12391
Executive Summary
CVE-2024-12391 is a Regular Expression Denial of Service (ReDoS) vulnerability identified in the binary-husky/gpt_academic project, specifically in the function 解析项目源码(手动指定和筛选源码文件类型) (Parse Project Source Code – Manually Specify and Filter Source File Types). The vulnerability exists as of commit 310122f and allows an attacker who can control both the regular expression pattern and the search string to cause the Python RE engine to take exponential time to process the input. This leads to a Denial of Service (DoS) condition, potentially hanging the server for an arbitrary amount of time. The CVSS score is 6.5 (Medium), with a network attack vector and low attack complexity. No patch is currently available, and the EPSS score is 0.0%, indicating low exploitation likelihood at this time.
This record — and 170,000+ others — are available via the ValtersIT API. One endpoint replaces five: NVD + GitHub CVE + CISA KEV + EPSS + Nuclei + Exploit-DB, unified.
No signup required
Supply Chain & Package Security
This vulnerability affects the gpt_academic Python package, which is distributed via PyPI. Users who install this package from PyPI are at risk if they expose the vulnerable function to user-controlled input. The supply chain risk is elevated because the package is open-source and may be integrated into larger systems without proper input validation. Organizations should audit their use of gpt_academic and ensure that the vulnerable function is not exposed to untrusted users.
CVSS Breakdown & Threat Level
- CVSS Vector: CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H
- Base Score: 6.5 (Medium)
- Attack Vector: Network (AV:N) – The vulnerability can be exploited remotely.
- Attack Complexity: Low (AC:L) – No special conditions are required.
- Privileges Required: None (PR:N) – No authentication needed.
- User Interaction: None (UI:N) – No user action required.
- Scope: Unchanged (S:U) – The impact is confined to the vulnerable component.
- Confidentiality Impact: None (C:N) – No data exposure.
- Integrity Impact: None (I:N) – No data modification.
- Availability Impact: High (A:H) – The service can be rendered unavailable.
Threat Level: Medium. While the impact is limited to availability, the ease of exploitation (low complexity, no privileges) makes it a viable vector for disrupting services.
Deep Dive & Mechanics
The vulnerability resides in the function 解析项目源码(手动指定和筛选源码文件类型), which allows users to provide custom regular expressions for filtering source code files. The Python re module uses a backtracking engine that can exhibit exponential time complexity for certain patterns, especially when combined with a malicious input string.
Mechanism:
- The user supplies a regular expression pattern (e.g.,
^(a+)+$) and a search string (e.g.,"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaab"). - The Python RE engine attempts to match the pattern against the string. For patterns with nested quantifiers (like
(a+)+), the engine may try an exponential number of backtracking paths. - This causes the CPU to spike and the server to hang, effectively denying service to legitimate users.
Example of a malicious pattern: ^(a+)+$ with input "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaab" (where the b at the end forces backtracking).
Vulnerable vs. Patched Code
Vulnerable Code (commit 310122f):
The function 解析项目源码 accepts user-provided regular expressions without any validation or timeout. The vulnerable snippet is conceptually:
def parse_source_code(regex_pattern, search_string):
# No validation or timeout
matches = re.findall(regex_pattern, search_string)
return matches
Patched Code (Hypothetical): A patch would involve:
- Validating the regular expression pattern to reject known ReDoS patterns.
- Implementing a timeout for regex execution using
signalorthreading. - Using a safer regex engine like
regexlibrary with timeout support.
import signal
import re
def parse_source_code(regex_pattern, search_string):
# Validate pattern (basic check)
if '^(' in regex_pattern and '+)+' in regex_pattern:
raise ValueError("Potentially dangerous regex pattern")
# Set a timeout for regex execution
class TimeoutError(Exception):
pass
def handler(signum, frame):
raise TimeoutError("Regex execution timed out")
signal.signal(signal.SIGALRM, handler)
signal.alarm(5) # 5-second timeout
try:
matches = re.findall(regex_pattern, search_string)
except TimeoutError:
return []
finally:
signal.alarm(0)
return matches
Exploitation & Lab Setup
Red Team (Exploitation)
Prerequisites:
- Access to the
gpt_academicinstance with the vulnerable function exposed. - Ability to send HTTP requests to the endpoint that triggers
解析项目源码.
Exploit Steps:
- Identify the endpoint that accepts user-provided regular expressions (e.g.,
/parse_source_code). - Craft a malicious regex pattern and input string. Example:
- Pattern:
^(a+)+$ - Input:
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaab"(30as followed byb)
- Pattern:
- Send the request:
curl -X POST http://target:port/parse_source_code \ -d 'pattern=^(a+)+$&input=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaab' - The server will hang for an extended period (potentially minutes or hours), causing a DoS.
Expected Outcome: The server becomes unresponsive to other requests, and CPU usage spikes to 100%.
Blue Team (Detection & Defense)
Detection:
- Monitor for requests with suspicious regex patterns (e.g., patterns with nested quantifiers like
(a+)+,(a|aa)+, etc.). - Set up alerts for high CPU usage on the server.
- Use WAF rules to block requests containing known ReDoS patterns.
Defense:
- Implement input validation to reject patterns that match ReDoS signatures.
- Use a timeout mechanism for all regex operations (e.g.,
signal.alarmorthreading.Timer). - Limit the length of input strings and regex patterns.
- Consider using a non-backtracking regex engine like
re2(viapython-re2).
Indicators of Compromise (IOCs)
Network IOCs:
- HTTP POST requests to endpoints like
/parse_source_codewith parameterspatternandinput. - Patterns containing nested quantifiers (e.g.,
(a+)+,(a|aa)+,(a*)*). - Input strings with long sequences of repeating characters followed by a non-matching character.
- HTTP POST requests to endpoints like
System IOCs:
- High CPU usage (near 100%) on the server.
- Long-running Python processes consuming excessive CPU time.
- Log entries showing regex execution times exceeding normal thresholds.
File IOCs:
- Temporary files or logs containing malicious regex patterns.
Detection Engineering
YARA Rule
rule ReDoS_Attack_Pattern {
meta:
description = "Detects potential ReDoS attack patterns in HTTP requests"
author = "Valters IT"
date = "2025-04-10"
cve = "CVE-2024-12391"
strings:
$pattern1 = /pattern=.*\([a-z]+\+\)\+/ nocase
$pattern2 = /pattern=.*\([a-z]+\|\)\+/ nocase
$pattern3 = /pattern=.*\([a-z]+\*\)\*/ nocase
$input_long = /input=[a-z]{20,}[^a-z]/ nocase
condition:
any of ($pattern*) and $input_long
}
Sigma Rule
title: ReDoS Attack Detection for CVE-2024-12391
id: 7a8b9c0d-1e2f-3a4b-5c6d-7e8f9a0b1c2d
status: experimental
description: Detects HTTP requests with malicious regex patterns targeting gpt_academic
author: Valters IT
date: 2025-04-10
logsource:
category: webserver
product: apache
detection:
selection:
cs-method: 'POST'
cs-uri-query|contains: '/parse_source_code'
cs-uri-query|re: 'pattern=.*\([a-z]+\+\)\+'
cs-uri-query|re: 'input=[a-z]{20,}[^a-z]'
condition: selection
falsepositives:
- Legitimate use of complex regex patterns (rare)
level: high
Suricata Rule
alert http any any -> any any (msg:"CVE-2024-12391 ReDoS Attack Attempt"; flow:to_server; content:"POST"; http_method; content:"/parse_source_code"; http_uri; pcre:"/pattern=.*\([a-z]+\+\)\+/Ri"; pcre:"/input=[a-z]{20,}[^a-z]/Ri"; classtype:attempted-dos; sid:1000001; rev:1;)
Remediation & Mitigation
Immediate Actions:
- Disable the vulnerable function if not required, or restrict access to trusted users only.
- Implement input validation to reject regex patterns with nested quantifiers (e.g.,
(a+)+,(a|aa)+,(a*)*). - Set a timeout for all regex operations using
signal.alarmorthreading.Timer(e.g., 5 seconds). - Use a safer regex engine like
re2(viapython-re2library) which guarantees linear time execution.
Long-term Fix:
- Monitor the
binary-husky/gpt_academicrepository for an official patch. - Apply the patch as soon as it becomes available.
- Conduct a security review of all user-supplied input handling in the application.
Workaround:
- If patching is not possible, consider using a reverse proxy (e.g., Nginx) to rate-limit requests to the vulnerable endpoint and block suspicious patterns.
Valters IT
What is this? Security researchers use these exact search queries on Shodan.io (a search engine for internet-connected devices) to find vulnerable systems globally. You can copy these dorks to see if your organization's IP space is exposed.
💣 PoC CODE AVAILABLE
Exploit code for this CVE is tracked in our database.
Access poc_code via API Pro.