lonkero 3.6.2

Web scanner built for actual pentests. Fast, modular, Rust.
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
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
// Copyright (c) 2026 Bountyy Oy. All rights reserved.
// This software is proprietary and confidential.

use crate::http_client::HttpClient;
use crate::types::{Confidence, ScanConfig, Severity, Vulnerability};
use anyhow::Result;
use regex::Regex;
use std::collections::HashMap;
use std::sync::Arc;
use tracing::{debug, info};

pub struct DrupalSecurityScanner {
    http_client: Arc<HttpClient>,
    known_vulnerable_modules: HashMap<String, Vec<ModuleVulnerability>>,
}

#[derive(Clone)]
struct ModuleVulnerability {
    name: String,
    vulnerable_version: String,
    sa_id: Option<String>,
    cve: Option<String>,
    severity: Severity,
    description: String,
}

#[derive(Debug, Clone)]
struct DrupalVersion {
    major: u32,
    minor: u32,
    patch: u32,
}

impl DrupalSecurityScanner {
    pub fn new(http_client: Arc<HttpClient>) -> Self {
        Self {
            http_client,
            known_vulnerable_modules: Self::build_vulnerable_modules_db(),
        }
    }

    /// Build database of known vulnerable modules
    fn build_vulnerable_modules_db() -> HashMap<String, Vec<ModuleVulnerability>> {
        let mut db = HashMap::new();

        let vulnerabilities = vec![
            // Core Drupalgeddon vulnerabilities
            ModuleVulnerability {
                name: "drupal_core".to_string(),
                vulnerable_version: "7.31".to_string(),
                sa_id: Some("SA-CORE-2014-005".to_string()),
                cve: Some("CVE-2014-3704".to_string()),
                severity: Severity::Critical,
                description: "Drupalgeddon - SQL injection allowing arbitrary PHP execution"
                    .to_string(),
            },
            ModuleVulnerability {
                name: "drupal_core".to_string(),
                vulnerable_version: "8.5.0".to_string(),
                sa_id: Some("SA-CORE-2018-002".to_string()),
                cve: Some("CVE-2018-7600".to_string()),
                severity: Severity::Critical,
                description: "Drupalgeddon2 - Remote code execution via Form API".to_string(),
            },
            ModuleVulnerability {
                name: "drupal_core".to_string(),
                vulnerable_version: "8.5.3".to_string(),
                sa_id: Some("SA-CORE-2018-004".to_string()),
                cve: Some("CVE-2018-7602".to_string()),
                severity: Severity::Critical,
                description: "Drupalgeddon3 - Remote code execution".to_string(),
            },
            // Contributed modules
            ModuleVulnerability {
                name: "ctools".to_string(),
                vulnerable_version: "7.x-1.14".to_string(),
                sa_id: Some("SA-CONTRIB-2018-012".to_string()),
                cve: None,
                severity: Severity::High,
                description: "Object injection vulnerability".to_string(),
            },
            ModuleVulnerability {
                name: "views".to_string(),
                vulnerable_version: "7.x-3.20".to_string(),
                sa_id: Some("SA-CONTRIB-2017-020".to_string()),
                cve: None,
                severity: Severity::High,
                description: "SQL injection in Views module".to_string(),
            },
            ModuleVulnerability {
                name: "services".to_string(),
                vulnerable_version: "7.x-3.19".to_string(),
                sa_id: Some("SA-CONTRIB-2016-011".to_string()),
                cve: None,
                severity: Severity::Critical,
                description: "Remote code execution via PHP object injection".to_string(),
            },
            ModuleVulnerability {
                name: "restws".to_string(),
                vulnerable_version: "7.x-2.6".to_string(),
                sa_id: Some("SA-CONTRIB-2016-040".to_string()),
                cve: Some("CVE-2016-6211".to_string()),
                severity: Severity::Critical,
                description: "Remote code execution".to_string(),
            },
            ModuleVulnerability {
                name: "webform".to_string(),
                vulnerable_version: "7.x-4.15".to_string(),
                sa_id: Some("SA-CONTRIB-2017-063".to_string()),
                cve: None,
                severity: Severity::High,
                description: "Access bypass vulnerability".to_string(),
            },
            ModuleVulnerability {
                name: "paragraphs".to_string(),
                vulnerable_version: "8.x-1.11".to_string(),
                sa_id: Some("SA-CONTRIB-2019-066".to_string()),
                cve: None,
                severity: Severity::High,
                description: "Access bypass - unpublished content access".to_string(),
            },
            ModuleVulnerability {
                name: "link".to_string(),
                vulnerable_version: "7.x-1.6".to_string(),
                sa_id: Some("SA-CONTRIB-2018-027".to_string()),
                cve: None,
                severity: Severity::Medium,
                description: "Cross-site scripting vulnerability".to_string(),
            },
            ModuleVulnerability {
                name: "token".to_string(),
                vulnerable_version: "7.x-1.7".to_string(),
                sa_id: Some("SA-CONTRIB-2018-014".to_string()),
                cve: None,
                severity: Severity::Medium,
                description: "Information disclosure".to_string(),
            },
            ModuleVulnerability {
                name: "media".to_string(),
                vulnerable_version: "7.x-2.21".to_string(),
                sa_id: Some("SA-CONTRIB-2018-066".to_string()),
                cve: None,
                severity: Severity::High,
                description: "Remote code execution via file upload".to_string(),
            },
            ModuleVulnerability {
                name: "entity".to_string(),
                vulnerable_version: "7.x-1.9".to_string(),
                sa_id: Some("SA-CONTRIB-2019-007".to_string()),
                cve: None,
                severity: Severity::High,
                description: "Access bypass vulnerability".to_string(),
            },
            ModuleVulnerability {
                name: "colorbox".to_string(),
                vulnerable_version: "7.x-2.13".to_string(),
                sa_id: Some("SA-CONTRIB-2017-033".to_string()),
                cve: None,
                severity: Severity::Medium,
                description: "Cross-site scripting".to_string(),
            },
            ModuleVulnerability {
                name: "rules".to_string(),
                vulnerable_version: "7.x-2.11".to_string(),
                sa_id: Some("SA-CONTRIB-2019-012".to_string()),
                cve: None,
                severity: Severity::High,
                description: "PHP object injection".to_string(),
            },
            ModuleVulnerability {
                name: "captcha".to_string(),
                vulnerable_version: "7.x-1.5".to_string(),
                sa_id: Some("SA-CONTRIB-2017-012".to_string()),
                cve: None,
                severity: Severity::Medium,
                description: "CAPTCHA bypass vulnerability".to_string(),
            },
        ];

        for vuln in vulnerabilities {
            db.entry(vuln.name.clone())
                .or_insert_with(Vec::new)
                .push(vuln);
        }

        db
    }

    /// Scan Drupal site for security issues
    pub async fn scan(
        &self,
        url: &str,
        config: &ScanConfig,
    ) -> Result<(Vec<Vulnerability>, usize)> {
        info!("[Drupal] Advanced Drupal security scan starting");

        let mut vulnerabilities = Vec::new();
        let mut tests_run = 0;

        // First, detect if this is a Drupal site and get version
        tests_run += 1;
        let drupal_info = self.detect_drupal(url).await;

        let (is_drupal, version) = match drupal_info {
            Some(info) => info,
            None => {
                debug!("Not a Drupal site, skipping Drupal-specific tests");
                return Ok((vulnerabilities, tests_run));
            }
        };

        if !is_drupal {
            return Ok((vulnerabilities, tests_run));
        }

        info!(
            "[Drupal] Drupal detected (version: {:?}), running comprehensive scan",
            version
        );

        // Test 1: Drupalgeddon vulnerabilities (based on version)
        tests_run += 1;
        let drupalgeddon_vulns = self.test_drupalgeddon(url, &version).await;
        vulnerabilities.extend(drupalgeddon_vulns);

        // Test 2: User enumeration
        tests_run += 1;
        let user_enum_vulns = self.test_user_enumeration(url, &version).await;
        vulnerabilities.extend(user_enum_vulns);

        // Test 3: Version disclosure
        tests_run += 1;
        let version_vulns = self.test_version_disclosure(url).await;
        vulnerabilities.extend(version_vulns);

        // Test 4: Configuration file exposure
        tests_run += 1;
        let config_vulns = self.test_config_exposure(url).await;
        vulnerabilities.extend(config_vulns);

        // Test 5: Admin paths exposure
        tests_run += 1;
        let admin_vulns = self.test_admin_exposure(url).await;
        vulnerabilities.extend(admin_vulns);

        // Test 6: Update.php / Install.php exposure
        tests_run += 1;
        let install_vulns = self.test_installation_exposure(url).await;
        vulnerabilities.extend(install_vulns);

        // Test 7: Cron.php exposure
        tests_run += 1;
        let cron_vulns = self.test_cron_exposure(url).await;
        vulnerabilities.extend(cron_vulns);

        // Test 8: REST/JSON API exposure
        tests_run += 1;
        let api_vulns = self.test_api_exposure(url, &version).await;
        vulnerabilities.extend(api_vulns);

        // Test 9: Module vulnerabilities
        tests_run += 1;
        let module_vulns = self.test_module_vulnerabilities(url, &version).await;
        vulnerabilities.extend(module_vulns);

        // Test 10: Directory listing
        tests_run += 1;
        let listing_vulns = self.test_directory_listing(url).await;
        vulnerabilities.extend(listing_vulns);

        // Test 11: Backup file exposure
        tests_run += 1;
        let backup_vulns = self.test_backup_exposure(url).await;
        vulnerabilities.extend(backup_vulns);

        // Test 12: Private files access
        tests_run += 1;
        let private_vulns = self.test_private_files(url).await;
        vulnerabilities.extend(private_vulns);

        // Test 13: Status report access
        tests_run += 1;
        let status_vulns = self.test_status_report_access(url).await;
        vulnerabilities.extend(status_vulns);

        // Deep scans (not in fast mode)
        if config.scan_mode.as_str() != "fast" {
            // Test 14: PHP filter module detection
            tests_run += 1;
            let php_vulns = self.test_php_filter_module(url).await;
            vulnerabilities.extend(php_vulns);

            // Test 15: Form API testing (Drupalgeddon2 variants)
            tests_run += 1;
            let form_vulns = self.test_form_api_vulnerabilities(url, &version).await;
            vulnerabilities.extend(form_vulns);
        }

        info!(
            "[Drupal] Scan complete: {} vulnerabilities found in {} tests",
            vulnerabilities.len(),
            tests_run
        );

        Ok((vulnerabilities, tests_run))
    }

    /// Detect if site is Drupal and get version
    async fn detect_drupal(&self, url: &str) -> Option<(bool, Option<DrupalVersion>)> {
        let base_url = self.get_base_url(url);
        let mut version: Option<DrupalVersion> = None;

        // Check for Drupal indicators in main page
        if let Ok(response) = self.http_client.get(url).await {
            let drupal_indicators = vec![
                "Drupal",
                "drupal.js",
                "drupal.css",
                "/sites/default/",
                "/sites/all/",
                "Drupal.settings",
                "drupal-link-system",
            ];

            let is_drupal = drupal_indicators.iter().any(|i| response.body.contains(i));

            // Check generator meta tag
            if let Some(re) = Regex::new(r#"<meta[^>]*generator[^>]*Drupal\s*(\d+)"#).ok() {
                if let Some(cap) = re.captures(&response.body) {
                    if let Some(major) = cap.get(1) {
                        version = Some(DrupalVersion {
                            major: major.as_str().parse().unwrap_or(0),
                            minor: 0,
                            patch: 0,
                        });
                    }
                }
            }

            if is_drupal || version.is_some() {
                return Some((true, version));
            }

            // Check X-Generator header
            if let Some(generator) = response.headers.get("x-generator") {
                if generator.to_lowercase().contains("drupal") {
                    let version_re = Regex::new(r#"Drupal\s*(\d+)(?:\.(\d+))?(?:\.(\d+))?"#).ok();
                    if let Some(re) = version_re {
                        if let Some(cap) = re.captures(generator) {
                            version = Some(DrupalVersion {
                                major: cap
                                    .get(1)
                                    .and_then(|m| m.as_str().parse().ok())
                                    .unwrap_or(0),
                                minor: cap
                                    .get(2)
                                    .and_then(|m| m.as_str().parse().ok())
                                    .unwrap_or(0),
                                patch: cap
                                    .get(3)
                                    .and_then(|m| m.as_str().parse().ok())
                                    .unwrap_or(0),
                            });
                        }
                    }
                    return Some((true, version));
                }
            }
        }

        // Try CHANGELOG.txt for version detection
        let changelog_url = format!("{}/CHANGELOG.txt", base_url);
        if let Ok(response) = self.http_client.get(&changelog_url).await {
            if response.status_code == 200 && response.body.contains("Drupal") {
                let version_re = Regex::new(r#"Drupal\s*(\d+)\.(\d+)(?:\.(\d+))?"#).ok();
                if let Some(re) = version_re {
                    if let Some(cap) = re.captures(&response.body) {
                        version = Some(DrupalVersion {
                            major: cap
                                .get(1)
                                .and_then(|m| m.as_str().parse().ok())
                                .unwrap_or(0),
                            minor: cap
                                .get(2)
                                .and_then(|m| m.as_str().parse().ok())
                                .unwrap_or(0),
                            patch: cap
                                .get(3)
                                .and_then(|m| m.as_str().parse().ok())
                                .unwrap_or(0),
                        });
                    }
                }
                return Some((true, version));
            }
        }

        // Try user/login path
        let login_url = format!("{}/user/login", base_url);
        if let Ok(response) = self.http_client.get(&login_url).await {
            if response.status_code == 200
                && (response.body.contains("drupal") || response.body.contains("Drupal"))
            {
                return Some((true, version));
            }
        }

        None
    }

    /// Test for Drupalgeddon vulnerabilities
    async fn test_drupalgeddon(
        &self,
        url: &str,
        version: &Option<DrupalVersion>,
    ) -> Vec<Vulnerability> {
        let mut vulnerabilities = Vec::new();
        let base_url = self.get_base_url(url);

        if let Some(ver) = version {
            // Check for Drupalgeddon (SA-CORE-2014-005) - Drupal 7 < 7.32
            if ver.major == 7 && ver.minor < 32 {
                vulnerabilities.push(Vulnerability {
                    id: format!("drupal_drupalgeddon1_{}", Self::generate_id()),
                    vuln_type: "Drupalgeddon (SA-CORE-2014-005)".to_string(),
                    severity: Severity::Critical,
                    confidence: Confidence::High,
                    category: "Remote Code Execution".to_string(),
                    url: base_url.clone(),
                    parameter: None,
                    payload: "SQL injection in form API".to_string(),
                    description: format!(
                        "Drupal version {}.{}.{} is vulnerable to Drupalgeddon (CVE-2014-3704).\n\
                        This SQL injection vulnerability allows unauthenticated attackers to \
                        execute arbitrary PHP code on the server.\n\n\
                        This is one of the most critical Drupal vulnerabilities ever discovered.",
                        ver.major, ver.minor, ver.patch
                    ),
                    evidence: Some(format!(
                        "Drupal version {}.{}.{} detected",
                        ver.major, ver.minor, ver.patch
                    )),
                    cwe: "CWE-89".to_string(),
                    cvss: 10.0,
                    verified: true,
                    false_positive: false,
                    remediation:
                        "CRITICAL: Update Drupal core to version 7.32 or later IMMEDIATELY.\n\
                                  1. Backup your database and files\n\
                                  2. Update Drupal core: drush up drupal\n\
                                  3. Check for signs of compromise\n\
                                  4. Review user accounts for unauthorized additions\n\
                                  Reference: https://www.drupal.org/SA-CORE-2014-005"
                            .to_string(),
                    discovered_at: chrono::Utc::now().to_rfc3339(),
                    ml_data: None,
                });
            }

            // Check for Drupalgeddon2 (SA-CORE-2018-002) - Drupal 7 < 7.58, 8 < 8.3.9, 8.4.x < 8.4.6, 8.5.x < 8.5.1
            let drupalgeddon2_vulnerable = match ver.major {
                7 => ver.minor < 58,
                8 => {
                    (ver.minor < 3)
                        || (ver.minor == 3 && ver.patch < 9)
                        || (ver.minor == 4 && ver.patch < 6)
                        || (ver.minor == 5 && ver.patch < 1)
                }
                _ => false,
            };

            if drupalgeddon2_vulnerable {
                vulnerabilities.push(Vulnerability {
                    id: format!("drupal_drupalgeddon2_{}", Self::generate_id()),
                    vuln_type: "Drupalgeddon2 (SA-CORE-2018-002)".to_string(),
                    severity: Severity::Critical,
                    confidence: Confidence::High,
                    category: "Remote Code Execution".to_string(),
                    url: base_url.clone(),
                    parameter: None,
                    payload: "Form API RCE".to_string(),
                    description: format!(
                        "Drupal version {}.{}.{} is vulnerable to Drupalgeddon2 (CVE-2018-7600).\n\
                        This vulnerability allows unauthenticated remote code execution via the \
                        Form API by exploiting the render array system.\n\n\
                        Exploit code is publicly available and actively exploited in the wild.",
                        ver.major, ver.minor, ver.patch
                    ),
                    evidence: Some(format!(
                        "Drupal version {}.{}.{} detected",
                        ver.major, ver.minor, ver.patch
                    )),
                    cwe: "CWE-94".to_string(),
                    cvss: 10.0,
                    verified: true,
                    false_positive: false,
                    remediation: "CRITICAL: Update Drupal core IMMEDIATELY.\n\
                                  Drupal 7: Update to 7.58 or later\n\
                                  Drupal 8.3.x: Update to 8.3.9\n\
                                  Drupal 8.4.x: Update to 8.4.6\n\
                                  Drupal 8.5.x: Update to 8.5.1 or later\n\
                                  Reference: https://www.drupal.org/SA-CORE-2018-002"
                        .to_string(),
                    discovered_at: chrono::Utc::now().to_rfc3339(),
                    ml_data: None,
                });
            }

            // Check for Drupalgeddon3 (SA-CORE-2018-004) - Drupal 7 < 7.59, 8 < 8.4.8, 8.5.x < 8.5.3
            let drupalgeddon3_vulnerable = match ver.major {
                7 => ver.minor < 59,
                8 => {
                    (ver.minor < 4)
                        || (ver.minor == 4 && ver.patch < 8)
                        || (ver.minor == 5 && ver.patch < 3)
                }
                _ => false,
            };

            if drupalgeddon3_vulnerable && !drupalgeddon2_vulnerable {
                vulnerabilities.push(Vulnerability {
                    id: format!("drupal_drupalgeddon3_{}", Self::generate_id()),
                    vuln_type: "Drupalgeddon3 (SA-CORE-2018-004)".to_string(),
                    severity: Severity::Critical,
                    confidence: Confidence::High,
                    category: "Remote Code Execution".to_string(),
                    url: base_url.clone(),
                    parameter: None,
                    payload: "Authenticated RCE".to_string(),
                    description: format!(
                        "Drupal version {}.{}.{} is vulnerable to Drupalgeddon3 (CVE-2018-7602).\n\
                        This vulnerability allows authenticated users with the ability to delete \
                        content to execute arbitrary code.",
                        ver.major, ver.minor, ver.patch
                    ),
                    evidence: Some(format!(
                        "Drupal version {}.{}.{} detected",
                        ver.major, ver.minor, ver.patch
                    )),
                    cwe: "CWE-94".to_string(),
                    cvss: 9.0,
                    verified: true,
                    false_positive: false,
                    remediation: "Update Drupal core immediately.\n\
                                  Drupal 7: Update to 7.59 or later\n\
                                  Drupal 8.4.x: Update to 8.4.8\n\
                                  Drupal 8.5.x: Update to 8.5.3 or later\n\
                                  Reference: https://www.drupal.org/SA-CORE-2018-004"
                        .to_string(),
                    discovered_at: chrono::Utc::now().to_rfc3339(),
                    ml_data: None,
                });
            }
        }

        vulnerabilities
    }

    /// Test user enumeration
    async fn test_user_enumeration(
        &self,
        url: &str,
        version: &Option<DrupalVersion>,
    ) -> Vec<Vulnerability> {
        let mut vulnerabilities = Vec::new();
        let base_url = self.get_base_url(url);
        let mut found_users = Vec::new();

        // Test user/N paths (Drupal 7 style)
        for i in 1..=10 {
            let user_url = format!("{}/user/{}", base_url, i);
            if let Ok(response) = self.http_client.get(&user_url).await {
                if response.status_code == 200 {
                    // Extract username from page
                    let title_re = Regex::new(r#"<title>([^|<]+)"#).ok();
                    if let Some(re) = title_re {
                        if let Some(cap) = re.captures(&response.body) {
                            if let Some(name) = cap.get(1) {
                                let username = name.as_str().trim().to_string();
                                if !username.is_empty() && !found_users.contains(&username) {
                                    found_users.push(username);
                                }
                            }
                        }
                    }
                }
            }
        }

        // Test JSON API user enumeration (Drupal 8+)
        if version.as_ref().map(|v| v.major >= 8).unwrap_or(true) {
            let json_endpoints = vec![
                "/jsonapi/user/user",
                "/api/user",
                "/?_format=json&_path=/user",
            ];

            for endpoint in json_endpoints {
                let test_url = format!("{}{}", base_url, endpoint);
                if let Ok(response) = self.http_client.get(&test_url).await {
                    if response.status_code == 200 && response.body.contains("\"name\"") {
                        let name_re = Regex::new(r#""name"\s*:\s*"([^"]+)""#).ok();
                        if let Some(re) = name_re {
                            for cap in re.captures_iter(&response.body) {
                                if let Some(name) = cap.get(1) {
                                    let username = name.as_str().to_string();
                                    if !found_users.contains(&username) {
                                        found_users.push(username);
                                    }
                                }
                            }
                        }

                        vulnerabilities.push(Vulnerability {
                            id: format!("drupal_jsonapi_users_{}", Self::generate_id()),
                            vuln_type: "Drupal JSON API User Enumeration".to_string(),
                            severity: Severity::Medium,
                            confidence: Confidence::High,
                            category: "Information Disclosure".to_string(),
                            url: test_url,
                            parameter: None,
                            payload: endpoint.to_string(),
                            description: "Drupal JSON API exposes user information. \
                                This allows attackers to enumerate usernames for targeted attacks.".to_string(),
                            evidence: Some(format!(
                                "JSON API returns user data. Preview: {}...",
                                &response.body.chars().take(300).collect::<String>()
                            )),
                            cwe: "CWE-200".to_string(),
                            cvss: 5.3,
                            verified: true,
                            false_positive: false,
                            remediation: "1. Disable JSON API for anonymous users\n\
                                          2. Configure permissions to restrict user endpoint access\n\
                                          3. Use the JSON:API Extras module for fine-grained control".to_string(),
                            discovered_at: chrono::Utc::now().to_rfc3339(),
                ml_data: None,
                        });
                        break;
                    }
                }
            }
        }

        // Test password reset user enumeration
        let reset_url = format!("{}/user/password", base_url);
        if let Ok(response) = self.http_client.get(&reset_url).await {
            if response.status_code == 200 {
                // Try to submit password reset for a known user
                let test_users = vec!["admin", "root", "administrator", "webmaster"];
                for test_user in test_users {
                    let form_data = format!("name={}&form_id=user_pass", test_user);
                    if let Ok(reset_response) = self.http_client.post(&reset_url, form_data).await {
                        // Different error messages can indicate if user exists
                        if reset_response.body.contains("Further instructions")
                            || reset_response.body.contains("sent to")
                            || (reset_response.status_code == 302
                                && !reset_response.body.contains("not recognized"))
                        {
                            found_users.push(test_user.to_string());
                        }
                    }
                }
            }
        }

        if !found_users.is_empty() {
            vulnerabilities.push(Vulnerability {
                id: format!("drupal_user_enum_{}", Self::generate_id()),
                vuln_type: "Drupal User Enumeration".to_string(),
                severity: Severity::Medium,
                confidence: Confidence::High,
                category: "Information Disclosure".to_string(),
                url: format!("{}/user/1", base_url),
                parameter: None,
                payload: "/user/N".to_string(),
                description: format!(
                    "Drupal allows user enumeration. {} usernames discovered: {}",
                    found_users.len(),
                    found_users.join(", ")
                ),
                evidence: Some(format!("Enumerated users: {}", found_users.join(", "))),
                cwe: "CWE-200".to_string(),
                cvss: 5.3,
                verified: true,
                false_positive: false,
                remediation:
                    "1. Install and configure the Username Enumeration Prevention module\n\
                              2. Configure permissions to restrict user profile access\n\
                              3. Use the Rabbit Hole module to control access to user pages"
                        .to_string(),
                discovered_at: chrono::Utc::now().to_rfc3339(),
                ml_data: None,
            });
        }

        vulnerabilities
    }

    /// Test version disclosure
    async fn test_version_disclosure(&self, url: &str) -> Vec<Vulnerability> {
        let mut vulnerabilities = Vec::new();
        let base_url = self.get_base_url(url);

        let version_files = vec![
            ("/CHANGELOG.txt", "Changelog - version history"),
            ("/core/CHANGELOG.txt", "Drupal 8+ core changelog"),
            ("/INSTALL.txt", "Installation instructions"),
            ("/core/INSTALL.txt", "Drupal 8+ install file"),
            ("/README.txt", "Readme file"),
            ("/core/README.txt", "Drupal 8+ readme"),
            ("/UPGRADE.txt", "Upgrade instructions"),
            ("/LICENSE.txt", "License file"),
            ("/MAINTAINERS.txt", "Maintainers list"),
        ];

        for (file, description) in version_files {
            let test_url = format!("{}{}", base_url, file);
            if let Ok(response) = self.http_client.get(&test_url).await {
                if response.status_code == 200 && response.body.contains("Drupal") {
                    // Extract version if present
                    let version_re = Regex::new(r#"Drupal\s*(\d+\.\d+(?:\.\d+)?)"#).ok();
                    let version = version_re.and_then(|re| {
                        re.captures(&response.body)
                            .and_then(|cap| cap.get(1).map(|v| v.as_str().to_string()))
                    });

                    vulnerabilities.push(Vulnerability {
                        id: format!("drupal_version_file_{}", Self::generate_id()),
                        vuln_type: "Drupal Version Disclosure via File".to_string(),
                        severity: Severity::Low,
                        confidence: Confidence::High,
                        category: "Information Disclosure".to_string(),
                        url: test_url,
                        parameter: None,
                        payload: file.to_string(),
                        description: format!(
                            "Drupal {} exposed: {}\n{}",
                            description,
                            file,
                            version
                                .as_ref()
                                .map(|v| format!("Detected version: {}", v))
                                .unwrap_or_default()
                        ),
                        evidence: Some(format!(
                            "File content preview: {}...",
                            &response.body.chars().take(300).collect::<String>()
                        )),
                        cwe: "CWE-200".to_string(),
                        cvss: 3.7,
                        verified: true,
                        false_positive: false,
                        remediation: "Remove or restrict access to version disclosure files:\n\
                                      Add to .htaccess:\n\
                                      <FilesMatch \"\\.(txt|md)$\">\n\
                                        Order Allow,Deny\n\
                                        Deny from all\n\
                                      </FilesMatch>"
                            .to_string(),
                        discovered_at: chrono::Utc::now().to_rfc3339(),
                        ml_data: None,
                    });
                    break; // Found one, no need to continue
                }
            }
        }

        vulnerabilities
    }

    /// Test configuration file exposure
    async fn test_config_exposure(&self, url: &str) -> Vec<Vulnerability> {
        let mut vulnerabilities = Vec::new();
        let base_url = self.get_base_url(url);

        let config_files = vec![
            "/sites/default/settings.php",
            "/sites/default/settings.php.bak",
            "/sites/default/settings.php.old",
            "/sites/default/settings.php~",
            "/sites/default/settings.local.php",
            "/sites/default/services.yml",
            "/sites/default/default.settings.php",
            "/.env",
            "/.env.local",
        ];

        for file in config_files {
            let test_url = format!("{}{}", base_url, file);
            if let Ok(response) = self.http_client.get(&test_url).await {
                if response.status_code == 200 {
                    let has_sensitive = response.body.contains("database")
                        || response.body.contains("password")
                        || response.body.contains("DB_")
                        || response.body.contains("$databases")
                        || response.body.contains("hash_salt");

                    if has_sensitive {
                        vulnerabilities.push(Vulnerability {
                            id: format!("drupal_config_exposed_{}", Self::generate_id()),
                            vuln_type: "Drupal Configuration File Exposed".to_string(),
                            severity: Severity::Critical,
                            confidence: Confidence::High,
                            category: "Sensitive Data Exposure".to_string(),
                            url: test_url,
                            parameter: None,
                            payload: file.to_string(),
                            description: format!(
                                "Drupal configuration file exposed: {}\n\
                                This file may contain database credentials, hash salt, \
                                and other sensitive configuration values.",
                                file
                            ),
                            evidence: Some(
                                "Configuration file contains sensitive settings".to_string(),
                            ),
                            cwe: "CWE-200".to_string(),
                            cvss: 9.8,
                            verified: true,
                            false_positive: false,
                            remediation:
                                "1. CRITICAL: Change all exposed credentials immediately!\n\
                                          2. Remove backup configuration files\n\
                                          3. Ensure settings.php is not publicly accessible\n\
                                          4. Regenerate hash_salt\n\
                                          5. Check for signs of compromise"
                                    .to_string(),
                            discovered_at: chrono::Utc::now().to_rfc3339(),
                            ml_data: None,
                        });
                        break;
                    }
                }
            }
        }

        vulnerabilities
    }

    /// Test admin paths exposure
    async fn test_admin_exposure(&self, url: &str) -> Vec<Vulnerability> {
        let mut vulnerabilities = Vec::new();
        let base_url = self.get_base_url(url);

        let admin_paths = vec![
            "/admin",
            "/admin/config",
            "/admin/structure",
            "/admin/people",
            "/admin/modules",
            "/admin/reports/status",
            "/admin/reports/dblog",
        ];

        for path in admin_paths {
            let test_url = format!("{}{}", base_url, path);
            if let Ok(response) = self.http_client.get(&test_url).await {
                if response.status_code == 200 {
                    let has_admin_content = response.body.contains("Administration")
                        || response.body.contains("admin-menu")
                        || response.body.contains("system-admin")
                        || response.body.contains("toolbar-menu");

                    if has_admin_content {
                        vulnerabilities.push(Vulnerability {
                            id: format!("drupal_admin_access_{}", Self::generate_id()),
                            vuln_type: "Drupal Admin Area Accessible".to_string(),
                            severity: Severity::Critical,
                            confidence: Confidence::High,
                            category: "Access Control".to_string(),
                            url: test_url,
                            parameter: None,
                            payload: path.to_string(),
                            description: format!(
                                "Drupal admin area is accessible without authentication: {}\n\
                                This indicates a severe misconfiguration or compromised site.",
                                path
                            ),
                            evidence: Some("Admin interface content detected".to_string()),
                            cwe: "CWE-284".to_string(),
                            cvss: 9.8,
                            verified: true,
                            false_positive: false,
                            remediation:
                                "1. CRITICAL: Review and fix user permissions immediately\n\
                                          2. Check for unauthorized admin accounts\n\
                                          3. Review recent changes to the site\n\
                                          4. Check for signs of compromise\n\
                                          5. Ensure anonymous user role has no admin permissions"
                                    .to_string(),
                            discovered_at: chrono::Utc::now().to_rfc3339(),
                            ml_data: None,
                        });
                        break;
                    }
                }
            }
        }

        vulnerabilities
    }

    /// Test installation/update file exposure
    async fn test_installation_exposure(&self, url: &str) -> Vec<Vulnerability> {
        let mut vulnerabilities = Vec::new();
        let base_url = self.get_base_url(url);

        // Test install.php
        let install_url = format!("{}/install.php", base_url);
        if let Ok(response) = self.http_client.get(&install_url).await {
            if response.status_code == 200 {
                let is_installer = response.body.contains("Install Drupal")
                    || response.body.contains("installation")
                    || response.body.contains("Choose language");

                if is_installer {
                    vulnerabilities.push(Vulnerability {
                        id: format!("drupal_install_exposed_{}", Self::generate_id()),
                        vuln_type: "Drupal Installation Script Accessible".to_string(),
                        severity: Severity::High,
                        confidence: Confidence::High,
                        category: "Misconfiguration".to_string(),
                        url: install_url,
                        parameter: None,
                        payload: "/install.php".to_string(),
                        description: "Drupal installation script is accessible. \
                            While Drupal prevents reinstallation if already configured, \
                            this script should be removed or protected."
                            .to_string(),
                        evidence: Some(
                            "Installation script responds with installer page".to_string(),
                        ),
                        cwe: "CWE-16".to_string(),
                        cvss: 7.5,
                        verified: true,
                        false_positive: false,
                        remediation: "1. Remove install.php after installation\n\
                                      2. Or block access via .htaccess:\n\
                                      <Files install.php>\n\
                                        Order Allow,Deny\n\
                                        Deny from all\n\
                                      </Files>"
                            .to_string(),
                        discovered_at: chrono::Utc::now().to_rfc3339(),
                        ml_data: None,
                    });
                }
            }
        }

        // Test update.php
        let update_url = format!("{}/update.php", base_url);
        if let Ok(response) = self.http_client.get(&update_url).await {
            if response.status_code == 200 && response.body.contains("update") {
                vulnerabilities.push(Vulnerability {
                    id: format!("drupal_update_exposed_{}", Self::generate_id()),
                    vuln_type: "Drupal Update Script Accessible".to_string(),
                    severity: Severity::High,
                    confidence: Confidence::High,
                    category: "Misconfiguration".to_string(),
                    url: update_url,
                    parameter: None,
                    payload: "/update.php".to_string(),
                    description: "Drupal update.php script is accessible. \
                        This script should be protected to prevent unauthorized database updates."
                        .to_string(),
                    evidence: Some("Update script accessible".to_string()),
                    cwe: "CWE-16".to_string(),
                    cvss: 7.5,
                    verified: true,
                    false_positive: false,
                    remediation: "1. Set $settings['update_free_access'] = FALSE in settings.php\n\
                                  2. Block access via .htaccess when not needed"
                        .to_string(),
                    discovered_at: chrono::Utc::now().to_rfc3339(),
                    ml_data: None,
                });
            }
        }

        vulnerabilities
    }

    /// Test cron.php exposure
    async fn test_cron_exposure(&self, url: &str) -> Vec<Vulnerability> {
        let mut vulnerabilities = Vec::new();
        let base_url = self.get_base_url(url);

        // Test cron.php without key
        let cron_url = format!("{}/cron.php", base_url);
        if let Ok(response) = self.http_client.get(&cron_url).await {
            if response.status_code == 200 {
                vulnerabilities.push(Vulnerability {
                    id: format!("drupal_cron_exposed_{}", Self::generate_id()),
                    vuln_type: "Drupal Cron Accessible Without Key".to_string(),
                    severity: Severity::Low,
                    confidence: Confidence::High,
                    category: "Misconfiguration".to_string(),
                    url: cron_url,
                    parameter: None,
                    payload: "/cron.php".to_string(),
                    description: "Drupal cron.php is accessible without cron key. \
                        This can be used to trigger cron tasks externally or for DoS attacks."
                        .to_string(),
                    evidence: Some("Cron script accessible without authentication".to_string()),
                    cwe: "CWE-16".to_string(),
                    cvss: 3.7,
                    verified: true,
                    false_positive: false,
                    remediation: "Configure cron key in settings.php and update cron URL:\n\
                                  $settings['cron_key'] = 'YOUR_SECRET_KEY';\n\
                                  Then use: /cron/YOUR_SECRET_KEY"
                        .to_string(),
                    discovered_at: chrono::Utc::now().to_rfc3339(),
                    ml_data: None,
                });
            }
        }

        vulnerabilities
    }

    /// Test REST/JSON API exposure
    async fn test_api_exposure(
        &self,
        url: &str,
        _version: &Option<DrupalVersion>,
    ) -> Vec<Vulnerability> {
        let mut vulnerabilities = Vec::new();
        let base_url = self.get_base_url(url);

        let api_endpoints = vec![
            ("/jsonapi", "JSON:API"),
            ("/rest", "REST API"),
            ("/api", "API endpoint"),
            ("/services", "Services module"),
            ("/?_format=json", "Format parameter"),
            ("/?_format=hal_json", "HAL+JSON format"),
        ];

        for (endpoint, name) in api_endpoints {
            let test_url = format!("{}{}", base_url, endpoint);
            if let Ok(response) = self.http_client.get(&test_url).await {
                if response.status_code == 200
                    && (response.body.contains("\"data\"")
                        || response.body.contains("\"links\"")
                        || response.body.contains("\"jsonapi\"")
                        || response.body.contains("services"))
                {
                    vulnerabilities.push(Vulnerability {
                        id: format!("drupal_api_exposed_{}", Self::generate_id()),
                        vuln_type: format!("Drupal {} Exposed", name),
                        severity: Severity::Medium,
                        confidence: Confidence::High,
                        category: "API Security".to_string(),
                        url: test_url,
                        parameter: None,
                        payload: endpoint.to_string(),
                        description: format!(
                            "Drupal {} is publicly accessible.\n\
                            This may expose sensitive data or functionality depending on configuration.",
                            name
                        ),
                        evidence: Some(format!(
                            "API response preview: {}...",
                            &response.body.chars().take(300).collect::<String>()
                        )),
                        cwe: "CWE-200".to_string(),
                        cvss: 5.3,
                        verified: true,
                        false_positive: false,
                        remediation: "1. Review API permissions and restrict anonymous access\n\
                                      2. Configure JSON:API resource types carefully\n\
                                      3. Use authentication for sensitive endpoints\n\
                                      4. Consider disabling unused API modules".to_string(),
                        discovered_at: chrono::Utc::now().to_rfc3339(),
                ml_data: None,
                    });
                    break;
                }
            }
        }

        vulnerabilities
    }

    /// Test for vulnerable modules
    async fn test_module_vulnerabilities(
        &self,
        url: &str,
        version: &Option<DrupalVersion>,
    ) -> Vec<Vulnerability> {
        let mut vulnerabilities = Vec::new();
        let base_url = self.get_base_url(url);

        // Common module paths to check
        let modules_base = if version.as_ref().map(|v| v.major >= 8).unwrap_or(false) {
            vec!["/modules/contrib/", "/modules/"]
        } else {
            vec!["/sites/all/modules/", "/sites/default/modules/"]
        };

        for (module_name, vulns) in &self.known_vulnerable_modules {
            for base in &modules_base {
                let info_path = format!("{}{}{}/{}.info", base_url, base, module_name, module_name);
                let info_yml_path = format!(
                    "{}{}{}/{}.info.yml",
                    base_url, base, module_name, module_name
                );

                for path in [info_path, info_yml_path] {
                    if let Ok(response) = self.http_client.get(&path).await {
                        if response.status_code == 200 {
                            // Extract version
                            let version_re = Regex::new(r#"version\s*[=:]\s*["']?([^"'\n]+)"#).ok();
                            let module_version = version_re.and_then(|re| {
                                re.captures(&response.body)
                                    .and_then(|cap| cap.get(1).map(|v| v.as_str().to_string()))
                            });

                            if let Some(ver) = module_version {
                                for vuln in vulns {
                                    if Self::is_version_vulnerable(&ver, &vuln.vulnerable_version) {
                                        vulnerabilities.push(Vulnerability {
                                            id: format!(
                                                "drupal_module_vuln_{}",
                                                Self::generate_id()
                                            ),
                                            vuln_type: format!(
                                                "Vulnerable Drupal Module: {}",
                                                module_name
                                            ),
                                            severity: vuln.severity.clone(),
                                            confidence: Confidence::High,
                                            category: "Known Vulnerability".to_string(),
                                            url: path.clone(),
                                            parameter: None,
                                            payload: module_name.clone(),
                                            description: format!(
                                                "Vulnerable module detected: {} version {}\n\n\
                                                Vulnerability: {}\n\
                                                SA ID: {}\n\
                                                CVE: {}",
                                                module_name,
                                                ver,
                                                vuln.description,
                                                vuln.sa_id.as_ref().unwrap_or(&"N/A".to_string()),
                                                vuln.cve.as_ref().unwrap_or(&"N/A".to_string())
                                            ),
                                            evidence: Some(format!(
                                                "Module {} version {} detected",
                                                module_name, ver
                                            )),
                                            cwe: "CWE-1035".to_string(),
                                            cvss: match vuln.severity {
                                                Severity::Critical => 9.8,
                                                Severity::High => 7.5,
                                                Severity::Medium => 5.3,
                                                _ => 3.7,
                                            },
                                            verified: true,
                                            false_positive: false,
                                            remediation: format!(
                                                "Update {} module to the latest version:\n\
                                                drush up {}\n\
                                                Reference: https://www.drupal.org/project/{}",
                                                module_name, module_name, module_name
                                            ),
                                            discovered_at: chrono::Utc::now().to_rfc3339(),
                                            ml_data: None,
                                        });
                                    }
                                }
                            }
                            break;
                        }
                    }
                }
            }
        }

        vulnerabilities
    }

    /// Test directory listing
    async fn test_directory_listing(&self, url: &str) -> Vec<Vulnerability> {
        let mut vulnerabilities = Vec::new();
        let base_url = self.get_base_url(url);

        let directories = vec![
            "/sites/default/files/",
            "/sites/all/modules/",
            "/sites/all/themes/",
            "/modules/",
            "/themes/",
            "/profiles/",
        ];

        for dir in directories {
            let test_url = format!("{}{}", base_url, dir);
            if let Ok(response) = self.http_client.get(&test_url).await {
                if response.status_code == 200 {
                    let has_listing = response.body.contains("Index of")
                        || response.body.contains("Parent Directory");

                    if has_listing {
                        vulnerabilities.push(Vulnerability {
                            id: format!("drupal_dir_listing_{}", Self::generate_id()),
                            vuln_type: "Drupal Directory Listing Enabled".to_string(),
                            severity: Severity::Medium,
                            confidence: Confidence::High,
                            category: "Information Disclosure".to_string(),
                            url: test_url,
                            parameter: None,
                            payload: dir.to_string(),
                            description: format!("Directory listing enabled at: {}", dir),
                            evidence: Some("Directory index visible".to_string()),
                            cwe: "CWE-548".to_string(),
                            cvss: 5.3,
                            verified: true,
                            false_positive: false,
                            remediation: "Add to .htaccess: Options -Indexes".to_string(),
                            discovered_at: chrono::Utc::now().to_rfc3339(),
                            ml_data: None,
                        });
                    }
                }
            }
        }

        vulnerabilities
    }

    /// Test backup file exposure
    async fn test_backup_exposure(&self, url: &str) -> Vec<Vulnerability> {
        let mut vulnerabilities = Vec::new();
        let base_url = self.get_base_url(url);

        let backup_patterns = vec![
            "/backup.sql",
            "/database.sql",
            "/drupal.sql",
            "/sites/default/files/backup.sql",
            "/sites/default/files/backup/",
        ];

        for pattern in backup_patterns {
            let test_url = format!("{}{}", base_url, pattern);
            if let Ok(response) = self.http_client.get(&test_url).await {
                if response.status_code == 200
                    && (response.body.contains("CREATE TABLE")
                        || response.body.contains("INSERT INTO")
                        || response.body.contains("Index of"))
                {
                    let severity = if response.body.contains("CREATE TABLE") {
                        Severity::Critical
                    } else {
                        Severity::High
                    };

                    vulnerabilities.push(Vulnerability {
                        id: format!("drupal_backup_{}", Self::generate_id()),
                        vuln_type: "Drupal Database Backup Exposed".to_string(),
                        severity,
                        confidence: Confidence::High,
                        category: "Sensitive Data Exposure".to_string(),
                        url: test_url,
                        parameter: None,
                        payload: pattern.to_string(),
                        description: "Database backup or backup directory exposed publicly."
                            .to_string(),
                        evidence: Some("Backup file/directory accessible".to_string()),
                        cwe: "CWE-200".to_string(),
                        cvss: 9.8,
                        verified: true,
                        false_positive: false,
                        remediation: "1. Remove backup files immediately\n\
                                      2. Change all credentials\n\
                                      3. Never store backups in web-accessible directories"
                            .to_string(),
                        discovered_at: chrono::Utc::now().to_rfc3339(),
                        ml_data: None,
                    });
                    break;
                }
            }
        }

        vulnerabilities
    }

    /// Test private files access
    async fn test_private_files(&self, url: &str) -> Vec<Vulnerability> {
        let mut vulnerabilities = Vec::new();
        let base_url = self.get_base_url(url);

        let private_paths = vec![
            "/sites/default/files/private/",
            "/system/files/",
            "/private/",
        ];

        for path in private_paths {
            let test_url = format!("{}{}", base_url, path);
            if let Ok(response) = self.http_client.get(&test_url).await {
                if response.status_code == 200 && response.body.contains("Index of") {
                    vulnerabilities.push(Vulnerability {
                        id: format!("drupal_private_files_{}", Self::generate_id()),
                        vuln_type: "Drupal Private Files Directory Exposed".to_string(),
                        severity: Severity::High,
                        confidence: Confidence::High,
                        category: "Access Control".to_string(),
                        url: test_url,
                        parameter: None,
                        payload: path.to_string(),
                        description: "Private files directory is publicly accessible with directory listing.".to_string(),
                        evidence: Some("Private directory listing visible".to_string()),
                        cwe: "CWE-284".to_string(),
                        cvss: 7.5,
                        verified: true,
                        false_positive: false,
                        remediation: "1. Move private files outside web root\n\
                                      2. Configure file system path in admin/config/media/file-system\n\
                                      3. Disable directory listing".to_string(),
                        discovered_at: chrono::Utc::now().to_rfc3339(),
                ml_data: None,
                    });
                }
            }
        }

        vulnerabilities
    }

    /// Test status report access
    async fn test_status_report_access(&self, url: &str) -> Vec<Vulnerability> {
        let mut vulnerabilities = Vec::new();
        let base_url = self.get_base_url(url);

        let status_url = format!("{}/admin/reports/status", base_url);
        if let Ok(response) = self.http_client.get(&status_url).await {
            if response.status_code == 200 && response.body.contains("Status report") {
                vulnerabilities.push(Vulnerability {
                    id: format!("drupal_status_report_{}", Self::generate_id()),
                    vuln_type: "Drupal Status Report Publicly Accessible".to_string(),
                    severity: Severity::High,
                    confidence: Confidence::High,
                    category: "Information Disclosure".to_string(),
                    url: status_url,
                    parameter: None,
                    payload: "/admin/reports/status".to_string(),
                    description: "Drupal status report page is publicly accessible. \
                        This page reveals detailed system information including PHP version, \
                        database info, and security issues."
                        .to_string(),
                    evidence: Some(
                        "Status report page accessible without authentication".to_string(),
                    ),
                    cwe: "CWE-200".to_string(),
                    cvss: 7.5,
                    verified: true,
                    false_positive: false,
                    remediation: "Review and fix permissions for administrator role. \
                        Status report should only be accessible to authenticated admins."
                        .to_string(),
                    discovered_at: chrono::Utc::now().to_rfc3339(),
                    ml_data: None,
                });
            }
        }

        vulnerabilities
    }

    /// Test PHP filter module
    async fn test_php_filter_module(&self, url: &str) -> Vec<Vulnerability> {
        let mut vulnerabilities = Vec::new();
        let base_url = self.get_base_url(url);

        // Check for PHP filter module presence
        let php_filter_paths = vec![
            "/modules/php/",
            "/core/modules/php/",
            "/sites/all/modules/php/",
        ];

        for path in php_filter_paths {
            let test_url = format!("{}{}", base_url, path);
            if let Ok(response) = self.http_client.get(&test_url).await {
                if response.status_code == 200 || response.status_code == 403 {
                    vulnerabilities.push(Vulnerability {
                        id: format!("drupal_php_filter_{}", Self::generate_id()),
                        vuln_type: "Drupal PHP Filter Module Detected".to_string(),
                        severity: Severity::High,
                        confidence: Confidence::Medium,
                        category: "Dangerous Configuration".to_string(),
                        url: test_url,
                        parameter: None,
                        payload: path.to_string(),
                        description: "PHP Filter module appears to be present. \
                            This module allows PHP code execution in content and is highly dangerous. \
                            It should never be enabled in production.".to_string(),
                        evidence: Some("PHP filter module directory detected".to_string()),
                        cwe: "CWE-94".to_string(),
                        cvss: 8.1,
                        verified: true,
                        false_positive: false,
                        remediation: "1. Disable and uninstall PHP Filter module immediately\n\
                                      2. Remove the module files from the server\n\
                                      3. Audit content for embedded PHP code\n\
                                      4. Use safer alternatives like Twig templates".to_string(),
                        discovered_at: chrono::Utc::now().to_rfc3339(),
                ml_data: None,
                    });
                    break;
                }
            }
        }

        vulnerabilities
    }

    /// Test Form API vulnerabilities
    async fn test_form_api_vulnerabilities(
        &self,
        url: &str,
        version: &Option<DrupalVersion>,
    ) -> Vec<Vulnerability> {
        let mut vulnerabilities = Vec::new();
        let base_url = self.get_base_url(url);

        // Test Drupalgeddon2 style payloads (non-destructive test)
        if version.as_ref().map(|v| v.major >= 8).unwrap_or(true) {
            let test_paths = vec!["/user/register", "/user/password", "/contact"];

            for path in test_paths {
                let test_url = format!("{}{}", base_url, path);
                if let Ok(response) = self.http_client.get(&test_url).await {
                    if response.status_code == 200 && response.body.contains("form") {
                        // Check for unprotected form elements
                        if response.body.contains("mail[#post_render]")
                            || response.body.contains("account[mail]")
                            || response.body.contains("#lazy_builder")
                        {
                            vulnerabilities.push(Vulnerability {
                                id: format!("drupal_form_api_{}", Self::generate_id()),
                                vuln_type: "Potentially Vulnerable Form API Usage".to_string(),
                                severity: Severity::Medium,
                                confidence: Confidence::Low,
                                category: "Code Quality".to_string(),
                                url: test_url,
                                parameter: None,
                                payload: path.to_string(),
                                description: "Form contains render elements that may be vulnerable \
                                    to Drupalgeddon2-style attacks if not properly sanitized.".to_string(),
                                evidence: Some("Suspicious form elements detected".to_string()),
                                cwe: "CWE-94".to_string(),
                                cvss: 5.3,
                                verified: false,
                                false_positive: false,
                                remediation: "Update Drupal core to the latest version and review form implementations.".to_string(),
                                discovered_at: chrono::Utc::now().to_rfc3339(),
                ml_data: None,
                            });
                        }
                    }
                }
            }
        }

        vulnerabilities
    }

    /// Helper: Get base URL
    fn get_base_url(&self, url: &str) -> String {
        if let Ok(parsed) = url::Url::parse(url) {
            let host = parsed.host_str().unwrap_or("localhost");
            let scheme = parsed.scheme();
            if let Some(port) = parsed.port() {
                format!("{}://{}:{}", scheme, host, port)
            } else {
                format!("{}://{}", scheme, host)
            }
        } else {
            url.to_string()
        }
    }

    /// Helper: Check if version is vulnerable
    fn is_version_vulnerable(current: &str, vulnerable: &str) -> bool {
        // Simple version comparison - in production would use semver
        let parse_version = |v: &str| -> Vec<u32> {
            v.split(|c: char| !c.is_numeric())
                .filter_map(|s| s.parse().ok())
                .collect()
        };

        let current_parts = parse_version(current);
        let vuln_parts = parse_version(vulnerable);

        for (c, v) in current_parts.iter().zip(vuln_parts.iter()) {
            if c < v {
                return true;
            }
            if c > v {
                return false;
            }
        }

        current_parts.len() <= vuln_parts.len()
    }

    /// Generate unique ID
    fn generate_id() -> String {
        use rand::Rng;
        let mut rng = rand::rng();
        format!("{:08x}", rng.random::<u32>())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_version_comparison() {
        assert!(DrupalSecurityScanner::is_version_vulnerable("7.31", "7.32"));
        assert!(DrupalSecurityScanner::is_version_vulnerable(
            "8.4.5", "8.4.6"
        ));
        assert!(!DrupalSecurityScanner::is_version_vulnerable(
            "7.58", "7.32"
        ));
    }
}