-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubdiscover.sh
More file actions
executable file
·290 lines (229 loc) · 6.93 KB
/
subdiscover.sh
File metadata and controls
executable file
·290 lines (229 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env bash
set -euo pipefail
# subdiscover.sh - Passive subdomain discovery from multiple OSINT sources
# Outputs a wordlist compatible with: domainips.sh -w <output>
# Usage: ./subdiscover.sh -d <domain> [-o <output-file>]
readonly VERSION="1.0.0"
readonly TMPDIR_BASE="${TMPDIR:-/tmp}"
readonly WORK_DIR=$(mktemp -d "${TMPDIR_BASE}/subdiscover.XXXXXX")
trap 'rm -rf "$WORK_DIR"' EXIT
readonly RAW_FILE="${WORK_DIR}/raw.txt"
touch "$RAW_FILE"
usage() {
cat <<EOF
subdiscover v${VERSION} - Passive Subdomain Discovery
Discovers subdomains from public OSINT sources. Output can be
piped directly into domainips.sh via the -w parameter.
Usage: $(basename "$0") -d <domain> [-o <output-file>]
Options:
-d <domain> Target domain (required)
-o <file> Output file (default: stdout)
-q Quiet mode (no progress output)
-h Show this help
Examples:
$(basename "$0") -d example.com -o subs.txt
./domainips.sh -d example.com -w subs.txt
# Or piped directly:
$(basename "$0") -d example.com -o subs.txt && ./domainips.sh -d example.com -w subs.txt
EOF
exit 0
}
log() {
[[ "${QUIET}" == "true" ]] && return
echo -e "\033[1;34m[*]\033[0m $1" >&2
}
warn() {
echo -e "\033[1;33m[!]\033[0m $1" >&2
}
success() {
echo -e "\033[1;32m[+]\033[0m $1" >&2
}
error() {
echo -e "\033[1;31m[-]\033[0m $1" >&2
exit 1
}
# Extract subdomains matching the target domain from raw text
extract_subs() {
local domain="$1"
local escaped
escaped=$(echo "$domain" | sed 's/\./\\./g')
# Match any word that ends with .domain.tld or is domain.tld itself
grep -oiE "([a-zA-Z0-9]([a-zA-Z0-9_-]*[a-zA-Z0-9])?\.)*${escaped}" \
| tr '[:upper:]' '[:lower:]' \
| sed 's/\.$//' \
| sort -u || true
}
# --- Sources ---
source_crtsh() {
local domain="$1"
log " crt.sh (Certificate Transparency)..."
local result
result=$(curl -s --max-time 30 \
"https://crt.sh/?q=%25.${domain}&output=json" 2>/dev/null || true)
if [[ -z "$result" || "$result" == "null" ]]; then
warn " crt.sh: no results or unreachable"
return
fi
local count
count=$(echo "$result" \
| jq -r '.[].name_value' 2>/dev/null \
| extract_subs "$domain" \
| tee -a "$RAW_FILE" \
| wc -l | tr -d ' ')
success " crt.sh: ${count} entries"
}
source_alienvault() {
local domain="$1"
log " AlienVault OTX..."
local result
result=$(curl -s --max-time 20 \
"https://otx.alienvault.com/api/v1/indicators/domain/${domain}/passive_dns" \
2>/dev/null || true)
if [[ -z "$result" ]]; then
warn " AlienVault: unreachable"
return
fi
local count
count=$(echo "$result" \
| jq -r '.passive_dns[]?.hostname' 2>/dev/null \
| extract_subs "$domain" \
| tee -a "$RAW_FILE" \
| wc -l | tr -d ' ')
success " AlienVault OTX: ${count} entries"
}
source_hackertarget() {
local domain="$1"
log " HackerTarget..."
local result
result=$(curl -s --max-time 20 \
"https://api.hackertarget.com/hostsearch/?q=${domain}" \
2>/dev/null || true)
if [[ -z "$result" || "$result" == *"error"* ]]; then
warn " HackerTarget: no results or rate-limited"
return
fi
local count
count=$(echo "$result" \
| cut -d',' -f1 \
| extract_subs "$domain" \
| tee -a "$RAW_FILE" \
| wc -l | tr -d ' ')
success " HackerTarget: ${count} entries"
}
source_rapiddns() {
local domain="$1"
log " RapidDNS..."
local result
result=$(curl -s --max-time 20 \
"https://rapiddns.io/subdomain/${domain}?full=1" \
2>/dev/null || true)
if [[ -z "$result" ]]; then
warn " RapidDNS: unreachable"
return
fi
local count
count=$(echo "$result" \
| extract_subs "$domain" \
| tee -a "$RAW_FILE" \
| wc -l | tr -d ' ')
success " RapidDNS: ${count} entries"
}
source_wayback() {
local domain="$1"
log " Wayback Machine (web.archive.org)..."
local result
result=$(curl -s --max-time 30 \
"https://web.archive.org/cdx/search/cdx?url=*.${domain}/*&output=text&fl=original&collapse=urlkey&limit=5000" \
2>/dev/null || true)
if [[ -z "$result" ]]; then
warn " Wayback Machine: no results or unreachable"
return
fi
local count
count=$(echo "$result" \
| extract_subs "$domain" \
| tee -a "$RAW_FILE" \
| wc -l | tr -d ' ')
success " Wayback Machine: ${count} entries"
}
source_urlscan() {
local domain="$1"
log " urlscan.io..."
local result
result=$(curl -s --max-time 20 \
"https://urlscan.io/api/v1/search/?q=domain:${domain}&size=1000" \
2>/dev/null || true)
if [[ -z "$result" ]]; then
warn " urlscan.io: unreachable"
return
fi
local count
count=$(echo "$result" \
| jq -r '.results[]?.page?.domain' 2>/dev/null \
| extract_subs "$domain" \
| tee -a "$RAW_FILE" \
| wc -l | tr -d ' ')
success " urlscan.io: ${count} entries"
}
source_threatminer() {
local domain="$1"
log " ThreatMiner..."
local result
result=$(curl -s --max-time 20 \
"https://api.threatminer.org/v2/domain.php?q=${domain}&rt=5" \
2>/dev/null || true)
if [[ -z "$result" ]]; then
warn " ThreatMiner: unreachable"
return
fi
local count
count=$(echo "$result" \
| jq -r '.results[]?' 2>/dev/null \
| extract_subs "$domain" \
| tee -a "$RAW_FILE" \
| wc -l | tr -d ' ')
success " ThreatMiner: ${count} entries"
}
# --- Main ---
DOMAIN=""
OUTPUT_FILE=""
QUIET=false
while getopts "d:o:qh" opt; do
case "$opt" in
d) DOMAIN="$OPTARG" ;;
o) OUTPUT_FILE="$OPTARG" ;;
q) QUIET=true ;;
h) usage ;;
*) usage ;;
esac
done
[[ -z "$DOMAIN" ]] && error "Domain is required. Use -d <domain>"
if ! echo "$DOMAIN" | grep -qE '^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)+$'; then
error "Invalid domain format: ${DOMAIN}"
fi
log "Passive subdomain discovery for: ${DOMAIN}"
log "========================================="
log "Querying OSINT sources..."
# Query all sources
source_crtsh "$DOMAIN"
source_alienvault "$DOMAIN"
source_hackertarget "$DOMAIN"
source_rapiddns "$DOMAIN"
source_wayback "$DOMAIN"
source_urlscan "$DOMAIN"
source_threatminer "$DOMAIN"
# Deduplicate and sort
FINAL=$(sort -u "$RAW_FILE")
TOTAL=$(echo "$FINAL" | grep -c '.' || true)
log "========================================="
success "Total unique subdomains found: ${TOTAL}"
if [[ -n "$OUTPUT_FILE" ]]; then
echo "$FINAL" > "$OUTPUT_FILE"
success "Saved to: ${OUTPUT_FILE}"
log ""
log "Next step:"
log " ./domainips.sh -d ${DOMAIN} -w ${OUTPUT_FILE} -o results.csv"
else
# Output to stdout
echo "$FINAL"
fi