web-analyzer 0.1.9

Enterprise domain security & intelligence platform — WHOIS, DNS, SEO, tech detection, subdomain takeover, API security scanning, and more
Documentation
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
use regex::Regex;
use reqwest::Client;
use scraper::{Html, Selector};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::time::{Duration, Instant};

use crate::payloads;

// ── Result structs ──────────────────────────────────────────────────────────

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiEndpoint {
    pub url: String,
    pub status_code: u16,
    pub api_type: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VulnerabilityFinding {
    pub vuln_type: String,
    pub subtype: String,
    pub endpoint: String,
    pub parameter: String,
    pub payload: String,
    pub severity: String,
    pub confidence: String,
    pub evidence: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiScanResult {
    pub domain: String,
    pub endpoints_found: Vec<ApiEndpoint>,
    pub vulnerabilities: Vec<VulnerabilityFinding>,
    pub total_paths_probed: usize,
    pub endpoints_tested: usize,
}

// ── HTML killwords — definitive NOT-API indicators ──────────────────────────

const HTML_KILLERS: &[&str] = &[
    "<!doctype html",
    "<html",
    "<head>",
    "<body>",
    "<title>",
    "<div",
    "<form",
    "<table",
    "<script",
    "not found</title>",
    "404 not found",
    "404 - not found",
    "page not found",
    "file not found",
    "apache/2.",
    "nginx/",
    "microsoft-iis",
    "server error",
    "access denied",
    "forbidden",
    "directory listing",
    "index of /",
    "<h1>404</h1>",
    "<h1>error</h1>",
];

// ── Swagger/OpenAPI documentation indicators ────────────────────────────────

const DOC_INDICATORS: &[&str] = &[
    "\"openapi\":",
    "\"swagger\":",
    "\"info\":",
    "\"paths\":",
    "\"components\":",
    "\"definitions\":",
    "\"host\":",
    "\"basepath\":",
    "\"schemes\":",
    "\"consumes\":",
    "\"produces\":",
];

const DOC_URL_HINTS: &[&str] = &[
    "openapi",
    "swagger",
    "docs",
    "spec",
    "schema",
    "definition",
    ".json",
    ".yaml",
    ".yml",
];

// ── API-specific response headers ───────────────────────────────────────────

const API_HEADERS: &[&str] = &[
    "x-api-version",
    "x-api-key",
    "x-rate-limit",
    "x-ratelimit",
    "x-request-id",
    "x-correlation-id",
    "x-trace-id",
];

const FRAMEWORK_SERVERS: &[&str] = &[
    "express", "koa", "fastify", "spring", "django", "flask", "tornado", "rails", "sinatra",
    "fastapi",
];

// ── Auth error patterns (regex) ─────────────────────────────────────────────

const AUTH_ERROR_PATTERNS: &[&str] = &[
    r#""error"\s*:\s*"(unauthorized|forbidden|invalid.*token|missing.*auth)"#,
    r#""message"\s*:\s*"(unauthorized|forbidden|authentication|authorization)"#,
    r#""code"\s*:\s*"(401|403|auth_required|token_invalid)"#,
    r#""status"\s*:\s*"(unauthorized|forbidden|error)","#,
    r#""access_token""#,
    r#""api_key""#,
    r#""authentication.*required""#,
    r#""invalid.*credentials""#,
];

// ── RESTful API structure patterns ──────────────────────────────────────────

const API_STRUCTURE_PATTERNS: &[&str] = &[
    r#"^\s*\{\s*"data"\s*:\s*[\{\[]"#,
    r#"^\s*\{\s*"result"\s*:\s*[\{\[]"#,
    r#"^\s*\{\s*"results"\s*:\s*\["#,
    r#"^\s*\{\s*"items"\s*:\s*\["#,
    r#"^\s*\{\s*"records"\s*:\s*\["#,
    r#"^\s*\{\s*"version"\s*:\s*"[^"]*""#,
    r#"^\s*\{\s*"api_version"\s*:\s*"[^"]*""#,
    r#"^\s*\{\s*"timestamp"\s*:\s*\d+"#,
    r#"^\s*\{\s*"error"\s*:\s*\{\s*"code""#,
    r#"^\s*\{\s*"error"\s*:\s*\{\s*"message""#,
    r#"^\s*\{\s*"errors"\s*:\s*\[.*"message""#,
    r#"^\s*\{\s*"success"\s*:\s*(true|false)"#,
    r#"^\s*\{\s*"status"\s*:\s*"(up|down|ok|healthy|error|fail|success)""#,
    r#"^\s*\{\s*"health"\s*:\s*"(up|down|ok)""#,
];

// ── SQL error patterns ──────────────────────────────────────────────────────

const SQL_ERROR_PATTERNS: &[&str] = &[
    r"You have an error in your SQL syntax",
    r"MySQL server version for the right syntax",
    r"PostgreSQL.*ERROR.*syntax error",
    r"ORA-[0-9]{5}.*invalid identifier",
    r"SQLite error.*syntax error",
    r"SQLException.*invalid column name",
    r"mysql_fetch_array\(\).*expects parameter",
    r"Warning.*mysql_.*\(\).*supplied argument",
];

// ── JS API endpoint extraction patterns ─────────────────────────────────────

const JS_API_PATTERNS: &[&str] = &[
    r#"fetch\s*\(\s*['"`](/[^'"`\s]+)['"`]"#,
    r#"axios\.[a-z]+\s*\(\s*['"`](/[^'"`\s]+)['"`]"#,
    r#"\$\.ajax\([^)]*url\s*:\s*['"`](/[^'"`\s]+)['"`]"#,
    r#"\$\.get\s*\(\s*['"`](/[^'"`\s]+)['"`]"#,
    r#"\$\.post\s*\(\s*['"`](/[^'"`\s]+)['"`]"#,
    r#"apiUrl\s*[:=]\s*['"`](/[^'"`\s]+)['"`]"#,
    r#"API_URL\s*[:=]\s*['"`](/[^'"`\s]+)['"`]"#,
    r#"baseURL\s*[:=]\s*['"`](/[^'"`\s]+)['"`]"#,
    r#"endpoint\s*[:=]\s*['"`](/[^'"`\s]+)['"`]"#,
];

// ── Main scanner ────────────────────────────────────────────────────────────

pub async fn scan_api_endpoints(
    domain: &str,
    progress_tx: Option<tokio::sync::mpsc::Sender<crate::ScanProgress>>,
) -> Result<ApiScanResult, Box<dyn std::error::Error + Send + Sync>> {
    let base_url = if domain.starts_with("http") {
        domain.to_string()
    } else {
        format!("https://{}", domain)
    };

    let client = Client::builder()
        .timeout(Duration::from_secs(15))
        .danger_accept_invalid_certs(true)
        .redirect(reqwest::redirect::Policy::limited(3))
        .build()?;

    // ── Phase 1: Endpoint Discovery ─────────────────────────────────────
    let mut verified_endpoints: Vec<ApiEndpoint> = Vec::new();

    if let Some(t) = &progress_tx { let _ = t.send(crate::ScanProgress { module: "API Security".into(), percentage: 5.0, message: "Started API endpoint discovery...".into(), status: "Info".into() }).await; }

    // 1a. Probe paths from embedded api_endpoints.txt
    let api_paths = payloads::lines(payloads::API_ENDPOINTS);
    let total_paths_probed = api_paths.len();

    for (i, path) in api_paths.iter().enumerate() {
        if i % 10 == 0 {
            if let Some(t) = &progress_tx { let _ = t.send(crate::ScanProgress { module: "API Security".into(), percentage: 5.0 + (15.0 * (i as f32 / total_paths_probed as f32)), message: format!("Probing paths: {}", path), status: "Info".into() }).await; }
        }
        let url = format!("{}{}", base_url.trim_end_matches('/'), path);
        if let Some(endpoint) = verify_endpoint(&client, &url).await {
            verified_endpoints.push(endpoint);
        }
    }

    // 1b. Extract endpoints from JavaScript on main page
    if let Some(t) = &progress_tx { let _ = t.send(crate::ScanProgress { module: "API Security".into(), percentage: 20.0, message: "Extracting JavaScript endpoints...".into(), status: "Info".into() }).await; }
    let js_endpoints = extract_js_endpoints(&client, &base_url).await;
    for url in &js_endpoints {
        if !verified_endpoints.iter().any(|e| e.url == *url) {
            if let Some(endpoint) = verify_endpoint(&client, url).await {
                verified_endpoints.push(endpoint);
            }
        }
    }

    // 1c. Extract API paths from robots.txt and sitemap.xml
    if let Some(t) = &progress_tx { let _ = t.send(crate::ScanProgress { module: "API Security".into(), percentage: 25.0, message: "Checking robots.txt & sitemap.xml...".into(), status: "Info".into() }).await; }
    let robots_endpoints = extract_robots_sitemap_endpoints(&client, &base_url).await;
    for url in &robots_endpoints {
        if !verified_endpoints.iter().any(|e| e.url == *url) {
            if let Some(endpoint) = verify_endpoint(&client, url).await {
                verified_endpoints.push(endpoint);
            }
        }
    }

    // 1d. Scrape Swagger/OpenAPI documentation for real paths
    if let Some(t) = &progress_tx { let _ = t.send(crate::ScanProgress { module: "API Security".into(), percentage: 30.0, message: "Hunting for OpenAPI/Swagger docs...".into(), status: "Info".into() }).await; }
    let doc_endpoints = scrape_documentation_endpoints(&client, &base_url).await;
    for url in &doc_endpoints {
        if !verified_endpoints.iter().any(|e| e.url == *url) {
            if let Some(endpoint) = verify_endpoint(&client, url).await {
                verified_endpoints.push(endpoint);
            }
        }
    }

    // 1e. Check common API subdomains
    if let Some(t) = &progress_tx { let _ = t.send(crate::ScanProgress { module: "API Security".into(), percentage: 35.0, message: "Bruting common API subdomains...".into(), status: "Info".into() }).await; }
    let subdomain_endpoints = check_api_subdomains(&client, domain).await;
    for url in &subdomain_endpoints {
        if !verified_endpoints.iter().any(|e| e.url == *url) {
            if let Some(endpoint) = verify_endpoint(&client, url).await {
                verified_endpoints.push(endpoint);
            }
        }
    }

    // ── Phase 2: Vulnerability Testing ──────────────────────────────────
    let mut vulnerabilities: Vec<VulnerabilityFinding> = Vec::new();
    let endpoints_tested = verified_endpoints.len();

    if let Some(t) = &progress_tx { let _ = t.send(crate::ScanProgress { module: "API Security".into(), percentage: 40.0, message: format!("Found {} endpoints, starting active fuzzing...", endpoints_tested), status: "Info".into() }).await; }

    for (i, ep) in verified_endpoints.iter().enumerate() {
        if let Some(t) = &progress_tx { let _ = t.send(crate::ScanProgress { module: "API Security".into(), percentage: 40.0 + (60.0 * (i as f32 / endpoints_tested.max(1) as f32)), message: format!("Fuzzing endpoint: {}", ep.url), status: "Info".into() }).await; }
        let mut findings = test_endpoint(&client, &ep.url).await;
        vulnerabilities.append(&mut findings);

        // Early exit on excessive criticals
        let critical_count = vulnerabilities
            .iter()
            .filter(|v| v.severity == "CRITICAL")
            .count();
        if critical_count >= 10 {
            break;
        }
    }

    Ok(ApiScanResult {
        domain: domain.to_string(),
        endpoints_found: verified_endpoints,
        vulnerabilities,
        total_paths_probed,
        endpoints_tested,
    })
}

// ── Advanced API endpoint verification ──────────────────────────────────────

async fn verify_endpoint(client: &Client, url: &str) -> Option<ApiEndpoint> {
    // Try GET first, then OPTIONS, HEAD — majority voting
    let methods = ["GET", "OPTIONS", "HEAD"];
    let mut votes: Vec<(String, u16)> = Vec::new(); // (api_type, status)

    for method in &methods {
        let req = match *method {
            "GET" => client.get(url),
            "OPTIONS" => client.request(reqwest::Method::OPTIONS, url),
            "HEAD" => client.head(url),
            _ => continue,
        };

        let resp = match req.send().await {
            Ok(r) => r,
            Err(_) => continue,
        };

        let status = resp.status().as_u16();

        // Immediate disqualifiers
        if matches!(status, 404 | 502 | 503 | 500) {
            continue;
        }

        let headers: Vec<(String, String)> = resp
            .headers()
            .iter()
            .map(|(k, v)| {
                (
                    k.as_str().to_lowercase(),
                    v.to_str().unwrap_or("").to_lowercase(),
                )
            })
            .collect();

        let content_type = headers
            .iter()
            .find(|(k, _)| k == "content-type")
            .map(|(_, v)| v.as_str())
            .unwrap_or("");

        // For HEAD/OPTIONS we can't read body, just check headers
        if *method != "GET" {
            if let Some(api_type) = detect_api_from_headers(content_type, &headers, status) {
                votes.push((api_type, status));
            }
            continue;
        }

        // GET — full body analysis
        let body = match resp.text().await {
            Ok(t) => t,
            Err(_) => continue,
        };

        if body.trim().len() < 5 {
            continue;
        }

        let sample = if body.len() > 5000 {
            &body[..5000]
        } else {
            &body
        };
        let sample_lower = sample.to_lowercase();

        // HTML killer filter
        if HTML_KILLERS.iter().any(|k| sample_lower.contains(k)) {
            continue;
        }

        // Documentation file detection
        let is_doc_url = DOC_URL_HINTS.iter().any(|h| url.to_lowercase().contains(h));
        if is_doc_url {
            let doc_score: usize = DOC_INDICATORS
                .iter()
                .filter(|d| sample_lower.contains(*d))
                .count();
            if doc_score >= 3 {
                continue; // Skip API documentation files
            }
        }

        // Content-type based definitive detection
        let ct_api = if content_type.contains("application/json") {
            // Verify valid JSON
            if serde_json::from_str::<serde_json::Value>(sample).is_ok() {
                Some("REST/JSON".to_string())
            } else {
                None
            }
        } else if content_type.contains("application/xml") || content_type.contains("text/xml") {
            Some("REST/XML".to_string())
        } else if content_type.contains("graphql") {
            Some("GraphQL".to_string())
        } else if content_type.contains("application/vnd.api+json") {
            Some("JSON:API".to_string())
        } else if content_type.contains("application/hal+json") {
            Some("HAL+JSON".to_string())
        } else if content_type.contains("application/problem+json") {
            Some("Problem Details".to_string())
        } else {
            None
        };

        if let Some(api_type) = ct_api {
            votes.push((api_type, status));
            continue;
        }

        // Auth-protected endpoint detection (401/403)
        if matches!(status, 401 | 403) {
            let auth_headers = [
                "www-authenticate",
                "x-api-key",
                "x-auth-token",
                "x-rate-limit",
            ];
            if auth_headers
                .iter()
                .any(|h| headers.iter().any(|(k, _)| k == h))
            {
                votes.push(("Protected API".to_string(), status));
                continue;
            }
            // Check body for API-style auth errors
            let auth_regexes: Vec<Regex> = AUTH_ERROR_PATTERNS
                .iter()
                .filter_map(|p| Regex::new(p).ok())
                .collect();
            if auth_regexes.iter().any(|rx| rx.is_match(&sample_lower)) {
                votes.push(("Protected API".to_string(), status));
                continue;
            }
        }

        // API structure pattern scoring
        let structure_regexes: Vec<Regex> = API_STRUCTURE_PATTERNS
            .iter()
            .filter_map(|p| Regex::new(p).ok())
            .collect();
        let structure_score: usize = structure_regexes
            .iter()
            .filter(|rx| rx.is_match(sample))
            .count();

        // API header scoring
        let api_header_score: usize = API_HEADERS
            .iter()
            .filter(|h| headers.iter().any(|(k, _)| k == **h))
            .count();

        // Framework detection via Server header
        let framework_score: usize = headers
            .iter()
            .filter(|(k, _)| k == "server")
            .map(|(_, v)| FRAMEWORK_SERVERS.iter().filter(|f| v.contains(*f)).count() * 2)
            .sum();

        let total_score = structure_score + api_header_score + framework_score;

        if total_score >= 4 || (total_score >= 2 && status == 200) {
            votes.push(("REST API".to_string(), status));
        }
    }

    // Majority voting
    if votes.is_empty() {
        return None;
    }

    // Pick the best vote (prefer 2xx status)
    let best = votes
        .iter()
        .max_by_key(|(_, s)| {
            if *s < 400 {
                1000 - *s as i32
            } else {
                -((*s) as i32)
            }
        })
        .unwrap();

    Some(ApiEndpoint {
        url: url.to_string(),
        status_code: best.1,
        api_type: best.0.clone(),
    })
}

fn detect_api_from_headers(
    content_type: &str,
    headers: &[(String, String)],
    status: u16,
) -> Option<String> {
    if content_type.contains("application/json") {
        return Some("REST/JSON".to_string());
    }
    if content_type.contains("application/xml") || content_type.contains("text/xml") {
        return Some("REST/XML".to_string());
    }
    if content_type.contains("graphql") {
        return Some("GraphQL".to_string());
    }
    if matches!(status, 401 | 403) {
        let auth_headers = ["www-authenticate", "x-api-key", "x-rate-limit"];
        if auth_headers
            .iter()
            .any(|h| headers.iter().any(|(k, _)| k == h))
        {
            return Some("Protected API".to_string());
        }
    }
    None
}

// ── Endpoint Discovery Helpers ──────────────────────────────────────────────

async fn extract_js_endpoints(client: &Client, base_url: &str) -> Vec<String> {
    let mut endpoints = HashSet::new();
    let resp = match client.get(base_url).send().await {
        Ok(r) if r.status().is_success() => r,
        _ => return Vec::new(),
    };
    let body = match resp.text().await {
        Ok(t) => t,
        Err(_) => return Vec::new(),
    };

    // Collect inline JS
    let mut all_js = String::new();
    let mut external_urls = Vec::new();

    {
        let doc = Html::parse_document(&body);
        let script_sel = Selector::parse("script").unwrap();
        for el in doc.select(&script_sel) {
            let inline = el.text().collect::<String>();
            if inline.len() > 10 {
                all_js.push('\n');
                all_js.push_str(&inline);
            }
            // Fetch up to 10 external JS files
            if let Some(src) = el.value().attr("src") {
                if external_urls.len() < 10 {
                    external_urls.push(src.to_string());
                }
            }
        }
    }

    for src in external_urls {
        if endpoints.len() > 10 {
            break;
        }
        if let Some(js_url) = resolve_url(base_url, &src) {
            if let Ok(resp) = client.get(&js_url).send().await {
                if resp.status().is_success() {
                    if let Ok(js_body) = resp.text().await {
                        all_js.push('\n');
                        all_js.push_str(&js_body);
                    }
                }
            }
        }
    }

    // Extract API paths from JS content
    let regexes: Vec<Regex> = JS_API_PATTERNS
        .iter()
        .filter_map(|p| Regex::new(p).ok())
        .collect();

    for rx in &regexes {
        for cap in rx.captures_iter(&all_js) {
            if let Some(m) = cap.get(1) {
                let path = m.as_str().trim();
                if path.is_empty() {
                    continue;
                }
                // Skip static assets
                if [".js", ".css", ".png", ".jpg", ".gif", ".ico", ".svg"]
                    .iter()
                    .any(|ext| path.to_lowercase().ends_with(ext))
                {
                    continue;
                }
                let full = format!("{}{}", base_url.trim_end_matches('/'), path);
                endpoints.insert(full);
            }
        }
    }

    endpoints.into_iter().collect()
}

async fn extract_robots_sitemap_endpoints(client: &Client, base_url: &str) -> Vec<String> {
    let mut endpoints = HashSet::new();

    // robots.txt
    let robots_url = format!("{}/robots.txt", base_url.trim_end_matches('/'));
    if let Ok(resp) = client.get(&robots_url).send().await {
        if resp.status().is_success() {
            if let Ok(body) = resp.text().await {
                for line in body.lines() {
                    let line = line.trim().to_lowercase();
                    if (line.starts_with("disallow:") || line.starts_with("allow:"))
                        && line.contains(':')
                    {
                        let path = line.split_once(':').map(|(_, v)| v.trim()).unwrap_or("");
                        if !path.is_empty()
                            && path != "/"
                            && ["api", "graphql", "rest"]
                                .iter()
                                .any(|kw| path.contains(kw))
                        {
                            endpoints.insert(format!("{}{}", base_url.trim_end_matches('/'), path));
                        }
                    }
                }
            }
        }
    }

    // sitemap.xml
    let sitemap_url = format!("{}/sitemap.xml", base_url.trim_end_matches('/'));
    if let Ok(resp) = client.get(&sitemap_url).send().await {
        if resp.status().is_success() {
            if let Ok(body) = resp.text().await {
                if let Ok(rx) = Regex::new(r"<loc>([^<]+)</loc>") {
                    for cap in rx.captures_iter(&body) {
                        if let Some(m) = cap.get(1) {
                            let url = m.as_str();
                            if ["api", "graphql", "rest"]
                                .iter()
                                .any(|kw| url.to_lowercase().contains(kw))
                            {
                                endpoints.insert(url.to_string());
                            }
                        }
                    }
                }
            }
        }
    }

    endpoints.into_iter().collect()
}

async fn scrape_documentation_endpoints(client: &Client, base_url: &str) -> Vec<String> {
    let mut endpoints = HashSet::new();
    let doc_paths = [
        "/swagger.json",
        "/openapi.json",
        "/api-docs",
        "/docs",
        "/swagger",
        "/api/swagger.json",
        "/api/docs",
    ];

    for path in &doc_paths {
        let url = format!("{}{}", base_url.trim_end_matches('/'), path);
        let resp = match client.get(&url).send().await {
            Ok(r) if r.status().is_success() => r,
            _ => continue,
        };
        let body = match resp.text().await {
            Ok(t) => t,
            Err(_) => continue,
        };

        // Try to parse as JSON and extract "paths" key
        if let Ok(doc) = serde_json::from_str::<serde_json::Value>(&body) {
            if let Some(paths) = doc.get("paths").and_then(|p| p.as_object()) {
                for path_key in paths.keys() {
                    if path_key.starts_with('/') {
                        endpoints.insert(format!("{}{}", base_url.trim_end_matches('/'), path_key));
                    }
                }
            }
            if let Some(base_path) = doc.get("basePath").and_then(|b| b.as_str()) {
                if !base_path.is_empty() {
                    endpoints.insert(format!("{}{}", base_url.trim_end_matches('/'), base_path));
                }
            }
        }
    }

    endpoints.into_iter().collect()
}

async fn check_api_subdomains(client: &Client, domain: &str) -> Vec<String> {
    let mut endpoints = Vec::new();
    let bare_domain = domain
        .trim_start_matches("https://")
        .trim_start_matches("http://")
        .split('/')
        .next()
        .unwrap_or(domain);

    let parts: Vec<&str> = bare_domain.split('.').collect();
    if parts.len() < 2 {
        return endpoints;
    }

    let base = format!("{}.{}", parts[parts.len() - 2], parts[parts.len() - 1]);

    let prefixes = [
        "api",
        "rest",
        "graphql",
        "gateway",
        "api-v1",
        "api-v2",
        "api-dev",
        "dev-api",
        "api-staging",
        "staging-api",
        "mobile-api",
        "app-api",
        "admin-api",
        "auth-api",
    ];

    for prefix in &prefixes[..8] {
        // limit to avoid excessive requests
        for proto in &["https", "http"] {
            let url = format!("{}://{}.{}", proto, prefix, base);
            if let Ok(resp) = client.get(&url).send().await {
                if resp.status().is_success() || matches!(resp.status().as_u16(), 401 | 403) {
                    endpoints.push(url);
                    break; // Found, skip other protocol
                }
            }
        }
    }

    endpoints
}

// ── Vulnerability Testing ───────────────────────────────────────────────────

async fn test_endpoint(client: &Client, endpoint: &str) -> Vec<VulnerabilityFinding> {
    let mut findings = Vec::new();

    findings.append(&mut test_sql_injection(client, endpoint).await);
    findings.append(&mut test_xss(client, endpoint).await);
    findings.append(&mut test_ssti(client, endpoint).await);
    findings.append(&mut test_ssrf(client, endpoint).await);
    findings.append(&mut test_auth_bypass(client, endpoint).await);
    findings.append(&mut test_command_injection(client, endpoint).await);
    findings.append(&mut test_nosql_injection(client, endpoint).await);
    findings.append(&mut test_xxe(client, endpoint).await);
    findings.append(&mut test_lfi(client, endpoint).await);

    findings
}

// ── SQLi ────────────────────────────────────────────────────────────────────

async fn test_sql_injection(client: &Client, endpoint: &str) -> Vec<VulnerabilityFinding> {
    let mut findings = Vec::new();
    let sqli_payloads = payloads::lines(payloads::SQL_INJECTION);
    let params = ["id", "user", "search", "q", "filter"];

    let error_regexes: Vec<Regex> = SQL_ERROR_PATTERNS
        .iter()
        .filter_map(|p| Regex::new(p).ok())
        .collect();

    for param in &params[..3] {
        // Baseline
        let baseline_url = format!("{}?{}=1", endpoint, param);
        let baseline_body = match fetch_body(client, &baseline_url).await {
            Some(b) => b,
            None => continue,
        };
        if error_regexes.iter().any(|rx| rx.is_match(&baseline_body)) {
            continue; // Baseline already has SQL errors
        }

        for payload in sqli_payloads.iter().take(5) {
            let encoded = urlencoding::encode(payload);
            let test_url = format!("{}?{}={}", endpoint, param, encoded);

            // Time-based detection
            if payload.to_uppercase().contains("SLEEP")
                || payload.to_uppercase().contains("WAITFOR")
            {
                let start = Instant::now();
                if let Ok(resp) = client.get(&test_url).send().await {
                    let elapsed = start.elapsed().as_secs_f64();
                    let _ = resp.text().await;
                    if elapsed > 4.8 {
                        findings.push(VulnerabilityFinding {
                            vuln_type: "SQL_INJECTION".into(),
                            subtype: "Time-based Blind".into(),
                            endpoint: endpoint.into(),
                            parameter: param.to_string(),
                            payload: payload.to_string(),
                            severity: "CRITICAL".into(),
                            confidence: "MEDIUM".into(),
                            evidence: format!("Response delayed {:.1}s", elapsed),
                        });
                        return findings;
                    }
                }
                continue;
            }

            // Error-based detection
            if let Some(body) = fetch_body(client, &test_url).await {
                for rx in &error_regexes {
                    if let Some(m) = rx.find(&body) {
                        if !rx.is_match(&baseline_body) {
                            findings.push(VulnerabilityFinding {
                                vuln_type: "SQL_INJECTION".into(),
                                subtype: "Error-based".into(),
                                endpoint: endpoint.into(),
                                parameter: param.to_string(),
                                payload: payload.to_string(),
                                severity: "CRITICAL".into(),
                                confidence: "HIGH".into(),
                                evidence: format!("SQL error: {}", m.as_str()),
                            });
                            return findings;
                        }
                    }
                }
            }
        }
    }

    findings
}

// ── XSS ─────────────────────────────────────────────────────────────────────

async fn test_xss(client: &Client, endpoint: &str) -> Vec<VulnerabilityFinding> {
    let mut findings = Vec::new();
    let xss_payloads = payloads::lines(payloads::XSS);
    let params = ["q", "search", "query", "keyword", "name"];

    for payload in xss_payloads.iter().take(5) {
        for param in &params[..3] {
            let encoded = urlencoding::encode(payload);
            let test_url = format!("{}?{}={}", endpoint, param, encoded);

            let resp = match client.get(&test_url).send().await {
                Ok(r) => r,
                Err(_) => continue,
            };

            if !resp.status().is_success() {
                continue;
            }

            let ct = resp
                .headers()
                .get("content-type")
                .and_then(|v| v.to_str().ok())
                .unwrap_or("")
                .to_lowercase();

            if !ct.contains("text/html") {
                continue;
            }

            let body = match resp.text().await {
                Ok(t) => t,
                Err(_) => continue,
            };

            // Payload reflected unencoded in HTML
            if body.contains(payload) && !is_payload_safe_context(&body, payload) {
                findings.push(VulnerabilityFinding {
                    vuln_type: "XSS".into(),
                    subtype: "Reflected".into(),
                    endpoint: endpoint.into(),
                    parameter: param.to_string(),
                    payload: payload.to_string(),
                    severity: "HIGH".into(),
                    confidence: "HIGH".into(),
                    evidence: "Payload reflected in HTML without encoding".into(),
                });
                return findings;
            }
        }
    }
    findings
}

fn is_payload_safe_context(content: &str, payload: &str) -> bool {
    let pos = match content.find(payload) {
        Some(p) => p,
        None => return true,
    };
    // Inside HTML comment?
    let before = &content[..pos];
    let after = &content[pos..];
    if before.rfind("<!--").is_some() && after.contains("-->") {
        let comment_start = before.rfind("<!--").unwrap();
        if !before[comment_start..].contains("-->") {
            return true;
        }
    }
    // Properly encoded?
    let encoded = payload.replace('<', "&lt;").replace('>', "&gt;");
    if content.contains(&encoded) {
        return true;
    }
    false
}

// ── SSTI ────────────────────────────────────────────────────────────────────

async fn test_ssti(client: &Client, endpoint: &str) -> Vec<VulnerabilityFinding> {
    let mut findings = Vec::new();
    let tests = [
        ("{{7*7*7}}", "343"),
        ("{{9*9*9}}", "729"),
        ("${8*8*8}", "512"),
        ("{{42*13}}", "546"),
    ];
    let params = ["template", "name", "msg", "content"];

    for &(payload, expected) in &tests {
        for param in &params[..3] {
            // Baseline
            let baseline_url = format!("{}?{}=normaltext", endpoint, param);
            let baseline = match fetch_body(client, &baseline_url).await {
                Some(b) => b,
                None => continue,
            };

            let encoded = urlencoding::encode(payload);
            let test_url = format!("{}?{}={}", endpoint, param, encoded);

            if let Some(body) = fetch_body(client, &test_url).await {
                if body.contains(expected)
                    && !body.contains(payload)
                    && !baseline.contains(expected)
                {
                    findings.push(VulnerabilityFinding {
                        vuln_type: "SSTI".into(),
                        subtype: "Template Injection".into(),
                        endpoint: endpoint.into(),
                        parameter: param.to_string(),
                        payload: payload.to_string(),
                        severity: "CRITICAL".into(),
                        confidence: "HIGH".into(),
                        evidence: format!("Template executed: {} = {}", payload, expected),
                    });
                    return findings;
                }
            }
        }
    }
    findings
}

// ── SSRF ────────────────────────────────────────────────────────────────────

async fn test_ssrf(client: &Client, endpoint: &str) -> Vec<VulnerabilityFinding> {
    let mut findings = Vec::new();
    let ssrf_payloads = payloads::lines(payloads::SSRF);
    let params = ["url", "uri", "path", "dest", "redirect"];
    let indicators = [
        "root:",
        "daemon:",
        "localhost",
        "metadata",
        "ami-id",
        "instance-id",
    ];

    for param in &params[..3] {
        for payload in ssrf_payloads.iter().take(3) {
            let encoded = urlencoding::encode(payload);
            let test_url = format!("{}?{}={}", endpoint, param, encoded);

            if let Some(body) = fetch_body(client, &test_url).await {
                for indicator in &indicators {
                    if body.contains(indicator) {
                        findings.push(VulnerabilityFinding {
                            vuln_type: "SSRF".into(),
                            subtype: "Server-Side Request Forgery".into(),
                            endpoint: endpoint.into(),
                            parameter: param.to_string(),
                            payload: payload.to_string(),
                            severity: "CRITICAL".into(),
                            confidence: "HIGH".into(),
                            evidence: format!("Internal data leaked: {}", indicator),
                        });
                        return findings;
                    }
                }
            }
        }
    }
    findings
}

// ── Auth Bypass ─────────────────────────────────────────────────────────────

async fn test_auth_bypass(client: &Client, endpoint: &str) -> Vec<VulnerabilityFinding> {
    let mut findings = Vec::new();

    // Check if endpoint is normally protected
    let normal_status = match client.get(endpoint).send().await {
        Ok(r) => r.status().as_u16(),
        Err(_) => return findings,
    };
    if !matches!(normal_status, 401 | 403) {
        return findings; // Not protected, skip
    }

    let bypass_headers = payloads::auth_headers(payloads::AUTH_BYPASS_HEADERS);

    for (name, value) in bypass_headers.iter().take(10) {
        let resp = match client
            .get(endpoint)
            .header(name as &str, value as &str)
            .send()
            .await
        {
            Ok(r) => r,
            Err(_) => continue,
        };

        if resp.status().as_u16() == 200 {
            findings.push(VulnerabilityFinding {
                vuln_type: "AUTH_BYPASS".into(),
                subtype: "Header-based".into(),
                endpoint: endpoint.into(),
                parameter: String::new(),
                payload: format!("{}: {}", name, value),
                severity: "CRITICAL".into(),
                confidence: "HIGH".into(),
                evidence: format!("Bypass with header {}: {}", name, value),
            });
            return findings;
        }
    }
    findings
}

// ── Command Injection ───────────────────────────────────────────────────────

async fn test_command_injection(client: &Client, endpoint: &str) -> Vec<VulnerabilityFinding> {
    let mut findings = Vec::new();
    let cmd_payloads = payloads::lines(payloads::COMMAND_INJECTION);
    let params = ["cmd", "exec", "command", "ping", "host"];

    for param in &params[..3] {
        for payload in cmd_payloads.iter().take(3) {
            if payload.to_lowercase().contains("sleep") {
                let encoded = urlencoding::encode(payload);
                let test_url = format!("{}?{}={}", endpoint, param, encoded);
                let start = Instant::now();
                if let Ok(resp) = client.get(&test_url).send().await {
                    let elapsed = start.elapsed().as_secs_f64();
                    let _ = resp.text().await;
                    if elapsed > 4.5 {
                        findings.push(VulnerabilityFinding {
                            vuln_type: "COMMAND_INJECTION".into(),
                            subtype: "Time-based".into(),
                            endpoint: endpoint.into(),
                            parameter: param.to_string(),
                            payload: payload.to_string(),
                            severity: "CRITICAL".into(),
                            confidence: "HIGH".into(),
                            evidence: format!("Command executed (delay: {:.1}s)", elapsed),
                        });
                        return findings;
                    }
                }
            }
        }
    }
    findings
}

// ── NoSQL Injection ─────────────────────────────────────────────────────────

async fn test_nosql_injection(client: &Client, endpoint: &str) -> Vec<VulnerabilityFinding> {
    let mut findings = Vec::new();
    let nosql_payloads = payloads::lines(payloads::NOSQL_INJECTION);

    for payload in nosql_payloads.iter().take(3) {
        let resp = match client
            .post(endpoint)
            .header("Content-Type", "application/json")
            .body(payload.to_string())
            .send()
            .await
        {
            Ok(r) => r,
            Err(_) => continue,
        };

        if matches!(resp.status().as_u16(), 200 | 201) {
            let body = match resp.text().await {
                Ok(t) => t,
                Err(_) => continue,
            };
            if body.len() > 100 && !body.to_lowercase().contains("error") {
                findings.push(VulnerabilityFinding {
                    vuln_type: "NOSQL_INJECTION".into(),
                    subtype: "Operator Injection".into(),
                    endpoint: endpoint.into(),
                    parameter: String::new(),
                    payload: payload.to_string(),
                    severity: "HIGH".into(),
                    confidence: "MEDIUM".into(),
                    evidence: "NoSQL operator accepted, returned data".into(),
                });
                return findings;
            }
        }
    }
    findings
}

// ── XXE ─────────────────────────────────────────────────────────────────────

async fn test_xxe(client: &Client, endpoint: &str) -> Vec<VulnerabilityFinding> {
    let mut findings = Vec::new();
    let xxe_payloads = payloads::lines(payloads::XXE);
    let indicators = ["root:", "daemon:", "Windows", "[fonts]"];

    for payload in xxe_payloads.iter().take(2) {
        let resp = match client
            .post(endpoint)
            .header("Content-Type", "application/xml")
            .body(payload.to_string())
            .send()
            .await
        {
            Ok(r) => r,
            Err(_) => continue,
        };

        if resp.status().is_success() {
            let body = match resp.text().await {
                Ok(t) => t,
                Err(_) => continue,
            };
            for indicator in &indicators {
                if body.contains(indicator) {
                    findings.push(VulnerabilityFinding {
                        vuln_type: "XXE".into(),
                        subtype: "XML External Entity".into(),
                        endpoint: endpoint.into(),
                        parameter: String::new(),
                        payload: payload.to_string(),
                        severity: "CRITICAL".into(),
                        confidence: "HIGH".into(),
                        evidence: "File contents disclosed via XXE".into(),
                    });
                    return findings;
                }
            }
        }
    }
    findings
}

// ── LFI ─────────────────────────────────────────────────────────────────────

async fn test_lfi(client: &Client, endpoint: &str) -> Vec<VulnerabilityFinding> {
    let mut findings = Vec::new();
    let lfi_payloads = payloads::lines(payloads::LFI);
    let params = ["file", "path", "page", "include", "template"];
    let indicators = ["root:x:", "daemon:", "[fonts]", "[extensions]"];

    for param in &params[..3] {
        for payload in lfi_payloads.iter().take(3) {
            let encoded = urlencoding::encode(payload);
            let test_url = format!("{}?{}={}", endpoint, param, encoded);

            if let Some(body) = fetch_body(client, &test_url).await {
                for indicator in &indicators {
                    if body.contains(indicator) {
                        findings.push(VulnerabilityFinding {
                            vuln_type: "LFI".into(),
                            subtype: "Local File Inclusion".into(),
                            endpoint: endpoint.into(),
                            parameter: param.to_string(),
                            payload: payload.to_string(),
                            severity: "HIGH".into(),
                            confidence: "HIGH".into(),
                            evidence: "Local file contents exposed".into(),
                        });
                        return findings;
                    }
                }
            }
        }
    }
    findings
}

// ── Shared helpers ──────────────────────────────────────────────────────────

async fn fetch_body(client: &Client, url: &str) -> Option<String> {
    let resp = client.get(url).send().await.ok()?;
    if resp.status().as_u16() == 404 {
        return None;
    }
    resp.text().await.ok()
}

fn resolve_url(base: &str, href: &str) -> Option<String> {
    if href.starts_with("javascript:") || href.starts_with('#') || href.starts_with("mailto:") {
        return None;
    }
    if href.starts_with("//") {
        return Some(format!("https:{}", href));
    }
    if href.starts_with("http://") || href.starts_with("https://") {
        return Some(href.to_string());
    }
    let base_trimmed = if let Some(idx) = base.rfind('/') {
        &base[..idx + 1]
    } else {
        base
    };
    Some(format!("{}{}", base_trimmed, href.trim_start_matches('/')))
}