信息收集是渗透测试的第一阶段,也是最关键的阶段之一。充分的信息收集能够帮助渗透测试人员全面了解目标的攻击面,发现潜在的薄弱环节。正如安全圈的一句老话:“知己知彼,百战不殆。“本文将系统地介绍被动信息收集与主动信息收集的方法论,并深入讲解 Nmap、Recon-ng 等核心工具的高级用法,帮助你构建完整的信息侦察能力。
信息收集方法论
信息收集从大的维度上可以分为两类:
被动信息收集(Passive Reconnaissance)
不与目标系统直接交互,通过公开渠道获取情报。目标不会感知到侦察行为,隐蔽性极高。
- WHOIS 查询、DNS 记录
- 搜索引擎(Google Hacking)
- 社交媒体和招聘信息
- 公开代码仓库(GitHub)
- 证书透明度日志(Certificate Transparency)
- 网络空间搜索引擎(Shodan、Censys、FOFA)
主动信息收集(Active Reconnaissance)
直接与目标系统交互,发送探测数据包,获取更详细的技术信息。目标可能会在日志中记录这些行为。
- 端口扫描(Nmap)
- 服务版本探测
- 操作系统识别
- Web 目录枚举
- 漏洞扫描
被动信息收集实战
WHOIS 查询
WHOIS 可以获取域名注册信息,包括注册人、注册商、DNS 服务器、注册日期等。
# Linux 命令行查询
whois example.com
# 只关注关键字段
whois example.com | grep -E "(Registrant|Name Server|Creation|Expiration)"
# 查询 IP 地址归属
whois 93.184.216.34
# 在线查询工具
# https://who.is/
# https://whois.domaintools.com/
DNS 信息收集
DNS 记录中隐藏着大量有价值的信息:A 记录指向服务器 IP、MX 记录揭示邮件服务器、TXT 记录可能包含 SPF 配置等。
# 使用 dig 查询各类记录
dig example.com A # 查询 A 记录(IPv4地址)
dig example.com AAAA # 查询 AAAA 记录(IPv6地址)
dig example.com MX # 查询邮件服务器
dig example.com NS # 查询 DNS 服务器
dig example.com TXT # 查询 TXT 记录
dig example.com ANY # 查询所有记录
# 指定 DNS 服务器查询
dig @8.8.8.8 example.com
# 查询 SOA 记录(域名权威信息)
dig example.com SOA
# 尝试 DNS 区域传送(信息泄露的金矿)
dig axfr @ns1.example.com example.com
# 使用 nslookup
nslookup example.com
nslookup -type=mx example.com
# 使用 host 命令
host -t any example.com
# 反向 DNS 查询
dig -x 93.184.216.34
# DNS 枚举工具
dnsenum example.com
dnsrecon -d example.com -t std
fierce --domain example.com
Google Hacking(Google Dorks)
利用搜索引擎高级语法发现目标的敏感信息:
# 常用 Google Dorks
site:example.com # 限定域名
site:example.com filetype:pdf # 搜索PDF文件
site:example.com inurl:admin # 搜索管理后台
site:example.com intitle:"index of" # 目录浏览
site:example.com ext:sql | ext:bak # 数据库备份文件
site:example.com intext:"password" # 页面包含密码关键词
site:example.com ext:log # 日志文件
# 搜索配置文件
site:example.com ext:conf | ext:cfg | ext:ini
# 搜索 Git 泄露
site:example.com inurl:".git"
# 搜索敏感目录
site:example.com inurl:wp-admin # WordPress后台
site:example.com inurl:phpmyadmin # phpMyAdmin
# Google Hacking Database (GHDB) 收录更多 Dorks
# https://www.exploit-db.com/google-hacking-database
Shodan — 网络空间搜索引擎
Shodan 是物联网设备和网络服务的搜索引擎,可以发现暴露在互联网上的各种设备和服务。
# 安装 Shodan CLI
pip install shodan
# 初始化 API Key
shodan init YOUR_API_KEY
# 搜索目标
shodan host 93.184.216.34
# 搜索特定服务
shodan search "apache country:CN"
shodan search "port:3389 country:CN" # 搜索 RDP 服务
shodan search "product:MySQL country:CN" # 搜索 MySQL
# 统计搜索结果
shodan count "apache"
shodan stats --facets country "nginx"
# 常用 Shodan Dorks
# hostname:example.com 按域名搜索
# org:"Example Corp" 按组织搜索
# net:192.168.1.0/24 按IP段搜索
# port:22 按端口搜索
# os:"Linux" 按操作系统搜索
# vuln:CVE-2021-44228 按漏洞搜索
证书透明度与子域名发现
# 通过 Certificate Transparency 日志查询子域名
# https://crt.sh
curl -s "https://crt.sh/?q=%25.example.com&output=json" | jq '.[].name_value' | sort -u
# 使用 subfinder 进行子域名枚举
subfinder -d example.com -o subdomains.txt
# 使用 amass(功能更强大)
amass enum -d example.com -o amass_results.txt
# 使用 amass 的被动模式
amass enum -passive -d example.com
# 子域名暴力枚举
amass enum -brute -d example.com -w /usr/share/wordlists/amass/subdomains-top1mil-5000.txt
Nmap 扫描技术大全
Nmap(Network Mapper)是最强大的网络扫描工具,掌握其各种扫描技术是渗透测试的基本功。
主机发现
# Ping 扫描(仅发现存活主机)
nmap -sn 192.168.1.0/24
# ARP 扫描(局域网最可靠)
nmap -PR -sn 192.168.1.0/24
# TCP SYN Ping(绕过禁ping)
nmap -PS80,443 -sn 192.168.1.0/24
# ICMP 扫描
nmap -PE -sn 192.168.1.0/24
# 从文件导入目标列表
nmap -sn -iL targets.txt
端口扫描技术
# TCP SYN 扫描(半开扫描,默认,需要root)
sudo nmap -sS 192.168.1.100
# TCP Connect 扫描(完整三次握手)
nmap -sT 192.168.1.100
# UDP 扫描(较慢)
sudo nmap -sU 192.168.1.100
# TCP SYN + UDP 联合扫描
sudo nmap -sS -sU 192.168.1.100
# 指定端口范围
nmap -p 1-1000 192.168.1.100 # 扫描1-1000端口
nmap -p 80,443,8080 192.168.1.100 # 扫描指定端口
nmap -p- 192.168.1.100 # 扫描全部65535端口
nmap --top-ports 100 192.168.1.100 # 扫描最常见的100个端口
# FIN 扫描(绕过部分防火墙)
sudo nmap -sF 192.168.1.100
# Xmas 扫描
sudo nmap -sX 192.168.1.100
# NULL 扫描
sudo nmap -sN 192.168.1.100
# ACK 扫描(检测防火墙规则)
sudo nmap -sA 192.168.1.100
版本探测与操作系统识别
# 服务版本探测
nmap -sV 192.168.1.100
# 设置版本探测强度(0-9,默认7)
nmap -sV --version-intensity 9 192.168.1.100
# 操作系统识别
sudo nmap -O 192.168.1.100
# 综合扫描(版本+脚本+OS+traceroute)
sudo nmap -A 192.168.1.100
# 激进版本探测
nmap -sV --version-all 192.168.1.100
NSE 脚本引擎
Nmap Scripting Engine(NSE)是 Nmap 的灵魂,拥有数百个脚本。
# 使用默认脚本集
nmap -sC 192.168.1.100
# 等同于
nmap --script=default 192.168.1.100
# 运行特定脚本
nmap --script=http-title 192.168.1.100
nmap --script=ssh-brute 192.168.1.100
nmap --script=smb-vuln* 192.168.1.100
# 漏洞扫描脚本
nmap --script vuln 192.168.1.100
# 多个脚本组合
nmap --script "http-* and not http-brute" 192.168.1.100
# 查看脚本帮助
nmap --script-help=http-enum
# 列出所有可用脚本
ls /usr/share/nmap/scripts/
nmap --script-help all 2>&1 | grep "Categories"
# 按类别运行脚本
nmap --script auth 192.168.1.100 # 认证相关
nmap --script broadcast 192.168.1.0/24 # 广播发现
nmap --script discovery 192.168.1.100 # 服务发现
扫描速度与隐蔽性
# 时间模板(T0最慢最隐蔽,T5最快最暴力)
nmap -T0 192.168.1.100 # 偏执模式(极慢,IDS规避)
nmap -T1 192.168.1.100 # 鬼祟模式
nmap -T2 192.168.1.100 # 礼貌模式
nmap -T3 192.168.1.100 # 正常模式(默认)
nmap -T4 192.168.1.100 # 激进模式(推荐)
nmap -T5 192.168.1.100 # 疯狂模式
# 自定义速率
nmap --min-rate 1000 192.168.1.100
nmap --max-rate 500 192.168.1.100
# 分片绕过(逃避 IDS)
sudo nmap -f 192.168.1.100
# 诱饵扫描(混淆源IP)
sudo nmap -D RND:10 192.168.1.100
# 指定源端口(绕过防火墙规则)
sudo nmap --source-port 53 192.168.1.100
输出格式
# 标准输出到文件
nmap -oN scan_result.txt 192.168.1.100
# XML 格式(便于后续处理)
nmap -oX scan_result.xml 192.168.1.100
# Grep 友好格式
nmap -oG scan_result.grep 192.168.1.100
# 同时输出所有格式
nmap -oA scan_result 192.168.1.100
# 综合实战命令
sudo nmap -sS -sV -sC -O -p- -T4 --min-rate 5000 -oA full_scan 192.168.1.100
Recon-ng 框架使用
Recon-ng 是一个功能强大的 OSINT 侦察框架,采用模块化设计,类似 Metasploit 的操作风格。
# 启动 Recon-ng
recon-ng
# 创建工作空间
recon-ng
[recon-ng][default] > workspaces create pentest_project
# 安装模块
[recon-ng][pentest_project] > marketplace search
[recon-ng][pentest_project] > marketplace install recon/domains-hosts/hackertarget
[recon-ng][pentest_project] > marketplace install recon/hosts-hosts/resolve
# 添加目标域名
[recon-ng][pentest_project] > db insert domains
domain (TEXT): example.com
# 加载和运行模块
[recon-ng][pentest_project] > modules load recon/domains-hosts/hackertarget
[recon-ng][pentest_project][hackertarget] > run
# 查看收集到的数据
[recon-ng][pentest_project] > show hosts
[recon-ng][pentest_project] > show contacts
[recon-ng][pentest_project] > show credentials
# 设置 API Keys(部分模块需要)
[recon-ng][pentest_project] > keys add shodan_api YOUR_SHODAN_API_KEY
[recon-ng][pentest_project] > keys add virustotal_api YOUR_VT_API_KEY
# 导出报告
[recon-ng][pentest_project] > modules load reporting/html
[recon-ng][pentest_project][html] > options set FILENAME /tmp/recon_report.html
[recon-ng][pentest_project][html] > run
Web 目录与路径扫描
dirsearch
# 基本目录扫描
dirsearch -u http://target.com
# 指定字典和扩展名
dirsearch -u http://target.com -w /usr/share/wordlists/dirb/common.txt -e php,asp,html
# 递归扫描
dirsearch -u http://target.com -r -R 3
# 自定义线程和延迟
dirsearch -u http://target.com -t 50 --delay 0.5
# 排除特定状态码
dirsearch -u http://target.com --exclude-status 403,404,500
gobuster
# 目录扫描模式
gobuster dir -u http://target.com -w /usr/share/wordlists/dirb/common.txt
# 指定扩展名
gobuster dir -u http://target.com -w /usr/share/wordlists/dirb/common.txt -x php,txt,html,bak
# 子域名枚举模式
gobuster dns -d example.com -w /usr/share/wordlists/amass/subdomains-top1mil-5000.txt
# VHOST 枚举
gobuster vhost -u http://target.com -w /usr/share/wordlists/dirb/common.txt
# 自定义线程和状态码过滤
gobuster dir -u http://target.com -w wordlist.txt -t 50 -s "200,301,302"
# 使用 Cookie 认证
gobuster dir -u http://target.com -w wordlist.txt -c "PHPSESSID=abc123"
ffuf — 高速模糊测试工具
# 目录模糊测试
ffuf -u http://target.com/FUZZ -w /usr/share/wordlists/dirb/common.txt
# 参数模糊测试
ffuf -u "http://target.com/page?FUZZ=test" -w /usr/share/wordlists/dirb/common.txt
# 过滤响应(按大小、状态码、字数)
ffuf -u http://target.com/FUZZ -w wordlist.txt -fc 404 -fs 4242
# POST 参数模糊测试
ffuf -u http://target.com/login -X POST -d "user=admin&pass=FUZZ" -w passwords.txt -fc 401
# 子域名模糊测试
ffuf -u http://FUZZ.target.com -w subdomains.txt -fc 404
安全建议与防御措施
作为防御方,了解攻击者的信息收集手段有助于更好地保护资产:
- 最小信息暴露:WHOIS 开启隐私保护,减少公开信息的泄露
- DNS 安全加固:禁用区域传送,使用 DNSSEC 防止 DNS 欺骗
- 搜索引擎管理:合理使用 robots.txt,清理搜索引擎中的敏感缓存
- 端口管理:关闭不必要的端口和服务,使用防火墙规则限制访问
- IDS/IPS 部署:检测和阻断端口扫描等侦察行为
- 蜜罐部署:使用蜜罐技术诱捕和识别攻击者
- 代码仓库审查:定期检查 GitHub 等平台是否有敏感信息泄露
- Rate Limiting:对 Web 接口实施访问频率限制
总结
信息收集是渗透测试中投入时间最多、也最能体现功力的阶段。本文覆盖了从被动侦察(WHOIS、DNS、Google Hacking、Shodan)到主动扫描(Nmap 全系列扫描技术)的完整流程,以及 Recon-ng、subfinder、dirsearch、gobuster 等专业工具的使用方法。掌握这些技术后,你将能够系统地对目标进行全方位的信息侦察,发现潜在的攻击面。记住,信息收集的深度直接决定了后续渗透测试的成功率——“磨刀不误砍柴工”。在下一篇文章中,我们将基于收集到的信息,进入实际的服务漏洞利用阶段。