SMTP
Last updated
Last updated
Source CTF and HTB Academy
Usually on port 25, 465, 587
The Simple Mail Transfer Protocol (SMTP) is a protocol for sending emails in an IP network. It can be used between an email client and an outgoing mail server or between two SMTP servers. SMTP is often combined with the IMAP or POP3 protocols, which can fetch emails and send emails. In principle, it is a client-server-based protocol, although SMTP can be used between a client and a server and between two SMTP servers. In this case, a server effectively acts as a client. An essential function of an SMTP server is preventing spam using authentication mechanisms that allow only authorized users to send e-mails. For this purpose, most modern SMTP servers support the protocol extension ESMTP with SMTP-Auth. After sending his e-mail, the SMTP client, also known as Mail User Agent (MUA), converts it into a header and a body and uploads both to the SMTP server. This has a so-called Mail Transfer Agent (MTA), the software basis for sending and receiving e-mails. The MTA checks the e-mail for size and spam and then stores it. To relieve the MTA, it is occasionally preceded by a 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 can be carried out on many SMTP servers due to incorrect configuration.
ESMTP extended smtp: will return delivery confirmation and uses TLS
When people talk about SMTP in general, they usually mean ESMTP
cat /etc/postfix/main.cf | grep -v "#" | sed -r "/^\s*$/d"
Command | Description |
---|---|
| AUTH is a service extension used to authenticate the client. |
| The client logs in with its computer name and thus starts the session. |
| The client names the email sender. |
| The client names the email recipient. |
| The client initiates the transmission of the email. |
| The client aborts the initiated transmission but keeps the connection between client and server. |
| The client checks if a mailbox is available for message transfer. |
| The client also checks if a mailbox is available for messaging with this command. |
| The client requests a response from the server to prevent disconnection due to time-out. |
| The client terminates the session. |
Open relay configuration mynetworks = 0.0.0.0/0
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.
host -t MX hackthebox.eu
dig mx plaintext.do | grep "MX" | grep -v ";"
host -t A mail1.inlanefreight.htb
sudo nmap 10.129.14.128 -sC -sV -p25
sudo nmap 10.129.14.128 -p25 --script smtp-open-relay -v
sudo nmap -Pn -sV -sC -p25,143,110,465,587,993,995 10.129.14.128
Example
openssl s_client -starttls smtp -connect mail.target.com:587
hydra -L users.txt TARGET-IP smtp-enum
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.
smtp-user-enum -M RCPT -U userlist.txt -D inlanefreight.htb -t 10.129.203.7
Cloud service providers use their own implementation for email services. Those services commonly have custom features that we can abuse for operation, such as username enumeration.
hydra -L users.txt -p 'Company01!' -f 10.10.110.20 pop3
python3 o365spray.py --spray -U usersfound.txt -p 'March2022!' --count 1 --lockout 1 --domain msp
See above for more info on this misconfiguration
nmap -p25 -Pn --script smtp-open-relay 10.10.11.213
Next, we can use any mail client to connect to the mail server and send our email.
swaks --from notifications@inlanefreight.com --to employees@inlanefreight.com --header 'Subject: Company Notification' --body 'Hi All, we want to hear from you! Please complete the following survey. http://mycustomphishinglink.com/' --server 10.10.11.213