use native_tls::TlsConnector;
use openssl::x509::X509;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use std::net::TcpStream;
use std::time::Duration;
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())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NseVulnResult {
pub id: String,
pub name: String,
pub severity: String,
pub description: String,
}
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)
}
}
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)
}
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();
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"),
);
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",
),
);
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",
),
);
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",
),
);
m.insert(
"CVE-2018-15473",
(
"OpenSSH User Enumeration",
"medium",
"OpenSSH information disclosure via username enumeration",
),
);
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();
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
}
#[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,
}
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()))
}
}
pub fn nse_sslcert_get_chain(host: &str, port: u16) -> NseResult<Vec<NseSslCertResult>> {
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> {
Vec::new()
}
#[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>,
}
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()))?;
let title = extract_title(&body);
Ok(NseHttpResponse {
status,
status_line,
headers,
body,
title,
})
}
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,
})
}
#[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
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NseDnsResult {
pub query: String,
pub record_type: String,
pub results: Vec<String>,
}
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,
})
}
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) => {
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],
})
}
#[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>,
}
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();
let client_banner = "SSH-2.0-Eggsec_0.1\r\n";
stream.write_all(client_banner.as_bytes()).ok();
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,
}
}
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()],
})
}
#[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>,
}
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)))?;
let _snmp_oid = [0x30, 0x00];
let mut request = vec![
0x30, 0x00, 0x02, 0x01, 0x00, 0x04, ];
let community_bytes = community.as_bytes();
request.push(community_bytes.len() as u8);
request.extend_from_slice(community_bytes);
request.extend_from_slice(&[
0xa0, 0x00, 0x02, 0x04, 0x00, 0x00, 0x00, 0x01, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x00, ]);
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);
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()))?;
let _response = &buf[..len];
let sys_description = if len > 20 {
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,
})
}
#[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>,
}
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();
let negotiate = [
0xfe, 0x53, 0x4d, 0x42, 0x00, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x03, 0x00, ];
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 {
if response[0] == 0xfe
&& response[1] == 0x53
&& response[2] == 0x4d
&& response[3] == 0x42
{
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 {
(
"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()],
})
}
#[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>,
}
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,
})
}
#[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,
}
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");
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);
}
}