Skip to content

Commit

Permalink
Feature v2.5.0 优化会员API接口获取数据代码
Browse files Browse the repository at this point in the history
  • Loading branch information
panyi committed Aug 6, 2024
1 parent 0892483 commit dcd1001
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 202 deletions.
19 changes: 13 additions & 6 deletions core/fofaMain.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import time
from urllib.parse import quote_plus
from tookit import unit, fofaUseragent, config
from tookit.client_main import get_result_from_api
from tookit.levelData import LevelData
from tookit.outputData import OutputData
import re, requests
Expand Down Expand Up @@ -637,18 +638,24 @@ def start(self):
print(colorize(_('无搜索结果,执行下一条'), "red"))
continue
else:
self.fofaSpider(self.search_key, searchbs64, 0)
print(colorize(_('[+] 抓取结束,{}关键字共抓取数据 {} 条\n').format(self.search_key,
str(len(self.host_set))),
"green"))
if config.FOFA_KEY:
get_result_from_api(self.search_key)
else:
self.fofaSpider(self.search_key, searchbs64, 0)
print(colorize(_('[+] 抓取结束,{}关键字共抓取数据 {} 条\n').format(self.search_key,
str(len(self.host_set))),
"green"))

else:
searchbs64, countnum = self.getFofaKeywordsCount(self.search_key)
if str(countnum) == "0" and len(str(countnum)) == 1:
print(colorize(_('无搜索结果'), "red"))
else:
self.fofaSpider(self.search_key, searchbs64, 0)
print(colorize(_('[*] 抓取结束,共抓取数据 {} 条').format(str(len(self.host_set))), "green"))
if config.FOFA_KEY:
get_result_from_api(self.search_key)
else:
self.fofaSpider(self.search_key, searchbs64, 0)
print(colorize(_('[*] 抓取结束,共抓取数据 {} 条').format(str(len(self.host_set))), "green"))

def _destroy(self):
self.removeDuplicate()
Expand Down
6 changes: 6 additions & 0 deletions fofa.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ def main():
parser.add_argument('--authorization', type=str, help="指定Authorization值")
parser.add_argument('--authorization-file', type=str, help="从文件中读取authorization列表 --authorization-file authorization.txt")

parser.add_argument('--fofa-key', type=str,
help="fofa api key值(配合fofa终身会员使用)")

proxy_group = parser.add_mutually_exclusive_group()
proxy_group.add_argument('--proxy', help=_("指定代理,代理格式 --proxy '127.0.0.1:7890'"))
proxy_group.add_argument('--proxy-url', help=_("指定代理url,即访问URL响应为proxy,代理格式 --proxy-url http://127.0.0.1/proxy_pool/get"))
Expand All @@ -66,6 +69,9 @@ def main():
# help="运行类型,默认为普通方式")
args = parser.parse_args()

if args.fofa_key:
config.FOFA_KEY = args.fofa_key

if args.authorization_file:
config.AUTHORIZATION_FILE = args.authorization_file
with open(config.AUTHORIZATION_FILE, 'r') as f:
Expand Down
196 changes: 0 additions & 196 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,196 +0,0 @@
# -*- coding: utf-8 -*-
from tookit import config
from tookit.fofa_client import Client
from tookit.unit import colorize


def get_base_results(search_key,size=10000,page=1):
client = Client(config.FOFA_KEY)
data = client.search(search_key, size=size, page=page,fields="link,port,protocol,country,region,city,as_number,as_organization,host,domain,os,"
"server,product")
return data["results"]

def get_link_results(search_key,size=10000,page=1):
client = Client(config.FOFA_KEY)
data = client.search(search_key, size=size, page=page,
fields="link")
return data["results"]

def get_search_data(search_key,size=10000,page=1):
client = Client(config.FOFA_KEY)
data = client.search(search_key, size=size, page=page,
fields="")
return data

def add_city_to_region(result_pool,current_country, current_region, current_city):
"""
添加城市信息 包含 country region city
@param result_pool:
@param current_country:
@param current_region:
@param current_city:
"""
# 检查 current_country 是否存在于 result_pool["country"]
if current_country not in result_pool["country"]:
# 如果不存在,则创建它,并初始化为一个空字典
result_pool["country"][current_country] = {}

# 检查 current_region 是否存在于 current_country 对应的字典中
if current_region not in result_pool["country"][current_country]:
# 如果不存在,则创建它,并初始化为一个空集合
result_pool["country"][current_country][current_region] = set()

# 现在可以安全地向 current_region 的集合中添加 current_city
result_pool["country"][current_country][current_region].add(current_city)


def init_result_pool(results):
result_pool = {
"country":{},
"port":set(),
"product":set(),
}
for result in results:
current_country = result[3]
current_region = result[4]
current_city = result[5]
add_city_to_region(result_pool,current_country,current_region,current_city)
result_pool["port"].add(result[1])
products = result[12].split(",")
for product in products:
result_pool["product"].add(product)

# result_pool["country"][current_country][current_region].add(current_city)
# print(result_pool)
return result_pool


def check_search_key_exceed(search_key):
'''size'''
data = get_search_data(search_key)
if data.get("size") > 10000:
return True
return False

def get_results(source_key,result_pool):
link_results = set()

client = Client(config.FOFA_KEY)
data = client.search(source_key, size=10000, page=1,
fields="link")
if data.get("size") > 10000:
## 城市
country_result = []
for country_code, country_data in result_pool['country'].items():

for region_code, region_data in country_data.items():
for city in region_data:
country_result.append(
f'({source_key}) && country="{country_code}" && region="{region_code}" && city="{city}"')

## 产品
product_result = []
for search_key in country_result:
for product in result_pool['product']:
product_result.append(f'{search_key} && product="{product}"')

## 端口
port_result = []
for search_key in product_result:
for port in result_pool["port"]:
port_result.append(f'{search_key} && port="{port}"')

# false_port_key = ""
# for port in result_pool["port"]:
# false_port_key += f" && port != {port}"
#
# for search_key in country_result:
# port_result.append(f"{search_key} {false_port_key}")

return port_result
else:
print(
colorize("[*] 未超过FOFA会员单次限制", "green"))
link_results.update(data["results"])
return link_results


# def get_results(search_key_result):
# link_results = set()
# for search_key in search_key_result:
# links = get_link_results(search_key, 10000)
# print(
# colorize("[*] 当前搜索关键词: {}".format(search_key), "blue"))
# link_results.update(links)
# print(
# colorize("[*] 搜索结束 去重后数量为: {}".format(len(link_results)), "blue"))
# return link_results

import socket
from urllib.parse import urlparse


def is_ip(url):
url = urlparse(url).netloc
if ":" in url:
url = url.split(":")[0]
try:
ip = socket.gethostbyname(url)
if ip == url:
return True
else:
return False
except:
return False

from tldextract import tldextract

def parse_url(url):
parsed_url = urlparse(url)
return parsed_url.scheme + "://" + parsed_url.netloc

def get_ip(url):
parsed_url = urlparse(url)
if ":" in parsed_url.netloc:
return parsed_url.netloc.split(":")[0]
return parsed_url.netloc

def save_scan_target(results):
print(
colorize("[*] 结果去重中...", "green"))
result_set = set()
with open('target.txt', 'w', encoding='utf-8') as file:
for target in results:
print(
colorize("[*] 当前去重检测项: {}".format(target), "green"))
if is_ip(target):
ip = get_ip(target)
if ip not in result_set:
# print('ip '+ip)
# print(parse_url(target))
file.write(target+"\n")
result_set.add(ip)
else:
url1 = tldextract.extract(target)
host = url1.domain + "." + url1.suffix
if host not in result_set:
# print('host '+host)
# print(parse_url(target))
file.write(target+"\n")
result_set.add(host)



if __name__ == "__main__":
source_key_1='app="APACHE-Tomcat" && icon_hash="-656811182"'
# source_key = '"tomcat" && icon_hash="-656811182"'
result = get_base_results(source_key_1,100)
# print(result)
result_pool = init_result_pool(result)
results = get_results(source_key_1,result_pool)
save_scan_target(results)
# print(results)
# print(result)
# print(len(result))


Loading

0 comments on commit dcd1001

Please sign in to comment.