[Shodan]使用Python API的环境准备及示例

本文介绍Shodan提供的Python API开发环境基本配置过程及脚本示例。以上内容在官方说明文档中都有详细描述,如果想深入了解推荐前往以下链接查阅:
Shodan官网:https://www.shodan.io
Python API Document:https://shodan.readthedocs.io/en/latest/

环境说明

OS: Kali GNU/Linux Rolling 2019.4
Python 2.7.16+

环境准备

首先要去官网注册一个账户,然后趁着黑五打折入一个5$的membership终身会员,否则即使有了API Key,也无法执行带过滤条件的查询,笔者这里分享自己的API Key: Vvm2atebzfedrKuMDLFNbJf2hlxyDRed ,不过一个月只有100查询凭证,先用先得吧。

# 安装pip
root@kali:~/Downloads# apt-get install python-pip
......
# 安装shodan库
root@kali:~/Downloads# pip install shodan
Collecting shodan
  Downloading https://files.pythonhosted.org/packages/f9/46/b3aaa376a5c3e5ad15b61bb0e2914477926e900885ee7ce482fe4f1cd237/shodan-1.21.3.tar.gz (50kB)
    100% |████████████████████████████████| 51kB 4.4kB/s 
Collecting XlsxWriter (from shodan)
  Downloading https://files.pythonhosted.org/packages/00/1f/2092a81056d36c1b6651a645aa84c1f76bcee03103072d4fe1cb58501d69/XlsxWriter-1.2.8-py2.py3-none-any.whl (141kB)
    100% |████████████████████████████████| 143kB 5.8kB/s 
Requirement already satisfied: click in /usr/lib/python2.7/dist-packages (from shodan) (7.0)
Collecting click-plugins (from shodan)
  Downloading https://files.pythonhosted.org/packages/e9/da/824b92d9942f4e472702488857914bdd50f73021efea15b4cad9aca8ecef/click_plugins-1.1.1-py2.py3-none-any.whl
Requirement already satisfied: colorama in /usr/lib/python2.7/dist-packages (from shodan) (0.3.7)
Requirement already satisfied: requests>=2.2.1 in /usr/lib/python2.7/dist-packages (from shodan) (2.21.0)
Requirement already satisfied: ipaddress in /usr/lib/python2.7/dist-packages (from shodan) (1.0.17)
Building wheels for collected packages: shodan
  Running setup.py bdist_wheel for shodan ... done
  Stored in directory: /root/.cache/pip/wheels/d6/6b/12/2778a7ecb0a13669e8476cb7a1be4aec237273da58e19c532c
Successfully built shodan
Installing collected packages: XlsxWriter, click-plugins, shodan
Successfully installed XlsxWriter-1.2.8 click-plugins-1.1.1 shodan-1.21.3

脚本示例

统计某查询条件下国家分布数量

# -- coding:utf-8 --
# Python v2.7.10+
# topcountry.py
# Written by Gaearrow

import shodan
import sys

# MY_API_KEY
API_KEY = "XXXXXX"

# The list of properties we want summary information on
FACETS = [
    ('country', 10),
]

try:
    # Setup the api
    api = shodan.Shodan(API_KEY)

    # Perform the search
    query = "product:\"Microsoft SQL Server\""
    result = api.count(query,facets=FACETS)

    print '==================================='
    print 'Shodan Summary Information'
    print 'Query: %s' % query
    print 'Total Results: %s' % result['total']
    print 'Top 10 Countries\n'
    for term in result['facets']['country']:
        print '%s: %s' % (term['value'], term['count'])
    print '==================================='
    
except Exception as e:
    print 'Error: %s' % e
    sys.exit(1)

迭代获取某查询条件下所有IP地址

# -- coding:utf-8 --
# Python v2.7.10+
# GetIPs.py
# Written by Gaearrow

import shodan
import sys

# MY_API_KEY
API_KEY = "XXXXXX"

# 过两天贴上来

你可能感兴趣的:(网络安全)