使用API获取fofa数据时出现KeyError: 'results'
时间: 2023-03-30 16:03:47 浏览: 86
这个问题可能是由于你的 API 请求参数不正确或者是 API 返回的数据格式不正确导致的。建议你检查一下 API 请求参数是否正确,并且确认 API 返回的数据格式是否符合你的预期。如果问题仍然存在,可以尝试联系 fofa 官方客服寻求帮助。
相关问题
写一段调用fofa API的python代码
以下是一个使用Python调用FOFA API的示例代码:
```python
import requests
# 设置API地址和访问密钥
api_url = "https://fofa.so/api/v1/search/all"
email = "your_email"
key = "your_key"
# 设置查询语句
query_str = "title=\"admin\" && protocol=\"https\""
# 设置查询参数
params = {
"qbase64": query_str.encode("base64"),
"email": email,
"key": key,
"size": "10000", # 设置查询结果数量
"fields": "ip,port,title", # 设置返回字段
}
# 发起API请求
response = requests.get(api_url, params=params)
# 处理API响应
if response.status_code == 200:
data = response.json()
if data.get("error") == 0:
results = data.get("results")
for result in results:
print(result)
else:
print("API请求失败")
```
以上代码演示了如何使用FOFA API进行查询,并解析API响应结果。需要注意的是,FOFA API需要提供访问密钥才能进行查询,因此需要先在FOFA官网上注册并获取访问密钥。同时,由于FOFA API返回的是JSON格式的数据,因此需要使用Python的JSON库对API响应结果进行解析。
加入fofa、shodan、github、virustotal 的api
加入这些API可以增强你的搜索和安全分析能力。以下是每个API的简要介绍和如何使用它们:
1. fofa API:可以使用 Fofa API 来搜索互联网上的设备和服务。在使用之前,你需要注册并获取 API 密钥。然后,你可以使用 API 来执行各种查询,例如根据 IP 地址、端口、协议、地理位置等来搜索设备。示例代码如下:
```python
import requests
api_email = 'your_email'
api_key = 'your_api_key'
query = 'title="admin" && country="CN"'
res = requests.get(f"https://fofa.so/api/v1/search/all?email={api_email}&key={api_key}&qbase64={query}")
print(res.json())
```
2. Shodan API:与 Fofa API 类似,Shodan API 也可以用来搜索互联网上的设备和服务。你需要注册并获取 API 密钥,然后可以使用 API 来执行各种查询。例如,根据 IP 地址、端口、协议、服务等搜索设备。示例代码如下:
```python
import shodan
api_key = 'your_api_key'
api = shodan.Shodan(api_key)
query = 'apache'
results = api.search(query)
for result in results['matches']:
print(result['ip_str'])
```
3. GitHub API:GitHub API 可以用来搜索代码库、获取代码库信息、获取提交历史记录等。你需要注册并获取 API 密钥,然后可以使用 API 来执行各种查询。例如,搜索代码仓库、获取仓库信息、获取提交历史记录等。示例代码如下:
```python
import requests
api_key = 'your_api_key'
headers = {'Authorization': f'token {api_key}'}
# Search repositories
query = 'language:python'
res = requests.get(f'https://api.github.com/search/repositories?q={query}', headers=headers)
print(res.json())
# Get repository information
repo_name = 'torvalds/linux'
res = requests.get(f'https://api.github.com/repos/{repo_name}', headers=headers)
print(res.json())
# Get commit history
res = requests.get(f'https://api.github.com/repos/{repo_name}/commits', headers=headers)
print(res.json())
```
4. VirusTotal API:VirusTotal API 可以用来分析文件、URL 和 IP 地址的恶意程度。你需要注册并获取 API 密钥,然后可以使用 API 来执行各种分析。例如,分析文件、URL 和 IP 地址的恶意程度,获取恶意软件样本等。示例代码如下:
```python
import requests
api_key = 'your_api_key'
headers = {'x-apikey': api_key}
# Analyze file
file_hash = 'e8f91b8e4b9f0dbd382bfe6c9f776b2c'
res = requests.get(f'https://www.virustotal.com/api/v3/files/{file_hash}', headers=headers)
print(res.json())
# Analyze URL
url = 'https://www.google.com'
res = requests.post(f'https://www.virustotal.com/api/v3/urls', headers=headers, json={'url': url})
print(res.json())
# Analyze IP address
ip_address = '8.8.8.8'
res = requests.get(f'https://www.virustotal.com/api/v3/ip_addresses/{ip_address}', headers=headers)
print(res.json())
```
以上是如何使用 fofa、shodan、github 和 virustotal 的 API 进行搜索和分析的示例代码。注意,在使用这些 API 时需要遵守相应的使用条款和政策。