12. Enumeration - SMTP (25,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.

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 to switch the existing plaintext connection to an encrypted connection.

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. We will discuss this attack and how to identify the weak point for it a little later. The MTA then searches the DNS for the IP address of the recipient mail server.

On arrival at the destination SMTP server, the data packets are reassembled to form a complete e-mail. From there, the Mail delivery agent (MDA) transfers it to the recipient's mailbox.

Client (MUA) ➞ Submission Agent (MSA) ➞ Open Relay (MTA) ➞ Mail Delivery Agent (MDA) ➞ Mailbox (POP3/IMAP)
Command Description
telnet <FQDN/IP> 25
IMAP/POP3
Command Description
curl -k 'imaps://<FQDN/IP>' --user <user>:<password> Log in to the IMAPS service using cURL.
openssl s_client -connect <FQDN/IP>:imaps Connect to the IMAPS service.
openssl s_client -connect <FQDN/IP>:pop3s Connect to the POP3s service.
But SMTP has two disadvantages inherent to the network protocol.
  1. The first is that sending an email using SMTP does not return a usable delivery confirmation. Although the specifications of the protocol provide for this type of notification, its formatting is not specified by default, so that usually only an English-language error message, including the header of the undelivered message, is returned.

  2. Users are not authenticated when a connection is established, and the sender of an email is therefore unreliable. As a result, open SMTP relays are often misused to send spam en masse. The originators use arbitrary fake sender addresses for this purpose to not be traced (mail spoofing). Today, many different security techniques are used to prevent the misuse of SMTP servers. For example, suspicious emails are rejected or moved to quarantine (spam folder). For example, responsible for this are the identification protocol DomainKeys (DKIM), the Sender Policy Framework (SPF).

For this purpose, an extension for SMTP has been developed called Extended SMTP (ESMTP). When people talk about SMTP in general, they usually mean ESMTP. ESMTP uses TLS, which is done after the EHLO command by sending STARTTLS. The sending and communication are also done by special commands that cause the SMTP server to do what the user requires.

Command Description
AUTH PLAIN AUTH is a service extension used to authenticate the client.
HELO The client logs in with its computer name and thus starts the session.
MAIL FROM The client names the email sender.
RCPT TO The client names the email recipient.
DATA The client initiates the transmission of the email.
RSET The client aborts the initiated transmission but keeps the connection between client and server.
VRFY The client checks if a mailbox is available for message transfer.
EXPN The client also checks if a mailbox is available for messaging with this command.
NOOP The client requests a response from the server to prevent disconnection due to time-out.
QUIT The client terminates the session.
nmap usage
stejwes@htb[/htb]$ sudo nmap 10.129.14.128 -sC -sV -p25

OPEN RELAY:

stejwes@htb[/htb]$ sudo nmap 10.129.14.128 -p25 --script smtp-open-relay -v

bruteforce usernames: https://www.kali.org/tools/smtp-user-enum/

alternative script made with chatgpt (which actually worked better than the above)

import telnetlib
import time
import argparse

def smtp_vrfy(host, port, wordlist, delay=1):
    def connect_to_smtp():
        tn = telnetlib.Telnet(host, port)
        tn.read_until(b"220 ")
        tn.write(b"EHLO example.com\r\n")
        tn.read_until(b"250 ")
        return tn

    try:
        tn = connect_to_smtp()

        with open(wordlist, 'r') as file:
            for line in file:
                user = line.strip()
                command = f"VRFY {user}\r\n"
                try:
                    tn.write(command.encode('ascii'))
                    response = tn.read_until(b"\r\n")
                    print(response.decode('ascii').strip())
                    time.sleep(delay)  # Delay between requests
                except (ConnectionResetError, EOFError):
                    print("Connection reset by peer. Reconnecting...")
                    tn.close()
                    tn = connect_to_smtp()
                    tn.write(command.encode('ascii'))
                    response = tn.read_until(b"\r\n")
                    print(response.decode('ascii').strip())
                    time.sleep(delay)  # Delay between requests

        tn.write(b"QUIT\r\n")
        tn.close()
    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Send VRFY requests to an SMTP server using a wordlist.')
    parser.add_argument('--host', required=True, help='SMTP server address')
    parser.add_argument('--port', type=int, required=True, help='SMTP server port')
    parser.add_argument('--wordlist', required=True, help='Path to the wordlist file')
    parser.add_argument('--delay', type=float, default=1, help='Delay between requests in seconds (default: 1 second)')

    args = parser.parse_args()

    smtp_vrfy(args.host, args.port, args.wordlist, args.delay)