nmaprs 0.1.1

High-performance parallel network scanner with nmap-compatible CLI surface
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
//! Normal (`-oN`), grepable (`-oG`), and minimal XML (`-oX`) writers.

use std::fs::{File, OpenOptions};
use std::io::Write;
use std::net::IpAddr;
use std::path::Path;

use anyhow::{Context, Result};

use crate::scan::PortLine;

pub struct OutputSet {
    pub normal: Option<File>,
    pub grep: Option<File>,
    pub xml: Option<File>,
    /// Mirrors `-oN`-style lines in script-kiddie form (Nmap `-oS`).
    pub skiddie: Option<File>,
    /// Nmap `-oM` machine-parseable (same content family as `-oG` in nmaprs).
    pub machine: Option<File>,
    /// Nmap `-oH` hex dump (placeholder).
    pub hex: Option<File>,
}

impl OutputSet {
    pub fn open(
        normal: Option<&Path>,
        grep: Option<&Path>,
        xml: Option<&Path>,
        skiddie: Option<&Path>,
        machine: Option<&Path>,
        hex: Option<&Path>,
        append: bool,
    ) -> Result<Self> {
        let open = |p: &Path| {
            if append {
                OpenOptions::new().create(true).append(true).open(p)
            } else {
                File::create(p)
            }
        };
        Ok(Self {
            normal: normal.map(open).transpose().with_context(|| "oN")?,
            grep: grep.map(open).transpose().with_context(|| "oG")?,
            xml: xml.map(open).transpose().with_context(|| "oX")?,
            skiddie: skiddie.map(open).transpose().with_context(|| "oS")?,
            machine: machine.map(open).transpose().with_context(|| "oM")?,
            hex: hex.map(open).transpose().with_context(|| "oH")?,
        })
    }

    pub fn write_headers(
        &mut self,
        cmdline: &str,
        stylesheet: Option<&str>,
        webxml: bool,
        no_stylesheet: bool,
    ) -> Result<()> {
        if let Some(f) = &mut self.xml {
            writeln!(f, r#"<?xml version="1.0" encoding="UTF-8"?>"#)?;
            writeln!(f, r#"<!DOCTYPE nmaprun>"#)?;
            if !no_stylesheet {
                if let Some(s) = stylesheet {
                    writeln!(
                        f,
                        r#"<?xml-stylesheet href="{}" type="text/xsl"?>"#,
                        xml_escape(s)
                    )?;
                } else if webxml {
                    writeln!(
                        f,
                        r#"<?xml-stylesheet href="https://svn.nmap.org/nmap/docs/nmap.xsl" type="text/xsl"?>"#
                    )?;
                }
            }
            let now = chrono_timestamp();
            writeln!(
                f,
                r#"<nmaprun scanner="nmaprs" args="{}" start="{}" startstr="{}" version="0.1.0" xmloutputversion="1.05">"#,
                xml_escape(cmdline),
                now.0,
                xml_escape(&now.1)
            )?;
            writeln!(f, r#"<verbose level="0"/>"#)?;
            writeln!(f, r#"<debugging level="0"/>"#)?;
        }
        Ok(())
    }

    pub fn write_scaninfo(
        &mut self,
        scan_type: &str,
        protocol: &str,
        num_services: usize,
    ) -> Result<()> {
        if let Some(f) = &mut self.xml {
            writeln!(
                f,
                r#"<scaninfo type="{}" protocol="{}" numservices="{}"/>"#,
                xml_escape(scan_type),
                xml_escape(protocol),
                num_services
            )?;
        }
        Ok(())
    }

    pub fn write_footer(
        &mut self,
        hosts_up: usize,
        hosts_down: usize,
        hosts_total: usize,
    ) -> Result<()> {
        if let Some(f) = &mut self.xml {
            let now = chrono_timestamp();
            writeln!(
                f,
                r#"<runstats><finished time="{}" timestr="{}" summary="nmaprs done: {} IP address{} ({} host{} up)" exit="success"/>"#,
                now.0,
                xml_escape(&now.1),
                hosts_total,
                if hosts_total == 1 { "" } else { "es" },
                hosts_up,
                if hosts_up == 1 { "" } else { "s" },
            )?;
            writeln!(
                f,
                r#"<hosts up="{}" down="{}" total="{}"/>"#,
                hosts_up, hosts_down, hosts_total
            )?;
            writeln!(f, "</runstats></nmaprun>")?;
        }
        Ok(())
    }
}

fn xml_escape(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('\"', "&quot;")
}

/// Returns (unix_timestamp, human_readable_string).
fn chrono_timestamp() -> (u64, String) {
    use std::time::{SystemTime, UNIX_EPOCH};
    let dur = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default();
    let secs = dur.as_secs();
    // Format a simple UTC timestamp without pulling in chrono crate.
    // Days since epoch → date; seconds of day → HH:MM:SS.
    let days = secs / 86400;
    let rem = secs % 86400;
    let h = rem / 3600;
    let m = (rem % 3600) / 60;
    let s = rem % 60;
    // Compute year/month/day from days since 1970-01-01 (civil calendar algorithm).
    let (y, mo, d) = days_to_ymd(days);
    let months = [
        "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
    ];
    let month_name = months.get(mo as usize).unwrap_or(&"???");
    let weekdays = ["Thu", "Fri", "Sat", "Sun", "Mon", "Tue", "Wed"];
    let dow = weekdays[(days % 7) as usize];
    (
        secs,
        format!(
            "{} {} {:2} {:02}:{:02}:{:02} {}",
            dow, month_name, d, h, m, s, y
        ),
    )
}

fn days_to_ymd(days: u64) -> (i64, u32, u32) {
    // Algorithm from Howard Hinnant's chrono-compatible date library.
    let z = days as i64 + 719468;
    let era = if z >= 0 { z } else { z - 146096 } / 146097;
    let doe = (z - era * 146097) as u64;
    let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
    let y = yoe as i64 + era * 400;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let d = doy - (153 * mp + 2) / 5 + 1;
    let m = if mp < 10 { mp + 3 } else { mp - 9 };
    let y = if m <= 2 { y + 1 } else { y };
    (y, m as u32 - 1, d as u32) // m-1 for 0-indexed month
}

/// Single port line as printed / written to `-oN` (tab-separated).
pub fn port_line_text(l: &PortLine, show_reason: bool) -> String {
    use std::fmt::Write;
    let mut s = String::with_capacity(48);
    let _ = write!(s, "{}/{}\t{}", l.port, l.proto, l.state);
    if let Some(ref v) = l.version_info {
        let _ = write!(s, "\t{v}");
    }
    if show_reason {
        let _ = write!(s, "\t{}", reason_str(l));
    }
    s
}

pub fn print_stdout(lines: &[PortLine], open_only: bool, show_reason: bool, verbosity: u8) {
    for l in lines {
        if open_only && l.state != "open" {
            continue;
        }
        if verbosity == 0 && l.state == "closed" {
            continue;
        }
        println!("{}", port_line_text(l, show_reason));
    }
}

fn reason_str(l: &PortLine) -> &'static str {
    use crate::scan::PortReason::*;
    match l.reason {
        SynAck => "syn-ack",
        ConnRefused => "conn-refused",
        TcpRst => "reset",
        TcpWindowRst => "tcp-window",
        Timeout => "no-response",
        HostTimeout => "host-timeout",
        Error => "error",
        UdpResponse => "udp-response",
        IcmpPortUnreachable => "icmp-port-unreachable",
        IcmpUnreachableFiltered => "icmp-unreachable",
        IcmpProtoUnreachable => "icmp-proto-unreachable",
        FtpBounceOpen => "ftp-bounce-open",
        FtpBounceClosed => "ftp-bounce-closed",
        SctpInitAck => "sctp-init-ack",
        SctpCookieAck => "sctp-cookie-ack",
        SctpAbort => "sctp-abort",
        IdleIpIdOpen => "idle-ipid-open",
        IdleIpIdClosed => "idle-ipid-closed",
        IdleProbeFailed => "idle-probe-failed",
    }
}

/// Write `-oN` and/or `-oS` host sections (same tab-separated lines; `-oS` is script-kiddie transformed).
pub fn write_normal_files(
    mut normal: Option<&mut File>,
    mut skiddie: Option<&mut File>,
    host: IpAddr,
    lines: &[PortLine],
    show_reason: bool,
) -> Result<()> {
    if normal.is_none() && skiddie.is_none() {
        return Ok(());
    }
    let hdr = format!("Nmap scan report for {host}");
    if let Some(f) = normal.as_mut() {
        writeln!(f, "{}", hdr)?;
    }
    if let Some(sf) = skiddie.as_mut() {
        writeln!(sf, "{}", crate::skiddie::skid_line(&hdr))?;
    }
    for l in lines {
        let line = port_line_text(l, show_reason);
        if let Some(f) = normal.as_mut() {
            writeln!(f, "{}", line)?;
        }
        if let Some(sf) = skiddie.as_mut() {
            writeln!(sf, "{}", crate::skiddie::skid_line(&line))?;
        }
    }
    if let Some(f) = normal.as_mut() {
        writeln!(f)?;
    }
    if let Some(sf) = skiddie.as_mut() {
        writeln!(sf)?;
    }
    Ok(())
}

/// `-sn` ping scan: host up line and optional `OS guess:` line (mirrors stdout to output files).
pub fn write_sn_host_files(
    mut normal: Option<&mut File>,
    mut skiddie: Option<&mut File>,
    mut grep: Option<&mut File>,
    mut xml: Option<&mut File>,
    host: IpAddr,
    os_guess_line: Option<&str>,
) -> Result<()> {
    let report = format!("Nmap scan report for {host} - Host is up");
    if let Some(f) = normal.as_mut() {
        writeln!(f, "{}", report)?;
    }
    if let Some(sf) = skiddie.as_mut() {
        writeln!(sf, "{}", crate::skiddie::skid_line(&report))?;
    }
    if let Some(gf) = grep.as_mut() {
        writeln!(gf, "Host: {host} ()\tStatus: Up")?;
    }
    if let Some(xf) = xml.as_mut() {
        let ty = if host.is_ipv4() { "ipv4" } else { "ipv6" };
        writeln!(xf, r#"<host>"#)?;
        writeln!(
            xf,
            r#"<status state="up" reason="echo-reply" reason_ttl="0"/>"#
        )?;
        writeln!(xf, r#"<address addr="{host}" addrtype="{ty}"/>"#)?;
        writeln!(xf, r#"<hostnames/>"#)?;
        writeln!(xf, r#"</host>"#)?;
    }
    if let Some(line) = os_guess_line {
        if let Some(f) = normal.as_mut() {
            writeln!(f, "{}", line)?;
        }
        if let Some(sf) = skiddie.as_mut() {
            writeln!(sf, "{}", crate::skiddie::skid_line(line))?;
        }
    }
    if normal.is_some() || skiddie.is_some() {
        if let Some(f) = normal.as_mut() {
            writeln!(f)?;
        }
        if let Some(sf) = skiddie.as_mut() {
            writeln!(sf)?;
        }
    }
    Ok(())
}

pub fn write_grep(f: &mut File, host: IpAddr, lines: &[PortLine]) -> Result<()> {
    for l in lines {
        writeln!(
            f,
            "Host: {} ()\tPorts: {}/{}/open////",
            host, l.port, l.proto
        )?;
    }
    Ok(())
}

pub fn write_xml_host(
    f: &mut File,
    host: IpAddr,
    lines: &[PortLine],
    os_info: Option<&str>,
) -> Result<()> {
    let ty = if host.is_ipv4() { "ipv4" } else { "ipv6" };
    writeln!(f, r#"<host starttime="0" endtime="0">"#)?;
    writeln!(
        f,
        r#"<status state="up" reason="user-set" reason_ttl="0"/>"#
    )?;
    writeln!(f, r#"<address addr="{}" addrtype="{}"/>"#, host, ty)?;
    writeln!(f, r#"<hostnames/>"#)?;

    // Compute extraports (most common non-shown state).
    let closed_count = lines.iter().filter(|l| l.state == "closed").count();
    let filtered_count = lines.iter().filter(|l| l.state == "filtered").count();

    writeln!(f, "<ports>")?;

    // extraports: summarize closed/filtered ports
    if closed_count > 0 {
        writeln!(
            f,
            r#"<extraports state="closed" count="{}"/>"#,
            closed_count
        )?;
    }
    if filtered_count > 0 {
        writeln!(
            f,
            r#"<extraports state="filtered" count="{}"/>"#,
            filtered_count
        )?;
    }

    for l in lines {
        write!(
            f,
            r#"<port protocol="{}" portid="{}"><state state="{}" reason="{}" reason_ttl="0"/>"#,
            l.proto,
            l.port,
            l.state,
            reason_str(l)
        )?;
        if let Some(ref v) = l.version_info {
            // Parse version_info into product/version if possible.
            let (product, version) = split_version_info(v);
            write!(
                f,
                r#"<service name="{}" product="{}" version="{}"/>"#,
                xml_escape(l.proto),
                xml_escape(product),
                xml_escape(version)
            )?;
        }
        writeln!(f, "</port>")?;
    }
    writeln!(f, "</ports>")?;

    if let Some(os) = os_info {
        writeln!(
            f,
            r#"<os><osmatch name="{}" accuracy="0"/></os>"#,
            xml_escape(os)
        )?;
    }

    writeln!(f, "</host>")?;
    Ok(())
}

fn split_version_info(v: &str) -> (&str, &str) {
    // Version info format: "product version (info) [os] {device} cpe:/..."
    // Simple split on first space for product vs version.
    match v.split_once(' ') {
        Some((p, rest)) => (p, rest),
        None => (v, ""),
    }
}