From ed4af59ae956c13dd1a8ea64a600c976d94767ec Mon Sep 17 00:00:00 2001 From: bergabman Date: Sun, 29 Nov 2020 15:59:18 +0100 Subject: [PATCH] Scripting engine extra user argument interpretation error fixed. Scanner engine errors and error handling clarified. --- src/main.rs | 19 ++++++++++--------- src/scanner/mod.rs | 28 ++++++++++++++++------------ src/scripts/mod.rs | 14 +++----------- 3 files changed, 29 insertions(+), 32 deletions(-) diff --git a/src/main.rs b/src/main.rs index ff1da9eb2..aa028befa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -154,19 +154,20 @@ fn main() { // Run all the scripts we found and parsed based on the script config file tags field. for mut script_f in scripts_to_run.clone() { - output!( - format!("Script to be run {:?}\n", script_f.call_format,), - opts.greppable, - opts.accessible - ); - // This part allows us to add commandline arguments to the Script call_format, appending them to the end of the command. if !opts.command.is_empty() { - let user_extra_args: Vec = shell_words::split(&opts.command.join(" ")) - .expect("Failed to parse extra user commandline arguments"); + let user_extra_args = &opts.command.join(" "); + debug!("Extra args vec {:?}", user_extra_args); if script_f.call_format.is_some() { let mut call_f = script_f.call_format.unwrap(); - call_f.push_str(&format!(" {}", &user_extra_args.join(" "))); + call_f.push(' '); + call_f.push_str(user_extra_args); + output!( + format!("Running script {:?} on ip {}\nDepending on the complexity of the script, results may take some time to appear.", call_f, &ip), + opts.greppable, + opts.accessible + ); + debug!("Call format {}", call_f); script_f.call_format = Some(call_f); } } diff --git a/src/scanner/mod.rs b/src/scanner/mod.rs index 7fe2f9aae..d30521c0a 100644 --- a/src/scanner/mod.rs +++ b/src/scanner/mod.rs @@ -9,7 +9,7 @@ use async_std::prelude::*; use colored::Colorize; use futures::stream::FuturesUnordered; use std::{ - io::ErrorKind, + collections::HashSet, net::{IpAddr, Shutdown, SocketAddr}, num::NonZeroU8, time::Duration, @@ -62,6 +62,7 @@ impl Scanner { let mut socket_iterator: SocketIterator = SocketIterator::new(&self.ips, &ports); let mut open_sockets: Vec = Vec::new(); let mut ftrs = FuturesUnordered::new(); + let mut errors: HashSet = HashSet::with_capacity(self.ips.len() * 1000); for _ in 0..self.batch_size { if let Some(socket) = socket_iterator.next() { @@ -82,10 +83,17 @@ impl Scanner { ftrs.push(self.scan_socket(socket)); } - if let Ok(socket) = result { - open_sockets.push(socket); + match result { + Ok(socket) => open_sockets.push(socket), + Err(e) => { + let error_string = e.to_string(); + if errors.len() < self.ips.len() * 1000 { + errors.insert(error_string); + } + } } } + debug!("Typical socket connection errors {:?}", errors); debug!("Open Sockets found: {:?}", &open_sockets); open_sockets } @@ -127,19 +135,15 @@ impl Scanner { return Ok(socket); } Err(e) => { - let error_string = e.to_string(); - - if e.kind() == ErrorKind::Other { - debug!("Socket connect error: {} {}", &error_string, &socket); + let mut error_string = e.to_string(); - if !error_string.contains("No route to host") - && !error_string.contains("Network is unreachable") - { - panic!("Too many open files. Please reduce batch size. The default is 5000. Try -b 2500."); - } + if error_string.to_lowercase().contains("too many open files") { + panic!("Too many open files. Please reduce batch size. The default is 5000. Try -b 2500."); } if nr_try == tries { + error_string.push(' '); + error_string.push_str(&socket.ip().to_string()); return Err(io::Error::new(io::ErrorKind::Other, error_string)); } } diff --git a/src/scripts/mod.rs b/src/scripts/mod.rs index a598df193..a5b868980 100644 --- a/src/scripts/mod.rs +++ b/src/scripts/mod.rs @@ -231,17 +231,9 @@ impl Script { }; to_run = default_template.fill_with_struct(&exec_parts)?; } + debug!("\nScript format to run {}", to_run); - debug!("\nTo run {}", to_run); - - let arguments = shell_words::split( - &to_run - .split(' ') - .map(ToString::to_string) - .collect::>() - .join(" "), - ) - .expect("Failed to parse script arguments"); + let arguments = shell_words::split(&to_run).expect("Failed to parse script arguments"); execute_script(arguments) } @@ -249,7 +241,7 @@ impl Script { #[cfg(not(tarpaulin_include))] fn execute_script(mut arguments: Vec) -> Result { - debug!("\nArguments vec: {:?}", &arguments); + debug!("\nScript arguments vec: {:?}", &arguments); let process = Exec::cmd(&arguments.remove(0)).args(&arguments); match process.capture() { Ok(c) => {