Email Services
A mail server (also called email server) is a server that manages and delivers emails across a network, typically over the Internet. A mail server can receive emails from client devices and send them to other mail servers, as well as deliver emails to clients. A client is typically the device from which we read our emails (like computers, mobile devices, etc.).
When we press the Send button in our email application (mail client), the program establishes a connection with an SMTP server on the network or Internet. SMTP (Simple Mail Transfer Protocol) is the protocol used to send emails from clients to servers and between servers.
When we download emails in our application, it connects to a POP3 or IMAP4 server, which allows the user to store messages in the server's mailbox and download them periodically.
By default, POP3 clients delete downloaded messages from the server. This behavior makes it difficult to access mail from multiple devices, as messages remain stored locally. However, we can usually configure the POP3 client to keep copies on the server.
On the other hand, IMAP4 clients do not delete messages from the server by default, allowing easy access to emails from multiple devices.
Let's see how we can attack mail servers.
Enumeration
Mail servers are complex and typically require us to enumerate multiple servers, ports, and services. Additionally, nowadays most companies have their email services in the cloud, such as Microsoft 365 or G-Suite. Therefore, our way of attacking the email service will depend on the type of service they are using.
We can use the DNS MX (Mail eXchanger) record to identify the mail server. This record specifies the server responsible for accepting email messages on behalf of a domain. It's possible to configure multiple MX records, typically pointing to a set of mail servers for load balancing and redundancy.
We can use tools like host
or dig
, or websites like MXToolbox to query information about MX records.
Host - MX Records
DIG - MX Records
Host - A Records
These MX records indicate that the first three email services are using cloud services like G-Suite (aspmx.l.google.com
), Microsoft 365 (microsoft-com.mail.protection.outlook.com
), and Zoho (mx.zoho.com
), while the last one is probably a custom mail server hosted by the company.
This information is important because enumeration methods can vary depending on the service. For example, most cloud providers use their own implementation of the mail server and adopt modern authentication, which opens unique attack vectors specific to each provider. In contrast, if the company has configured its own service, we might find bad practices and insecure configurations that allow common attacks on mail server protocols.
If we are targeting a custom mail server like inlanefreight.htb
, we can enumerate the following ports:
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
We can use Nmap with the -sC
option (default scripts) to enumerate these ports on the target system:
Misconfigurations
Email services use authentication to allow users to send and receive emails. A misconfiguration can occur when the SMTP service allows anonymous authentication or supports commands that can be used to enumerate valid usernames.
Authentication
The SMTP server has different commands that can be leveraged to enumerate valid users: VRFY
, EXPN
, and RCPT TO
. If we can enumerate valid users, we can attempt to perform password spraying, brute force, or even guess a valid password. Let's see how these commands work:
VRFY: This command tells the SMTP server to verify if a specific username or email address exists. The server will respond indicating whether the user exists or not. This functionality is usually disabled for security reasons.
VRFY Command
EXPN Command
EXPN
is similar to VRFY
, with the difference that when used with a distribution list, it returns all users included in that list. This can be more problematic than the VRFY
command, as there are often aliases like "all" that group many users.
As we can see, the EXPN
command returns individual user addresses that are part of the distribution list. This expands our attack surface.
RCPT TO Command
RCPT TO
identifies the recipient of an email message. This command can be repeated multiple times to send a single message to multiple recipients.
As we can see, this command also allows us to validate if a user exists in the system based on the server's response. This can be used to enumerate valid users before launching attacks like password spraying or brute force.
USER Command
We can also use the POP3 protocol to enumerate users, depending on how the service is implemented. For example, we can use the USER
command followed by the username, and if the server responds with +OK
, that means the user exists on the server.
This behavior can be leveraged to enumerate valid accounts before attempting authentication attacks like brute force or password spraying.
We can automate the user enumeration process on SMTP servers with the smtp-user-enum
tool. This utility allows checking if valid email addresses exist using commands like VRFY
, EXPN
, or RCPT
.
In the following example, we are using:
-M RCPT
: specifies the enumeration mode-U userlist.txt
: list of usernames to test-D inlanefreight.htb
: domain to be added to each user-t 10.129.203.7
: target SMTP server IP
Cloud Enumeration
As we saw before, cloud service providers like Microsoft implement their own email systems. In the case of Office 365, we can abuse specific functions like user enumeration.
A useful tool for this is O365spray
, which allows validating if a domain uses Office 365 and then enumerating valid users, as well as performing password spraying.
O365 Spray - Identify Domain
Validate Users
Password Attacks
Hydra - Password Attack
We can use Hydra to perform brute force attacks or password spraying against email services like SMTP, POP3, or IMAP4. We only need a list of users (-L
) and a password or password list (-p
or -P
), in addition to specifying the service.
O365 Spray - Password Spraying
If cloud services allow the use of SMTP, POP3, or IMAP4 protocols, we could try to perform password spraying attacks using tools like Hydra, but typically these attempts are blocked by the provider's security measures.
Instead, it's preferable to use specialized tools such as:
o365spray
orMailSniper
→ for Microsoft Office 365CredKing
→ for Gmail or Okta
Open Relay
An open relay is a misconfigured SMTP (Simple Mail Transfer Protocol) server that allows email forwarding without authentication. Mail servers that are configured as open relays, either accidentally or intentionally, allow any source to send emails through the server, thus masking the real origin of the messages and making them appear to come from the server itself.
From an attacker's perspective, this can be leveraged to perform phishing, sending emails as if they were from non-existent users or impersonating someone else's identity. For example, if we detect that a company has a misconfigured mail server as an open relay and uses a specific address for notifications, we can send an email with that same address and add a malicious link.
With Nmap's smtp-open-relay
script, we can identify if an SMTP port allows this type of forwarding:
Then, we can use any mail client to connect to the server and send our message:
Last updated