From 8d09204c819ef0fcaf2e5d8cc261c7da33064fa4 Mon Sep 17 00:00:00 2001 From: funtimes909 Date: Wed, 23 Apr 2025 01:33:56 +1200 Subject: [PATCH] masscan stuff --- src/masscan.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/masscan.rs diff --git a/src/masscan.rs b/src/masscan.rs new file mode 100644 index 0000000..c7dc293 --- /dev/null +++ b/src/masscan.rs @@ -0,0 +1,50 @@ +use serde::Deserialize; +use std::io::Read; +use std::process::Command; + +// TODO: Better name for this struct +#[derive(Deserialize)] +pub struct MasscanServer { + pub ip: String, + pub ports: Vec, +} + +#[derive(Deserialize)] +pub struct Port { + port: u16, +} + +pub fn start_masscan(masscan_config: String) { + let mut output = if cfg!(target_os = "windows") { + Command::new("cmd.exe") + .arg("/c") + .arg(format!("masscan -c {masscan_config}")) + .spawn() + .expect("failed to execute process") + } else { + Command::new("/bin/sh") + .arg("-c") + // Masscan needs to be run as root, nothing I can do about it + // TODO! Change this to be a toggle in the config for security + .arg(format!("sudo masscan -c {masscan_config}")) + .spawn() + .expect("failed to execute process") + }; + + output.wait().expect("failed to wait on child"); +} + +pub fn parse_output(masscan_output: String) -> Vec<(String, u16)> { + let file = std::fs::read_to_string(masscan_output).expect("failed to read masscan"); + let output = serde_json::from_str::>(&file).expect("failed to read masscan"); + let mut servers = vec![]; + + // TODO: I know this can be done better + for server in output { + for port in server.ports { + servers.push((server.ip.clone(), port.port)); + } + } + + servers +}