-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubrecon.py
More file actions
572 lines (442 loc) · 18.8 KB
/
Copy pathsubrecon.py
File metadata and controls
572 lines (442 loc) · 18.8 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#!/usr/bin/env python3
from __future__ import annotations
import argparse
import json
import random
import re
import shutil
import socket
import string
import subprocess
import threading
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
from dataclasses import dataclass, field
from pathlib import Path
from typing import Iterable
from urllib.parse import quote, urlparse
from urllib.request import Request, urlopen
try:
from rich.console import Console
from rich.live import Live
from rich.panel import Panel
from rich.progress import BarColumn, Progress, SpinnerColumn, TextColumn, TimeElapsedColumn
from rich.table import Table
except Exception:
Console = Live = Panel = Progress = SpinnerColumn = TextColumn = BarColumn = TimeElapsedColumn = Table = None
try:
import dns.resolver
except Exception:
dns = None
AUTHOR = "EnCrYpTeD05"
VERSION = "8.1.0"
UA = f"subrecon/{VERSION}"
HOST_RE = re.compile(
r"^(?=.{1,253}$)(?!-)(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}$"
)
HOST_EXTRACT_RE = re.compile(
r"(?:\*\.)?([a-zA-Z0-9](?:[a-zA-Z0-9_-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9_-]{0,61}[a-zA-Z0-9])?)+)"
)
@dataclass
class Counters:
sources_total: int = 0
sources_processed: int = 0
raw_collected: int = 0
normalized: int = 0
unique_subdomains: int = 0
duplicates_removed: int = 0
checked: int = 0
exported_subdomains: int = 0
false_positives: int = 0
started: float = field(default_factory=time.time)
class UI:
def __init__(self) -> None:
self.console = Console() if Console else None
self.progress = None
self.live = None
self.collect_task = None
self.filter_task = None
def start(self, counters: Counters) -> None:
banner = r"""
_____ ____ ______
/ ___/__ __/ __ )_______ / ____/___ ____
\__ \/ / / / __ / ___/ _ \/ / / __ \/ __ \
___/ / /_/ / /_/ / / / __/ /___/ /_/ / / / /
/____/\__,_/_____/_/ \___/\____/\____/_/ /_/
"""
if self.console and Progress:
self.console.print(f"[bold cyan]{banner}[/bold cyan]")
self.console.print(f"[bold red]Created by {AUTHOR}[/bold red]\n")
self.progress = Progress(
SpinnerColumn(),
TextColumn("[bold cyan]{task.description}"),
BarColumn(bar_width=42),
TextColumn("[progress.percentage]{task.percentage:>3.0f}%"),
TimeElapsedColumn(),
)
self.collect_task = self.progress.add_task("Collecting from all sources", total=max(counters.sources_total, 1))
self.filter_task = self.progress.add_task("Filtering clear false positives", total=1)
self.live = Live(self.render(counters), console=self.console, refresh_per_second=8)
self.live.__enter__()
else:
print(f"\033[96m{banner}\033[0m")
print(f"\033[91mCreated by {AUTHOR}\033[0m\n")
def render(self, counters: Counters):
if not Table or not Panel:
return None
table = Table.grid(expand=True)
table.add_column(justify="left")
table.add_column(justify="right")
table.add_row("[bold]Sources Processed[/bold]", f"{counters.sources_processed}/{counters.sources_total}")
table.add_row("[bold]Raw Collected[/bold]", str(counters.raw_collected))
table.add_row("[bold]Normalized[/bold]", str(counters.normalized))
table.add_row("[bold]Unique Subdomains[/bold]", str(counters.unique_subdomains))
table.add_row("[bold]Exported Subdomains[/bold]", str(counters.exported_subdomains))
table.add_row("[bold]Duplicates Removed[/bold]", str(counters.duplicates_removed))
table.add_row("[bold]False Positives[/bold]", str(counters.false_positives))
table.add_row("[bold]Elapsed Time[/bold]", f"{time.time() - counters.started:.1f}s")
outer = Table.grid(expand=True)
outer.add_row(self.progress)
outer.add_row(Panel(table, title="Live Counters", border_style="cyan"))
return outer
def update(self, counters: Counters, filter_total: int | None = None) -> None:
if self.progress and self.collect_task is not None:
self.progress.update(self.collect_task, completed=counters.sources_processed)
if self.progress and self.filter_task is not None:
if filter_total is not None:
self.progress.update(self.filter_task, total=max(filter_total, 1))
self.progress.update(self.filter_task, completed=counters.checked)
if self.live:
self.live.update(self.render(counters))
def stop(self) -> None:
if self.live:
self.live.__exit__(None, None, None)
self.live = None
def warn(self, message: str) -> None:
if self.console:
self.console.print(f"[yellow]{message}[/yellow]")
else:
print(f"\033[93m{message}\033[0m")
def internet_online(timeout: float = 5.0) -> bool:
try:
with socket.create_connection(("1.1.1.1", 53), timeout=timeout):
return True
except OSError:
try:
req = Request("https://example.com", headers={"User-Agent": UA})
with urlopen(req, timeout=timeout):
return True
except Exception:
return False
def wait_for_internet(ui: UI) -> None:
if internet_online():
return
ui.warn("Internet unavailable. Waiting to start...")
while not internet_online():
time.sleep(5)
def fetch(url: str, timeout: float) -> str:
req = Request(url, headers={"User-Agent": UA})
with urlopen(req, timeout=timeout) as response:
return response.read().decode("utf-8", errors="replace")
def valid_hostname(hostname: str) -> bool:
hostname = hostname.strip().lower().removesuffix(".")
if not HOST_RE.match(hostname):
return False
return all(0 < len(label) <= 63 for label in hostname.split("."))
def extract_hosts(text: str) -> Iterable[str]:
for match in HOST_EXTRACT_RE.findall(text):
yield match
def normalize(raw: str, domain: str) -> tuple[str | None, str | None]:
original = raw.strip()
if not original:
return None, "empty"
value = original.lower().strip()
if "://" in value:
parsed = urlparse(value)
value = parsed.hostname or value
matches = list(extract_hosts(value))
if matches:
value = matches[0]
value = value.strip().lower()
value = value.removeprefix("*.").removesuffix(".")
value = value.split("/", 1)[0].split(":", 1)[0]
root = domain.lower().removesuffix(".")
if value == root:
return None, f"{original} | root-domain"
if not value.endswith("." + root):
return None, f"{original} | out-of-scope"
if not valid_hostname(value):
return None, f"{original} | invalid-syntax"
return value, None
class Collector:
name = "collector"
def collect(self, domain: str, timeout: float) -> Iterable[str]:
return []
class CrtShCollector(Collector):
name = "crt.sh"
def collect(self, domain: str, timeout: float) -> Iterable[str]:
data = json.loads(fetch(f"https://crt.sh/?q=%25.{quote(domain)}&output=json", timeout))
for row in data:
for name in str(row.get("name_value", "")).splitlines():
yield name
class HackerTargetCollector(Collector):
name = "hackertarget"
def collect(self, domain: str, timeout: float) -> Iterable[str]:
text = fetch(f"https://api.hackertarget.com/hostsearch/?q={quote(domain)}", timeout)
for line in text.splitlines():
yield line.split(",", 1)[0] if "," in line else line
class AlienVaultCollector(Collector):
name = "alienvault-otx"
def collect(self, domain: str, timeout: float) -> Iterable[str]:
url = f"https://otx.alienvault.com/api/v1/indicators/domain/{quote(domain)}/passive_dns"
data = json.loads(fetch(url, timeout))
for item in data.get("passive_dns", []):
if item.get("hostname"):
yield str(item["hostname"])
class UrlscanCollector(Collector):
name = "urlscan"
def collect(self, domain: str, timeout: float) -> Iterable[str]:
url = f"https://urlscan.io/api/v1/search/?q=domain:{quote(domain)}&size=10000"
data = json.loads(fetch(url, timeout))
for item in data.get("results", []):
page = item.get("page", {})
if page.get("domain"):
yield str(page["domain"])
if page.get("url"):
yield str(page["url"])
class RapidDnsCollector(Collector):
name = "rapiddns"
def collect(self, domain: str, timeout: float) -> Iterable[str]:
yield from extract_hosts(fetch(f"https://rapiddns.io/subdomain/{quote(domain)}?full=1", timeout))
class ExternalToolCollector(Collector):
def __init__(self, name: str, variants: list[list[str]], timeout_floor: int) -> None:
self.name = name
self.variants = variants
self.timeout_floor = timeout_floor
def command(self) -> list[str] | None:
for variant in self.variants:
if shutil.which(variant[0]):
return variant
return None
def collect(self, domain: str, timeout: float) -> Iterable[str]:
variant = self.command()
if not variant:
return []
cmd = [part.format(domain=domain) for part in variant]
proc = subprocess.run(
cmd,
capture_output=True,
text=True,
timeout=max(int(timeout), self.timeout_floor),
check=False,
)
for line in (proc.stdout + "\n" + proc.stderr).splitlines():
yield from extract_hosts(line)
def internal_collectors() -> list[Collector]:
return [
CrtShCollector(),
HackerTargetCollector(),
AlienVaultCollector(),
UrlscanCollector(),
RapidDnsCollector(),
]
def external_collectors() -> list[Collector]:
candidates = [
ExternalToolCollector("subfinder", [["subfinder", "-silent", "-d", "{domain}"]], 900),
ExternalToolCollector("amass", [["amass", "enum", "-passive", "-norecursive", "-noalts", "-d", "{domain}"]], 1200),
ExternalToolCollector("assetfinder", [["assetfinder", "--subs-only", "{domain}"]], 600),
ExternalToolCollector(
"sublist3r",
[
["sublist3r", "-d", "{domain}"],
["Sublist3r", "-d", "{domain}"],
["sublist3r.py", "-d", "{domain}"],
],
900,
),
]
return [collector for collector in candidates if collector.command()]
def run_collector(collector: Collector, domain: str, timeout: float) -> tuple[str, list[str]]:
results: list[str] = []
try:
for value in collector.collect(domain, timeout):
if value:
results.append(str(value))
except Exception:
pass
return collector.name, results
def resolve_records(hostname: str, timeout: float) -> tuple[bool, set[str]]:
if dns is not None:
resolver = dns.resolver.Resolver()
resolver.lifetime = timeout
resolver.timeout = timeout
values: set[str] = set()
for record_type in ("A", "AAAA", "CNAME"):
try:
answers = resolver.resolve(hostname, record_type)
for answer in answers:
values.add(str(answer).rstrip("."))
except Exception:
continue
return bool(values), values
socket.setdefaulttimeout(timeout)
try:
values = {item[4][0] for item in socket.getaddrinfo(hostname, None)}
return bool(values), values
except OSError:
return False, set()
def detect_wildcard_values(domain: str, timeout: float) -> set[str]:
wildcard_sets: list[set[str]] = []
for _ in range(5):
random_host = "sr-" + "".join(random.choices(string.ascii_lowercase + string.digits, k=24))
resolved, values = resolve_records(f"{random_host}.{domain}", timeout)
if resolved and values:
wildcard_sets.append(values)
if len(wildcard_sets) < 3:
return set()
common = set.intersection(*wildcard_sets)
if common:
return common
repeated: dict[str, int] = {}
for values in wildcard_sets:
for value in values:
repeated[value] = repeated.get(value, 0) + 1
return {value for value, count in repeated.items() if count >= 4}
def filter_one(hostname: str, wildcard_values: set[str], timeout: float) -> tuple[str, bool, str | None]:
if not valid_hostname(hostname):
return hostname, False, "invalid-syntax"
resolved, values = resolve_records(hostname, timeout)
# Important: DNS failure is NOT treated as false positive.
# Passive sources often contain historical or temporarily unresolved subdomains.
if not resolved:
return hostname, True, None
# Only strong wildcard evidence removes a hostname.
if wildcard_values and values and values.issubset(wildcard_values):
return hostname, False, "wildcard-dns"
return hostname, True, None
def collect_merge_dedupe(
domain: str,
collectors: list[Collector],
counters: Counters,
ui: UI,
timeout: float,
workers: int,
) -> tuple[set[str], set[str]]:
subdomains: set[str] = set()
falsepositives: set[str] = set()
lock = threading.RLock()
counters.sources_total = len(collectors)
ui.update(counters)
with ThreadPoolExecutor(max_workers=max(1, workers)) as pool:
futures = [pool.submit(run_collector, collector, domain, timeout) for collector in collectors]
for future in as_completed(futures):
_, raw_values = future.result()
with lock:
for raw in raw_values:
counters.raw_collected += 1
normalized, fp_reason = normalize(raw, domain)
if fp_reason:
falsepositives.add(fp_reason)
counters.false_positives = len(falsepositives)
continue
if not normalized:
continue
counters.normalized += 1
if normalized in subdomains:
counters.duplicates_removed += 1
else:
subdomains.add(normalized)
counters.sources_processed += 1
counters.unique_subdomains = len(subdomains)
counters.exported_subdomains = len(subdomains)
ui.update(counters)
return subdomains, falsepositives
def filter_all(
subdomains: set[str],
falsepositives: set[str],
domain: str,
counters: Counters,
ui: UI,
timeout: float,
workers: int,
) -> set[str]:
valid: set[str] = set()
wildcard_values = detect_wildcard_values(domain, timeout)
lock = threading.RLock()
ui.update(counters, filter_total=len(subdomains))
with ThreadPoolExecutor(max_workers=max(1, workers)) as pool:
futures = [
pool.submit(filter_one, hostname, wildcard_values, timeout)
for hostname in sorted(subdomains)
]
for future in as_completed(futures):
hostname, ok, reason = future.result()
with lock:
counters.checked += 1
if ok:
valid.add(hostname)
else:
falsepositives.add(f"{hostname} | {reason}")
counters.exported_subdomains = len(valid)
counters.false_positives = len(falsepositives)
ui.update(counters, filter_total=len(subdomains))
return valid
def write_lines(path: Path, lines: Iterable[str]) -> None:
data = sorted(set(line.strip() for line in lines if line and line.strip()))
path.write_text("\n".join(data) + ("\n" if data else ""), encoding="utf-8")
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description="SuBreCon - fast subdomain merger and cleaner.")
parser.add_argument("domain", help="Root domain, example: example.com")
parser.add_argument("--timeout", type=float, default=8.0, help="DNS and source timeout")
parser.add_argument("--collector-workers", type=int, default=9, help="Concurrent source workers")
parser.add_argument("--filter-workers", type=int, default=120, help="Concurrent filter workers")
parser.add_argument("--no-external-tools", action="store_true", help="Disable external tool collection")
return parser
def main() -> int:
args = build_parser().parse_args()
domain = args.domain.strip().lower().removesuffix(".")
collectors = internal_collectors()
if not args.no_external_tools:
collectors.extend(external_collectors())
counters = Counters(sources_total=len(collectors))
ui = UI()
ui.start(counters)
wait_for_internet(ui)
final_subdomains: set[str] = set()
falsepositives: set[str] = set()
try:
merged, falsepositives = collect_merge_dedupe(
domain=domain,
collectors=collectors,
counters=counters,
ui=ui,
timeout=args.timeout,
workers=args.collector_workers,
)
final_subdomains = filter_all(
subdomains=merged,
falsepositives=falsepositives,
domain=domain,
counters=counters,
ui=ui,
timeout=args.timeout,
workers=args.filter_workers,
)
write_lines(Path("subdomains.txt"), final_subdomains)
write_lines(Path("false_positive.txt"), falsepositives)
finally:
ui.stop()
print()
print(f"Domain: {domain}")
print(f"Sources Processed: {counters.sources_processed}/{counters.sources_total}")
print(f"Raw Collected: {counters.raw_collected}")
print(f"Normalized: {counters.normalized}")
print(f"Unique Subdomains: {counters.unique_subdomains}")
print(f"Exported Subdomains: {len(final_subdomains)}")
print(f"Duplicates Removed: {counters.duplicates_removed}")
print(f"False Positives: {len(falsepositives)}")
print("Output File: subdomains.txt")
print("False Positive File: false_positive.txt")
return 0
if __name__ == "__main__":
raise SystemExit(main())