netscan 0.30.0

Cross-platform network scan library
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
use netdev::Interface;
use netdev::MacAddr;
use nex::packet::tcp::TcpFlags;

use crate::host::{Host, Port, PortStatus};
use crate::packet::frame::PacketFrame;
use std::collections::HashSet;
use std::net::{IpAddr, SocketAddr};
use std::time::Duration;

use super::setting::{HostScanSetting, HostScanType, PortScanSetting};

/// Error details for scan task failures.
#[derive(Clone, Debug, PartialEq)]
pub enum ScanError {
    InterfaceNotFound,
    UnhandledChannelType,
    UnhandledAsyncChannelType,
    ChannelCreationFailed(String),
    AsyncChannelCreationFailed(String),
    RuntimeError(String),
    Message(String),
}

impl From<String> for ScanError {
    fn from(value: String) -> Self {
        ScanError::Message(value)
    }
}

impl From<&str> for ScanError {
    fn from(value: &str) -> Self {
        ScanError::Message(value.to_string())
    }
}

/// Status of scan task
#[derive(Clone, Debug, PartialEq)]
pub enum ScanStatus {
    Done,
    Timeout,
    Error(ScanError),
}

/// Result of scan
#[derive(Clone, Debug)]
pub struct ScanResult {
    /// List of scanned Host info and their respective ports
    pub hosts: Vec<Host>,
    /// Time taken to scan
    pub scan_time: Duration,
    /// Status of the scan task
    pub scan_status: ScanStatus,
    /// Captured packet fingerprints
    pub fingerprints: Vec<PacketFrame>,
}

impl ScanResult {
    pub fn new() -> ScanResult {
        ScanResult {
            hosts: vec![],
            scan_time: Duration::from_millis(0),
            scan_status: ScanStatus::Done,
            fingerprints: vec![],
        }
    }
    pub fn error<E: Into<ScanError>>(error: E) -> ScanResult {
        ScanResult {
            hosts: vec![],
            scan_time: Duration::from_millis(0),
            scan_status: ScanStatus::Error(error.into()),
            fingerprints: vec![],
        }
    }
    /// Returns IP addresses from the scan result
    pub fn get_hosts(&self) -> Vec<IpAddr> {
        let mut hosts: Vec<IpAddr> = vec![];
        for host in self.hosts.clone() {
            hosts.push(host.ip_addr);
        }
        hosts
    }
    /// Get open ports of the specified IP address from the scan results
    pub fn get_open_port_numbers(&self, ip_addr: IpAddr) -> Vec<u16> {
        let mut open_ports: Vec<u16> = vec![];
        self.hosts.iter().for_each(|host_info| {
            if host_info.ip_addr == ip_addr {
                host_info
                    .ports
                    .iter()
                    .for_each(|port_info| match port_info.status {
                        PortStatus::Open => {
                            open_ports.push(port_info.number);
                        }
                        _ => {}
                    });
            }
        });
        open_ports
    }
    /// Get open port fingerprint
    pub fn get_syn_ack_fingerprint(&self, ip_addr: IpAddr, port: u16) -> Option<PacketFrame> {
        for fingerprint in self.fingerprints.iter() {
            if let Some(ipv4_packet) = &fingerprint.ipv4_header {
                if ipv4_packet.source == ip_addr {
                    if let Some(tcp_packet) = &fingerprint.tcp_header {
                        if tcp_packet.source == port
                            && tcp_packet.flags == TcpFlags::SYN | TcpFlags::ACK
                        {
                            return Some(fingerprint.clone());
                        }
                    }
                }
            } else if let Some(ipv6_packet) = &fingerprint.ipv6_header {
                if ipv6_packet.source == ip_addr {
                    if let Some(tcp_packet) = &fingerprint.tcp_header {
                        if tcp_packet.source == port
                            && tcp_packet.flags == TcpFlags::SYN | TcpFlags::ACK
                        {
                            return Some(fingerprint.clone());
                        }
                    }
                }
            }
        }
        None
    }
    pub fn get_host(&self, ip_addr: IpAddr) -> Option<Host> {
        for host in self.hosts.iter() {
            if host.ip_addr == ip_addr {
                return Some(host.clone());
            }
        }
        None
    }
    pub fn sort_hosts(&mut self) {
        self.hosts.sort_by(|a, b| a.ip_addr.cmp(&b.ip_addr));
    }
    pub fn sort_ports(&mut self) {
        for host in self.hosts.iter_mut() {
            host.ports.sort_by(|a, b| a.number.cmp(&b.number));
        }
    }
}

/// Result of a service probe
#[derive(Clone, Debug, PartialEq)]
pub struct ServiceProbeResult {
    pub port: u16,
    pub service_name: String,
    pub service_detail: Option<String>,
    pub response: Vec<u8>,
    pub error: Option<ServiceProbeError>,
}

impl ServiceProbeResult {
    /// Create a new successful probe result
    pub fn new(port: u16, service_name: String, response: Vec<u8>) -> Self {
        ServiceProbeResult {
            port,
            service_name,
            service_detail: None,
            response,
            error: None,
        }
    }

    /// Create a new probe result with an error
    pub fn with_error(port: u16, service_name: String, error: ServiceProbeError) -> Self {
        ServiceProbeResult {
            port,
            service_name,
            service_detail: None,
            response: Vec::new(),
            error: Some(error),
        }
    }

    /// Check if the result contains an error
    pub fn has_error(&self) -> bool {
        self.error.is_some()
    }

    /// Get a reference to the contained error, if any
    pub fn error(&self) -> Option<&ServiceProbeError> {
        self.error.as_ref()
    }

    /// Extract the error, consuming the result
    pub fn into_error(self) -> Option<ServiceProbeError> {
        self.error
    }
}

#[derive(Clone, Debug, PartialEq)]
pub enum ServiceProbeError {
    ConnectionError(String),
    WriteError(String),
    ReadError(String),
    TlsError(String),
    CustomError(String),
}

pub(crate) fn parse_hostscan_result(
    packets: Vec<PacketFrame>,
    scan_setting: HostScanSetting,
) -> ScanResult {
    let mut result: ScanResult = ScanResult::new();
    let iface: Interface = match crate::interface::get_interface_by_index(scan_setting.if_index) {
        Some(iface) => iface,
        None => return ScanResult::error(ScanError::InterfaceNotFound),
    };
    let iface_ips: HashSet<IpAddr> = crate::interface::get_local_ips(scan_setting.if_index);
    for p in packets {
        let mac_addr: MacAddr;
        if let Some(ethernet_frame) = &p.ethernet_header {
            if ethernet_frame.destination != iface.mac_addr.unwrap_or(MacAddr::zero()) {
                continue;
            }
            mac_addr = ethernet_frame.source;
        } else {
            mac_addr = MacAddr::zero();
        }
        let mut ports: Vec<Port> = vec![];
        match scan_setting.scan_type {
            HostScanType::IcmpPingScan => {
                if p.icmp_header.is_none() && p.icmpv6_header.is_none() {
                    continue;
                }
            }
            HostScanType::TcpPingScan => {
                if p.tcp_header.is_none() {
                    continue;
                }
                if let Some(tcp_packet) = &p.tcp_header {
                    if tcp_packet.flags == TcpFlags::SYN | TcpFlags::ACK {
                        let port_info: Port = Port {
                            number: tcp_packet.source,
                            status: PortStatus::Open,
                            service_name: String::new(),
                            service_version: String::new(),
                        };
                        ports.push(port_info);
                    } else if tcp_packet.flags == TcpFlags::RST | TcpFlags::ACK {
                        let port_info: Port = Port {
                            number: tcp_packet.source,
                            status: PortStatus::Closed,
                            service_name: String::new(),
                            service_version: String::new(),
                        };
                        ports.push(port_info);
                    } else {
                        continue;
                    }
                } else {
                    continue;
                }
            }
            HostScanType::UdpPingScan => {
                if p.icmp_header.is_none() && p.icmp_header.is_none() {
                    continue;
                }
            }
        }
        let host_info: Host = if let Some(ipv4_packet) = &p.ipv4_header {
            Host {
                ip_addr: IpAddr::V4(ipv4_packet.source),
                hostname: scan_setting
                    .dns_map
                    .get(&IpAddr::V4(ipv4_packet.source))
                    .unwrap_or(&String::new())
                    .clone(),
                ports: ports,
                mac_addr: if iface_ips.contains(&IpAddr::V4(ipv4_packet.source)) {
                    iface.mac_addr.unwrap_or(MacAddr::zero())
                } else {
                    mac_addr
                },
                ttl: ipv4_packet.ttl,
            }
        } else if let Some(ipv6_packet) = &p.ipv6_header {
            Host {
                ip_addr: IpAddr::V6(ipv6_packet.source),
                hostname: scan_setting
                    .dns_map
                    .get(&IpAddr::V6(ipv6_packet.source))
                    .unwrap_or(&String::new())
                    .clone(),
                ports: ports,
                mac_addr: if iface_ips.contains(&IpAddr::V6(ipv6_packet.source)) {
                    iface.mac_addr.unwrap_or(MacAddr::zero())
                } else {
                    mac_addr
                },
                ttl: ipv6_packet.hop_limit,
            }
        } else {
            continue;
        };
        if !result.hosts.contains(&host_info) {
            result.hosts.push(host_info);
            result.fingerprints.push(p.clone());
        }
    }
    return result;
}

pub(crate) fn parse_portscan_result(
    packets: Vec<PacketFrame>,
    scan_setting: PortScanSetting,
) -> ScanResult {
    let mut result: ScanResult = ScanResult::new();
    let mut socket_set: HashSet<SocketAddr> = HashSet::new();
    let iface: Interface = match crate::interface::get_interface_by_index(scan_setting.if_index) {
        Some(iface) => iface,
        None => return ScanResult::error(ScanError::InterfaceNotFound),
    };
    for p in packets {
        if p.ipv4_header.is_none() && p.ipv6_header.is_none() {
            continue;
        }
        let mac_addr: MacAddr;
        if let Some(ethernet_frame) = &p.ethernet_header {
            if ethernet_frame.destination != iface.mac_addr.unwrap_or(MacAddr::zero()) {
                continue;
            }
            mac_addr = ethernet_frame.source;
        } else {
            mac_addr = MacAddr::zero();
        }
        let ip_addr: IpAddr = {
            if let Some(ipv4_packet) = &p.ipv4_header {
                if let Some(tcp_packet) = &p.tcp_header {
                    if socket_set.contains(&SocketAddr::new(
                        IpAddr::V4(ipv4_packet.source),
                        tcp_packet.source,
                    )) {
                        continue;
                    }
                } else {
                    continue;
                }
                IpAddr::V4(ipv4_packet.source)
            } else if let Some(ipv6_packet) = &p.ipv6_header {
                if let Some(tcp_packet) = &p.tcp_header {
                    if socket_set.contains(&SocketAddr::new(
                        IpAddr::V6(ipv6_packet.source),
                        tcp_packet.source,
                    )) {
                        continue;
                    }
                } else {
                    continue;
                }
                IpAddr::V6(ipv6_packet.source)
            } else {
                continue;
            }
        };
        let ttl = if let Some(ipv4_packet) = &p.ipv4_header {
            ipv4_packet.ttl
        } else if let Some(ipv6_packet) = &p.ipv6_header {
            ipv6_packet.hop_limit
        } else {
            0
        };
        let port_info: Port = if let Some(tcp_packet) = &p.tcp_header {
            if tcp_packet.flags == TcpFlags::SYN | TcpFlags::ACK {
                Port {
                    number: tcp_packet.source,
                    status: PortStatus::Open,
                    service_name: String::new(),
                    service_version: String::new(),
                }
            } else if tcp_packet.flags == TcpFlags::RST | TcpFlags::ACK {
                Port {
                    number: tcp_packet.source,
                    status: PortStatus::Closed,
                    service_name: String::new(),
                    service_version: String::new(),
                }
            } else {
                continue;
            }
        } else {
            continue;
        };
        let mut exists: bool = false;
        for host in result.hosts.iter_mut() {
            if host.ip_addr == ip_addr {
                host.ports.push(port_info.clone());
                exists = true;
            }
        }
        if !exists {
            let host_info: Host = Host {
                ip_addr: ip_addr,
                hostname: scan_setting
                    .dns_map
                    .get(&ip_addr)
                    .unwrap_or(&String::new())
                    .clone(),
                ports: vec![port_info.clone()],
                mac_addr: mac_addr,
                ttl: ttl,
            };
            result.hosts.push(host_info);
        }
        result.fingerprints.push(p.clone());
        socket_set.insert(SocketAddr::new(ip_addr, port_info.number));
    }
    result
}