Skip to content

Commit 5dc8acf

Browse files
committed
feat: detect nmap for default scripts
if using the equivalent of `--scripts default`, use [`which`](https://github.com/harryfei/which-rs) to verify that an `nmap` binary can be found. closes bee-san#691
1 parent 4e3c2a5 commit 5dc8acf

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

Cargo.lock

+28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ anyhow = "1.0.40"
4040
subprocess = "0.2.6"
4141
text_placeholder = { version = "0.5", features = ["struct_context"] }
4242
once_cell = "1.20.2"
43+
which = "7.0.0"
4344

4445
[dev-dependencies]
4546
parameterized = "2.0.0"

src/scripts/mod.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
#![allow(clippy::module_name_repetitions)]
7777

7878
use crate::input::ScriptsRequired;
79-
use anyhow::{anyhow, Result};
79+
use anyhow::{anyhow, Context, Result};
8080
use log::debug;
8181
use serde_derive::{Deserialize, Serialize};
8282
use std::convert::TryInto;
@@ -87,6 +87,7 @@ use std::path::PathBuf;
8787
use std::string::ToString;
8888
use subprocess::{Exec, ExitStatus};
8989
use text_placeholder::Template;
90+
use which::which;
9091

9192
static DEFAULT: &str = r#"tags = ["core_approved", "RustScan", "default"]
9293
developer = [ "RustScan", "https://github.com/RustScan" ]
@@ -101,8 +102,12 @@ pub fn init_scripts(scripts: &ScriptsRequired) -> Result<Vec<ScriptFile>> {
101102
match scripts {
102103
ScriptsRequired::None => {}
103104
ScriptsRequired::Default => {
105+
which("nmap")
106+
.with_context(|| "nmap: command not found. See <https://nmap.org/download>")?;
107+
104108
let default_script =
105109
toml::from_str::<ScriptFile>(DEFAULT).expect("Failed to parse Script file.");
110+
106111
scripts_to_run.push(default_script);
107112
}
108113
ScriptsRequired::Custom => {
@@ -184,14 +189,14 @@ struct ExecPartsScript {
184189
script: String,
185190
ip: String,
186191
port: String,
187-
ipversion: String
192+
ipversion: String,
188193
}
189194

190195
#[derive(Serialize)]
191196
struct ExecParts {
192197
ip: String,
193198
port: String,
194-
ipversion: String
199+
ipversion: String,
195200
}
196201

197202
impl Script {
@@ -248,8 +253,8 @@ impl Script {
248253
port: ports_str,
249254
ipversion: match &self.ip {
250255
IpAddr::V4(_) => String::from("4"),
251-
IpAddr::V6(_) => String::from("6")
252-
}
256+
IpAddr::V6(_) => String::from("6"),
257+
},
253258
};
254259
to_run = default_template.fill_with_struct(&exec_parts_script)?;
255260
} else {
@@ -258,8 +263,8 @@ impl Script {
258263
port: ports_str,
259264
ipversion: match &self.ip {
260265
IpAddr::V4(_) => String::from("4"),
261-
IpAddr::V6(_) => String::from("6")
262-
}
266+
IpAddr::V6(_) => String::from("6"),
267+
},
263268
};
264269
to_run = default_template.fill_with_struct(&exec_parts)?;
265270
}

0 commit comments

Comments
 (0)