zucchini-scanner 0.1.1

Blazing-fast TCP port scanner for Linux
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
#![doc = env!("CARGO_PKG_DESCRIPTION")]
#![doc = ""]
#![cfg_attr(doc, doc = include_str!("../README.md"))]
#![doc(
    html_logo_url = "https://raw.githubusercontent.com/0xdea/singsing-rs/master/.img/logo_zucchini.png"
)]

use std::fmt;
use std::io::{self, Write};
use std::net::Ipv4Addr;
use std::process::ExitCode;
use std::str::FromStr;
use std::time::{Duration, Instant};

use anyhow::Context as _;
use chrono::{DateTime, Local, TimeZone};
use clap::Parser;
use singsing_rs::{
    CallbackError, Port, PortState, PortsError, ScanConfig, ScanError, ScanProgress, ScanResult,
    TargetsError, interface_ipv4, parse_ports, parse_targets, ports_from_services,
    scan_with_callbacks,
};

/// Binary name.
const PROGRAM: &str = env!("CARGO_BIN_NAME");
/// Package version.
const VERSION: &str = env!("CARGO_PKG_VERSION");
/// Package description.
const DESCRIPTION: &str = env!("CARGO_PKG_DESCRIPTION");
/// Package authors.
const AUTHORS: &str = env!("CARGO_PKG_AUTHORS");

/// IPv4 scan targets parsed from a `--host` argument.
///
/// Wrapped in a newtype so clap treats a single `--host` occurrence as one parsed value rather
/// than inferring multi-occurrence behavior from a bare `Vec<Ipv4Addr>` field type.
#[derive(Clone, Debug, Eq, PartialEq)]
struct Targets(Vec<Ipv4Addr>);

impl FromStr for Targets {
    type Err = TargetsError;

    fn from_str(input: &str) -> Result<Self, Self::Err> {
        parse_targets(input).map(Self)
    }
}

/// TCP ports parsed from a `--ports` argument.
///
/// Wrapped in a newtype for the same reason as [`Targets`].
#[derive(Clone, Debug, Eq, PartialEq)]
struct Ports(Vec<Port>);

impl FromStr for Ports {
    type Err = PortsError;

    fn from_str(input: &str) -> Result<Self, Self::Err> {
        parse_ports(input).map(Self)
    }
}

/// Command-line arguments.
#[derive(Debug, Parser)]
#[command(name = PROGRAM, disable_help_flag = true, about = None)]
struct Arguments {
    /// Network interface to use for the scan.
    #[arg(short = 'i', long)]
    interface: String,

    /// IPv4 address or CIDR to scan (e.g., 192.168.0.0/24).
    #[arg(short = 'h', long)]
    host: Targets,

    /// Ports (e.g., 21-23,80,443) [default: /etc/services].
    #[arg(short = 'p', long)]
    ports: Option<Ports>,

    /// Display ports that reply with RST.
    #[arg(short = 'c', long)]
    closed: bool,

    /// Usable bandwidth in KiB/s.
    #[arg(short = 'b', long, default_value_t = 15, value_parser = clap::value_parser!(u64).range(1..))]
    bandwidth: u64,

    /// Seconds to wait for late replies.
    #[arg(short = 't', long, default_value_t = 30, value_parser = clap::value_parser!(u64).range(1..))]
    timeout: u64,

    /// Stream scan results as soon as they arrive.
    #[arg(short = 'v', long)]
    verbose: bool,

    /// Print command help.
    #[arg(long, action = clap::ArgAction::Help)]
    help: Option<bool>,
}

fn main() -> ExitCode {
    match run() {
        Ok(()) => ExitCode::SUCCESS,
        Err(error) => {
            eprintln!("[!] Error: {error:#}");
            ExitCode::FAILURE
        }
    }
}

/// Runs the main scan logic.
fn run() -> anyhow::Result<()> {
    write_banner()?;

    let args = Arguments::parse();
    let Targets(host) = args.host;
    let ports = args
        .ports
        .map(|Ports(ports)| ports)
        .map_or_else(|| ports_from_services("/etc/services"), Ok)?;
    let source = interface_ipv4(&args.interface)?;

    let mut config = ScanConfig::new(host, ports, source);
    config.show_closed = args.closed;
    config.bandwidth_kib = args.bandwidth;
    config.timeout = Duration::from_secs(args.timeout);

    let probes = config
        .targets
        .len()
        .checked_mul(config.ports.len())
        .context("scan size overflow")?;
    write_scan_summary(probes, &args.interface, source)?;

    let started = Instant::now();
    let verbose = args.verbose;
    let scan_result = scan_with_callbacks(
        &config,
        move |result| {
            if verbose {
                write_verbose_result(result)
            } else {
                Ok(())
            }
            .map_err(|error| to_boxed_error(&error))
        },
        move |progress| write_progress(progress).map_err(|error| to_boxed_error(&error)),
    );

    match scan_result {
        Ok(results) => write_results(&results)?,
        Err(error) => {
            if let ScanError::Incomplete(incomplete) = &error {
                write_incomplete_summary(incomplete.probes_sent(), incomplete.total_probes())?;
                write_results(incomplete.partial_results())?;
            }
            return Err(error.into());
        }
    }

    write_done_summary(probes, started.elapsed().as_secs_f64())
}

/// Adapts an `anyhow::Error` from a `write_*` helper into the boxed error type the scanning
/// library's callbacks expect.
fn to_boxed_error(error: &anyhow::Error) -> CallbackError {
    format!("{error:#}").into()
}

/// Prints the program banner to stderr and flushes the output stream.
fn write_banner() -> anyhow::Result<()> {
    let stderr = io::stderr();
    let mut output = stderr.lock();

    write_banner_to(&mut output)?;
    output.flush().context("failed to flush output stream")
}

/// Writes the program banner to the given output stream.
fn write_banner_to(output: &mut impl Write) -> anyhow::Result<()> {
    write!(
        output,
        "{PROGRAM} {VERSION} - {DESCRIPTION}\nCopyright (c) 2026 {AUTHORS}\n\n"
    )
    .context("failed to write program banner")
}

/// Writes the scan summary to stderr and flushes the output stream before the scan starts.
fn write_scan_summary(probes: usize, interface: &str, source: Ipv4Addr) -> anyhow::Result<()> {
    let stderr = io::stderr();
    let mut output = stderr.lock();

    write_scan_summary_to(&mut output, probes, interface, source)?;
    output.flush().context("failed to flush output stream")
}

/// Writes the scan summary to the given output stream.
fn write_scan_summary_to(
    output: &mut impl Write,
    probes: usize,
    interface: &str,
    source: Ipv4Addr,
) -> anyhow::Result<()> {
    writeln!(
        output,
        "Scanning: {probes} host/port pairs via {interface} ({source})..."
    )
    .context("failed to write scan summary")
}

/// Writes the done summary to stderr.
fn write_done_summary(probes: usize, elapsed: f64) -> anyhow::Result<()> {
    let stderr = io::stderr();
    let mut output = stderr.lock();

    write_done_summary_to(&mut output, probes, elapsed)
}

/// Writes the done summary to the given output stream.
fn write_done_summary_to(
    output: &mut impl Write,
    probes: usize,
    elapsed: f64,
) -> anyhow::Result<()> {
    writeln!(
        output,
        "\nDone: {probes} host/port pairs scanned in {elapsed:.1} seconds"
    )
    .context("failed to write scan completion")
}

/// Writes the incomplete scan summary to stderr and flushes the output stream before the partial
/// results that follow.
fn write_incomplete_summary(probes_sent: usize, total_probes: usize) -> anyhow::Result<()> {
    let stderr = io::stderr();
    let mut output = stderr.lock();

    write_incomplete_summary_to(&mut output, probes_sent, total_probes)?;
    output.flush().context("failed to flush output stream")
}

/// Writes the incomplete scan summary to the given output stream.
fn write_incomplete_summary_to(
    output: &mut impl Write,
    probes_sent: usize,
    total_probes: usize,
) -> anyhow::Result<()> {
    writeln!(
        output,
        "\nIncomplete: sent {probes_sent} of {total_probes} host/port pairs"
    )
    .context("failed to write incomplete scan summary")
}

/// Writes the results to stdout.
fn write_results(results: &[ScanResult]) -> anyhow::Result<()> {
    let stdout = io::stdout();
    let mut output = stdout.lock();

    write_results_to(&mut output, results)
}

/// Writes the results to the given output stream.
fn write_results_to(output: &mut impl Write, results: &[ScanResult]) -> anyhow::Result<()> {
    if !results.is_empty() {
        writeln!(output, "\nScan results:").context("failed to write results heading")?;
    }

    for &result in results {
        write_result_to(output, result, false)?;
    }

    Ok(())
}

/// Writes a verbose scan result to stdout and flushes the output stream for live feedback.
fn write_verbose_result(result: ScanResult) -> anyhow::Result<()> {
    let stdout = io::stdout();
    let mut output = stdout.lock();

    write_result_to(&mut output, result, true)?;
    output.flush().context("failed to flush output stream")
}

/// Writes the scan result to the given output stream.
fn write_result_to(
    output: &mut impl Write,
    result: ScanResult,
    verbose: bool,
) -> anyhow::Result<()> {
    let state = match result.state {
        PortState::Open => "open",
        PortState::Closed => "closed",
        _ => "unknown",
    };
    let prefix = if verbose { "[verbose] " } else { "" };

    writeln!(output, "{prefix}{state} {}:{}", result.host, result.port)
        .context("failed to write scan result")
}

/// Writes the scan progress to stderr and flushes the output stream for live feedback.
fn write_progress(progress: ScanProgress) -> anyhow::Result<()> {
    let line = format_progress(progress, Local::now());
    let stderr = io::stderr();
    let mut output = stderr.lock();

    writeln!(output, "{line}").context("failed to write scan progress")?;
    output.flush().context("failed to flush output stream")
}

/// Formats the scan progress as a string.
fn format_progress<Tz>(progress: ScanProgress, now: DateTime<Tz>) -> String
where
    Tz: TimeZone,
    Tz::Offset: fmt::Display,
{
    let eta = progress
        .estimated_remaining()
        .and_then(|remaining| chrono::Duration::from_std(remaining).ok())
        .and_then(|remaining| now.checked_add_signed(remaining))
        .map_or_else(
            || "unknown".to_owned(),
            |eta| eta.format("%a %Y-%m-%d %H:%M:%S %Z").to_string(),
        );

    format!("[stats] {}% done | ETA {eta}", progress.percent())
}

#[cfg(test)]
#[expect(clippy::panic_in_result_fn, reason = "panics are allowed in test code")]
#[expect(clippy::unwrap_used, reason = "tests can use `unwrap`")]
mod tests {
    use clap::error::ErrorKind;

    use super::*;

    #[test]
    fn parses_default_options() -> anyhow::Result<()> {
        let arguments =
            Arguments::try_parse_from(["zucchini", "--host", "127.0.0.1", "--interface", "lo"])?;

        assert_eq!(arguments.host, Targets(vec!["127.0.0.1".parse()?]));
        assert_eq!(arguments.interface, "lo");
        assert_eq!(arguments.bandwidth, 15);
        assert!(arguments.ports.is_none());
        assert!(!arguments.closed);
        assert_eq!(arguments.timeout, 30);
        assert!(!arguments.verbose);
        Ok(())
    }

    #[test]
    fn parses_all_scanner_options() -> anyhow::Result<()> {
        let arguments = Arguments::try_parse_from([
            "zucchini",
            "--host",
            "192.168.2.0/24",
            "--interface",
            "eth0",
            "--bandwidth",
            "100",
            "--ports",
            "22,80",
            "--closed",
            "--timeout",
            "60",
            "--verbose",
        ])?;

        assert_eq!(arguments.host, Targets(parse_targets("192.168.2.0/24")?));
        assert_eq!(arguments.interface, "eth0");
        assert_eq!(arguments.bandwidth, 100);
        assert_eq!(arguments.ports, Some(Ports(vec![22, 80])));
        assert!(arguments.closed);
        assert_eq!(arguments.timeout, 60);
        assert!(arguments.verbose);
        Ok(())
    }

    #[test]
    fn rejects_missing_unknown_and_invalid_options() {
        Arguments::try_parse_from(["zucchini", "-i", "lo"]).unwrap_err();
        Arguments::try_parse_from(["zucchini", "-h", "127.0.0.1"]).unwrap_err();
        Arguments::try_parse_from(["zucchini", "-h", "127.0.0.1", "-i", "lo", "--unknown"])
            .unwrap_err();
        Arguments::try_parse_from(["zucchini", "-h", "127.0.0.1", "-i", "lo", "--timeout", "0"])
            .unwrap_err();
        Arguments::try_parse_from([
            "zucchini",
            "-h",
            "127.0.0.1",
            "-i",
            "lo",
            "--bandwidth",
            "0",
        ])
        .unwrap_err();
        Arguments::try_parse_from(["zucchini", "-h", "not-an-address", "-i", "lo"]).unwrap_err();
        Arguments::try_parse_from([
            "zucchini",
            "-h",
            "127.0.0.1",
            "-i",
            "lo",
            "--ports",
            "80-79",
        ])
        .unwrap_err();
    }

    #[test]
    fn formats_banner_scan_and_completion_summaries() -> anyhow::Result<()> {
        let mut output = Vec::new();
        write_banner_to(&mut output)?;
        write_scan_summary_to(&mut output, 3, "eth0", "192.168.2.1".parse()?)?;
        write_done_summary_to(&mut output, 3, 30.14)?;

        assert_eq!(
            String::from_utf8(output)?,
            format!(
                "{PROGRAM} {VERSION} - {DESCRIPTION}\nCopyright (c) 2026 {AUTHORS}\n\n\
                Scanning: 3 host/port pairs via eth0 (192.168.2.1)...\n\n\
                Done: 3 host/port pairs scanned in 30.1 seconds\n"
            )
        );
        Ok(())
    }

    #[test]
    fn formats_incomplete_summary() -> anyhow::Result<()> {
        let mut output = Vec::new();
        write_incomplete_summary_to(&mut output, 1, 3)?;

        assert_eq!(
            String::from_utf8(output)?,
            "\nIncomplete: sent 1 of 3 host/port pairs\n"
        );
        Ok(())
    }

    #[test]
    fn formats_empty_buffered_and_verbose_results() -> anyhow::Result<()> {
        let open = ScanResult::new("172.16.100.2".parse()?, 443, PortState::Open);
        let closed = ScanResult::new("172.16.100.3".parse()?, 80, PortState::Closed);
        let mut output = Vec::new();
        write_results_to(&mut output, &[])?;
        assert!(output.is_empty());

        write_results_to(&mut output, &[open, closed])?;
        assert_eq!(
            String::from_utf8(output)?,
            concat!(
                "\n",
                "Scan results:\n",
                "open 172.16.100.2:443\n",
                "closed 172.16.100.3:80\n",
            )
        );

        let mut verbose = Vec::new();
        write_result_to(&mut verbose, open, true)?;
        assert_eq!(
            String::from_utf8(verbose)?,
            "[verbose] open 172.16.100.2:443\n"
        );
        Ok(())
    }

    #[test]
    fn formats_progress_with_fixed_time() {
        let now = chrono::Utc
            .with_ymd_and_hms(2026, 1, 1, 12, 0, 0)
            .single()
            .unwrap();
        let progress = ScanProgress::new(25, 100, Duration::from_secs(60));
        let not_started = ScanProgress::new(0, 100, Duration::from_secs(60));
        let complete = ScanProgress::new(100, 100, Duration::from_secs(60));

        assert_eq!(
            format_progress(progress, now),
            "[stats] 25% done | ETA Thu 2026-01-01 12:03:00 UTC"
        );
        assert_eq!(
            format_progress(not_started, now),
            "[stats] 0% done | ETA unknown"
        );
        assert_eq!(
            format_progress(complete, now),
            "[stats] 100% done | ETA Thu 2026-01-01 12:00:00 UTC"
        );
    }

    #[test]
    fn formats_progress_with_non_utc_offset() {
        let offset = chrono::FixedOffset::east_opt(3600).unwrap();
        let now = offset
            .with_ymd_and_hms(2026, 1, 1, 12, 0, 0)
            .single()
            .unwrap();
        let progress = ScanProgress::new(25, 100, Duration::from_secs(60));

        assert_eq!(
            format_progress(progress, now),
            "[stats] 25% done | ETA Thu 2026-01-01 12:03:00 +01:00"
        );
    }

    #[test]
    fn help_flag_displays_help() {
        let error = Arguments::try_parse_from(["zucchini", "--help"]).unwrap_err();

        assert_eq!(error.kind(), ErrorKind::DisplayHelp);
    }

    #[test]
    fn version_flag_is_unknown() {
        let error =
            Arguments::try_parse_from(["zucchini", "-h", "127.0.0.1", "-i", "lo", "--version"])
                .unwrap_err();

        assert_eq!(error.kind(), ErrorKind::UnknownArgument);
    }
}