Ports 25, 565, 587 - Simple Mail Tranfer Protocol (SMTP)
Related resources
The Simple Mail Transfer Protocol (SMTP) is a protocol for sending emails in an IP network. By default, SMTP servers accept connection requests on port 25. However, newer SMTP servers also use other ports such as TCP port 587. This port is used to receive mail from authenticated users/servers, usually using the STARTTLS command. SMTP works unencrypted without further measures and transmits all commands, data, or authentication information in plain text. To prevent unauthorized reading of data, the SMTP is used in conjunction with SSL/TLS encryption. Under certain circumstances, a server uses a port other than the standard TCP port 25 for the encrypted connection, for example, TCP port 465.
Mail User Agent
(MUA
): SMTP client who sends the email. MUA converts it into a header and a body and uploads both to the SMTP server.
Mail Submission Agent
(MSA
), which checks the validity, i.e., the origin of the e-mail. This MSA
is also called Relay
server. These are very important later on, as the so-called Open Relay Attack
. Proxy that occasionally precedes the MTA to relieve the load. It checks the validity, i.e., the origin of the e-mail. This MSA
is also called Relay
server.
Mail Transfer Agent
(MTA
): The MTA checks the e-mail for size and spam and then stores it. At this point of the process, this MTA works as the sender's server. The MTA then searches the DNS for the IP address of the recipient mail server. On arrival at the destination SMTP server, the receiver's MTA reassembles the data packets to form a complete e-mail.
Mail delivery agent
(MDA
): It deals with transferring the email to the recipient's mailbox.
Other concepts:
- DKIM : DomainKeys Identified Mail (DKIM) provides a method for validating a domain name identity that is associated with a message through cryptographic authentication. The identity is independent of other email identities, such as the author's From: field.
- Sender Policy Framework (
SPF
): Sender Policy Framework (SPF) is used to authenticate the sender of an email. With an SPF record in place, Internet Service Providers can verify that a mail server is authorized to send email for a specific domain. An SPF record is a DNS TXT record containing a list of the IP addresses that are allowed to send email on behalf of your domain.
Extended SMTP (ESMTP)
Extended SMTP (ESMTP) deals with the main two shortcomings of SMTP protocol:
- In SMTP, users are not authenticated, therefore the sender is unreliable.
- SMTP doesn't have confirmations.
For this, ESMTP uses TLS for encryption and AUTH PLAIN extension for authentication. See also Postfix
Basic commands
If we are connected to a proxy and we want this proxy to connect to a SMTP server, the command that we would send than would look something like this:
Example:
With this setting, this SMTP server can send fake emails and thus initialize communication between multiple parties. Another attack possibility would be to spoof the email and read it.
Footprinting SMTP
Port | Service |
---|---|
TCP/25 |
SMTP Unencrypted |
TCP/143 |
IMAP4 Unencrypted |
TCP/110 |
POP3 Unencrypted |
TCP/465 |
SMTP Encrypted |
TCP/587 |
SMTP Encrypted/STARTTLS |
TCP/993 |
IMAP4 Encrypted |
TCP/995 |
POP3 Encrypted |
Enumeration
When it comes to enumeration it is critical to understand if we are facing a cloud service or a custom mail server implementation.
Custom mail server implementation
We will focus on typical misconfigurations
User enumeration with commands
The SMTP server has different commands that can be used to enumerate valid usernames VRFY
, EXPN
, and RCPT TO
. If we successfully enumerate valid usernames, we can attempt to password spray, brute-forcing, or guess a valid password.
VRFY
First we try to connect to the server:
If anonymous login is enabled, we are in. Otherwise we could try to login, for instance with user root
:
The code 252 in the answer means that the user exists. The code 550 means that it is an unknown user.
EXPN
EXPN
is similar to VRFY
, except that when used with a distribution list.
First we try to connect to the server:
Then we try to list all users on that distribution list with EXPN:
A typical response could be:
RCPT TO Command
RCPT TO
identifies the recipient of the email message. This command can be repeated multiple times for a given message to deliver a single message to multiple recipients.
First we try to connect to the server:
Then we attempt the RCPT TO
command:
USER
We can also use the POP3
protocol to enumerate users depending on the service implementation. For example, we can use the command USER
followed by the username, and if the server responds OK
. This means that the user exists on the server. First we try to connect to the server:
Then we attempt the USER
command:
User enumeration with commands
A way to automate this enumeration is this script for user enumeration:
Results from script in user enumeration:
smtp-user-enum
Username guessing tool primarily for use against the default Solaris SMTP service. Can use either EXPN, VRFY or RCPT TO.
Repo: https://github.com/pentestmonkey/smtp-user-enum
Basic usage:
Cloud mail server implementation
Enumerating users in Microsoft Office 365 (O365)
O365spray
O365spray is a username enumeration and password spraying tool aimed at Microsoft Office 365 (O365). This tool reimplements a collection of enumeration and spray techniques.
Basic usage:
Password Attacks
Custom mail servers
hydra
Cloud mail services
If cloud services support SMTP, POP3, or IMAP4 protocols, we may be able to attempt to perform password spray using tools like Hydra
, but these tools are usually blocked.
MSOffice 365
O365spray
See O365spray
MailSniper
See MailSniper
Gmail and Okta
Open relay attack
An open relay is a Simple Mail Transfer Protocol (SMTP
) server, which is improperly configured and allows an unauthenticated email relay.
First, identify if an SMTP port allows an open relay:
Next, we can use any mail client to connect to the mail server and send our email.
See swaks.
Attacks on SMTPD
Affected version: OpenSMTPD up to version 6.6.2 Vulnerability: CVE-2020-7247 and leads to RCE.
The vulnerability in this service lies in the program's code, namely in the function that records the sender's email address. This offers the possibility of escaping the function using a semicolon (;
) and making the system execute arbitrary shell commands. However, there is a limit of 64 characters, which can be inserted as a command. The technical details of this vulnerability can be found here. Exploit here
Postfix, an example of a SMTP server
Configuration file
See how to install postfix server.
The configuration file for Porsfix service is /etc/postfix/main.cf