Shodan 使用

2023-06-25

本文来自:Shodan新手入坑指南, 记录简要用法,以便使用。

文章先给出搜索过滤方法,然后再简单介绍两种使用shodan的方法:使用命令和编写代码。

搜索过滤

hostname:搜索指定的主机或域名,例如 hostname:"google"
port:搜索指定的端口或服务,例如 port:"21"
country:搜索指定的国家,例如 country:"CN"
city:搜索指定的城市,例如 city:"Hefei"
org:搜索指定的组织或公司,例如 org:"google"
isp:搜索指定的ISP供应商,例如 isp:"China Telecom"
product:搜索指定的操作系统/软件/平台,例如 product:"Apache httpd"
version:搜索指定的软件版本,例如 version:"1.6.2"
geo:搜索指定的地理位置,参数为经纬度,例如 geo:"31.8639, 117.2808"
before/after:搜索指定收录时间前后的数据,格式为dd-mm-yy,例如 before:"11-11-15"
net:搜索指定的IP地址或子网,例如 net:"210.45.240.0/24"

命令下使用shodan

Shodan 是由官方提供的 Python 库的,项目位于:https://github.com/achillean/shodan-python

安装

pip install shodan

或者

git clone https://github.com/achillean/shodan-python.git && cd shodan-python python setup.py install

查看帮助信息

starnight:~ starnight$ shodan 
Usage: shodan [OPTIONS] COMMAND [ARGS]...
Options:
-h, --help Show this message and exit.
Commands:
alert Manage the network alerts for your account    # 管理账户的网络提示
convert Convert the given input data file into a...   # 转换输入文件
count Returns the number of results for a search   # 返回查询结果数量
download Download search results and save them in a... # 下载查询结果到文件
honeyscore Check whether the IP is a honeypot or not.    # 检查 IP 是否为蜜罐
host View all available information for an IP...   # 显示一个 IP 所有可用的详细信息
info Shows general information about your account    # 显示账户的一般信息
init Initialize the Shodan command-line         # 初始化命令行
myip Print your external IP address           # 输出用户当前公网IP
parse Extract information out of compressed JSON... # 解析提取压缩的JSON信息,即使用download下载的数据
scan Scan an IP/ netblock using Shodan.          # 使用 Shodan 扫描一个IP或者网段
search Search the Shodan database             # 查询 Shodan 数据库
stats Provide summary information about a search... # 提供搜索结果的概要信息
stream Stream data in real-time.             # 实时显示流数据

常用实例

init : 初始化

shodan init [API_Key]
Successfully initialized

需要提供你自己的API key, 注册后可能还需要升级账户。

count: 返回查询的结果数量

starnight:~ starnight$ shodan count tomcat country:cn
14826

download: 下载查询结果

将搜索结果下载到一个文件中,文件中的每一行都是 JSON 格式存储的目标 banner 信息。默认情况下,该命令只会下载1000条结果,如果想下载更多结果需要增加 --limit 参数。

starnight:~ starnight$ shodan download
Usage: shodan download [OPTIONS] <filename> <search query>

parse: 过滤搜索结果

我们可以使用 parse 来解析之前下载数据,它可以帮助我们过滤出自己感兴趣的内容,也可以用来将下载的数据格式从 JSON 转换成 CSV 等等其他格式,当然更可以用作传递给其他处理脚本的管道。例如,我们想将上面下载的数据以CSV格式输出IP地址、端口号和组织名称:

starnight:~ starnight$ shodan parse --fields ip_str,port,org --separator , tomcat-cn.json.gz

host:查询主机信息

查看指定主机的相关信息,如地理位置信息,开放端口,甚至是否存在某些漏洞等信息。

以上诉查询结果的最后一个IP:222.243.129.135 为例做演示

search: 在命令行下搜索

直接将查询结果展示在命令行中,默认情况下只显示IP、端口号、主机名和HTTP数据。当然我们也可以通过使用 –fields 来自定义显示内容,例如,我们只显示IP、端口号、组织名称和主机名:

starnight:~ starnight$ shodan search --fields ip_str,port,org,hostnames tomcat country:cn --limit 10

代码中使用 Shodan 库

还是使用上一节讲到的 shodan 库,安装方式这里不在阐述了。同样的,在使用 shodan 库之前需要初始化连接 API,代码如下:

import shodan
SHODAN_API_KEY = "API_Key"
api = shodan.Shodan(SHODAN_API_KEY)

随后,我们就可以搜索数据了,示例代码片如下:

try:
# 搜索 Shodan
results = api.search('apache')
# 显示结果
print 'Results found: %s' % results['total']
for result in results['matches']:
print result['ip_str']
except shodan.APIError, e:
print 'Error: %s' % e

这里 Shodan.search() 会返回类似如下格式的 JSON 数据:

{
'total': 8669969,
'matches': [
{
'data': 'HTTP/1.0 200 OK\r\nDate: Mon, 08 Nov 2010 05:09:59 GMT\r\nSer...',
'hostnames': ['pl4t1n.de'],
'ip': 3579573318,
'ip_str': '89.110.147.239',
'os': 'FreeBSD 4.4',
'port': 80,
'timestamp': '2014-01-15T05:49:56.283713'
},
...
]
}

常用 Shodan 库函数

shodan.Shodan(key) :初始化连接API
Shodan.count(query, facets=None):返回查询结果数量
Shodan.host(ip, history=False):返回一个IP的详细信息
Shodan.ports():返回Shodan可查询的端口号
Shodan.protocols():返回Shodan可查询的协议
Shodan.services():返回Shodan可查询的服务
Shodan.queries(page=1, sort='timestamp', order='desc'):查询其他用户分享的查询规则
Shodan.scan(ips, force=False):使用Shodan进行扫描,ips可以为字符或字典类型
Shodan.search(query, page=1, limit=None, offset=None, facets=None, minify=True):查询Shodan数据

参考:

Shodan新手入坑指南:http://www.freebuf.com/sectool/121339.html

Shodan documentation: https://shodan.readthedocs.io/en/latest/tutorial.html

Shodan记录的标识信息:https://developer.shodan.io/api/banner-specification

More: 国内的ZoomEye使用 请参考:

从ZoomEye API 到 Weblogic 弱口令扫描

Shodan 使用的相关教程结束。

《Shodan 使用.doc》

下载本文的Word格式文档,以方便收藏与打印。