shodan使用教程
Shodan簡介
Shodan是互聯網上最可怕的搜索引擎,與谷歌不同的是,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
常用示例
init
初始化命令行工具。
➜ ~ shodan init [API_Key]
Successfully initialized
count
返回查詢的結果數量。
➜ ~ shodan count microsoft iis 6.0
575862
download
將搜索結果下載到一個文件中,文件中的每一行都是 JSON 格式存儲的目標 banner 信息。默認情況下,該命令只會下載1000條結果,如果想下載更多結果需要增加 --limit 參數。
parse
我們可以使用 parse 來解析之前下載數據,它可以幫助我們過濾出自己感興趣的內容,也可以用來將下載的數據格式從 JSON 轉換成 CSV 等等其他格式,當然更可以用作傳遞給其他處理腳本的管道。例如,我們想將上面下載的數據以CSV格式輸出IP地址、端口號和組織名稱:
➜ ~ shodan parse --fields ip_str,port,org --separator , microsoft-data.json.gz
host
查看指定主機的相關信息,如地理位置信息,開放端口,甚至是否存在某些漏洞等信息。
search
直接將查詢結果展示在命令行中,默認情況下只顯示IP、端口號、主機名和HTTP數據。當然我們也可以通過使用 –fields 來自定義顯示內容,例如,我們只顯示IP、端口號、組織名稱和主機名:
➜ ~ shodan search --fields ip_str,port,org,hostnames microsoft iis 6.0
代碼中使用 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):初始化連接APIShodan.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數據





