解决方案

Shodan API使用指南 撒旦网

seo靠我 2023-09-25 08:25:43

0x00 前言

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

本文将要介绍Shodan API在使用过程需要注意的问SEO靠我题,分享使用心得和脚本开发技巧。

0x01 本文将要介绍以下内容

Shodan API的简单使用

利用python调用Shodan API获得搜索结果

对搜索结果作进一步处理

三种积分(credits)的区别

SEO靠我过Shodan官网导出搜索结果和进一步处理

0x02 Shodan API的简单使用

1、注册账号,获得API Key

测试API Key为:SkVS0RAbiTQpzzEsahqnq2Hv6SwjUfs3SEO靠我

2、安装python包

pip install shodan

3、通过Shodan CLI获得搜索结果

参考资料:https://cli.shodan.io/

注:

未付费只能获得100个搜索结果

CLI全称为cSEO靠我ommand-line interface,即shodan的命令行模式

Windows系统下使用pip install在同级目录产生文件Shodan.exe

如下图

(1) 初始化

shodan init <SEO靠我api key>

实际命令为:

shodan init SkVS0RAbiTQpzzEsahqnq2Hv6SwjUfs3

如下图

(2) 搜索指定内容(apache)的数量

shodan count apachSEO靠我e

如下图

获得结果23803090

(3) 搜索指定内容(apache)的信息

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

搜索关键词:apSEO靠我ache

输出:ip_str,port,org,hostnames

(4) 下载指定内容(apache)的搜索结果

shodan download result apache

搜索关键词:apache

保存文件SEO靠我名:result.json.gz

如下图

(5) 解析文件,获得搜索结果

shodan parse --fields ip_str,port,org --separator , result.json.gzSEO靠我

如下图

(6) 搜索指定IP的信息

shodan host 189.201.128.250

如下图

0x03 三种积分(credits)的区别

Shodan共有三种积分(credits):

Export credSEO靠我its

Query credits

Scan credits

官方文档:

https://help.shodan.io/the-basics/credit-types-explained

简单理解:

ExportSEO靠我 Credits

通过Shodan官网下载数据时使用

1 export credit = 10,000 results

注:

导出一次结果消耗一个credit,无论获取到的结果有多少,最多为10000个结果

SEO靠我初不会更新

Query Credits

调用Shodan API时使用

1 query credit = 100 results

月初更新,也就是说如果只买了一个月的会员,那么下一个月清零

Scan CrediSEO靠我ts

调用Shodan API时使用

1 scan credit = 1 IP

月初更新

0x04 通过python调用Shodan API获得搜索结果

注:

未付费不仅无法使用搜索过滤条件,而且只能获得100个SEO靠我搜索结果

(1) 搜索指定内容(apache)的信息

python代码如下:

import shodan SHODAN_API_KEY = "SkVS0RAbiTQpzzEsahqnq2HvSEO靠我6SwjUfs3" api = shodan.Shodan(SHODAN_API_KEY) try:results = api.search(Apache)print SEO靠我Results found: %s % results[total]for result in results[matches]:         print ("%s:%s|%s|%s"%(resuSEO靠我lt[ip_str],result[port],result[location][country_name],result[hostnames])) except shodan.APISEO靠我Error, e:print Error: %s % e

如下图

如果未付费,无法使用搜索过滤条件,例如Apache country:"US"

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

python代码如下SEO靠我

import shodan SHODAN_API_KEY = "SkVS0RAbiTQpzzEsahqnq2Hv6SwjUfs3" api = shodan.ShodSEO靠我an(SHODAN_API_KEY) file_object = open(ip.txt, w) try:results = api.search(Apache)priSEO靠我nt Results found: %s % results[total]for result in results[matches]:          #            pSEO靠我rint result[ip_str]file_object.writelines(result[ip_str]+\n) except shodan.APIError, e:printSEO靠我 Error: %s % e file_object.close()

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

python代码如下:

import shodan SEO靠我 import sys SHODAN_API_KEY = "SkVS0RAbiTQpzzEsahqnq2Hv6SwjUfs3" api = shodan.ShodSEO靠我an(SHODAN_API_KEY) if len(sys.argv)<2:print [!]Wrong parametersys.exit(0) print [*]SSEO靠我earch string: %s % sys.argv[1] file_object = open(ip.txt, w) try:results = api.searcSEO靠我h(sys.argv[1])print [+]Results found: %s % results[total]for result in results[matches]:          SEO靠我 #            print result[ip_str]file_object.writelines(result[ip_str]+\n) except shodSEO靠我an.APIError, e:print Error: %s % e file_object.close()

命令行参数:

search.py apache

注:

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

search.py "apache country:US"

(4) 读取文件中的IP列表,反查IP信息

python代码如下:

import sys   reloadSEO靠我(sys)   sys.setdefaultencoding(utf8)   SHODAN_API_KEY = "SkVS0RAbiTQpzzEsahqnq2Hv6SwSEO靠我jUfs3" api = shodan.Shodan(SHODAN_API_KEY) def searchip( str ):try:host = api.host(sSEO靠我tr)except shodan.exception.APIError:print "[!]No information available"print "----------------------SEO靠我-----------------------"returnelse:# Print general infotry:print "IP: %s\r\nOrganization: %s\r\nOperSEO靠我ating System: %s" % (host[ip_str], host.get(org, n/a), host.get(os, n/a))except UnicodeEncodeError:pSEO靠我rint "[!]UnicodeEncode Error\r\n"     else:# Print all bannersfor item in host[data]:print "Port: %sSEO靠我\r\nBanner: %s" % (item[port], item[data])print "---------------------------------------------"   reSEO靠我turn file_object = open(ip.txt, r) for line in file_object:searchip(line)

0x05 通过ShodSEO靠我an官网下载搜索结果

通过Shodan官网下载数据时使用Export credits,如下图

查询一次消耗一个export credit,无论结果有多少个,最多为10000个

导出格式选择为json

(1) SEO靠我从下载的json结果文件中提取IP

python代码如下:

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

(2) SEO靠我从下载的json结果文件中提取指定国家的IP和端口

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

python代码如下:

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

命令行参数:

search.py US

生成文件US.txt,保存IPSEO靠我和对应的端口

0x06 小结

本文介绍了Shodan API的用法,分享使用心得和python脚本开发技巧。选择付费购买时,记得区分好三种积分(credits)

“SEO靠我”的新闻页面文章、图片、音频、视频等稿件均为自媒体人、第三方机构发布或转载。如稿件涉及版权等问题,请与 我们联系删除或处理,客服邮箱:html5sh@163.com,稿件内容仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同 其观点或证实其内容的真实性。

网站备案号:浙ICP备17034767号-2