import requests
import base64
import json
import csv
# Fofa API Key
key = "*******" # 替换为有效Key
# 搜索语法
query = '*******'
# 设置 API 请求参数
url = "https://fofa.info/api/v1/search/all"
payload = {
"key": key,
"qbase64": base64.b64encode(query.encode()).decode(),
"size": "10000",
"fields": "ip,port,host,icp,cert.subject.org,protocol,base_protocol,link,country_name,region,city",
"full": "True"
}
# 发送请求
response = requests.get(url, params=payload)
if response.status_code == 200:
data = json.loads(response.text)
fields = ["ip", "port", "host", "icp", "cert.subject.org", "protocol", "base_protocol", "link", "country_name",
"region", "city"]
results = data.get("results", [])
if not results:
print("未找到匹配结果。")
else:
with open("fofa_results.csv", "w", newline="", encoding="utf-8") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(
["IP", "Port", "Host", "ICP", "Cert_Subject_Org", "Protocol", "Base_Protocol", "Link", "Country_Name",
"Region", "City", "Company_Name"])
for result in results:
# 将列表转换为字典(字段名 → 值)
result_dict = dict(zip(fields, result))
ip = result_dict.get("ip", "")
port = result_dict.get("port", "")
host = result_dict.get("host", "")
icp = result_dict.get("icp", "")
cert_subject_org = result_dict.get("cert.subject.org", "")
protocol = result_dict.get("protocol", "")
base_protocol = result_dict.get("base_protocol", "")
link = result_dict.get("link", "")
country_name = result_dict.get("country_name", "")
region = result_dict.get("region", "")
city = result_dict.get("city", "")
# 判断 company_name
company_name = icp or cert_subject_org or "未知"
# 写入 CSV
writer.writerow(
[ip, port, host, icp, cert_subject_org, protocol, base_protocol, link, country_name, region, city,
company_name])
print(f"数据已保存到 fofa_results.csv,共 {len(results)} 条记录。")
else:
print("API请求失败:", response.text)
02-18
2210

03-18
525

03-18