From 726b5d2e87d1a093ee13a76ed3c1cbf8c73c97e4 Mon Sep 17 00:00:00 2001 From: _Frky <3105926+Frky@users.noreply.github.com> Date: Thu, 15 Sep 2022 17:46:56 +0200 Subject: [PATCH] Add option for list of IP to bind in command-line argument --- src/masscanned.rs | 21 +++++++++++++++++---- src/utils/parsers.rs | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/masscanned.rs b/src/masscanned.rs index 443b38c..5bdfc90 100644 --- a/src/masscanned.rs +++ b/src/masscanned.rs @@ -116,18 +116,23 @@ fn main() { ) .arg( Arg::new("mac") - .short('a') + .short('m') .long("mac-addr") .help("MAC address to use in the response packets") .takes_value(true), ) .arg( - Arg::new("ip") - .short('f') + Arg::new("ipfile") .long("ip-addr-file") .help("File with the list of IP addresses to impersonate") .takes_value(true), ) + .arg( + Arg::new("iplist") + .long("ip-addr") + .help("Inline list of IP addresses to impersonate, comma-separated") + .takes_value(true), + ) .arg( Arg::new("verbosity") .short('v') @@ -173,7 +178,7 @@ fn main() { }; /* Parse ip address file specified */ /* FIXME: .and_then(|path| File::open(path).map(|file| )).unwrap_or_default() ? */ - let ip_list = if let Some(ref path) = args.value_of("ip") { + let mut ip_list = if let Some(ref path) = args.value_of("ipfile") { if let Ok(file) = File::open(path) { info!("parsing ip address file: {}", &path); file.extract_ip_addresses_only(None) @@ -183,9 +188,17 @@ fn main() { } else { HashSet::new() }; + if let Some(ip_inline_list) = args.value_of("iplist") { + ip_list.extend(ip_inline_list.extract_ip_addresses_only(None)); + } let ip_addresses = if !ip_list.is_empty() { + for ip in &ip_list { + info!("binding........{}", ip); + } Some(&ip_list) } else { + info!("binding........0.0.0.0"); + info!("binding........::"); None }; let mut masscanned = Masscanned { diff --git a/src/utils/parsers.rs b/src/utils/parsers.rs index a17210d..4d4c9ad 100644 --- a/src/utils/parsers.rs +++ b/src/utils/parsers.rs @@ -134,6 +134,45 @@ impl IpAddrParser for File { } } +/* Parse IP addresses from a comma-separated list in a string */ +impl IpAddrParser for &str { + fn extract_ip_addresses_with_count( + self, + _blacklist: Option>, + ) -> HashMap { + panic!("not implemented"); + } + + fn extract_ip_addresses_only(self, blacklist: Option>) -> HashSet { + let mut ip_addresses = HashSet::new(); + for line in self.split(",") { + /* Should never occur */ + if line.is_empty() { + warn!("cannot parse line: {}", line); + continue; + } + let ip: IpAddr; + if let Ok(val) = line.parse::() { + ip = IpAddr::V4(val); + } else if let Ok(val) = line.parse::() { + ip = IpAddr::V6(val); + } else { + warn!( + "cannot parse IP address from line: {}", line + ); + continue; + } + if let Some(ref b) = blacklist { + if b.contains(&ip) { + info!("[blacklist] ignoring {}", &ip); + continue; + } + } + ip_addresses.insert(ip); + } + ip_addresses + } +} /* Get the IP address of source and dest. from an IP packet. * works with both IPv4 and IPv6 packets/addresses */ fn extract_ip(pkt: Packet) -> Option<(IpAddr, IpAddr)> {