使用Python编写的Shodan工具,批量查询互联网上的设备信息
发布时间:2024-01-12 09:03:07
Shodan是一款用于搜索互联网上连接的设备的工具。它可以帮助用户发现并了解与互联网连接的设备的详细信息,例如服务器、路由器、摄像头等等。在本文中,我们将使用Python编写一个简单的脚本来演示如何使用Shodan进行批量查询。
首先,我们需要安装Shodan Python库。可以通过以下命令使用pip进行安装:
pip install shodan
在安装完Shodan库后,我们需要一个Shodan账号。可以在Shodan网站(https://www.shodan.io/)上注册一个免费账号,并获取API密钥。
接下来,让我们编写一个Python脚本,进行批量查询设备信息。以下是一个使用Shodan API进行查询的示例代码:
import shodan
# 设置Shodan API密钥
API_KEY = "YOUR_API_KEY"
# 初始化Shodan API对象
api = shodan.Shodan(API_KEY)
# 设备查询函数
def search_devices(query):
try:
# 使用Shodan API进行查询
results = api.search(query)
# 打印查询结果
for result in results['matches']:
print(f"IP: {result['ip_str']}")
print(f"Port: {result['port']}")
print(f"Organization: {result.get('org', 'N/A')}")
print(f"OS: {result.get('os', 'N/A')}")
print(f"Data: {result['data']}")
print("")
except shodan.APIError as e:
print("Error: %s" % e)
# 执行设备查询
if __name__ == '__main__':
# 查询摄像头设备
search_devices('camera')
# 查询路由器设备
search_devices('router')
在上述代码中,我们首先导入了shodan库,并设置了我们的API密钥。然后我们定义了一个search_devices函数,它接受一个查询字符串作为参数,并使用Shodan API进行查询。查询结果被打印到控制台上,包括设备的IP地址、端口、组织、操作系统和其他相关信息。
最后,在主函数中,我们使用了两个不同的查询字符串,分别为'camera'和'router',以查询摄像头和路由器设备的信息。你可以根据自己的需求修改查询的关键词。
要运行这个脚本,只需在终端中执行以下命令:
python shodan_tool.py
当脚本运行时,它将使用Shodan API查询互联网上与查询字符串匹配的设备,并打印查询结果。
这只是一个简单的示例,你可以根据自己的需求对代码进行进一步的扩展和改进,以实现更多功能。官方的Shodan Python库文档(https://shodan.readthedocs.io/en/latest/index.html)还提供了更详细的API使用说明和示例代码,你可以通过它来深入了解Shodan的功能和用法。
