SMTP

Pentesting SMTP - TCP Ports 25,465,587

Theory

SMTP (Simple Mail Transfer Protocol) is a TCP/IP protocol used for sending e-mail. Default ports are 25 (SMTP), 465 (SMTPS), 587 (SMTPS)

Practice

Enumerate

Using nmap, we can enumerate SMTP servers

nmap --script smtp-* -p 25,465,587 <target-ip>

Commands

We may attempts to use EHLO and HELP commands to gather the Extended commands supported by an SMTP server.

Commands are not case sensitive.

We may list all supported enhanced functions of a SMTP server as follow

root@kali$ telnet example.com 587
220 example.com SMTP Server Banner 
>> HELO 
250 example.com Hello [x.x.x.x] 
>> EHLO all #or EHLO domain.com

Usernames

SMTP supports several interesting commands, such as VRFY, EXPN and RCPT TO.

  • VRFY requests asks the server to verify an email address.

  • EXPN asks the server for the membership of a mailing list.

  • RCPT TO is used to specify an email recipient but may trigger an "Unknown user" error if the specified user does not exist.

These can often be abused to verify existing users on a mail server, which is useful information during a penetration test.

smtp-user-enum is a python script for user enumeration via VRFY, EXPN and RCPT

# VRFY - check if the user exists in the SMTP server
smtp-user-enum -M VRFY -u <username> -t <target-ip>
smtp-user-enum -M VRFY -U usernames.txt -t <target-ip>

# RCPT - check if the user is allowed to receive mails in the SMTP server
smtp-user-enum -M RCPT -u <username> -t <target-ip>
smtp-user-enum -M RCPT -U usernames.txt -t <target-ip>

# EXPN - reveal the actual email address
smtp-user-enum -M EXPN -u <username> -t <target-ip>
smtp-user-enum -M EXPN -D <hostname> -U usernames.txt -t <target-ip>

NTLM Auth - Information disclosure

If the server supports NTLM auth (Windows) you can obtain sensitive info (versions). More information here.

We may leak sensitive information as follow

root@kali$ telnet example.com 587 
220 example.com SMTP Server Banner 
>> HELO 
250 example.com Hello [x.x.x.x] 
>> AUTH NTLM 334 
NTLM supported 
>> TlRMTVNTUAABAAAAB4IIAAAAAAAAAAAAAAAAAAAAAAA= 
334 TlRMTVNTUAACAAAACgAKADgAAAAFgooCBqqVKFrKPCMAAAAAAAAAAEgASABCAAAABgOAJQAAAA9JAEkAUwAwADEAAgAKAEkASQBTADAAMQABAAoASQBJAFMAMAAxAAQACgBJAEkAUwAwADEAAwAKAEkASQBTADAAMQAHAAgAHwMI0VPy1QEAAAAA

Connect

We may use following command to connect to a SMTP

# Netcat
nc <target-ip> 25

# Telnet
telnet <target-ip> 25

# From Windows
dism /online /Enable-Feature /FeatureName:TelnetClient
telnet <target-ip> 25

Authentication Bruteforce

We may use hydra to bruteforce SMTP accounts on the server

# Port 25
hydra -l <username> -P /path/to/passwords.txt <IP> smtp -V

# Port 587 for SMTP with SSL
hydra -l <username> -P /path/to/passwords.txt -s 587 <IP> -S -v -V

Send E-mail

swaks is a swiss army knife for SMTP and can be used to send emails from external domain

# Basic usage
swaks --to remote-user@example.com --from local-user@<local-ip> --server mail.example.com --header "Subject: test" --body "hello"

# Mass email
swaks --to $(cat emails | tr '\n' ',' | less) --from local-user@<local-ip> --server mail.example.com --header "Subject: test" --body "hello"

Mail Spoofing

Open Relay

To prevent the sent emails from being filtered by spam filters and not reaching the recipient, the sender can use a relay server that the recipient trusts. Often, administrators haven't overviewed of which IP ranges they have to allow. This results in a misconfiguration of the SMTP server that we will still often find in external and internal penetration tests. Therefore, they allow all IP addresses not to cause errors in the email traffic and thus not to disturb or unintentionally interrupt the communication with potential and current customers:

mynetworks = 0.0.0.0/0

We may use the smtp-open-relay script to enumerate if a SMTP server is vulnerable to mail relaying.

nmap -p25 --script smtp-open-relay <IP> -v

Tools

MagicSpoofing is a python script that checks & test SPF/DMARC DNS records an tries to spoof a domain with a open relay mail system.

# This will send a test email from test@victim.com to destination@gmail.com
python3 magicspoofmail.py -d victim.com -t -e destination@gmail.com

# But you can also modify more options of the email
python3 magicspoofmail.py -d victim.com -t -e destination@gmail.com --subject TEST --sender administrator@victim.com

Resources

Last updated