cipherrun 0.3.0

A fast, modular, and scalable TLS/SSL security scanner written in Rust
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
// Vulnerability Tester - Main orchestrator for all vulnerability checks

use super::{Severity, VulnerabilityResult, VulnerabilityType};
use crate::Result;
use crate::ciphers::tester::CipherTester;
use crate::protocols::{Protocol, tester::ProtocolTester};
use crate::utils::network::Target;
use std::collections::HashMap;

/// Collects a vulnerability result, logging any errors
fn collect_result(
    results: &mut Vec<VulnerabilityResult>,
    result: Result<VulnerabilityResult>,
    test_name: &str,
) {
    match result {
        Ok(r) => results.push(r),
        Err(e) => tracing::warn!("{} test failed: {}", test_name, e),
    }
}

/// Collects multiple vulnerability results, logging any errors
fn collect_results(
    results: &mut Vec<VulnerabilityResult>,
    result: Result<Vec<VulnerabilityResult>>,
    test_name: &str,
) {
    match result {
        Ok(r) => results.extend(r),
        Err(e) => tracing::warn!("{} test failed: {}", test_name, e),
    }
}

/// Collects an optional vulnerability result, logging any errors
fn collect_optional_result(
    results: &mut Vec<VulnerabilityResult>,
    result: Result<Option<VulnerabilityResult>>,
    test_name: &str,
) {
    match result {
        Ok(Some(r)) => results.push(r),
        Ok(None) => {}
        Err(e) => tracing::warn!("{} test failed: {}", test_name, e),
    }
}

/// Main vulnerability scanner
pub struct VulnerabilityScanner {
    target: Target,
    protocol_tester: ProtocolTester,
    cipher_tester: CipherTester,
    skip_fallback: bool,
    skip_compression: bool,
    skip_heartbleed: bool,
    skip_renegotiation: bool,
}

impl VulnerabilityScanner {
    /// Create new vulnerability scanner
    pub fn new(target: Target) -> Self {
        let protocol_tester = ProtocolTester::new(target.clone());
        let cipher_tester = CipherTester::new(target.clone());

        Self {
            target,
            protocol_tester,
            cipher_tester,
            skip_fallback: false,
            skip_compression: false,
            skip_heartbleed: false,
            skip_renegotiation: false,
        }
    }

    /// Create new vulnerability scanner with CLI args
    pub fn with_args(target: Target, args: &crate::Args) -> Self {
        let mut protocol_tester = ProtocolTester::new(target.clone());
        let mut cipher_tester = CipherTester::new(target.clone());

        // Enable testing all IPs if specified
        if args.network.test_all_ips {
            protocol_tester = protocol_tester.with_test_all_ips(true);
            cipher_tester = cipher_tester.with_test_all_ips(true);
        }

        Self {
            target,
            protocol_tester,
            cipher_tester,
            skip_fallback: args.scan.no_fallback,
            skip_compression: args.scan.no_compression,
            skip_heartbleed: args.scan.no_heartbleed,
            skip_renegotiation: args.scan.no_renegotiation,
        }
    }

    /// Test all vulnerabilities (PARALLELIZED for maximum performance)
    ///
    /// Orchestrates the complete vulnerability scan in three phases:
    /// 1. Protocol detection - identify supported TLS/SSL versions
    /// 2. Cipher caching - test ciphers for supported protocols
    /// 3. Vulnerability tests - run all checks in parallel
    pub async fn test_all(&self) -> Result<Vec<VulnerabilityResult>> {
        // Step 1: Detect supported protocols
        let protocols = self.detect_protocols().await?;

        // Step 2: Cache ciphers for supported protocols
        let cipher_cache = self.cache_ciphers(&protocols).await?;

        // Step 3: Run vulnerability tests in parallel
        let results = self.run_vulnerability_tests(&cipher_cache).await;

        Ok(results)
    }

    /// Detects which protocols are supported by the target
    async fn detect_protocols(&self) -> Result<Vec<Protocol>> {
        let protocol_results = self.protocol_tester.test_all_protocols().await?;

        let supported = protocol_results
            .iter()
            .filter(|r| r.supported)
            .map(|r| r.protocol)
            .collect();

        Ok(supported)
    }

    /// Caches cipher test results for all supported protocols
    async fn cache_ciphers(
        &self,
        protocols: &[Protocol],
    ) -> Result<HashMap<Protocol, crate::ciphers::tester::ProtocolCipherSummary>> {
        let mut cipher_cache = HashMap::new();

        for protocol in protocols {
            // Skip QUIC - not supported by cipher tester
            if matches!(protocol, Protocol::QUIC) {
                continue;
            }

            let cipher_summary = self.cipher_tester.test_protocol_ciphers(*protocol).await?;
            cipher_cache.insert(*protocol, cipher_summary);
        }

        Ok(cipher_cache)
    }

    /// Runs all vulnerability tests in parallel
    ///
    /// Executes all vulnerability checks concurrently using tokio::join! for
    /// maximum performance. Results are collected with partial success handling
    /// (if one test fails, others still succeed).
    async fn run_vulnerability_tests(
        &self,
        cipher_cache: &HashMap<Protocol, crate::ciphers::tester::ProtocolCipherSummary>,
    ) -> Vec<VulnerabilityResult> {
        let mut results = Vec::new();

        // Create futures for all tests
        let drown_future = self.test_drown();
        let poodle_ssl_future = self.test_poodle_ssl();
        let poodle_variants_future = self.test_poodle_variants();
        let rc4_future = self.test_rc4_cached(cipher_cache);
        let des3_future = self.test_3des_cached(cipher_cache);
        let null_future = self.test_null_ciphers_cached(cipher_cache);
        let export_future = self.test_export_ciphers_cached(cipher_cache);
        let beast_future = self.test_beast_cached(cipher_cache);
        let early_data_future = self.test_early_data();
        let padding_oracle_future = self.test_padding_oracle_2016();

        // Conditional futures (respect CLI flags)
        let renegotiation_future = self.test_renegotiation_if_enabled();
        let fallback_future = self.test_fallback_if_enabled();
        let compression_future = self.test_compression_if_enabled();
        let heartbleed_future = self.test_heartbleed_if_enabled();

        // Execute ALL tests in PARALLEL
        let (
            drown_result,
            poodle_ssl_result,
            poodle_variants_result,
            rc4_result,
            des3_result,
            null_result,
            export_result,
            beast_result,
            renegotiation_result,
            fallback_result,
            compression_result,
            heartbleed_result,
            early_data_result,
            padding_oracle_result,
        ) = tokio::join!(
            drown_future,
            poodle_ssl_future,
            poodle_variants_future,
            rc4_future,
            des3_future,
            null_future,
            export_future,
            beast_future,
            renegotiation_future,
            fallback_future,
            compression_future,
            heartbleed_future,
            early_data_future,
            padding_oracle_future,
        );

        // Collect results - allow partial success
        collect_result(&mut results, drown_result, "DROWN");
        collect_result(&mut results, poodle_ssl_result, "POODLE SSL");
        collect_results(&mut results, poodle_variants_result, "POODLE variants");
        collect_result(&mut results, rc4_result, "RC4");
        collect_result(&mut results, des3_result, "3DES");
        collect_result(&mut results, null_result, "NULL ciphers");
        collect_result(&mut results, export_result, "EXPORT ciphers");
        collect_result(&mut results, beast_result, "BEAST");
        collect_optional_result(&mut results, renegotiation_result, "Renegotiation");
        collect_optional_result(&mut results, fallback_result, "TLS Fallback");
        collect_optional_result(&mut results, compression_result, "Compression");
        collect_optional_result(&mut results, heartbleed_result, "Heartbleed");
        collect_result(&mut results, early_data_result, "Early data");
        collect_result(&mut results, padding_oracle_result, "Padding oracle");

        results
    }

    /// Tests renegotiation if not skipped via CLI flags
    async fn test_renegotiation_if_enabled(&self) -> Result<Option<VulnerabilityResult>> {
        if self.skip_renegotiation {
            return Ok(None);
        }
        self.test_renegotiation().await.map(Some)
    }

    /// Tests TLS fallback if not skipped via CLI flags
    async fn test_fallback_if_enabled(&self) -> Result<Option<VulnerabilityResult>> {
        if self.skip_fallback {
            return Ok(None);
        }
        self.test_tls_fallback().await.map(Some)
    }

    /// Tests compression if not skipped via CLI flags
    async fn test_compression_if_enabled(&self) -> Result<Option<VulnerabilityResult>> {
        if self.skip_compression {
            return Ok(None);
        }
        self.test_compression().await.map(Some)
    }

    /// Tests heartbleed if not skipped via CLI flags
    async fn test_heartbleed_if_enabled(&self) -> Result<Option<VulnerabilityResult>> {
        if self.skip_heartbleed {
            return Ok(None);
        }
        self.test_heartbleed().await.map(Some)
    }

    /// Test for DROWN (CVE-2016-0800)
    /// Simple: just check if SSLv2 is supported
    pub async fn test_drown(&self) -> Result<VulnerabilityResult> {
        let protocol_result = self.protocol_tester.test_protocol(Protocol::SSLv2).await?;

        Ok(VulnerabilityResult {
            vuln_type: VulnerabilityType::DROWN,
            vulnerable: protocol_result.supported,
            details: if protocol_result.supported {
                "Server supports SSLv2, vulnerable to DROWN attack".to_string()
            } else {
                "Server does not support SSLv2".to_string()
            },
            cve: Some("CVE-2016-0800".to_string()),
            cwe: Some("CWE-327".to_string()),
            severity: if protocol_result.supported {
                Severity::High
            } else {
                Severity::Info
            },
        })
    }

    /// Test for RC4 cipher usage (CVE-2013-2566, CVE-2015-2808)
    pub async fn test_rc4(&self) -> Result<VulnerabilityResult> {
        // Check if any protocol supports RC4 ciphers
        let mut has_rc4 = false;
        let mut rc4_details = Vec::new();

        for protocol in Protocol::all() {
            if matches!(protocol, Protocol::QUIC) {
                continue;
            }

            let cipher_summary = self.cipher_tester.test_protocol_ciphers(protocol).await?;

            let rc4_ciphers: Vec<_> = cipher_summary
                .supported_ciphers
                .iter()
                .filter(|c| c.encryption.contains("RC4"))
                .collect();

            if !rc4_ciphers.is_empty() {
                has_rc4 = true;
                rc4_details.push(format!("{}: {} RC4 cipher(s)", protocol, rc4_ciphers.len()));
            }
        }

        Ok(VulnerabilityResult {
            vuln_type: VulnerabilityType::RC4,
            vulnerable: has_rc4,
            details: if has_rc4 {
                format!("Server supports RC4 ciphers: {}", rc4_details.join(", "))
            } else {
                "Server does not support RC4 ciphers".to_string()
            },
            cve: Some("CVE-2013-2566, CVE-2015-2808".to_string()),
            cwe: Some("CWE-326".to_string()),
            severity: if has_rc4 {
                Severity::Medium
            } else {
                Severity::Info
            },
        })
    }

    /// Test for 3DES/SWEET32 (CVE-2016-2183)
    pub async fn test_3des(&self) -> Result<VulnerabilityResult> {
        let mut has_3des = false;
        let mut des_details = Vec::new();

        for protocol in Protocol::all() {
            if matches!(protocol, Protocol::QUIC) {
                continue;
            }

            let cipher_summary = self.cipher_tester.test_protocol_ciphers(protocol).await?;

            let des_ciphers: Vec<_> = cipher_summary
                .supported_ciphers
                .iter()
                .filter(|c| c.encryption.contains("3DES") || c.encryption.contains("DES"))
                .collect();

            if !des_ciphers.is_empty() {
                has_3des = true;
                des_details.push(format!(
                    "{}: {} 3DES/DES cipher(s)",
                    protocol,
                    des_ciphers.len()
                ));
            }
        }

        Ok(VulnerabilityResult {
            vuln_type: VulnerabilityType::SWEET32,
            vulnerable: has_3des,
            details: if has_3des {
                format!(
                    "Server supports 3DES/DES ciphers (SWEET32): {}",
                    des_details.join(", ")
                )
            } else {
                "Server does not support 3DES/DES ciphers".to_string()
            },
            cve: Some("CVE-2016-2183".to_string()),
            cwe: Some("CWE-327".to_string()),
            severity: if has_3des {
                Severity::Medium
            } else {
                Severity::Info
            },
        })
    }

    /// Test for NULL ciphers
    pub async fn test_null_ciphers(&self) -> Result<VulnerabilityResult> {
        let mut has_null = false;
        let mut null_details = Vec::new();

        for protocol in Protocol::all() {
            if matches!(protocol, Protocol::QUIC) {
                continue;
            }

            let cipher_summary = self.cipher_tester.test_protocol_ciphers(protocol).await?;

            if cipher_summary.counts.null_ciphers > 0 {
                has_null = true;
                null_details.push(format!(
                    "{}: {} NULL cipher(s)",
                    protocol, cipher_summary.counts.null_ciphers
                ));
            }
        }

        Ok(VulnerabilityResult {
            vuln_type: VulnerabilityType::RC4, // Reusing enum, should add NULL type
            vulnerable: has_null,
            details: if has_null {
                format!(
                    "Server supports NULL encryption ciphers: {}",
                    null_details.join(", ")
                )
            } else {
                "Server does not support NULL ciphers".to_string()
            },
            cve: None,
            cwe: Some("CWE-327".to_string()),
            severity: if has_null {
                Severity::Critical
            } else {
                Severity::Info
            },
        })
    }

    /// Test for EXPORT ciphers (FREAK CVE-2015-0204, LOGJAM CVE-2015-4000)
    pub async fn test_export_ciphers(&self) -> Result<VulnerabilityResult> {
        let mut has_export = false;
        let mut export_details = Vec::new();

        for protocol in Protocol::all() {
            if matches!(protocol, Protocol::QUIC) {
                continue;
            }

            let cipher_summary = self.cipher_tester.test_protocol_ciphers(protocol).await?;

            if cipher_summary.counts.export_ciphers > 0 {
                has_export = true;
                export_details.push(format!(
                    "{}: {} EXPORT cipher(s)",
                    protocol, cipher_summary.counts.export_ciphers
                ));
            }
        }

        Ok(VulnerabilityResult {
            vuln_type: VulnerabilityType::FREAK,
            vulnerable: has_export,
            details: if has_export {
                format!(
                    "Server supports EXPORT ciphers (FREAK/LOGJAM): {}",
                    export_details.join(", ")
                )
            } else {
                "Server does not support EXPORT ciphers".to_string()
            },
            cve: Some("CVE-2015-0204, CVE-2015-4000".to_string()),
            cwe: Some("CWE-327".to_string()),
            severity: if has_export {
                Severity::High
            } else {
                Severity::Info
            },
        })
    }

    /// Test for POODLE SSL (CVE-2014-3566)
    /// Simple: check if SSLv3 is supported
    pub async fn test_poodle_ssl(&self) -> Result<VulnerabilityResult> {
        let protocol_result = self.protocol_tester.test_protocol(Protocol::SSLv3).await?;

        Ok(VulnerabilityResult {
            vuln_type: VulnerabilityType::POODLE,
            vulnerable: protocol_result.supported,
            details: if protocol_result.supported {
                "Server supports SSLv3, vulnerable to POODLE attack".to_string()
            } else {
                "Server does not support SSLv3".to_string()
            },
            cve: Some("CVE-2014-3566".to_string()),
            cwe: Some("CWE-310".to_string()),
            severity: if protocol_result.supported {
                Severity::High
            } else {
                Severity::Info
            },
        })
    }

    /// Test for all POODLE variants (Zombie, GOLDENDOODLE, Sleeping, 0-Length)
    /// Returns a vector of vulnerability results, one for each variant
    pub async fn test_poodle_variants(&self) -> Result<Vec<VulnerabilityResult>> {
        use crate::vulnerabilities::poodle::PoodleTester;

        let tester = PoodleTester::new(&self.target);
        let test_result = tester.test_all_variants().await?;

        let mut results = Vec::new();

        // Convert each variant result to VulnerabilityResult
        for variant_result in test_result.variants {
            // Skip SSLv3 and TLS POODLE as they're already tested separately
            if matches!(
                variant_result.variant,
                crate::vulnerabilities::poodle::PoodleVariant::SslV3
                    | crate::vulnerabilities::poodle::PoodleVariant::Tls
            ) {
                continue;
            }

            let vuln_type = match variant_result.variant {
                crate::vulnerabilities::poodle::PoodleVariant::ZombiePoodle => {
                    VulnerabilityType::ZombiePoodle
                }
                crate::vulnerabilities::poodle::PoodleVariant::GoldenDoodle => {
                    VulnerabilityType::GoldenDoodle
                }
                crate::vulnerabilities::poodle::PoodleVariant::SleepingPoodle => {
                    VulnerabilityType::SleepingPoodle
                }
                crate::vulnerabilities::poodle::PoodleVariant::OpenSsl0Length => {
                    VulnerabilityType::OpenSsl0Length
                }
                _ => continue, // Skip SSLv3 and TLS variants
            };

            let severity = if variant_result.vulnerable {
                match variant_result.variant {
                    crate::vulnerabilities::poodle::PoodleVariant::ZombiePoodle
                    | crate::vulnerabilities::poodle::PoodleVariant::GoldenDoodle => Severity::High,
                    crate::vulnerabilities::poodle::PoodleVariant::SleepingPoodle => {
                        Severity::Medium
                    }
                    crate::vulnerabilities::poodle::PoodleVariant::OpenSsl0Length => Severity::High,
                    _ => Severity::Info,
                }
            } else {
                Severity::Info
            };

            results.push(VulnerabilityResult {
                vuln_type,
                vulnerable: variant_result.vulnerable,
                details: variant_result.details,
                cve: Some(variant_result.variant.cve().to_string()),
                cwe: Some("CWE-310".to_string()), // Cryptographic Issues
                severity,
            });
        }

        Ok(results)
    }

    /// Test for BEAST (CVE-2011-3389)
    /// Affects TLS 1.0 with CBC ciphers
    pub async fn test_beast(&self) -> Result<VulnerabilityResult> {
        let protocol_result = self.protocol_tester.test_protocol(Protocol::TLS10).await?;

        if !protocol_result.supported {
            return Ok(VulnerabilityResult {
                vuln_type: VulnerabilityType::BEAST,
                vulnerable: false,
                details: "Server does not support TLS 1.0".to_string(),
                cve: Some("CVE-2011-3389".to_string()),
                cwe: Some("CWE-326".to_string()),
                severity: Severity::Info,
            });
        }

        // Check for CBC ciphers in TLS 1.0
        let cipher_summary = self
            .cipher_tester
            .test_protocol_ciphers(Protocol::TLS10)
            .await?;

        let cbc_ciphers: Vec<_> = cipher_summary
            .supported_ciphers
            .iter()
            .filter(|c| c.encryption.contains("CBC"))
            .collect();

        let vulnerable = !cbc_ciphers.is_empty();

        Ok(VulnerabilityResult {
            vuln_type: VulnerabilityType::BEAST,
            vulnerable,
            details: if vulnerable {
                format!(
                    "Server supports TLS 1.0 with {} CBC cipher(s), potentially vulnerable to BEAST",
                    cbc_ciphers.len()
                )
            } else {
                "Server supports TLS 1.0 but no CBC ciphers".to_string()
            },
            cve: Some("CVE-2011-3389".to_string()),
            cwe: Some("CWE-326".to_string()),
            severity: if vulnerable {
                Severity::Medium
            } else {
                Severity::Info
            },
        })
    }

    /// Test for insecure renegotiation (CVE-2009-3555)
    /// Requires checking for renegotiation_info extension
    pub async fn test_renegotiation(&self) -> Result<VulnerabilityResult> {
        use crate::protocols::renegotiation::RenegotiationTester;

        let tester = RenegotiationTester::new(&self.target);
        let result = tester.test().await?;

        Ok(VulnerabilityResult {
            vuln_type: VulnerabilityType::Renegotiation,
            vulnerable: result.vulnerable,
            details: result.details,
            cve: Some("CVE-2009-3555".to_string()),
            cwe: Some("CWE-310".to_string()),
            severity: if result.vulnerable {
                Severity::High
            } else {
                Severity::Info
            },
        })
    }

    /// Test for TLS_FALLBACK_SCSV support (CVE-2014-8730)
    pub async fn test_tls_fallback(&self) -> Result<VulnerabilityResult> {
        use crate::protocols::fallback_scsv::FallbackScsvTester;

        let mut tester = FallbackScsvTester::new(&self.target);
        let result = tester.test().await?;

        // Determine severity based on TLS 1.3 support
        // If TLS 1.3+ is supported, downgrade attacks are mitigated by TLS 1.3's
        // built-in downgrade protection, reducing the severity from HIGH to MEDIUM
        let severity = if result.vulnerable {
            if result.has_tls13_or_higher {
                Severity::Medium // TLS 1.3 has built-in downgrade protection
            } else {
                Severity::High // No TLS 1.3, SCSV is critical
            }
        } else {
            Severity::Info
        };

        Ok(VulnerabilityResult {
            vuln_type: VulnerabilityType::TLSFallback,
            vulnerable: result.vulnerable,
            details: result.details,
            cve: Some("CVE-2014-8730".to_string()),
            cwe: Some("CWE-757".to_string()),
            severity,
        })
    }

    /// Test for TLS Compression (CRIME - CVE-2012-4929)
    pub async fn test_compression(&self) -> Result<VulnerabilityResult> {
        use crate::vulnerabilities::crime::CrimeTester;

        let tester = CrimeTester::new(&self.target);
        let result = tester.test().await?;

        Ok(VulnerabilityResult {
            vuln_type: VulnerabilityType::CRIME,
            vulnerable: result.vulnerable,
            details: result.details,
            cve: Some("CVE-2012-4929".to_string()),
            cwe: Some("CWE-310".to_string()),
            severity: if result.vulnerable {
                Severity::High
            } else {
                Severity::Info
            },
        })
    }

    /// Test for Heartbleed (CVE-2014-0160)
    pub async fn test_heartbleed(&self) -> Result<VulnerabilityResult> {
        use crate::vulnerabilities::heartbleed::HeartbleedTester;

        let tester = HeartbleedTester::new(&self.target);
        let vulnerable = tester.test().await?;

        Ok(VulnerabilityResult {
            vuln_type: VulnerabilityType::Heartbleed,
            vulnerable,
            details: if vulnerable {
                "Server is vulnerable to Heartbleed (CVE-2014-0160) - can leak memory contents including sensitive data".to_string()
            } else {
                "Server is not vulnerable to Heartbleed".to_string()
            },
            cve: Some("CVE-2014-0160".to_string()),
            cwe: Some("CWE-119".to_string()),
            severity: if vulnerable {
                Severity::Critical
            } else {
                Severity::Info
            },
        })
    }

    /// Test for 0-RTT / Early Data replay attacks (TLS 1.3)
    pub async fn test_early_data(&self) -> Result<VulnerabilityResult> {
        use crate::vulnerabilities::early_data::EarlyDataTester;

        let tester = EarlyDataTester::new(&self.target);
        let result = tester.test().await?;

        Ok(VulnerabilityResult {
            vuln_type: VulnerabilityType::EarlyDataReplay,
            vulnerable: result.vulnerable,
            details: result.details,
            cve: None,                        // No specific CVE, but references RFC 8446
            cwe: Some("CWE-294".to_string()), // CWE-294: Authentication Bypass by Capture-replay
            severity: if result.vulnerable {
                Severity::Medium
            } else {
                Severity::Info
            },
        })
    }

    /// Test for OpenSSL Padding Oracle (CVE-2016-2107)
    /// Affects OpenSSL 1.0.1 - 1.0.1t and 1.0.2 - 1.0.2h with AES-NI
    pub async fn test_padding_oracle_2016(&self) -> Result<VulnerabilityResult> {
        use crate::vulnerabilities::padding_oracle_2016::PaddingOracle2016Tester;

        let tester = PaddingOracle2016Tester::new(&self.target);
        let result = tester.test().await?;

        Ok(VulnerabilityResult {
            vuln_type: VulnerabilityType::PaddingOracle2016,
            vulnerable: result.vulnerable,
            details: result.details,
            cve: Some("CVE-2016-2107".to_string()),
            cwe: Some("CWE-203".to_string()), // CWE-203: Observable Discrepancy (timing attack)
            severity: if result.vulnerable {
                Severity::High
            } else {
                Severity::Info
            },
        })
    }

    /// Get vulnerability summary
    pub fn summarize_results(results: &[VulnerabilityResult]) -> VulnerabilitySummary {
        let mut summary = VulnerabilitySummary::default();

        for result in results {
            if result.vulnerable {
                summary.total_vulnerable += 1;

                match result.severity {
                    Severity::Critical => summary.critical += 1,
                    Severity::High => summary.high += 1,
                    Severity::Medium => summary.medium += 1,
                    Severity::Low => summary.low += 1,
                    Severity::Info => {}
                }
            }
        }

        summary.total_tested = results.len();
        summary
    }

    // ========================================================================
    // CACHED VERSIONS - Use pre-computed cipher results to avoid re-testing
    // ========================================================================

    /// Test for RC4 cipher usage - cached version
    async fn test_rc4_cached(
        &self,
        cipher_cache: &HashMap<Protocol, crate::ciphers::tester::ProtocolCipherSummary>,
    ) -> Result<VulnerabilityResult> {
        let mut has_rc4 = false;
        let mut rc4_details = Vec::new();

        for (protocol, cipher_summary) in cipher_cache {
            let rc4_ciphers: Vec<_> = cipher_summary
                .supported_ciphers
                .iter()
                .filter(|c| c.encryption.contains("RC4"))
                .collect();

            if !rc4_ciphers.is_empty() {
                has_rc4 = true;
                rc4_details.push(format!("{}: {} RC4 cipher(s)", protocol, rc4_ciphers.len()));
            }
        }

        Ok(VulnerabilityResult {
            vuln_type: VulnerabilityType::RC4,
            vulnerable: has_rc4,
            details: if has_rc4 {
                format!("Server supports RC4 ciphers: {}", rc4_details.join(", "))
            } else {
                "Server does not support RC4 ciphers".to_string()
            },
            cve: Some("CVE-2013-2566, CVE-2015-2808".to_string()),
            cwe: Some("CWE-326".to_string()),
            severity: if has_rc4 {
                Severity::Medium
            } else {
                Severity::Info
            },
        })
    }

    /// Test for 3DES/SWEET32 - cached version
    async fn test_3des_cached(
        &self,
        cipher_cache: &HashMap<Protocol, crate::ciphers::tester::ProtocolCipherSummary>,
    ) -> Result<VulnerabilityResult> {
        let mut has_3des = false;
        let mut des_details = Vec::new();

        for (protocol, cipher_summary) in cipher_cache {
            let des_ciphers: Vec<_> = cipher_summary
                .supported_ciphers
                .iter()
                .filter(|c| c.encryption.contains("3DES") || c.encryption.contains("DES"))
                .collect();

            if !des_ciphers.is_empty() {
                has_3des = true;
                des_details.push(format!(
                    "{}: {} 3DES/DES cipher(s)",
                    protocol,
                    des_ciphers.len()
                ));
            }
        }

        Ok(VulnerabilityResult {
            vuln_type: VulnerabilityType::SWEET32,
            vulnerable: has_3des,
            details: if has_3des {
                format!(
                    "Server supports 3DES/DES ciphers (SWEET32): {}",
                    des_details.join(", ")
                )
            } else {
                "Server does not support 3DES/DES ciphers".to_string()
            },
            cve: Some("CVE-2016-2183".to_string()),
            cwe: Some("CWE-327".to_string()),
            severity: if has_3des {
                Severity::Medium
            } else {
                Severity::Info
            },
        })
    }

    /// Test for NULL ciphers - cached version
    async fn test_null_ciphers_cached(
        &self,
        cipher_cache: &HashMap<Protocol, crate::ciphers::tester::ProtocolCipherSummary>,
    ) -> Result<VulnerabilityResult> {
        let mut has_null = false;
        let mut null_details = Vec::new();

        for (protocol, cipher_summary) in cipher_cache {
            if cipher_summary.counts.null_ciphers > 0 {
                has_null = true;
                null_details.push(format!(
                    "{}: {} NULL cipher(s)",
                    protocol, cipher_summary.counts.null_ciphers
                ));
            }
        }

        Ok(VulnerabilityResult {
            vuln_type: VulnerabilityType::RC4, // Reusing enum, should add NULL type
            vulnerable: has_null,
            details: if has_null {
                format!(
                    "Server supports NULL encryption ciphers: {}",
                    null_details.join(", ")
                )
            } else {
                "Server does not support NULL ciphers".to_string()
            },
            cve: None,
            cwe: Some("CWE-327".to_string()),
            severity: if has_null {
                Severity::Critical
            } else {
                Severity::Info
            },
        })
    }

    /// Test for EXPORT ciphers - cached version
    async fn test_export_ciphers_cached(
        &self,
        cipher_cache: &HashMap<Protocol, crate::ciphers::tester::ProtocolCipherSummary>,
    ) -> Result<VulnerabilityResult> {
        let mut has_export = false;
        let mut export_details = Vec::new();

        for (protocol, cipher_summary) in cipher_cache {
            if cipher_summary.counts.export_ciphers > 0 {
                has_export = true;
                export_details.push(format!(
                    "{}: {} EXPORT cipher(s)",
                    protocol, cipher_summary.counts.export_ciphers
                ));
            }
        }

        Ok(VulnerabilityResult {
            vuln_type: VulnerabilityType::FREAK,
            vulnerable: has_export,
            details: if has_export {
                format!(
                    "Server supports EXPORT ciphers (FREAK/LOGJAM): {}",
                    export_details.join(", ")
                )
            } else {
                "Server does not support EXPORT ciphers".to_string()
            },
            cve: Some("CVE-2015-0204, CVE-2015-4000".to_string()),
            cwe: Some("CWE-327".to_string()),
            severity: if has_export {
                Severity::High
            } else {
                Severity::Info
            },
        })
    }

    /// Test for BEAST - cached version
    async fn test_beast_cached(
        &self,
        cipher_cache: &HashMap<Protocol, crate::ciphers::tester::ProtocolCipherSummary>,
    ) -> Result<VulnerabilityResult> {
        // Check if TLS 1.0 is in cache (meaning it's supported)
        if let Some(cipher_summary) = cipher_cache.get(&Protocol::TLS10) {
            let cbc_ciphers: Vec<_> = cipher_summary
                .supported_ciphers
                .iter()
                .filter(|c| c.encryption.contains("CBC"))
                .collect();

            let vulnerable = !cbc_ciphers.is_empty();

            Ok(VulnerabilityResult {
                vuln_type: VulnerabilityType::BEAST,
                vulnerable,
                details: if vulnerable {
                    format!(
                        "Server supports TLS 1.0 with {} CBC cipher(s), potentially vulnerable to BEAST",
                        cbc_ciphers.len()
                    )
                } else {
                    "Server supports TLS 1.0 but no CBC ciphers".to_string()
                },
                cve: Some("CVE-2011-3389".to_string()),
                cwe: Some("CWE-326".to_string()),
                severity: if vulnerable {
                    Severity::Medium
                } else {
                    Severity::Info
                },
            })
        } else {
            // TLS 1.0 not supported
            Ok(VulnerabilityResult {
                vuln_type: VulnerabilityType::BEAST,
                vulnerable: false,
                details: "Server does not support TLS 1.0".to_string(),
                cve: Some("CVE-2011-3389".to_string()),
                cwe: Some("CWE-326".to_string()),
                severity: Severity::Info,
            })
        }
    }
}

/// Vulnerability scan summary
#[derive(Debug, Clone, Default)]
pub struct VulnerabilitySummary {
    pub total_tested: usize,
    pub total_vulnerable: usize,
    pub critical: usize,
    pub high: usize,
    pub medium: usize,
    pub low: usize,
}

impl std::fmt::Display for VulnerabilitySummary {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "Vulnerability Scan Summary:")?;
        writeln!(f, "  Total Tests:  {}", self.total_tested)?;
        writeln!(f, "  Vulnerabilities Found: {}", self.total_vulnerable)?;
        writeln!(f)?;

        if self.total_vulnerable > 0 {
            writeln!(f, "  By Severity:")?;
            if self.critical > 0 {
                writeln!(f, "    Critical: {}", self.critical)?;
            }
            if self.high > 0 {
                writeln!(f, "    High:     {}", self.high)?;
            }
            if self.medium > 0 {
                writeln!(f, "    Medium:   {}", self.medium)?;
            }
            if self.low > 0 {
                writeln!(f, "    Low:      {}", self.low)?;
            }
        }

        Ok(())
    }
}

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

    #[tokio::test]
    #[ignore] // Requires network access
    async fn test_drown_detection() {
        let target = Target::parse("www.google.com:443")
            .await
            .expect("test assertion should succeed");
        let scanner = VulnerabilityScanner::new(target);

        let result = scanner
            .test_drown()
            .await
            .expect("test assertion should succeed");

        // Google should not be vulnerable to DROWN
        assert!(!result.vulnerable);
    }

    #[tokio::test]
    #[ignore] // Requires network access
    async fn test_rc4_detection() {
        let target = Target::parse("www.google.com:443")
            .await
            .expect("test assertion should succeed");
        let scanner = VulnerabilityScanner::new(target);

        let result = scanner
            .test_rc4()
            .await
            .expect("test assertion should succeed");

        // Google should not support RC4
        assert!(!result.vulnerable);
    }

    #[test]
    fn test_summary_formatting() {
        let summary = VulnerabilitySummary {
            total_tested: 10,
            total_vulnerable: 3,
            critical: 1,
            high: 1,
            medium: 1,
            low: 0,
        };

        let output = summary.to_string();
        assert!(output.contains("Total Tests:  10"));
        assert!(output.contains("Vulnerabilities Found: 3"));
        assert!(output.contains("Critical: 1"));
    }
}