SRC 资产收集流水线脚本骨架:scope/roots 隔离 + 时间戳扫描快照 + 分阶段命令链与文件交接
pentest/recon · verified
一句话结论
把信息收集拆成“每目标一个 scope 目录(roots.txt)+ 每次运行一个时间戳 scans 目录 + 固定命名的中间产物文件”,再用一条 bash 流水线串起“被动/主动子域 → 解析与泛解析过滤 → 端口扫描 → vhost 感知 HTTP 探测 → 爬虫 → JS 留存”,即可得到可复现、可对比、可增量重跑的资产收集流程。
原理
- 目录约定解决可追溯性:
scope/<id>/roots.txt保存授权范围内的根域名;每次执行./scan.sh <id>以<id>-<Unix时间戳>新建scans/子目录并把 roots.txt 复制进去。这样每次扫描是一个不可变快照,便于事后对比某一轮新增了哪些资产(与定时子域监控卡的 diff 思路一致)。 - 统一命名解决工具衔接:每个阶段只认上一阶段的文件,例如
subs.txt(候选子域)→resolved.txt(puredns 解析、含泛解析过滤)→dns.json/ips.txt(dnsx 的 A 记录)→nmap.xml(开放端口)→hostport.txt/http.json/http.txt(tew+httpx 的存活 HTTP)→crawl.txt(gospider 抓到的 URL)→js/(保存的 JS 文件)。文件名固定使得任一阶段可单独重跑或用anew去重追加。 - 分阶段而非一个大脚本:子域枚举(subfinder 被动 + shuffledns 主动爆破 + haktrails 被动)、解析(puredns 带通配符检测)、端口(nmap top 3000)、HTTP(tew --vhost + httpx)、爬虫(gospider)、静态资源(httpx -sr -srd)各自可替换/跳过;出错只影响一个阶段,产物仍在。
- 脚本骨架自身要做的事:检查 scope 路径存在(不存在直接退出)、创建带时间戳的扫描目录、复制 roots.txt、记录开始时间并在结束时输出耗时;目标 ID 作为唯一参数,避免误扫。
- 这套结构成立的原因:信息收集的瓶颈不是单个工具,而是“结果如何落盘与复用”;固定目录与文件 contract 让工具链可以任意组合、回放与对比。
触发条件
- 已在授权 SRC/攻防演练范围内,root 域名清单已确定。
- 本机具备 subfinder、shuffledns、puredns、dnsx、nmap、tew、httpx、gospider、anew、jq 等工具与可用的解析器/字典。
- 允许对目标做 DNS 枚举与端口/HTTP 探测(注意速率与授权边界)。
利用步骤
- 初始化:
mkdir -p scope scans lists,创建scope/<id>/roots.txt写入根域名;下载字典与 resolver 列表到lists/。 - 编写
scan.sh骨架:参数取 id;校验scope/<id>;timestamp=$(date +%s);scan_path=scans/<id>-$timestamp;mkdir -p后在scan_path内工作;退出时换算并打印耗时。 - 子域阶段:
cat roots.txt | haktrails subdomains | anew subs.txt、subfinder | anew subs.txt、shuffledns -w lists/pry-dns.txt -r lists/resolvers.txt | anew subs.txt。 - 解析阶段:
puredns resolve subs.txt -r lists/resolvers.txt -w resolved.txt(自带通配符检测与信任 resolver 校验);dnsx -l resolved.txt -json -o dns.json,jq -r '.a?[]?' dns.json | anew ips.txt。 - 端口与 HTTP:
nmap -T4 -iL ips.txt --top-ports 3000 -n --open -oX nmap.xml;tew -x nmap.xml -dnsx dns.json --vhost -o hostport.txt | httpx -sr -srd responses -json -o http.json;从http.json用 jq 提取 URL、去掉 :80/:443 后写入http.txt。 - 爬虫与 JS:
gospider -S http.txt --json | grep '{' | jq -r '.output?' | tee crawl.txt;cat crawl.txt | grep "\.js" | httpx -sr -srd js。 - 复盘:每轮扫描目录就是一份资产快照;对
subs.txt/http.txt做跨轮 diff 找新增资产,对responses/、js/做离线 grep/trufflehog。
PoC
#!/bin/bash
id="$1"
ppath="$(pwd)"
scope_path="$ppath/scope/$id"
timestamp="$(date +%s)"
scan_path="$ppath/scans/$id-$timestamp"
if [ ! -d "$scope_path" ]; then echo "Path doesn't exist"; exit 1; fi
mkdir -p "$scan_path"
cd "$scan_path"
cp -v "$scope_path/roots.txt" roots.txt
echo "Starting scan against roots:"; cat roots.txt
## DNS Enumeration
cat roots.txt | haktrails subdomains | anew subs.txt
cat roots.txt | subfinder | anew subs.txt
cat roots.txt | shuffledns -w "$ppath/lists/pry-dns.txt" -r "$ppath/lists/resolvers.txt" | anew subs.txt
## Resolution + wildcard filtering
puredns resolve subs.txt -r "$ppath/lists/resolvers.txt" -w resolved.txt
dnsx -l resolved.txt -json -o dns.json
jq -r '.a?[]?' dns.json | anew ips.txt
## Port scan + vhost-aware HTTP
nmap -T4 -iL ips.txt --top-ports 3000 -n --open -oX nmap.xml
tew -x nmap.xml -dnsx dns.json --vhost -o hostport.txt \
| httpx -sr -srd responses -json -o http.json
jq -r '.url' http.json | sed -e 's/:80$//g' -e 's/:443$//g' | sort -u > http.txt
## Crawling + JS capture
gospider -S http.txt --json | grep '{' | jq -r '.output?' | tee crawl.txt
cat crawl.txt | grep "\.js" | httpx -sr -srd js
end_time=$(date +%s); echo "Scan $id took $((end_time - timestamp)) seconds"
变体与关联
- 关联 [[pentest-recon-tew-vhost-nmap-dnsx-hostport-join]]:流水线中“nmap 结果还原为 hostname:端口”的关键步骤单独展开。
- 关联 [[pentest-recon-httpx-store-response-js-bulk-capture]]:流水线末端的响应/JS 批量留存技巧单独展开。
- 关联 [[pentest-recon-subdomain-enum-false-positive-hardening]]、[[pentest-recon-subdomain-alive-validation-wildcard-vhost]]:子域阶段为什么必须用 puredns 通配符过滤与严格 A 记录验证,以及泛解析/虚拟主机的甄别。
- 关联 [[pentest-recon-scheduled-subdomain-monitor-new-asset-diff]]:基于每轮快照做新增资产 diff 的持续监控。
- 变体:把各阶段改为函数或 Makefile/任务编排器;把 puredns 换成 massdns+自定义过滤;把 gospider 换成 hakrawler/katana;把
anew换成sort -u。 - 边界:工具与字典需持续维护;泛解析、CDN、共享 IP 会让原始结果失真,必须依赖解析阶段的过滤;扫描速率应受控,避免影响业务。
检测与防御
- 只对授权范围内 root 域名运行,scope/roots.txt 即授权边界;不要把第三方域(CNAME 残留)纳入扫描。
- 速率控制:DNS 爆破与 nmap top 端口都会产生明显流量,必要时限速、分时段执行。
- 蓝队侧:监控短时间内的子域枚举 DNS 洪水、同源大量不同 Host 的 HTTP 探测、以及异常的 JS 批量拉取;对默认 vhost 统一返回 403 可减少资产暴露面。
- 资产管理者应定期自查公开 DNS 记录与端口暴露,避免“未被发现的 vhost”成为影子资产。