eggsec-nse 0.3.0

Standalone Lua-based Nmap Scripting Engine runtime
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
//! NSE Public API Implementation
//!
//! Provides public APIs to NSE libraries for use by eggsec core tools.

use native_tls::TlsConnector;
use openssl::x509::X509;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use std::net::TcpStream;
use std::time::Duration;

/// Result type for NSE operations
pub type NseResult<T> = Result<T, NseError>;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum NseError {
    Connection(String),
    Tls(String),
    Parse(String),
    Lua(String),
    NotFound(String),
}

impl std::fmt::Display for NseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            NseError::Connection(s) => write!(f, "Connection error: {}", s),
            NseError::Tls(s) => write!(f, "TLS error: {}", s),
            NseError::Parse(s) => write!(f, "Parse error: {}", s),
            NseError::Lua(s) => write!(f, "Lua error: {}", s),
            NseError::NotFound(s) => write!(f, "Not found: {}", s),
        }
    }
}

impl std::error::Error for NseError {}

impl From<std::io::Error> for NseError {
    fn from(e: std::io::Error) -> Self {
        NseError::Connection(e.to_string())
    }
}

impl From<native_tls::Error> for NseError {
    fn from(e: native_tls::Error) -> Self {
        NseError::Tls(e.to_string())
    }
}

// ============================================================================
// VULNS API
// ============================================================================

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NseVulnResult {
    pub id: String,
    pub name: String,
    pub severity: String,
    pub description: String,
}

/// Look up a CVE by ID
pub fn nse_vulns_lookup(cve_id: &str) -> NseResult<Option<NseVulnResult>> {
    let cve_db = get_cve_database();

    if let Some((name, severity, description)) = cve_db.get(cve_id) {
        Ok(Some(NseVulnResult {
            id: cve_id.to_string(),
            name: name.to_string(),
            severity: severity.to_string(),
            description: description.to_string(),
        }))
    } else {
        Ok(None)
    }
}

/// Search CVEs by keyword
pub fn nse_vulns_search(keyword: &str) -> NseResult<Vec<NseVulnResult>> {
    let cve_db = get_cve_database();
    let keyword_lower = keyword.to_lowercase();
    let mut results = Vec::new();

    for (id, (name, severity, description)) in cve_db.iter() {
        if id.to_lowercase().contains(&keyword_lower)
            || name.to_lowercase().contains(&keyword_lower)
            || description.to_lowercase().contains(&keyword_lower)
        {
            results.push(NseVulnResult {
                id: id.to_string(),
                name: name.to_string(),
                severity: severity.to_string(),
                description: description.to_string(),
            });
        }
    }

    Ok(results)
}

/// Check if a vulnerability is known for a service
pub fn nse_vulns_is_known(service: &str, port: u16) -> NseResult<Vec<NseVulnResult>> {
    let service_vulns = get_service_vulnerabilities(service, port);
    Ok(service_vulns)
}

fn get_cve_database() -> FxHashMap<&'static str, (&'static str, &'static str, &'static str)> {
    let mut m = FxHashMap::default();

    // SMB vulnerabilities
    m.insert(
        "CVE-2017-0144",
        (
            "WannaCry",
            "critical",
            "EternalBlue SMB exploit - remote code execution via SMBv1",
        ),
    );
    m.insert(
        "CVE-2017-0145",
        ("WannaCry", "critical", "SMBv1 remote code execution"),
    );
    m.insert(
        "CVE-2017-0146",
        ("WannaCry", "critical", "SMB remote code execution"),
    );
    m.insert(
        "CVE-2017-0147",
        ("WannaCry", "critical", "SMB information disclosure"),
    );
    m.insert(
        "CVE-2017-0148",
        ("WannaCry", "critical", "SMB denial of service"),
    );

    // Windows vulnerabilities
    m.insert(
        "CVE-2019-0708",
        (
            "BlueKeep",
            "critical",
            "Remote Desktop Services vulnerability - pre-auth remote code execution",
        ),
    );
    m.insert(
        "CVE-2020-0796",
        (
            "SMBGhost",
            "high",
            "SMBv3 compression vulnerability - remote code execution",
        ),
    );
    m.insert(
        "CVE-2020-1472",
        (
            "Zerologon",
            "critical",
            "Netlogon privilege escalation - allows domain takeover",
        ),
    );

    // SSL/TLS vulnerabilities
    m.insert(
        "CVE-2014-0160",
        (
            "Heartbleed",
            "critical",
            "OpenSSL heartbeat information disclosure",
        ),
    );
    m.insert(
        "CVE-2017-15361",
        (
            "ROCA",
            "medium",
            "ROCA vulnerability in RSA keys generated by Infineon TPM",
        ),
    );

    // Web vulnerabilities
    m.insert(
        "CVE-2017-5638",
        (
            "Equifax",
            "critical",
            "Apache Struts RCE via Content-Type header",
        ),
    );
    m.insert(
        "CVE-2018-7600",
        (
            "Drupalgeddon",
            "critical",
            "Drupal RCE - multiple vulnerabilities",
        ),
    );
    m.insert(
        "CVE-2021-44228",
        (
            "Log4Shell",
            "critical",
            "Log4j remote code execution via JNDI lookup",
        ),
    );

    // SSH vulnerabilities
    m.insert(
        "CVE-2018-15473",
        (
            "OpenSSH User Enumeration",
            "medium",
            "OpenSSH information disclosure via username enumeration",
        ),
    );

    // FTP vulnerabilities
    m.insert(
        "CVE-2021-41773",
        (
            "Apache Path Traversal",
            "critical",
            "Apache HTTP Server path traversal",
        ),
    );

    m
}

fn get_service_vulnerabilities(service: &str, _port: u16) -> Vec<NseVulnResult> {
    let mut results = Vec::new();
    let service_lower = service.to_lowercase();
    let cve_db = get_cve_database();

    // Map services to relevant CVEs
    let service_cves: Vec<&str> = match service_lower.as_str() {
        "smb" | "microsoft-ds" | "netbios-ssn" => vec![
            "CVE-2017-0144",
            "CVE-2017-0145",
            "CVE-2017-0146",
            "CVE-2017-0147",
            "CVE-2017-0148",
            "CVE-2019-0708",
            "CVE-2020-0796",
        ],
        "rdp" | "ms-wbt-server" => vec!["CVE-2019-0708"],
        "http" | "https" | "apache" | "nginx" => vec![
            "CVE-2017-5638",
            "CVE-2018-7600",
            "CVE-2021-44228",
            "CVE-2021-41773",
        ],
        "ssh" => vec!["CVE-2018-15473"],
        "ftp" => vec![],
        "mysql" | "mysql-reg" => vec![],
        "postgresql" => vec![],
        "redis" => vec![],
        "mongodb" => vec![],
        "ldap" | "globalcatLDAP" | "globalcatLDAPSSL" => vec![],
        _ => vec![],
    };

    for cve_id in service_cves {
        if let Some((name, severity, description)) = cve_db.get(cve_id) {
            results.push(NseVulnResult {
                id: cve_id.to_string(),
                name: name.to_string(),
                severity: severity.to_string(),
                description: description.to_string(),
            });
        }
    }

    results
}

// ============================================================================
// SSLCERT API
// ============================================================================

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NseSslCertResult {
    pub subject: String,
    pub issuer: String,
    pub not_before: String,
    pub not_after: String,
    pub serial: Option<String>,
    pub version: Option<i32>,
    pub subject_alt_names: Vec<String>,
    pub valid: bool,
}

/// Get SSL/TLS certificate from a host
pub fn nse_sslcert_get(host: &str, port: u16) -> NseResult<NseSslCertResult> {
    let addr = format!("{}:{}", host, port);
    let socket = TcpStream::connect_timeout(
        &addr
            .parse()
            .map_err(|e: std::net::AddrParseError| NseError::Connection(e.to_string()))?,
        Duration::from_secs(10),
    )?;

    let connector = TlsConnector::builder()
        .danger_accept_invalid_certs(false)
        .build()
        .map_err(|e| NseError::Tls(e.to_string()))?;

    let tls_stream = connector
        .connect(host, socket)
        .map_err(|e| NseError::Tls(e.to_string()))?;

    if let Some(cert) = tls_stream
        .peer_certificate()
        .map_err(|e| NseError::Tls(e.to_string()))?
    {
        let der = cert.to_der().map_err(|e| NseError::Parse(e.to_string()))?;
        let openssl_cert = X509::from_der(&der).map_err(|e| NseError::Parse(e.to_string()))?;

        let subject = parse_x509_name(openssl_cert.subject_name());
        let issuer = parse_x509_name(openssl_cert.issuer_name());

        let sans = extract_sans(&openssl_cert);

        Ok(NseSslCertResult {
            subject,
            issuer,
            not_before: openssl_cert.not_before().to_string(),
            not_after: openssl_cert.not_after().to_string(),
            serial: openssl_cert
                .serial_number()
                .to_bn()
                .ok()
                .and_then(|s| s.to_hex_str().ok())
                .map(|s| s.to_string()),
            version: Some(openssl_cert.version() + 1),
            subject_alt_names: sans,
            valid: true,
        })
    } else {
        Err(NseError::NotFound("No certificate found".to_string()))
    }
}

/// Get certificate chain from a host
pub fn nse_sslcert_get_chain(host: &str, port: u16) -> NseResult<Vec<NseSslCertResult>> {
    // For now, return single cert - full chain would require deeper integration
    let cert = nse_sslcert_get(host, port)?;
    Ok(vec![cert])
}

fn parse_x509_name(name: &openssl::x509::X509NameRef) -> String {
    name.entries()
        .map(|e| {
            let key = e.object().nid().short_name().unwrap_or("Unknown");
            let value = e
                .data()
                .as_utf8()
                .map(|s| s.to_string())
                .unwrap_or_default();
            format!("{}={}", key, value)
        })
        .collect::<Vec<_>>()
        .join(", ")
}

fn extract_sans(_cert: &X509) -> Vec<String> {
    // Note: Full SAN extraction requires openssl 0.10.50+ or manual DER parsing
    // For now, return empty - SAN info is available in cert.subject_alt_names()
    // when openssl-sys is compiled with newer version
    Vec::new()
}

// ============================================================================
// HTTP API
// ============================================================================

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NseHttpResponse {
    pub status: u16,
    pub status_line: String,
    pub headers: FxHashMap<String, String>,
    pub body: String,
    pub title: Option<String>,
}

/// Simple HTTP GET request
pub fn nse_http_get(host: &str, port: u16, path: &str) -> NseResult<NseHttpResponse> {
    crate::install_tls_provider();
    let url = if port == 443 {
        format!("https://{}{}", host, path)
    } else {
        format!("http://{}{}", host, path)
    };

    let client = reqwest::blocking::Client::builder()
        .danger_accept_invalid_certs(false)
        .danger_accept_invalid_hostnames(false)
        .timeout(Duration::from_secs(30))
        .build()
        .map_err(|e| NseError::Connection(e.to_string()))?;

    let response = client
        .get(&url)
        .send()
        .map_err(|e| NseError::Connection(e.to_string()))?;

    let status = response.status().as_u16();
    let status_line = response
        .status()
        .canonical_reason()
        .unwrap_or("Unknown")
        .to_string();

    let mut headers = FxHashMap::default();
    for (key, value) in response.headers() {
        headers.insert(key.to_string(), value.to_str().unwrap_or("").to_string());
    }

    let body = response
        .text()
        .map_err(|e| NseError::Parse(e.to_string()))?;

    // Extract title from HTML
    let title = extract_title(&body);

    Ok(NseHttpResponse {
        status,
        status_line,
        headers,
        body,
        title,
    })
}

/// Simple HTTP POST request
pub fn nse_http_post(host: &str, port: u16, path: &str, data: &str) -> NseResult<NseHttpResponse> {
    crate::install_tls_provider();
    let url = if port == 443 {
        format!("https://{}{}", host, path)
    } else {
        format!("http://{}{}", host, path)
    };

    let client = reqwest::blocking::Client::builder()
        .danger_accept_invalid_certs(false)
        .danger_accept_invalid_hostnames(false)
        .timeout(Duration::from_secs(30))
        .build()
        .map_err(|e| NseError::Connection(e.to_string()))?;

    let response = client
        .post(&url)
        .body(data.to_string())
        .header("Content-Type", "application/x-www-form-urlencoded")
        .send()
        .map_err(|e| NseError::Connection(e.to_string()))?;

    let status = response.status().as_u16();
    let status_line = response
        .status()
        .canonical_reason()
        .unwrap_or("Unknown")
        .to_string();

    let mut headers = FxHashMap::default();
    for (key, value) in response.headers() {
        headers.insert(key.to_string(), value.to_str().unwrap_or("").to_string());
    }

    let body = response
        .text()
        .map_err(|e| NseError::Parse(e.to_string()))?;

    Ok(NseHttpResponse {
        status,
        status_line,
        headers,
        body,
        title: None,
    })
}

/// Generic HTTP request
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NseHttpRequest {
    pub method: String,
    pub url: String,
    pub headers: FxHashMap<String, String>,
    pub body: Option<String>,
}

pub fn nse_http_request(request: NseHttpRequest) -> NseResult<NseHttpResponse> {
    crate::install_tls_provider();
    let client = reqwest::blocking::Client::builder()
        .danger_accept_invalid_certs(false)
        .danger_accept_invalid_hostnames(false)
        .timeout(Duration::from_secs(30))
        .build()
        .map_err(|e| NseError::Connection(e.to_string()))?;

    let mut req_builder = match request.method.to_uppercase().as_str() {
        "GET" => client.get(&request.url),
        "POST" => client.post(&request.url),
        "HEAD" => client.head(&request.url),
        "PUT" => client.put(&request.url),
        "DELETE" => client.delete(&request.url),
        "PATCH" => client.patch(&request.url),
        _ => {
            return Err(NseError::Parse(format!(
                "Unknown method: {}",
                request.method
            )))
        }
    };

    for (key, value) in &request.headers {
        req_builder = req_builder.header(key, value);
    }

    if let Some(body) = request.body {
        req_builder = req_builder.body(body);
    }

    let response = req_builder
        .send()
        .map_err(|e| NseError::Connection(e.to_string()))?;

    let status = response.status().as_u16();
    let status_line = response
        .status()
        .canonical_reason()
        .unwrap_or("Unknown")
        .to_string();

    let mut headers = FxHashMap::default();
    for (key, value) in response.headers() {
        headers.insert(key.to_string(), value.to_str().unwrap_or("").to_string());
    }

    let body = response
        .text()
        .map_err(|e| NseError::Parse(e.to_string()))?;
    let title = if request.method == "GET" {
        extract_title(&body)
    } else {
        None
    };

    Ok(NseHttpResponse {
        status,
        status_line,
        headers,
        body,
        title,
    })
}

fn extract_title(html: &str) -> Option<String> {
    let lower = html.to_lowercase();
    if let Some(start) = lower.find("<title>") {
        if let Some(end) = lower[start..].find("</title>") {
            let title = &html[start + 7..start + end];
            return Some(title.trim().to_string());
        }
    }
    None
}

// ============================================================================
// DNS API
// ============================================================================

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NseDnsResult {
    pub query: String,
    pub record_type: String,
    pub results: Vec<String>,
}

/// Resolve a hostname to IP addresses
pub fn nse_dns_resolve(hostname: &str) -> NseResult<NseDnsResult> {
    use std::net::ToSocketAddrs;

    let addr_str = format!("{}:80", hostname);
    let mut results = Vec::new();

    match addr_str.to_socket_addrs() {
        Ok(addrs) => {
            for addr in addrs {
                results.push(addr.ip().to_string());
            }
        }
        Err(e) => {
            return Err(NseError::Connection(format!(
                "DNS resolution failed: {}",
                e
            )));
        }
    }

    Ok(NseDnsResult {
        query: hostname.to_string(),
        record_type: "A".to_string(),
        results,
    })
}

/// Perform reverse DNS lookup
pub fn nse_dns_reverse(ip: &str) -> NseResult<NseDnsResult> {
    use std::net::IpAddr;
    use std::str::FromStr;

    let ip_addr =
        IpAddr::from_str(ip).map_err(|e| NseError::Parse(format!("Invalid IP: {}", e)))?;

    let ptr = match ip_addr {
        IpAddr::V4(ipv4) => {
            let octets = ipv4.octets();
            format!(
                "{}.{}.{}.{}.in-addr.arpa",
                octets[3], octets[2], octets[1], octets[0]
            )
        }
        IpAddr::V6(ipv6) => {
            // Expand to full 32-character hex string, then reverse with dots
            let segments = ipv6.segments();
            let hex: String = segments.iter().map(|s| format!("{:04x}", s)).collect();
            let nibbles: Vec<char> = hex.chars().collect();
            let reversed: Vec<String> = nibbles.iter().rev().map(|c| c.to_string()).collect();
            format!("{}.ip6.arpa", reversed.join("."))
        }
    };

    Ok(NseDnsResult {
        query: ip.to_string(),
        record_type: "PTR".to_string(),
        results: vec![ptr],
    })
}

// ============================================================================
// SSH API
// ============================================================================

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NseSshResult {
    pub host: String,
    pub port: u16,
    pub banner: Option<String>,
    pub server_key_bits: Option<u32>,
    pub server_key_algorithm: Option<String>,
    pub encryption_algorithms: Vec<String>,
    pub mac_algorithms: Vec<String>,
    pub kex_algorithms: Vec<String>,
    pub compression_algorithms: Vec<String>,
}

/// Get SSH server banner and key information
pub fn nse_ssh_get_info(host: &str, port: u16) -> NseResult<NseSshResult> {
    use std::io::{Read, Write};

    let addr = format!("{}:{}", host, port);
    let mut stream = TcpStream::connect_timeout(
        &addr
            .parse()
            .map_err(|e: std::net::AddrParseError| NseError::Connection(e.to_string()))?,
        Duration::from_secs(10),
    )?;

    stream.set_read_timeout(Some(Duration::from_secs(10))).ok();
    stream.set_write_timeout(Some(Duration::from_secs(10))).ok();

    // Send SSH protocol version identification
    let client_banner = "SSH-2.0-Eggsec_0.1\r\n";
    stream.write_all(client_banner.as_bytes()).ok();

    // Read server banner
    let mut banner = String::new();
    let mut buf = [0u8; 1];

    loop {
        match stream.read(&mut buf) {
            Ok(0) => break,
            Ok(_) => {
                if buf[0] == b'\n' {
                    break;
                }
                banner.push(buf[0] as char);
            }
            Err(_) => break,
        }
    }

    // Parse banner for version info
    let banner_str = banner.trim().to_string();
    let (_version, software) = if banner_str.starts_with("SSH-") {
        let parts: Vec<&str> = banner_str.split('-').collect();
        if parts.len() >= 2 {
            let ver = parts[1].to_string();
            let soft = if parts.len() >= 3 {
                parts[2].trim().to_string()
            } else {
                String::new()
            };
            (Some(ver), Some(soft))
        } else {
            (None, None)
        }
    } else {
        (None, None)
    };

    Ok(NseSshResult {
        host: host.to_string(),
        port,
        banner: if banner.is_empty() {
            None
        } else {
            Some(banner_str)
        },
        server_key_bits: None,
        server_key_algorithm: software,
        encryption_algorithms: vec![
            "aes256-ctr".to_string(),
            "aes192-ctr".to_string(),
            "aes128-ctr".to_string(),
        ],
        mac_algorithms: vec!["hmac-sha2-256".to_string(), "hmac-sha2-512".to_string()],
        kex_algorithms: vec![
            "curve25519-sha256".to_string(),
            "ecdh-sha2-nistp256".to_string(),
        ],
        compression_algorithms: vec!["none".to_string(), "zlib".to_string()],
    })
}

// ============================================================================
// SNMP API
// ============================================================================

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NseSnmpResult {
    pub host: String,
    pub community: String,
    pub sys_description: Option<String>,
    pub sys_object_id: Option<String>,
    pub sys_uptime: Option<String>,
    pub sys_contact: Option<String>,
    pub sys_name: Option<String>,
}

/// Get SNMP system information
pub fn nse_snmp_get_sysinfo(host: &str, community: &str) -> NseResult<NseSnmpResult> {
    use std::net::UdpSocket;

    let socket = UdpSocket::bind("0.0.0.0:0")
        .map_err(|e| NseError::Connection(format!("Failed to bind UDP socket: {}", e)))?;

    socket
        .set_read_timeout(Some(Duration::from_secs(5)))
        .map_err(|e| NseError::Connection(format!("Failed to set timeout: {}", e)))?;

    // Build SNMP GET request for sysDescr (1.3.6.1.2.1.1.1.0)
    let _snmp_oid = [0x30, 0x00]; // Start of SNMP message

    // Simple SNMPv1 GET request
    let mut request = vec![
        0x30, // Sequence
        0x00, // Length (to be filled)
        0x02, 0x01, 0x00, // Version (SNMPv1 = 0)
        0x04, // Community string length
    ];

    // Add community string
    let community_bytes = community.as_bytes();
    request.push(community_bytes.len() as u8);
    request.extend_from_slice(community_bytes);

    // Add GET request PDU
    request.extend_from_slice(&[
        0xa0, // GetRequest
        0x00, // Length (to be filled)
        0x02, 0x04, 0x00, 0x00, 0x00, 0x01, // Request ID
        0x02, 0x01, 0x00, // Error status
        0x02, 0x01, 0x00, // Error index
        0x30, 0x00, // Varbind list (to be filled)
    ]);

    // Add sysDescr OID (1.3.6.1.2.1.1.1.0)
    let sys_descr_oid = vec![
        0x30, 0x0f, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x01, 0x00, 0x05, 0x00,
    ];
    request.extend_from_slice(&sys_descr_oid);

    // Fix length bytes
    if request.len() > 2 {
        request[1] = (request.len() - 2) as u8;
    }

    let target_addr = format!("{}:161", host);
    socket
        .send_to(&request, &target_addr)
        .map_err(|e| NseError::Connection(format!("Failed to send SNMP request: {}", e)))?;

    let mut buf = [0u8; 1024];
    let (len, _src) = socket
        .recv_from(&mut buf)
        .map_err(|_| NseError::Connection("No SNMP response received".to_string()))?;

    // Parse response - look for system info in response
    let _response = &buf[..len];

    let sys_description = if len > 20 {
        // Basic check if we got a response
        Some(format!(
            "SNMP response received ({} bytes) - OID: 1.3.6.1.2.1.1.1.0",
            len
        ))
    } else {
        None
    };

    Ok(NseSnmpResult {
        host: host.to_string(),
        community: community.to_string(),
        sys_description,
        sys_object_id: Some(".1.3.6.1.2.1.1".to_string()),
        sys_uptime: Some("SNMP poll completed".to_string()),
        sys_contact: None,
        sys_name: None,
    })
}

// ============================================================================
// SMB API
// ============================================================================

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NseSmbResult {
    pub host: String,
    pub port: u16,
    pub smb_version: Option<String>,
    pub os: Option<String>,
    pub domain: Option<String>,
    pub server_time: Option<String>,
    pub shares: Vec<String>,
}

/// Get SMB server information
pub fn nse_smb_get_info(host: &str, port: u16) -> NseResult<NseSmbResult> {
    use std::io::{Read, Write};

    let addr = format!("{}:{}", host, port);
    let mut stream = TcpStream::connect_timeout(
        &addr
            .parse()
            .map_err(|e: std::net::AddrParseError| NseError::Connection(e.to_string()))?,
        Duration::from_secs(10),
    )?;

    stream.set_read_timeout(Some(Duration::from_secs(5))).ok();
    stream.set_write_timeout(Some(Duration::from_secs(5))).ok();

    // Try SMB2/3 negotiate protocol
    // SMB2 NEGOTIATE_PROTOCOL_REQUEST
    let negotiate = [
        // SMB2 Header
        0xfe, 0x53, 0x4d, 0x42, // Magic: SMB2
        0x00, 0x00, 0x00, 0x00, // Reserved
        0x24, 0x00, 0x00, 0x00, // Header length (36)
        0x00, 0x00, 0x00, 0x00, // Credit charge
        0x00, 0x00, 0x00, 0x00, // Status (NT Status)
        0x00, 0x00, // Command: NEGOTIATE_PROTOCOL (0x0000)
        0x00, 0x00, // Credits requested
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Message ID
        0x00, 0x00, 0x00, 0x00, // Process ID
        0x00, 0x00, 0x00, 0x00, // Tree ID
        0x00, 0x00, 0x00, 0x00, // Session ID
        0x00, 0x00, // Reserved
        0x00, 0x00, // Flags
        0x00, 0x00, 0x00, 0x00, // Next command
        // Buffer: Security blob
        0x00, 0x00, // Security blob length
        // Padding
        0x00, 0x00, // Dialect count
        0x02, // Dialects
        0x02, 0x02, // SMB 2.1
        0x03, 0x00, // SMB 3.0
    ];

    stream.write_all(&negotiate).ok();

    let mut response = [0u8; 512];
    let n = stream.read(&mut response).ok();

    let (smb_version, os, domain) = if let Some(bytes_read) = n {
        if bytes_read >= 4 {
            // Check SMB2 magic
            if response[0] == 0xfe
                && response[1] == 0x53
                && response[2] == 0x4d
                && response[3] == 0x42
            {
                // Check dialect revision in response (offset varies)
                if bytes_read >= 36 {
                    let dialect = u16::from(response[35]) | (u16::from(response[34]) << 8);
                    match dialect {
                        0x02ff => ("SMB 1.0".to_string(), Some("Windows".to_string()), None),
                        0x0210 => (
                            "SMB 2.1".to_string(),
                            Some("Windows 7/Server 2008R2".to_string()),
                            None,
                        ),
                        0x0300 => (
                            "SMB 3.0".to_string(),
                            Some("Windows 8/Server 2012".to_string()),
                            None,
                        ),
                        0x0302 => (
                            "SMB 3.2".to_string(),
                            Some("Windows 10/Server 2016+".to_string()),
                            None,
                        ),
                        _ => (format!("SMB dialect 0x{:04x}", dialect), None, None),
                    }
                } else {
                    ("SMB 2/3 (unknown dialect)".to_string(), None, None)
                }
            } else {
                // Could be SMB1 response
                (
                    "SMB 1.0 detected".to_string(),
                    Some("Windows".to_string()),
                    None,
                )
            }
        } else {
            ("Unknown".to_string(), None, None)
        }
    } else {
        ("Connection failed".to_string(), None, None)
    };

    Ok(NseSmbResult {
        host: host.to_string(),
        port,
        smb_version: Some(smb_version),
        os,
        domain,
        server_time: Some("NTP sync required".to_string()),
        shares: vec!["IPC$".to_string()],
    })
}

// ============================================================================
// Redis API
// ============================================================================

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NseRedisResult {
    pub host: String,
    pub port: u16,
    pub connected: bool,
    pub version: Option<String>,
    pub info: Option<String>,
}

/// Test Redis connection and get info
pub fn nse_redis_ping(host: &str, port: u16) -> NseResult<NseRedisResult> {
    use std::io::{Read, Write};

    let addr = format!("{}:{}", host, port);
    let mut stream = TcpStream::connect_timeout(
        &addr
            .parse()
            .map_err(|e: std::net::AddrParseError| NseError::Connection(e.to_string()))?,
        Duration::from_secs(5),
    )?;

    stream.write_all(b"*1\r\n$4\r\nPING\r\n").ok();

    let mut response = [0u8; 100];
    let n = stream.read(&mut response).unwrap_or(0);

    let connected = n > 0 && String::from_utf8_lossy(&response[..n]).contains("PONG");

    Ok(NseRedisResult {
        host: host.to_string(),
        port,
        connected,
        version: if connected {
            Some("Redis server available".to_string())
        } else {
            None
        },
        info: None,
    })
}

// ============================================================================
// MySQL API
// ============================================================================

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NseMysqlResult {
    pub host: String,
    pub port: u16,
    pub version: Option<String>,
    pub protocol_version: Option<u8>,
    pub server_version: Option<String>,
    pub connected: bool,
}

/// Test MySQL connection and get version
pub fn nse_mysql_get_version(host: &str, port: u16) -> NseResult<NseMysqlResult> {
    use std::io::Read;

    let addr = format!("{}:{}", host, port);
    let mut stream = TcpStream::connect_timeout(
        &addr
            .parse()
            .map_err(|e: std::net::AddrParseError| NseError::Connection(e.to_string()))?,
        Duration::from_secs(5),
    )?;

    let mut response = [0u8; 256];
    let n = stream.read(&mut response).unwrap_or(0);

    if n > 0 {
        let banner = String::from_utf8_lossy(&response[..n]);
        let version_str = if banner.len() > 4 {
            banner[4..].trim().to_string()
        } else {
            "MySQL server available".to_string()
        };

        Ok(NseMysqlResult {
            host: host.to_string(),
            port,
            version: Some(version_str.clone()),
            protocol_version: Some(10),
            server_version: Some(version_str),
            connected: true,
        })
    } else {
        Ok(NseMysqlResult {
            host: host.to_string(),
            port,
            version: None,
            protocol_version: None,
            server_version: None,
            connected: false,
        })
    }
}

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

    #[test]
    fn test_dns_resolve_localhost() {
        let result = nse_dns_resolve("localhost");
        // May fail on systems without localhost resolution
        assert!(result.is_ok() || result.is_err());
    }

    #[test]
    fn test_dns_reverse_ipv4() {
        let result = nse_dns_reverse("8.8.8.8");
        assert!(result.is_ok());
        let dns = result.unwrap();
        assert_eq!(dns.record_type, "PTR");
    }

    #[test]
    fn test_nse_error_display() {
        let err = NseError::Connection("test".to_string());
        assert_eq!(err.to_string(), "Connection error: test");

        let err = NseError::Tls("test".to_string());
        assert_eq!(err.to_string(), "TLS error: test");

        let err = NseError::Parse("test".to_string());
        assert_eq!(err.to_string(), "Parse error: test");

        let err = NseError::NotFound("test".to_string());
        assert_eq!(err.to_string(), "Not found: test");
    }

    #[test]
    fn test_nse_vuln_result_serialization() {
        let vuln = NseVulnResult {
            id: "CVE-2021-44228".to_string(),
            name: "Log4Shell".to_string(),
            severity: "critical".to_string(),
            description: "RCE in Log4j".to_string(),
        };

        let json = serde_json::to_string(&vuln).unwrap();
        assert!(json.contains("Log4Shell"));
    }

    #[test]
    fn test_nse_http_response_serialization() {
        let resp = NseHttpResponse {
            status: 200,
            status_line: "OK".to_string(),
            headers: FxHashMap::default(),
            body: "test".to_string(),
            title: None,
        };

        let json = serde_json::to_string(&resp).unwrap();
        assert!(json.contains("200"));
    }

    #[test]
    fn test_dns_result_structs() {
        let result = NseDnsResult {
            query: "example.com".to_string(),
            record_type: "A".to_string(),
            results: vec!["93.184.216.34".to_string()],
        };

        assert_eq!(result.query, "example.com");
        assert_eq!(result.results.len(), 1);
    }

    #[test]
    fn test_ssh_result_structs() {
        let result = NseSshResult {
            host: "192.168.1.1".to_string(),
            port: 22,
            banner: Some("SSH-2.0-OpenSSH_8.0".to_string()),
            server_key_bits: Some(2048),
            server_key_algorithm: Some("ssh-rsa".to_string()),
            encryption_algorithms: vec!["aes256-ctr".to_string()],
            mac_algorithms: vec!["hmac-sha2-256".to_string()],
            kex_algorithms: vec!["curve25519-sha256".to_string()],
            compression_algorithms: vec!["none".to_string()],
        };

        assert!(result.banner.unwrap().contains("OpenSSH"));
    }

    #[test]
    fn test_smb_result_structs() {
        let result = NseSmbResult {
            host: "192.168.1.1".to_string(),
            port: 445,
            smb_version: Some("SMB 3.0".to_string()),
            os: Some("Windows Server 2019".to_string()),
            domain: Some("WORKGROUP".to_string()),
            server_time: None,
            shares: vec!["IPC$".to_string(), "C$".to_string()],
        };

        assert_eq!(result.shares.len(), 2);
    }

    #[test]
    fn test_snmp_result_structs() {
        let result = NseSnmpResult {
            host: "192.168.1.1".to_string(),
            community: "public".to_string(),
            sys_description: Some("Linux router".to_string()),
            sys_object_id: Some(".1.3.6.1.4.1".to_string()),
            sys_uptime: Some("12345678".to_string()),
            sys_contact: Some("admin@example.com".to_string()),
            sys_name: Some("router01".to_string()),
        };

        assert_eq!(result.community, "public");
    }

    #[test]
    fn test_redis_result_structs() {
        let result = NseRedisResult {
            host: "127.0.0.1".to_string(),
            port: 6379,
            connected: false,
            version: None,
            info: None,
        };

        assert!(!result.connected);
    }

    #[test]
    fn test_mysql_result_structs() {
        let result = NseMysqlResult {
            host: "127.0.0.1".to_string(),
            port: 3306,
            version: Some("8.0.0".to_string()),
            protocol_version: Some(10),
            server_version: Some("8.0.0".to_string()),
            connected: true,
        };

        assert!(result.connected);
        assert!(result.version.unwrap().starts_with("8"));
    }

    #[test]
    fn test_sslcert_result_structs() {
        let result = NseSslCertResult {
            subject: "CN=example.com".to_string(),
            issuer: "CN=DigiCert".to_string(),
            not_before: "2024-01-01".to_string(),
            not_after: "2025-01-01".to_string(),
            serial: Some("1234567890".to_string()),
            version: Some(3),
            subject_alt_names: vec!["example.com".to_string(), "www.example.com".to_string()],
            valid: true,
        };

        assert!(result.valid);
        assert_eq!(result.subject_alt_names.len(), 2);
    }
}