CIDR & Refactor

main
E. Almqvist 2 years ago
parent 5f30f64434
commit e1d1a945de
  1. 15
      src/cli.rs
  2. 5
      src/ipv4.rs
  3. 8
      src/main.rs
  4. 22
      src/scanner.rs

@ -4,7 +4,7 @@ use std::path::PathBuf;
#[derive(Parser, Debug)]
#[clap(name = "World Wide Mapping", version, about = "Scan the world wide web for a certian port.", long_about = None)]
pub struct Args {
#[clap(help = "Which port to scan for.", short = 'p', long = "port")]
#[clap(help = "Which port to scan for.")]
pub port: u16,
#[clap(
@ -23,17 +23,6 @@ pub struct Args {
)]
pub ignorelist: PathBuf,
#[clap(help = "From IPv4 -", short = 'f', long = "from", default_value_t = 0)]
pub from: u32,
#[clap(
help = "To IPv4 -",
short = 't',
long = "to",
default_value_t = u32::max_value()
)]
pub to: u32,
#[clap(
help = "Enable verbose (debug) output",
short = 'v',
@ -45,8 +34,6 @@ pub struct Args {
#[clap(
help = "IPv4 subnet range (CIDR). Leave empty for the whole internet.",
short = 'r',
long = "range",
default_value = "0.0.0.0/0",
required = false
)]

@ -64,8 +64,9 @@ pub struct IPv4Range {
}
impl IPv4Range {
pub fn new(from: u32, to: u32, id_ignore: Vec<u32>) -> Self {
pub fn new(from: u32, to: u32, id_ignore: Option<Vec<u32>>) -> Self {
let to = to.clamp(0, u32::max_value());
let id_ignore = id_ignore.unwrap_or(Vec::new());
if from >= to {
panic!("Range size must be >= 1! from={} >= to={}", from, to);
@ -78,7 +79,7 @@ impl IPv4Range {
}
}
pub fn from_cidr(cidr_string: String, id_ignore: Vec<u32>) -> Self {
pub fn from_cidr(cidr_string: String, id_ignore: Option<Vec<u32>>) -> Self {
let cidr = Ipv4Cidr::from_str(cidr_string).unwrap();
let (from, to) = (cidr.first(), cidr.last());

@ -5,12 +5,18 @@ mod util;
use clap::Parser;
use cli::Args;
use ipv4::IPv4Range;
use scanner::start_scan;
fn main() {
// Get CLI arguments
let args = Args::parse();
let results = start_scan(args.from, args.to, args.port, args.threads, None);
// Get the IP range
let range = IPv4Range::from_cidr(args.cidr, None);
// Start the scan
let results = start_scan(range, args.port, args.threads);
for result in results {
println!("{:?}", result);

@ -45,17 +45,17 @@ fn create_scan_worker(
(thread_id * ips_per_thread),
((thread_id + 1) * ips_per_thread - 1),
);
let range = IPv4Range::new(f as u32, t as u32, ignorelist);
let range = IPv4Range::new(f as u32, t as u32, Some(ignorelist));
create_scan_thread(range, target_port)
}
fn get_scan_workers(
from: u32,
to: u32,
range: IPv4Range,
target_port: u16,
num_threads: u64,
ignorelist: Option<Vec<u32>>,
) -> Vec<JoinHandle<Vec<(u32, bool)>>> {
let (from, to) = (range.id_start, range.id_end);
let ip_amount: u64 = (to as u64 - from as u64) + 1;
let ips_per_thread: u64 = ((ip_amount as f32) / num_threads as f32).floor() as u64;
@ -64,7 +64,7 @@ fn get_scan_workers(
// TODO: make last thread do the "ips_left" work
for thread_id in 0..num_threads {
let id_ignorelist = ignorelist.clone().unwrap_or_default();
let id_ignorelist = range.id_ignore.clone();
// Create a worker
let worker = create_scan_worker(thread_id, ips_per_thread, target_port, id_ignorelist);
@ -79,7 +79,7 @@ fn get_scan_workers(
// Clean up the rest
warn!("Number of IPv4 addresses is not divisible by the amount of threads! Creating extra thread...");
let id_ignorelist = ignorelist.clone().unwrap_or_default();
let id_ignorelist = range.id_ignore.clone();
let worker = create_scan_worker(
threads.len() as u64 + 1,
ips_left,
@ -106,17 +106,11 @@ impl ScanResult {
}
}
pub fn start_scan(
from: u32,
to: u32,
target_port: u16,
num_threads: u64,
ignorelist: Option<Vec<u32>>,
) -> Vec<ScanResult> {
pub fn start_scan(range: IPv4Range, target_port: u16, num_threads: u64) -> Vec<ScanResult> {
println!("Starting wwmap scan...");
// Get the workers
let scan_workers = get_scan_workers(from, to, target_port, num_threads, ignorelist);
let scan_workers = get_scan_workers(range, target_port, num_threads);
println!("Loaded {} scan worker(s).", scan_workers.len());
let mut results: Vec<ScanResult> = Vec::new();

Loading…
Cancel
Save