theHarvester批量域名处理:CSV导入与结果合并技巧
你是否在处理多个域名的情报收集时感到效率低下?手动逐个输入域名、等待结果、再整理数据的过程是否占用了你大量时间?本文将教你如何利用theHarvester实现批量域名处理,通过CSV文件导入多个目标,并将分散的结果高效合并,让你的OSINT(开源情报)收集工作事半功倍。读完本文后,你将能够:掌握CSV批量导入方法、配置多引擎并行扫描、自动合并去重结果、导出结构化报告。
准备工作:环境与文件结构
在开始批量处理前,确保你已正确安装theHarvester并熟悉基本操作。项目核心文件结构如下:
- 主程序入口:theHarvester.py
- 批量处理逻辑:theHarvester/main.py
- 配置文件目录:theHarvester/data/
- 结果存储模块:theHarvester/lib/stash.py
安装与依赖检查
通过以下命令克隆仓库并安装依赖:
git clone https://gitcode.com/GitHub_Trending/th/theHarvester
cd theHarvester
pip install -r requirements.txt
CSV批量导入实现方案
theHarvester原生支持单域名扫描,但批量处理需要通过脚本扩展。我们将创建一个Python脚本,读取CSV文件中的域名列表,并为每个域名调用theHarvester的扫描引擎。
1. 准备CSV文件
创建domains.csv文件,格式如下(确保第一行为表头"domain"):
domain
example.com
target.com
test-domain.org
2. 批量扫描脚本
创建batch_scan.py,实现CSV导入与批量扫描:
import csv
import subprocess
import os
from theHarvester.__main__ import start
def batch_process(csv_path, output_dir="results"):
# 创建结果目录
os.makedirs(output_dir, exist_ok=True)
with open(csv_path, 'r') as f:
reader = csv.DictReader(f)
for row in reader:
domain = row['domain'].strip()
print(f"[*] 开始扫描: {domain}")
# 调用theHarvester主函数
result = subprocess.run(
["python3", "theHarvester.py",
"-d", domain,
"-b", "all",
"-l", "500",
"-f", f"{output_dir}/{domain}"],
capture_output=True,
text=True
)
if result.returncode == 0:
print(f"[+] {domain} 扫描完成,结果保存至 {output_dir}/{domain}")
else:
print(f"[-] {domain} 扫描失败: {result.stderr}")
if __name__ == "__main__":
batch_process("domains.csv")
多引擎并行扫描配置
为提高批量处理效率,可配置多引擎并行扫描。通过修改theHarvester/main.py中的参数,实现扫描任务的并行化。
支持的扫描引擎列表
theHarvester支持多种OSINT数据源,完整列表可通过以下代码获取:
from theHarvester.lib.core import Core
print("支持的扫描引擎:", Core.get_supportedengines())
主要引擎包括:
- 证书透明度:crtsh、certspotter
- 搜索引擎:baidu、brave、duckduckgo
- DNS数据库:dnsdumpster、rapiddns
- 威胁情报:virustotal、otx、threatcrowd
并行扫描配置
修改批量脚本,添加多进程支持:
from multiprocessing import Pool
def scan_domain(domain):
# 单个域名扫描逻辑
# ...
if __name__ == "__main__":
with open("domains.csv", 'r') as f:
domains = [row['domain'] for row in csv.DictReader(f)]
# 使用4个进程并行扫描
with Pool(processes=4) as pool:
pool.map(scan_domain, domains)
结果合并与去重技巧
多个域名的扫描结果分散在不同文件中,需要合并去重以生成统一报告。主要步骤包括:解析JSON结果、合并同类数据、去重处理、导出为CSV/HTML。
结果文件格式解析
theHarvester默认生成JSON和XML格式结果,以example.com.json为例,结构如下:
{
"domain": "example.com",
"hosts": ["sub1.example.com", "sub2.example.com"],
"emails": ["contact@example.com"],
"ips": ["93.184.216.34"]
}
合并脚本实现
创建merge_results.py,合并所有JSON结果:
import json
import os
from collections import defaultdict
def merge_results(input_dir="results", output_file="merged_results.json"):
merged = defaultdict(lambda: {
"hosts": set(),
"emails": set(),
"ips": set()
})
for filename in os.listdir(input_dir):
if filename.endswith(".json"):
domain = filename.replace(".json", "")
with open(f"{input_dir}/{filename}", 'r') as f:
data = json.load(f)
# 合并数据并去重
merged[domain]["hosts"].update(data.get("hosts", []))
merged[domain]["emails"].update(data.get("emails", []))
merged[domain]["ips"].update(data.get("ips", []))
# 转换集合为列表
for domain in merged:
merged[domain]["hosts"] = list(merged[domain]["hosts"])
merged[domain]["emails"] = list(merged[domain]["emails"])
merged[domain]["ips"] = list(merged[domain]["ips"])
# 保存合并结果
with open(output_file, 'w') as f:
json.dump(merged, f, indent=2)
print(f"[+] 结果合并完成,共处理 {len(merged)} 个域名,保存至 {output_file}")
if __name__ == "__main__":
merge_results()
可视化报告生成
合并后的JSON结果可通过Python转换为直观的HTML报告,便于分析和分享。
HTML报告生成脚本
import json
from jinja2 import Template
def generate_html_report(json_path="merged_results.json", html_path="report.html"):
with open(json_path, 'r') as f:
data = json.load(f)
# HTML模板
template = Template("""
<!DOCTYPE html>
<html>
<head>
<title>批量域名扫描报告</title>
<style>
table { width: 100%; border-collapse: collapse; }
th, td { border: 1px solid #ddd; padding: 8px; }
th { background-color: #f2f2f2; }
</style>
</head>
<body>
<h1>批量域名OSINT扫描报告</h1>
{% for domain, info in data.items() %}
<h2>{{ domain }}</h2>
<h3>子域名 ({{ info.hosts|length }})</h3>
<ul>{% for host in info.hosts %}<li>{{ host }}</li>{% endfor %}</ul>
<h3>邮箱 ({{ info.emails|length }})</h3>
<ul>{% for email in info.emails %}<li>{{ email }}</li>{% endfor %}</ul>
<h3>IP地址 ({{ info.ips|length }})</h3>
<ul>{% for ip in info.ips %}<li>{{ ip }}</li>{% endfor %}</ul>
{% endfor %}
</body>
</html>
""")
html = template.render(data=data)
with open(html_path, 'w') as f:
f.write(html)
print(f"[+] HTML报告生成完成: {html_path}")
if __name__ == "__main__":
generate_html_report()
常见问题解决方案
1. 扫描速度慢
- 解决方案:减少单域名扫描引擎数量,使用
-b参数指定关键引擎:python3 theHarvester.py -d example.com -b crtsh,rapiddns,virustotal
2. API密钥缺失
部分引擎需要API密钥,配置文件位于theHarvester/data/api-keys.yaml,添加所需密钥即可:
apikeys:
virustotal:
key: "your_api_key_here"
hunter:
key: "your_api_key_here"
3. 结果去重不彻底
修改合并脚本,使用更严格的去重逻辑:
# 对域名进行规范化处理
def normalize_host(host):
return host.lower().rstrip('.')
unique_hosts = {normalize_host(host) for host in all_hosts}
总结与进阶技巧
通过本文介绍的方法,你已掌握theHarvester的批量域名处理能力。进阶使用可考虑:
- 定时任务:结合crontab实现定期批量扫描
- 结果监控:使用ELK栈对合并结果进行可视化分析
- 自定义引擎:开发新的扫描模块,扩展theHarvester/discovery/目录下的引擎类
完整项目文档请参考README.md,更多高级用法可查看项目测试用例tests/。
希望本文能帮助你更高效地进行域名情报收集,让OSINT工作事半功倍!如果你有其他批量处理技巧,欢迎在项目Issues中分享。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考




