-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsaas_enum.py
More file actions
537 lines (481 loc) · 20.1 KB
/
saas_enum.py
File metadata and controls
537 lines (481 loc) · 20.1 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
import concurrent.futures
import argparse
import os
import json
import logging
import csv
import requests
from datetime import datetime
from rich.table import Table
from rich.progress import Progress, SpinnerColumn, TextColumn
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from modules.dns_utils import (
load_dns_integrations,
dns_check,
check_dns_providers,
)
from modules.reporting import display_results, console
# Suppress only the single InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
# Set up logging
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
def run_checks(
short_name,
providers=None,
max_workers=10,
output_json=False,
verbose=False,
output_file=None,
format="text",
timeout=20,
):
"""
Run checks for the given short_name against all or specified providers.
"""
results = []
# Load the DNS integrations
DNS_INTEGRATIONS = load_dns_integrations()
# If specific providers are specified, filter the integrations
integrations_to_check = DNS_INTEGRATIONS
if providers:
integrations_to_check = {
k: v for k, v in DNS_INTEGRATIONS.items() if k in providers
}
if not integrations_to_check:
console.print("[bold red]Error:[/bold red] No matching providers found.")
return []
with Progress(
SpinnerColumn(),
TextColumn("[bold blue]{task.description}[/bold blue]"),
console=console,
transient=True,
) as progress:
task = progress.add_task(
f"Checking {len(integrations_to_check)} providers for '{short_name}'...",
total=len(integrations_to_check),
)
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
# Create future to provider mapping
futures_dict = {}
for provider, domain_template in integrations_to_check.items():
future = executor.submit(
dns_check, provider, domain_template, short_name, timeout
)
futures_dict[future] = provider
# Use wait() instead of as_completed() for better control
remaining_futures = set(futures_dict.keys())
while remaining_futures:
# Wait for some futures to complete, with a reasonable timeout
done, not_done = concurrent.futures.wait(
remaining_futures,
timeout=10.0, # Wait up to 10 seconds for completions
return_when=concurrent.futures.FIRST_COMPLETED,
)
# Process completed futures
for future in done:
provider = futures_dict[future]
try:
result = future.result(
timeout=0.1
) # Should be immediate since it's done
results.append(result)
except concurrent.futures.TimeoutError:
logger.warning(
f"Unexpected timeout getting result for {provider}"
)
timeout_result = {
"provider": provider,
"hostname": integrations_to_check[provider].replace(
"SHORT_NAME", short_name
),
"status": "timeout",
"message": f"Result retrieval timed out",
"dns_result": None,
"web_check": {
"performed": False,
"details": "Result timeout",
},
}
results.append(timeout_result)
except Exception as e:
logger.error(
f"Error processing result for {provider}: {str(e)}"
)
error_result = {
"provider": provider,
"hostname": integrations_to_check[provider].replace(
"SHORT_NAME", short_name
),
"status": "error",
"message": f"Error: {str(e)}",
"dns_result": None,
"web_check": {
"performed": False,
"details": f"Error: {str(e)}",
},
}
results.append(error_result)
progress.update(task, advance=1)
# Remove completed futures from remaining set
remaining_futures -= done
# If no futures completed in this round and we still have some left,
# check if any are taking too long
if not done and not_done:
logger.warning(
f"No futures completed in this round. {len(not_done)} still running."
)
# Force timeout on futures that have been running too long
for future in list(not_done):
try:
# Try to get result with very short timeout
result = future.result(timeout=0.1)
provider = futures_dict[future]
results.append(result)
progress.update(task, advance=1)
remaining_futures.discard(future)
except concurrent.futures.TimeoutError:
# Still running, let it continue
continue
except Exception as e:
# Handle other errors
provider = futures_dict[future]
logger.error(f"Error with future for {provider}: {str(e)}")
error_result = {
"provider": provider,
"hostname": integrations_to_check[provider].replace(
"SHORT_NAME", short_name
),
"status": "error",
"message": f"Future error: {str(e)}",
"dns_result": None,
"web_check": {
"performed": False,
"details": f"Future error: {str(e)}",
},
}
results.append(error_result)
progress.update(task, advance=1)
remaining_futures.discard(future)
# This should not happen anymore, but just in case
if remaining_futures:
logger.warning(
f"Forcing completion for {len(remaining_futures)} remaining futures"
)
for future in remaining_futures:
provider = futures_dict[future]
future.cancel()
timeout_result = {
"provider": provider,
"hostname": integrations_to_check[provider].replace(
"SHORT_NAME", short_name
),
"status": "cancelled",
"message": f"Future was cancelled",
"dns_result": None,
"web_check": {"performed": False, "details": "Cancelled"},
}
results.append(timeout_result)
progress.update(task, advance=1)
# Sort results by status priority
def sort_key(x):
status_priority = {
"confirmed_valid": 0,
"confirmed_via_web": 1,
"url_only_match": 2,
"valid_dns": 3,
"dns_only_match": 4,
"wildcard_match": 5,
"no_record": 6,
"timeout": 7,
"error": 8,
}
priority = status_priority.get(x["status"], 9)
return priority
results.sort(key=sort_key)
# Handle different output formats
if format == "json" or output_json:
if output_file:
with open(output_file, "w") as f:
json.dump(results, f, indent=2)
console.print(f"[green]Results saved to {output_file}[/green]")
else:
print(json.dumps(results, indent=2))
elif format == "csv" and output_file:
with open(output_file, "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["Provider", "Hostname", "Status", "URL", "Details"])
for result in results:
writer.writerow(
[
result["provider"],
result["hostname"],
result["status"],
result["web_check"].get("final_url", ""),
result["web_check"].get("details", ""),
]
)
console.print(f"[green]Results saved to {output_file}[/green]")
else:
# Display results in console with rich formatting
display_results(results, short_name, verbose)
# Save to text file if specified
if output_file and format == "text":
with open(output_file, "w") as f:
f.write(f"SaaS Provider Detection Results for '{short_name}'\n")
f.write(
f"Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n"
)
confirmed_count = sum(
1
for r in results
if r["status"] in ["confirmed_valid", "confirmed_via_web"]
)
possible_count = sum(
1
for r in results
if r["status"] in ["url_only_match", "valid_dns", "dns_only_match"]
)
f.write(f"Summary:\n")
f.write(f" Confirmed integrations: {confirmed_count}\n")
f.write(f" Possible integrations: {possible_count}\n")
f.write(f" Total checked: {len(results)}\n\n")
f.write("Detailed Results:\n")
for result in results:
status = result["status"]
if status in ["confirmed_valid", "confirmed_via_web"]:
prefix = "[+]"
elif status in ["url_only_match", "valid_dns", "dns_only_match"]:
prefix = "[?]"
elif status == "wildcard_match":
prefix = "[!]"
else:
if not verbose:
continue
prefix = "[-]"
f.write(
f"{prefix} {result['provider']}: {result['hostname']} - {result['message']}\n"
)
if verbose or status in [
"confirmed_valid",
"confirmed_via_web",
"url_only_match",
"dns_only_match",
]:
if result["web_check"]["performed"]:
f.write(
f" URL: {result['web_check']['url_checked']} → {result['web_check']['final_url']}\n"
)
if result["web_check"]["page_title"]:
f.write(
f" Page Title: {result['web_check']['page_title']}\n"
)
if result["web_check"]["details"]:
f.write(
f" Details: {result['web_check']['details']}\n"
)
if result["web_check"].get("match_strength"):
f.write(
f" Match Strength: {result['web_check']['match_strength']}\n"
)
if result["web_check"]["text_snippets"]:
f.write(" Text Snippets:\n")
for i, snippet in enumerate(
result["web_check"]["text_snippets"][:3], 1
):
f.write(f" {i}. {snippet}\n")
f.write("\n")
console.print(f"[green]Results saved to {output_file}[/green]")
return results
def main():
parser = argparse.ArgumentParser(
description="Enhanced SaaS Provider Detection Tool"
)
parser.add_argument("-i", "--input", help="SHORT_NAME to use in lookup")
parser.add_argument(
"-j", "--json", action="store_true", help="Output results in JSON format"
)
parser.add_argument(
"-v",
"--verbose",
action="store_true",
help="Show detailed output for all checks",
)
parser.add_argument(
"-p", "--providers", help="Comma-separated list of specific providers to check"
)
parser.add_argument(
"-w",
"--workers",
type=int,
default=10,
help="Maximum number of concurrent workers",
)
parser.add_argument(
"-l",
"--list-providers",
action="store_true",
help="List all available providers and exit",
)
parser.add_argument("-o", "--output", help="Output file to save results")
parser.add_argument(
"-f",
"--format",
choices=["text", "json", "csv"],
default="text",
help="Output format",
)
parser.add_argument(
"-b",
"--batch",
help="File containing multiple SHORT_NAMEs to check (one per line)",
)
parser.add_argument(
"-c",
"--check-providers",
action="store_true",
help="Check providers for wildcard DNS",
)
parser.add_argument(
"-y",
"--yaml",
default="dns_integrations.yaml",
help="Path to YAML file with provider definitions",
)
parser.add_argument(
"-t",
"--timeout",
type=int,
default=20,
help="Timeout in seconds for each provider check",
)
args = parser.parse_args()
# Load DNS integrations
DNS_INTEGRATIONS = load_dns_integrations(args.yaml)
if args.list_providers:
console.print("[bold]Available providers:[/bold]")
table = Table()
table.add_column("#", style="cyan")
table.add_column("Provider", style="green")
table.add_column("Domain Template", style="blue")
for idx, (provider, template) in enumerate(sorted(DNS_INTEGRATIONS.items()), 1):
table.add_row(str(idx), provider, template)
console.print(table)
return
# Check providers for wildcard DNS
if args.check_providers:
check_dns_providers(args.yaml)
return
# Batch processing
if args.batch:
if not os.path.exists(args.batch):
console.print(
f"[bold red]Error:[/bold red] Batch file not found: {args.batch}"
)
return
with open(args.batch, "r") as f:
short_names = [line.strip() for line in f if line.strip()]
console.print(
f"[bold]Processing {len(short_names)} company names from batch file...[/bold]"
)
batch_results = {}
for name in short_names:
console.print(f"\n[bold]Checking: {name}[/bold]")
results = run_checks(
short_name=name,
providers=(
[p.strip() for p in args.providers.split(",")]
if args.providers
else None
),
max_workers=args.workers,
output_json=False,
verbose=args.verbose,
timeout=args.timeout,
)
batch_results[name] = results
# Save batch results if output file specified
if args.output:
if args.format == "json":
with open(args.output, "w") as f:
json.dump(batch_results, f, indent=2)
elif args.format == "csv":
with open(args.output, "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(
["Company", "Provider", "Hostname", "Status", "URL"]
)
for company, results in batch_results.items():
for result in results:
writer.writerow(
[
company,
result["provider"],
result["hostname"],
result["status"],
result["web_check"].get("final_url", ""),
]
)
else:
with open(args.output, "w") as f:
f.write(f"SaaS Provider Detection Batch Results\n")
f.write(
f"Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n"
)
for company, results in batch_results.items():
f.write(f"== Results for '{company}' ==\n")
confirmed_count = sum(
1
for r in results
if r["status"] in ["confirmed_valid", "confirmed_via_web"]
)
possible_count = sum(
1
for r in results
if r["status"] in ["valid_dns", "dns_only_match"]
)
f.write(f" Confirmed integrations: {confirmed_count}\n")
f.write(f" Possible integrations: {possible_count}\n")
f.write(f" Total checked: {len(results)}\n\n")
console.print(f"[green]Batch results saved to {args.output}[/green]")
return
# Single company check
if args.input:
short_name = args.input.strip()
providers = None
if args.providers:
input_providers = [p.strip().lower() for p in args.providers.split(",")]
# Create a case-insensitive mapping of available providers
lower_provider_map = {k.lower(): k for k in DNS_INTEGRATIONS}
unknown = [p for p in input_providers if p not in lower_provider_map]
if unknown:
console.print(
f"[bold yellow]Warning:[/bold yellow] Unknown providers: {', '.join(unknown)}"
)
console.print("Use --list-providers to see all available providers.")
# Filter DNS_INTEGRATIONS with matched original casing
matched_providers = [
lower_provider_map[p]
for p in input_providers
if p in lower_provider_map
]
providers = matched_providers
run_checks(
short_name=short_name,
providers=providers,
max_workers=args.workers,
output_json=args.json,
verbose=args.verbose,
output_file=args.output,
format=args.format,
timeout=args.timeout,
)
else:
console.print(
"[bold yellow]Error:[/bold yellow] No input provided. Use --input or --batch to specify company names to check."
)
console.print("Use --help for more information.")
if __name__ == "__main__":
main()