ntrace 0.1.5

A fast and secure network port scanner and protocol analyzer
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
use crate::protocol::Target;
use crate::traceroute::TraceConfig;
use clap::Parser;
use std::net::IpAddr;
use std::str::FromStr;
use std::time::Duration;

#[derive(Parser, Debug)]
#[command(
    name = "ntrace",
    version = "0.1.3",
    about = "Network port scanner and protocol analyzer",
    long_about = "ntrace is a tool for scanning TCP/UDP ports, analyzing network protocols, and performing traceroute.",
    next_line_help = true,
    after_help = "EXAMPLES:
    ntrace -H 192.168.1.1
    ntrace -H wikipedia.org -p 1-1000
    ntrace -H 10.0.0.1 -p 80,443,8080
    ntrace -H 192.168.1.1 -p common --fast
    ntrace -H wikipedia.org -o results.json
    ntrace trace 1.1.1.1 --tcp --port 443 --max-hops 20
    ntrace trace google.com --queries 5 --no-rdns"
)]

pub struct Cli {
    #[command(subcommand)]
    pub command: Option<Command>,

    #[arg(
        short = 'H',
        long,
        help = "Target IP address, hostname, or web domain (e.g., 192.168.1.1, wikipedia.org)",
        group = "ip_version",
        help_heading = "TARGET SPECIFICATION"
    )]
    pub host: Option<String>,

    #[arg(
        long,
        help = "Force IPv6 scanning",
        group = "ip_version",
        help_heading = "TARGET SPECIFICATION"
    )]
    pub ipv6: bool,

    #[arg(
        long,
        help = "Force IPv4 scanning",
        group = "ip_version",
        help_heading = "TARGET SPECIFICATION"
    )]
    pub ipv4: bool,

    #[arg(
        short,
        long,
        default_value = "1-1000",
        help = "Port range to scan (e.g., 1-1000, 80,443, or predefined groups)",
        long_help = "Port range to scan. Can be individual ports (80,443), ranges (1-1000), or predefined groups:\n  \
                    common: Most common ports\n  \
                    well-known: Standard ports (1-1023)\n  \
                    registered: Registered ports (1024-49151)\n  \
                    dynamic: Dynamic ports (49152-65535)\n  \
                    all: All ports (1-65535)",
        help_heading = "PORT SPECIFICATION"
    )]
    pub ports: String,

    #[arg(
        short = 'P',
        long,
        default_value = "tcp",
        help = "Protocol to scan (tcp, udp)",
        help_heading = "SCAN TECHNIQUES"
    )]
    pub protocol: String,

    #[arg(
        short,
        long,
        help = "Perform service detection",
        default_value_t = true,
        help_heading = "SCAN TECHNIQUES"
    )]
    pub service_detection: bool,

    #[arg(
        long,
        help = "Aggressive scan (more intrusive probes)",
        help_heading = "SCAN TECHNIQUES"
    )]
    pub aggressive: bool,

    #[arg(
        long,
        default_value_t = 2.0,
        help = "Timeout for each port scan in seconds",
        help_heading = "SCAN PERFORMANCE"
    )]
    pub timeout: f32,

    #[arg(
        long,
        default_value_t = 100,
        help = "Batch size for parallel scanning",
        help_heading = "SCAN PERFORMANCE"
    )]
    pub batch_size: usize,

    #[arg(
        long,
        help = "Rate limit in packets per second",
        default_value_t = 1000,
        help_heading = "SCAN PERFORMANCE"
    )]
    pub rate_limit: usize,

    #[arg(
        long,
        help = "Fast scan with shorter timeouts (less accurate)",
        help_heading = "SCAN PERFORMANCE"
    )]
    pub fast: bool,

    #[arg(
        long,
        help = "Skip host discovery (ping)",
        help_heading = "HOST DISCOVERY"
    )]
    pub skip_discovery: bool,

    #[arg(
        short,
        long,
        help = "Output file path (.json or .csv)",
        help_heading = "OUTPUT OPTIONS"
    )]
    pub output: Option<String>,

    #[arg(
        short = 'v',
        long,
        help = "Verbose output (show closed ports)",
        help_heading = "OUTPUT OPTIONS"
    )]
    pub verbose: bool,

    #[arg(
        long,
        help = "Skip problematic ports that often cause hangs",
        help_heading = "MISC OPTIONS"
    )]
    pub skip_problematic: bool,

    #[arg(
        long,
        help = "Use SYN scanning for faster results (requires root/admin privileges)",
        help_heading = "SCAN TECHNIQUES"
    )]
    pub syn_scan: bool,
}

#[derive(Parser, Debug)]
pub enum Command {
    /// Trace the route to a host
    #[command(name = "trace")]
    Trace {
        /// Target IP address or hostname to trace
        target: String,

        /// Use TCP packets for traceroute (default is ICMP)
        #[arg(long = "tcp", short = 'T')]
        use_tcp: bool,

        /// Use UDP packets for traceroute
        #[arg(long = "udp", short = 'U')]
        use_udp: bool,

        /// Port to use for TCP/UDP traceroute
        #[arg(long, short = 'p', default_value = "80")]
        port: u16,

        /// Maximum number of hops to try
        #[arg(long = "max-hops", short = 'm', default_value = "30")]
        max_hops: u8,

        /// Number of queries per hop
        #[arg(long, short = 'q', default_value = "3")]
        queries: u8,

        /// Disable reverse DNS lookups
        #[arg(long = "no-rdns", short = 'n')]
        no_rdns: bool,

        /// Always perform reverse DNS lookups
        #[arg(long = "always-rdns", short = 'a')]
        always_rdns: bool,

        /// Number of parallel requests
        #[arg(long = "parallel-requests", default_value = "18")]
        parallel_requests: u8,

        /// Time between sending packets in milliseconds
        #[arg(long = "send-time", short = 'z', default_value = "50")]
        send_time_ms: u64,

        /// Time between sending packets for different TTLs in milliseconds
        #[arg(long = "ttl-time", short = 'i', default_value = "50")]
        ttl_time_ms: u64,

        /// Timeout for each probe in milliseconds
        #[arg(long = "timeout", default_value = "1000")]
        timeout_ms: u64,

        /// Payload size for probe packets
        #[arg(long = "psize", default_value = "52")]
        payload_size: usize,

        /// Print route path by ASN and location
        #[arg(long = "route-path", short = 'P')]
        route_path: bool,

        /// Output trace results as table
        #[arg(long = "table", short = 't')]
        table: bool,

        /// Output trace results as JSON
        #[arg(long = "json", short = 'j')]
        json: bool,

        /// Output file path (.json or .txt)
        #[arg(long = "output", short = 'o')]
        output: Option<String>,
    },
}

impl Cli {
    pub fn to_config(&self) -> Result<crate::scanner::ScanConfig, anyhow::Error> {
        // If using a subcommand, return early
        if self.command.is_some() {
            return Err(anyhow::anyhow!("Using a subcommand"));
        }

        // Host must be provided when not using a subcommand
        let host = self
            .host
            .as_ref()
            .ok_or_else(|| anyhow::anyhow!("Host is required"))?;

        // Parse host (support both IP addresses, hostnames, and web domains)
        let target = match IpAddr::from_str(host) {
            Ok(ip) => {
                // Check if we need to enforce IP version
                if self.ipv4 && ip.is_ipv6() {
                    return Err(anyhow::anyhow!(
                        "IPv6 address provided but --ipv4 flag was set"
                    ));
                } else if self.ipv6 && ip.is_ipv4() {
                    return Err(anyhow::anyhow!(
                        "IPv4 address provided but --ipv6 flag was set"
                    ));
                }
                Target::Ip(ip)
            }
            Err(_) => {
                // Check if this looks like a domain name
                if host.contains('.') && !host.starts_with('.') && !host.ends_with('.') {
                    // For CLI parsing, we'll just store the domain and resolve it later
                    // This avoids the async runtime issues with DNS resolution
                    Target::Domain(host.clone())
                } else {
                    // Not a valid IP or domain
                    return Err(anyhow::anyhow!("Invalid host: {}", host));
                }
            }
        };

        // Parse ports
        let ports = self.parse_ports()?;

        // Parse protocol
        let protocol = match self.protocol.to_lowercase().as_str() {
            "tcp" => crate::Protocol::Tcp,
            "udp" => crate::Protocol::Udp,
            _ => return Err(anyhow::anyhow!("Unsupported protocol: {}", self.protocol)),
        };

        // Create config
        Ok(crate::scanner::ScanConfig {
            target,
            ports,
            timeout: if self.fast {
                Duration::from_millis(100)
            } else {
                Duration::from_secs_f32(self.timeout)
            },
            protocol,
            batch_size: self.batch_size,
            // Fewer retries in fast mode
            max_retries: if self.fast { 1 } else { 3 },
            retry_delay: if self.fast {
                Duration::from_millis(100)
            } else {
                Duration::from_millis(500)
            },
            // Fail fast in fast mode or when skipping problematic ports
            fail_fast: self.fast || self.skip_problematic,
        })
    }

    pub fn to_trace_config(&self) -> Result<TraceConfig, anyhow::Error> {
        if let Some(Command::Trace {
            target,
            use_tcp,
            use_udp,
            port,
            max_hops,
            queries,
            no_rdns,
            always_rdns,
            parallel_requests,
            send_time_ms,
            ttl_time_ms,
            timeout_ms,
            payload_size,
            ..
        }) = &self.command
        {
            // Parse target (support both IP addresses and hostnames)
            let target_obj = match IpAddr::from_str(target) {
                Ok(ip) => {
                    // Check if we need to enforce IP version
                    if self.ipv4 && ip.is_ipv6() {
                        return Err(anyhow::anyhow!(
                            "IPv6 address provided but --ipv4 flag was set"
                        ));
                    } else if self.ipv6 && ip.is_ipv4() {
                        return Err(anyhow::anyhow!(
                            "IPv4 address provided but --ipv6 flag was set"
                        ));
                    }
                    Target::Ip(ip)
                }
                Err(_) => {
                    // Check if this looks like a domain name
                    if target.contains('.') && !target.starts_with('.') && !target.ends_with('.') {
                        Target::Domain(target.clone())
                    } else {
                        // Not a valid IP or domain
                        return Err(anyhow::anyhow!("Invalid target: {}", target));
                    }
                }
            };

            // Determine protocol
            let protocol = if *use_tcp {
                crate::Protocol::Tcp
            } else if *use_udp {
                crate::Protocol::Udp
            } else {
                crate::Protocol::Icmp
            };

            // Determine hostname resolution
            let resolve_hostnames = !no_rdns || *always_rdns;

            Ok(TraceConfig {
                target: target_obj,
                protocol,
                port: *port,
                max_hops: *max_hops,
                queries: *queries,
                timeout_ms: *timeout_ms,
                resolve_hostnames,
                parallel_requests: *parallel_requests,
                send_time_ms: *send_time_ms,
                ttl_time_ms: *ttl_time_ms,
                payload_size: *payload_size,
            })
        } else {
            Err(anyhow::anyhow!("Not a trace command"))
        }
    }

    pub fn parse_ports(&self) -> Result<Vec<u16>, anyhow::Error> {
        let mut ports = Vec::new();

        // Handle predefined port groups
        match self.ports.to_lowercase().as_str() {
            "common" => {
                // Common ports for quick scanning
                ports.extend_from_slice(&[
                    21, 22, 23, 25, 53, 80, 110, 111, 135, 139, 143, 443, 445, 993, 995, 1723,
                    3306, 3389, 5900, 8080,
                ]);
            }
            "well-known" => {
                // Well known ports (1-1023)
                ports.extend(1..=1023);
            }
            "registered" => {
                // Registered ports (1024-49151)
                ports.extend(1024..=49151);
            }
            "dynamic" => {
                // Dynamic ports (49152-65535)
                ports.extend(49152..=65535);
            }
            "all" => {
                // All ports (1-65535)
                ports.extend(1..=65535);
            }
            _ => {
                // Parse custom port specification (e.g., "80,443,8080" or "1-1000")
                for part in self.ports.split(',') {
                    if part.contains('-') {
                        // Port range
                        let range: Vec<&str> = part.split('-').collect();
                        if range.len() == 2 {
                            let start = range[0].parse::<u16>()?;
                            let end = range[1].parse::<u16>()?;
                            ports.extend(start..=end);
                        } else {
                            return Err(anyhow::anyhow!("Invalid port range: {}", part));
                        }
                    } else {
                        // Single port
                        ports.push(part.parse::<u16>()?);
                    }
                }
            }
        }

        // Remove duplicates
        ports.sort();
        ports.dedup();

        Ok(ports)
    }
}