mcpField
S
SMTP Penetration Testing

SMTP Penetration Testing

github.com/davila7安全33.0k185 次安装1 天前更新
代码仓库

关于 SMTP Penetration Testing

SMTP Penetration Testing 是面向安全人员的 SMTP 服务授权评估技能,提供服务发现、用户枚举、中继与认证检查、TLS 和 DNS 记录分析的命令行流程及修复建议。

主要功能

  1. 01发现 SMTP 服务并检查横幅
  2. 02枚举用户并测试中继行为
  3. 03评估认证和 TLS 配置
  4. 04检查 SPF、DKIM 和 DMARC 记录
  5. 05提供修复建议

适用场景

  1. 01评估获授权的邮件服务器
  2. 02检查开放中继和用户枚举风险
  3. 03审查 SMTP 加密与邮件认证设置

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/davila7/claude-code-templates --skill smtp-penetration-testing

安装后在 Claude Code 中输入/smtp-penetration-testing确认已加载。

详细介绍

SMTP 渗透测试

目的

对 SMTP(简单邮件传输协议)服务器进行全面的安全评估,以识别包括开放中继、用户枚举、弱身份验证和配置错误在内的漏洞。本技能涵盖 Banner 获取、用户枚举技术、中继测试、暴力破解攻击和安全加固建议。

前提条件

必需工具
# Nmap with SMTP scripts
sudo apt-get install nmap

# Netcat
sudo apt-get install netcat

# Hydra for brute force
sudo apt-get install hydra

# SMTP user enumeration tool
sudo apt-get install smtp-user-enum

# Metasploit Framework
msfconsole
必备知识
  • SMTP 协议基础
  • 电子邮件架构(MTA、MDA、MUA)
  • DNS 和 MX 记录
  • 网络协议
所需访问权限
  • 目标 SMTP 服务器 IP/主机名
  • 测试的书面授权
  • 用于枚举和暴力破解的字典

输出与交付成果

  1. SMTP 安全评估报告 - 全面的漏洞发现结果
  2. 用户枚举结果 - 发现的有效电子邮件地址
  3. 中继测试结果 - 开放中继状态和利用潜力
  4. 修复建议 - 安全加固指南

核心工作流

阶段 1:了解 SMTP 架构
Components: MTA (transfer) → MDA (delivery) → MUA (client)

Ports: 25 (SMTP), 465 (SMTPS), 587 (submission), 2525 (alternative)

Workflow: Sender MUA → Sender MTA → DNS/MX → Recipient MTA → MDA → Recipient MUA
阶段 2:发现 SMTP 服务

识别 SMTP 服务器及其版本:

# Discover SMTP ports
nmap -p 25,465,587,2525 -sV TARGET_IP

# Aggressive service detection
nmap -sV -sC -p 25 TARGET_IP

# SMTP-specific scripts
nmap --script=smtp-* -p 25 TARGET_IP

# Discover MX records for domain
dig MX target.com
nslookup -type=mx target.com
host -t mx target.com
阶段 3:获取 Banner

获取 SMTP 服务器信息:

# Using Telnet
telnet TARGET_IP 25
# Response: 220 mail.target.com ESMTP Postfix

# Using Netcat
nc TARGET_IP 25
# Response: 220 mail.target.com ESMTP

# Using Nmap
nmap -sV -p 25 TARGET_IP
# Version detection extracts banner info

# Manual SMTP commands
EHLO test
# Response reveals supported extensions

解析 Banner 信息:

Banner reveals:
- Server software (Postfix, Sendmail, Exchange)
- Version information
- Hostname
- Supported SMTP extensions (STARTTLS, AUTH, etc.)
阶段 4:枚举 SMTP 命令

测试可用的 SMTP 命令:

# Connect and test commands
nc TARGET_IP 25

# Initial greeting
EHLO attacker.com

# Response shows capabilities:
250-mail.target.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-AUTH PLAIN LOGIN
250-8BITMIME
250 DSN

需要测试的关键命令:

# VRFY - Verify user exists
VRFY admin
250 2.1.5 [email protected]

# EXPN - Expand mailing list
EXPN staff
250 2.1.5 [email protected]
250 2.1.5 [email protected]

# RCPT TO - Recipient verification
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
# 250 OK = user exists
# 550 = user doesn't exist
阶段 5:用户枚举

枚举有效的电子邮件地址:

# Using smtp-user-enum with VRFY
smtp-user-enum -M VRFY -U /usr/share/wordlists/users.txt -t TARGET_IP

# Using EXPN method
smtp-user-enum -M EXPN -U /usr/share/wordlists/users.txt -t TARGET_IP

# Using RCPT method
smtp-user-enum -M RCPT -U /usr/share/wordlists/users.txt -t TARGET_IP

# Specify port and domain
smtp-user-enum -M VRFY -U users.txt -t TARGET_IP -p 25 -d target.com

使用 Metasploit:

use auxiliary/scanner/smtp/smtp_enum
set RHOSTS TARGET_IP
set USER_FILE /usr/share/wordlists/metasploit/unix_users.txt
set UNIXONLY true
run

使用 Nmap:

# SMTP user enumeration script
nmap --script smtp-enum-users -p 25 TARGET_IP

# With custom user list
nmap --script smtp-enum-users --script-args smtp-enum-users.methods={VRFY,EXPN,RCPT} -p 25 TARGET_IP
阶段 6:开放中继测试

测试是否存在未经授权的电子邮件中继:

# Using Nmap
nmap -p 25 --script smtp-open-relay TARGET_IP

# Manual testing via Telnet
telnet TARGET_IP 25
HELO attacker.com
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
DATA
Subject: Relay Test
This is a test.
.
QUIT

# If accepted (250 OK), server is open relay

使用 Metasploit:

use auxiliary/scanner/smtp/smtp_relay
set RHOSTS TARGET_IP
run

测试不同变体:

# Test different sender/recipient combinations
MAIL FROM:<>
MAIL FROM:<test@[attacker_IP]>
MAIL FROM:<[email protected]>

RCPT TO:<[email protected]>
RCPT TO:<"[email protected]">
RCPT TO:<test%[email protected]>
阶段 7:身份验证暴力破解

测试 SMTP 凭据是否薄弱:

# Using Hydra
hydra -l admin -P /usr/share/wordlists/rockyou.txt smtp://TARGET_IP

# With specific port and SSL
hydra -l admin -P passwords.txt -s 465 -S TARGET_IP smtp

# Multiple users
hydra -L users.txt -P passwords.txt TARGET_IP smtp

# Verbose output
hydra -l admin -P passwords.txt smtp://TARGET_IP -V

使用 Medusa:

medusa -h TARGET_IP -u admin -P /path/to/passwords.txt -M smtp

使用 Metasploit:

use auxiliary/scanner/smtp/smtp_login
set RHOSTS TARGET_IP
set USER_FILE /path/to/users.txt
set PASS_FILE /path/to/passwords.txt
set VERBOSE true
run
阶段 8:SMTP 命令注入

测试是否存在命令注入漏洞:

# Header injection test
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
DATA
Subject: Test
Bcc: [email protected]
X-Injected: malicious-header

Injected content
.

电子邮件欺骗测试:

# Spoofed sender (tests SPF/DKIM protection)
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
DATA
From: CEO <[email protected]>
Subject: Urgent Request
Please process this request immediately.
.
阶段 9:TLS/SSL 安全测试

测试加密配置:

# STARTTLS support check
openssl s_client -connect TARGET_IP:25 -starttls smtp

# Direct SSL (port 465)
openssl s_client -connect TARGET_IP:465

# Cipher enumeration
nmap --script ssl-enum-ciphers -p 25 TARGET_IP
阶段 10:SPF、DKIM、DMARC 分析

检查电子邮件身份验证记录:

# SPF/DKIM/DMARC record lookups
dig TXT target.com | grep spf            # SPF
dig TXT selector._domainkey.target.com    # DKIM
dig TXT _dmarc.target.com                 # DMARC

# SPF policy: -all = strict fail, ~all = soft fail, ?all = neutral

快速参考

SMTP 基本命令
命令用途示例
HELO标识客户端HELO client.com
EHLO扩展版 HELOEHLO client.com
MAIL FROM设置发件人MAIL FROM:<[email protected]>
RCPT TO设置收件人RCPT TO:<[email protected]>
DATA开始输入邮件正文DATA
VRFY验证用户VRFY admin
EXPN展开别名EXPN staff
QUIT结束会话QUIT
SMTP 响应代码
代码含义
220服务就绪
221正在关闭连接
250成功 / 已完成请求的操作
354开始输入邮件
421服务不可用
450邮箱不可用
550用户未知 / 未找到邮箱
553不允许使用该邮箱名称
枚举工具命令
工具命令
smtp-user-enumsmtp-user-enum -M VRFY -U users.txt -t IP
Nmapnmap --script smtp-enum-users -p 25 IP
Metasploituse auxiliary/scanner/smtp/smtp_enum
Netcatnc IP 25,然后手动输入命令
常见漏洞
漏洞风险测试方法
开放中继高使用外部收件人进行中继测试
用户枚举中使用 VRFY/EXPN/RCPT 命令
Banner 泄露低获取 Banner
弱身份验证高暴力破解攻击
未启用 TLS中STARTTLS 测试
缺少 SPF/DKIM中查询 DNS 记录

约束与限制

法律要求
  • 仅测试你拥有或获准测试的 SMTP 服务器
  • 发送垃圾邮件或恶意电子邮件属于违法行为
  • 记录所有测试活动
  • 不得滥用发现的开放中继
技术限制
  • VRFY/EXPN 在现代服务器上通常处于禁用状态
  • 速率限制可能会减慢枚举速度
  • 某些服务器对有效用户和无效用户的响应完全相同
  • 灰名单机制可能会延迟枚举响应
道德界限
  • 切勿通过发现的中继实际发送垃圾邮件
  • 不要出于恶意用途收集电子邮件地址
  • 向服务器管理员报告开放中继
  • 仅将发现用于经授权的安全改进

示例

示例 1:完整 SMTP 评估

场景: 对邮件服务器进行全面安全评估

# Step 1: Service discovery
nmap -sV -sC -p 25,465,587 mail.target.com

# Step 2: Banner grab
nc mail.target.com 25
EHLO test.com
QUIT

# Step 3: User enumeration
smtp-user-enum -M VRFY -U /usr/share/seclists/Usernames/top-usernames-shortlist.txt -t mail.target.com

# Step 4: Open relay test
nmap -p 25 --script smtp-open-relay mail.target.com

# Step 5: Authentication test
hydra -l admin -P /usr/share/wordlists/fasttrack.txt smtp://mail.target.com

# Step 6: TLS check
openssl s_client -connect mail.target.com:25 -starttls smtp

# Step 7: Check email authentication
dig TXT target.com | grep spf
dig TXT _dmarc.target.com
示例 2:用户枚举攻击

场景: 枚举有效用户,为钓鱼攻击做准备

# Method 1: VRFY
smtp-user-enum -M VRFY -U users.txt -t 192.168.1.100 -p 25

# Method 2: RCPT with timing analysis
smtp-user-enum -M RCPT -U users.txt -t 192.168.1.100 -p 25 -d target.com

# Method 3: Metasploit
msfconsole
use auxiliary/scanner/smtp/smtp_enum
set RHOSTS 192.168.1.100
set USER_FILE /usr/share/metasploit-framework/data/wordlists/unix_users.txt
run

# Results show valid users
[+] 192.168.1.100:25 - Found user: admin
[+] 192.168.1.100:25 - Found user: root
[+] 192.168.1.100:25 - Found user: postmaster
示例 3:开放中继利用

场景: 测试并记录开放中继漏洞

# Test via Telnet
telnet mail.target.com 25
HELO attacker.com
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
# If 250 OK - VULNERABLE

# Document with Nmap
nmap -p 25 --script smtp-open-relay --script-args [email protected],[email protected] mail.target.com

# Output:
# PORT   STATE SERVICE
# 25/tcp open  smtp
# |_smtp-open-relay: Server is an open relay (14/16 tests)

故障排除

问题原因解决方案
连接被拒绝端口被阻止或已关闭使用 nmap 检查端口;ISP 可能会阻止端口 25;尝试 587/465;使用 VPN
VRFY/EXPN 已禁用服务器已加固使用 RCPT TO 方法;分析响应时间/代码的差异
暴力破解被阻止速率限制/账户锁定减慢速度(hydra -W 5);使用密码喷洒;检查是否启用了 fail2ban
SSL/TLS 错误端口或协议错误SSL 使用 465,STARTTLS 使用 25/587;验证 EHLO 响应

安全建议

面向管理员
  1. 禁用开放中继 - 要求通过身份验证才能向外部投递邮件
  2. 禁用 VRFY/EXPN - 防止用户枚举
  3. 强制使用 TLS - 要求所有连接使用 STARTTLS
  4. 实施 SPF/DKIM/DMARC - 防止电子邮件欺骗
  5. 速率限制 - 防止暴力破解攻击
  6. 账户锁定 - 多次尝试失败后锁定账户
  7. 强化 Banner - 尽量减少服务器信息泄露
  8. 日志监控 - 对可疑活动发出警报
  9. 补丁管理 - 保持 SMTP 软件为最新版本
  10. 访问控制 - 将 SMTP 限制为仅允许授权 IP 访问

评论

支持 Markdown