关于 Shodan Reconnaissance and Pentesting
Shodan Reconnaissance and Pentesting 是面向安全从业者的 Shodan 方法指南,用于开展经授权的侦察。内容涵盖网页和 CLI 搜索、API 使用、按需扫描、监控及导出结果以供分析。
主要功能
- 01Shodan CLI 与 API 工作流程
- 02主机和筛选服务搜索
- 03按需扫描与状态查询
- 04网络监控设置
- 05导出并解析 JSON 或 CSV 数据
适用场景
- 01盘点主机及暴露服务
- 02查找与已知 CVE 相关的服务
- 03评估经授权的网络范围
- 04跟踪受监控资产的变化
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ davila7/ claude-code-templates --skill shodan-reconnaissance-and-pentesting安装后在 Claude Code 中输入/shodan-reconnaissance-and-pentesting确认已加载。
详细介绍
Shodan 侦察与渗透测试
用途
介绍在渗透测试过程中如何系统地利用 Shodan 进行侦察。本技能涵盖 Shodan 网页界面、命令行界面(CLI)、REST API、搜索筛选器、按需扫描和网络监控功能,可用于发现暴露的服务、存在漏洞的系统和 IoT 设备。
输入 / 先决条件
- Shodan 账户:shodan.io 上的免费或付费账户
- API 密钥:从 Shodan 账户控制面板获取
- 目标信息:要调查的 IP 地址、域名或网络范围
- Shodan CLI:已安装基于 Python 的命令行工具
- 授权:获得对目标网络进行侦察的书面许可
输出 / 交付物
- 资产清单:发现的主机、端口和服务列表
- 漏洞报告:已识别的 CVE 和暴露的易受攻击服务
- Banner 数据:揭示软件版本的服务 Banner
- 网络映射:资产的地理和组织分布
- 截图集:暴露接口的可视化侦察结果
- 导出数据:供进一步分析的 JSON/CSV 文件
核心工作流
1. 安装与配置
安装 Shodan CLI
# Using pip
pip install shodan
# Or easy_install
easy_install shodan
# On BlackArch/Arch Linux
sudo pacman -S python-shodan
初始化 API 密钥
# Set your API key
shodan init YOUR_API_KEY
# Verify setup
shodan info
# Output: Query credits available: 100
# Scan credits available: 100
检查账户状态
# View credits and plan info
shodan info
# Check your external IP
shodan myip
# Check CLI version
shodan version
2. 基本主机侦察
查询单个主机
# Get all information about an IP
shodan host 1.1.1.1
# Example output:
# 1.1.1.1
# Hostnames: one.one.one.one
# Country: Australia
# Organization: Mountain View Communications
# Number of open ports: 3
# Ports:
# 53/udp
# 80/tcp
# 443/tcp
检查主机是否为蜜罐
# Get honeypot probability score
shodan honeyscore 192.168.1.100
# Output: Not a honeypot
# Score: 0.3
3. 搜索查询
基本搜索(免费)
# Simple keyword search (no credits consumed)
shodan search apache
# Specify output fields
shodan search --fields ip_str,port,os smb
筛选搜索(1 个点数)
# Product-specific search
shodan search product:mongodb
# Search with multiple filters
shodan search product:nginx country:US city:"New York"
统计结果数量
# Get result count without consuming credits
shodan count openssh
# Output: 23128
shodan count openssh 7
# Output: 219
下载结果
# Download 1000 results (default)
shodan download results.json.gz "apache country:US"
# Download specific number of results
shodan download --limit 5000 results.json.gz "nginx"
# Download all available results
shodan download --limit -1 all_results.json.gz "query"
解析下载的数据
# Extract specific fields from downloaded data
shodan parse --fields ip_str,port,hostnames results.json.gz
# Filter by specific criteria
shodan parse --fields location.country_code3,ip_str -f port:22 results.json.gz
# Export to CSV format
shodan parse --fields ip_str,port,org --separator , results.json.gz > results.csv
4. 搜索筛选器参考
网络筛选器
ip:1.2.3.4 # Specific IP address
net:192.168.0.0/24 # Network range (CIDR)
hostname:example.com # Hostname contains
port:22 # Specific port
asn:AS15169 # Autonomous System Number
地理筛选器
country:US # Two-letter country code
country:"United States" # Full country name
city:"San Francisco" # City name
state:CA # State/region
postal:94102 # Postal/ZIP code
geo:37.7,-122.4 # Lat/long coordinates
组织筛选器
org:"Google" # Organization name
isp:"Comcast" # ISP name
服务/产品筛选器
product:nginx # Software product
version:1.14.0 # Software version
os:"Windows Server 2019" # Operating system
http.title:"Dashboard" # HTTP page title
http.html:"login" # HTML content
http.status:200 # HTTP status code
ssl.cert.subject.cn:*.example.com # SSL certificate
ssl:true # Has SSL enabled
漏洞筛选器
vuln:CVE-2019-0708 # Specific CVE
has_vuln:true # Has any vulnerability
截图筛选器
has_screenshot:true # Has screenshot available
screenshot.label:webcam # Screenshot type
5. 按需扫描
提交扫描
# Scan single IP (1 credit per IP)
shodan scan submit 192.168.1.100
# Scan with verbose output (shows scan ID)
shodan scan submit --verbose 192.168.1.100
# Scan and save results
shodan scan submit --filename scan_results.json.gz 192.168.1.100
监控扫描状态
# List recent scans
shodan scan list
# Check specific scan status
shodan scan status SCAN_ID
# Download scan results later
shodan download --limit -1 results.json.gz scan:SCAN_ID
可用的扫描协议
# List available protocols/modules
shodan scan protocols
6. 统计与分析
获取搜索统计信息
# Default statistics (top 10 countries, orgs)
shodan stats nginx
# Custom facets
shodan stats --facets domain,port,asn --limit 5 nginx
# Save to CSV
shodan stats --facets country,org -O stats.csv apache
7. 网络监控
设置警报(网页界面)
1. Navigate to Monitor Dashboard
2. Add IP, range, or domain to monitor
3. Configure notification service (email, Slack, webhook)
4. Select trigger events (new service, vulnerability, etc.)
5. View dashboard for exposed services
8. REST API 用法
直接调用 API
# Get API info
curl -s "https://api.shodan.io/api-info?key=YOUR_KEY" | jq
# Host lookup
curl -s "https://api.shodan.io/shodan/host/1.1.1.1?key=YOUR_KEY" | jq
# Search query
curl -s "https://api.shodan.io/shodan/host/search?key=YOUR_KEY&query=apache" | jq
Python 库
import shodan
api = shodan.Shodan('YOUR_API_KEY')
# Search
results = api.search('apache')
print(f'Results found: {results["total"]}')
for result in results['matches']:
print(f'IP: {result["ip_str"]}')
# Host lookup
host = api.host('1.1.1.1')
print(f'IP: {host["ip_str"]}')
print(f'Organization: {host.get("org", "n/a")}')
for item in host['data']:
print(f'Port: {item["port"]}')
快速参考
必备 CLI 命令
| 命令 | 描述 | 点数 |
|---|---|---|
shodan init KEY | 初始化 API 密钥 | 0 |
shodan info | 显示账户信息 | 0 |
shodan myip | 显示你的 IP | 0 |
shodan host IP | 主机详情 | 0 |
shodan count QUERY | 结果数量 | 0 |
shodan search QUERY | 基本搜索 | 0* |
shodan download FILE QUERY | 保存结果 | 每 100 条结果 1 点 |
shodan parse FILE | 提取数据 | 0 |
shodan stats QUERY | 统计信息 | 1 |
shodan scan submit IP | 按需扫描 | 每个 IP 1 点 |
shodan honeyscore IP | 蜜罐检查 | 0 |
*筛选器每次查询消耗 1 个点数
常见搜索查询
| 用途 | 查询 |
|---|---|
| 查找网络摄像头 | webcam has_screenshot:true |
| MongoDB 数据库 | product:mongodb |
| Redis 服务 | product:redis |
| Elasticsearch | product:elastic port:9200 |
| 默认密码 | "default password" |
| 易受攻击的 RDP | port:3389 vuln:CVE-2019-0708 |
| 工业系统 | port:502 modbus |
| Cisco 设备 | product:cisco |
| 开放的 VNC | port:5900 authentication disabled |
| 暴露的 FTP | port:21 anonymous |
| WordPress 站点 | http.component:wordpress |
| 打印机 | "HP-ChaiSOE" port:80 |
| 摄像头(RTSP) | port:554 has_screenshot:true |
| Jenkins 服务 | X-Jenkins port:8080 |
| Docker API | port:2375 product:docker |
有用的筛选组合
| 场景 | 查询 |
|---|---|
| 目标组织侦察 | org:"Company Name" |
| 域名枚举 | hostname:example.com |
| 网段扫描 | net:192.168.0.0/24 |
| SSL 证书搜索 | ssl.cert.subject.cn:*.target.com |
| 易受攻击的服务器 | vuln:CVE-2021-44228 country:US |
| 暴露的管理面板 | http.title:"admin" port:443 |
| 数据库暴露 | port:3306,5432,27017,6379 |
积分系统
| 操作 | 积分类型 | 费用 |
|---|---|---|
| 基本搜索 | 查询 | 0(无筛选条件) |
| 筛选搜索 | 查询 | 1 |
| 下载 100 条结果 | 查询 | 1 |
| 生成报告 | 查询 | 1 |
| 扫描 1 个 IP | 扫描 | 1 |
| 网络监控 | 受监控的 IP | 取决于套餐 |
约束和限制
操作边界
- 速率限制为每秒 1 个请求
- 扫描结果不会立即返回(异步)
- 非 Enterprise 用户无法在 24 小时内重新扫描同一 IP
- 免费账户的积分有限
- 部分数据需要付费订阅
数据时效性
- Shodan 持续进行爬取,但数据可能已有数天或数周的历史
- 按需扫描可提供当前数据,但需要消耗积分
- 付费套餐可获取历史数据
法律要求
- 仅对已获授权的目标进行侦察
- 被动侦察通常合法,但请确认所在司法管辖区的规定
- 主动扫描(提交扫描)需要授权
- 记录所有侦察活动
示例
示例 1:组织侦察
# Find all hosts belonging to target organization
shodan search 'org:"Target Company"'
# Get statistics on their infrastructure
shodan stats --facets port,product,country 'org:"Target Company"'
# Download detailed data
shodan download target_data.json.gz 'org:"Target Company"'
# Parse for specific info
shodan parse --fields ip_str,port,product target_data.json.gz
示例 2:易受攻击的服务发现
# Find hosts vulnerable to BlueKeep (RDP CVE)
shodan search 'vuln:CVE-2019-0708 country:US'
# Find exposed Elasticsearch with no auth
shodan search 'product:elastic port:9200 -authentication'
# Find Log4j vulnerable systems
shodan search 'vuln:CVE-2021-44228'
示例 3:IoT 设备发现
# Find exposed webcams
shodan search 'webcam has_screenshot:true country:US'
# Find industrial control systems
shodan search 'port:502 product:modbus'
# Find exposed printers
shodan search '"HP-ChaiSOE" port:80'
# Find smart home devices
shodan search 'product:nest'
示例 4:SSL/TLS 证书分析
# Find hosts with specific SSL cert
shodan search 'ssl.cert.subject.cn:*.example.com'
# Find expired certificates
shodan search 'ssl.cert.expired:true org:"Company"'
# Find self-signed certificates
shodan search 'ssl.cert.issuer.cn:self-signed'
示例 5:Python 自动化脚本
#!/usr/bin/env python3
import shodan
import json
API_KEY = 'YOUR_API_KEY'
api = shodan.Shodan(API_KEY)
def recon_organization(org_name):
"""Perform reconnaissance on an organization"""
try:
# Search for organization
query = f'org:"{org_name}"'
results = api.search(query)
print(f"[*] Found {results['total']} hosts for {org_name}")
# Collect unique IPs and ports
hosts = {}
for result in results['matches']:
ip = result['ip_str']
port = result['port']
product = result.get('product', 'unknown')
if ip not in hosts:
hosts[ip] = []
hosts[ip].append({'port': port, 'product': product})
# Output findings
for ip, services in hosts.items():
print(f"\n[+] {ip}")
for svc in services:
print(f" - {svc['port']}/tcp ({svc['product']})")
return hosts
except shodan.APIError as e:
print(f"Error: {e}")
return None
if __name__ == '__main__':
recon_organization("Target Company")
示例 6:网段评估
# Scan a /24 network range
shodan search 'net:192.168.1.0/24'
# Get port distribution
shodan stats --facets port 'net:192.168.1.0/24'
# Find specific vulnerabilities in range
shodan search 'net:192.168.1.0/24 vuln:CVE-2021-44228'
# Export all data for range
shodan download network_scan.json.gz 'net:192.168.1.0/24'
故障排除
| 问题 | 原因 | 解决方案 |
|---|---|---|
| 未配置 API 密钥 | 密钥尚未初始化 | 运行 shodan init YOUR_API_KEY,然后使用 shodan info 验证 |
| 查询积分已用完 | 月度积分已消耗完 | 使用不消耗积分的查询(不加筛选条件)、等待积分重置或升级套餐 |
| 主机最近已被爬取 | 24 小时内无法重新扫描 IP | 使用 shodan host IP 查看现有数据,或等待 24 小时 |
| 超出速率限制 | 每秒请求超过 1 个 | 在 API 请求之间添加 time.sleep(1) |
| 搜索结果为空 | 条件过于具体或语法错误 | 为短语加引号:'org:"Company Name"';放宽筛选条件 |
| 下载的文件无法解析 | 文件已损坏或格式错误 | 使用 gunzip -t file.gz 验证,并使用 --limit 重新下载 |
评论
支持 Markdown