shodan使用语法

shodan API 使用指南

0x00 前言

Shodan是一个针对网络设备的搜索引擎,通过Shodan API进行搜索,不仅数据更加丰富,而且能够配合自己的程序实现自动化分析。

0x01 shodan API 的简单使用

1.注册账号,获取API key

我的API key 为 nSBVNCGbzfYGiwURnj1Mexs2YnPCWXpk

2.安装python包

pip install shodan

3.初始化

shodan init <api key>

我的命令为

shoadn init nSBVNCGbzfYGiwURnj1Mexs2YnPCWXpk

4.搜索指定内容(apache)

shodan count apache

5.搜索指定信息

shadon search --fields ip_str,port,org,hostnames apache

搜索关键词:apache

输出:ip_str,port,org,hostnames

6.下载指定内容

shodan download result apache

保存文件名:result.json.gz

7.解析文件,获取搜索结果

shodan parse --fields ip_str,port,org --separator , result.json.gz

8.搜索指定ip的信息

shodan host 189.201.128.250

0x02 shodan 使用语法

面对大量的信息量,如果要获取我们想要的目标信息,那么就必须附加限制条件缩小范围是最后的结果更加精确,与Google一样Shodan也有相应的语言可以使用:

hostname——————————搜索指定的主机或域名,例如 hostname:baidu
port——————————————搜索指定的端口或服务,例如 port:80
country———————————搜索指定的国家,例如 country:US
city——————————————搜索指定的城市,例如 city:Chengdu
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"
  • 限定国家和城市

    限定国家country:"CN"

    限定城市city:"ShangHai"

  • 限定主机名或域名

    hostname:.org
    hostname:"google"
    hostname:baidu.com
    
  • 限定组织或机构

org:"alibaba"

  • 限定系统OS版本

    os:"Windows Server 2008 R2"
    os:"Windows 7 or 8"
    os:"Linux 2.6.x"
    
  • 限定端口

    port:22
    port:80
    
  • 指定网段

    net:"59.56.19.0/24"
    
  • 指定使用的软件或产品

    product:"Apache httpd"
    product:"nginx"
    product:"Microsoft IIS httpd"
    product:"mysql"
    
  • 指定CVE漏洞编号

    vuln:"CVE-2014-0723"
    
  • 指定网页内容

    http.html:"hello world"
    
  • 指定网页标题
    http.title:"hello"

  • 指定返回响应码
    http.status:200

  • 指定返回中的server类型
    http.server:Apache/2.4.7
    http.server:PHP

  • 指定地理位置
    geo:"31.25,121.44"

  • 指定ISP供应商
    isp:"China Telecom"

ssh default password
ssh default password country:"JP"
FTP anon successful

0x03 通过python调用shadon API获得搜索结果

1.搜索指定内容

代码:

import shodan
SHODAN_API_KEY = "SkVS0RAbiTQpzzEsahqnq2Hv6SwjUfs3"
api = shodan.Shodan(SHODAN_API_KEY)
try:
    results = api.search('Apache')
    print 'Results found: %s' % results['total']
    for result in results['matches']:         
            print ("%s:%s|%s|%s"%(result['ip_str'],result['port'],result['location']['country_name'],result['hostnames']))
except shodan.APIError, e:
    print 'Error: %s' % e

2.搜索指定内容,将获得的IP写入文件

import shodan
SHODAN_API_KEY = "SkVS0RAbiTQpzzEsahqnq2Hv6SwjUfs3"
api = shodan.Shodan(SHODAN_API_KEY)
file_object = open('ip.txt', 'w')
try:
    results = api.search('Apache')
    print 'Results found: %s' % results['total']
    for result in results['matches']:         
#            print result['ip_str']
            file_object.writelines(result['ip_str']+'\n')
except shodan.APIError, e:
    print 'Error: %s' % e
file_object.close()

3.通过命令行参数指定搜索条件,将搜索到的IP写入文件

python代码如下:

import shodan
import sys
SHODAN_API_KEY = "SkVS0RAbiTQpzzEsahqnq2Hv6SwjUfs3"
api = shodan.Shodan(SHODAN_API_KEY)
if len(sys.argv)<2:
    print '[!]Wrong parameter'
    sys.exit(0)
print '[*]Search string: %s' % sys.argv[1]
file_object = open('ip.txt', 'w')
try:
    results = api.search(sys.argv[1])
    print '[+]Results found: %s' % results['total']
    for result in results['matches']:         
#            print result['ip_str']
            file_object.writelines(result['ip_str']+'\n')
except shodan.APIError, e:
    print 'Error: %s' % e
file_object.close()

命令行参数:

search.py apache

注:

如果搜索多个关键词,需要用引号将搜索条件包含,例如:

search.py "apache country:US" 
  1. 读取文件中的IP列表,反查IP信息
import sys  
reload(sys)  
sys.setdefaultencoding('utf8')  
SHODAN_API_KEY = "SkVS0RAbiTQpzzEsahqnq2Hv6SwjUfs3"
api = shodan.Shodan(SHODAN_API_KEY)
def searchip( str ):
    try:
        host = api.host(str)
    except shodan.exception.APIError:
        print "[!]No information available"
        print "---------------------------------------------"
        return
    else:
        # Print general info
        try:
            print "IP: %s\r\nOrganization: %s\r\nOperating System: %s" % (host['ip_str'], host.get('org', 'n/a'), host.get('os', 'n/a'))
        except UnicodeEncodeError:
            print "[!]UnicodeEncode Error\r\n"     
        else:
            # Print all banners
            for item in host['data']:
                print "Port: %s\r\nBanner: %s" % (item['port'], item['data'])
        print "---------------------------------------------"   
        return
file_object = open('ip.txt', 'r')
for line in file_object:
    searchip(line)

0x04 从shadon官网下载搜索结果

1.从下载的json结果文件中提取IP

python代码如下:

import json
file_object = open("shodan_data.json", 'r')
for line in file_object:
    data = json.loads(line)
    print data["ip_str"]   
file_object.close()

2.从下载的json结果文件中提取指定国家的IP和端口

国家代号在二级元素中,对应结构:data["location"]["country_code"]

python代码如下:

import json
import sys
import re
def search(country):
    file_object = open("shodan_data.json", 'r')
    file_object2 = open(country+".txt", 'w')
    for line in file_object:
        data = json.loads(line)  
        if re.search(data["location"]["country_code"], country, re.IGNORECASE):
            str1 = "%s:%s" % (data["ip_str"],data["port"])
            print str1
            file_object2.writelines(str1+'\n')
    file_object.close()
    file_object2.close()
if __name__ == "__main__":
    if len(sys.argv)<2:
        print ('[!]Wrong parameter')
        sys.exit(0)
    else:
        print ('[*]Search country code: %s' % sys.argv[1])
        search(sys.argv[1])
        print ("[+]Done")

命令行参数:

search.py US
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值