Skip to content

Commit

Permalink
Added config file functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
bee-san committed Aug 24, 2020
1 parent 4f5dd20 commit c509172
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 7 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ shell-words = "1.0.0"
log = "0.4.0"
env_logger = "0.7.1"
dirs = "3.0.1"
toml = "0.5.6"
gcd = "2.0.1"
rand = "0.7.3"
colorful = "0.2.1"
ansi_term = "0.12.1"
ureq = "1.3.0"
serde = "1.0.115"
serde_derive = "1.0.115"

[package.metadata.deb]
depends = "$auto, nmap"
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,22 @@ To increase accuracy, the easiest way is to increase the timeout. The default is

Decreasing accuracy gives some speed bonus, but my testing found that batch size dramatically changed the speed whereas timeout did, but not so much.

## 🤖 Config File

RustScan uses a Config file to make things easier for you. And to also improve itself.
The config file is used as the basis of the _adaptive learning_ mechanisms. This means that RustScan _learns_ from your scans and improves itself over time. (Don't worry, no bloated AI framework here 😉)

The config file should be placed in APPDIRS. To find this folder, run RustScan and look out for RustScan saying "couldn't find config at <location>". That location is where your config file should be.

Once you have this location, copy and paste this:

```toml

# The top ports
[top_ports]
80, 60, 443
```

# 🎪 Community

Howdy Space Cow-Person 🤠🌌
Expand Down
28 changes: 28 additions & 0 deletions docker/ci_test/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM archlinux:latest

RUN yes | pacman -Syu
RUN yes | pacman -S fakeroot binutils sudo make gcc gettext awk git

WORKDIR /opt
RUN git clone https://aur.archlinux.org/yay-git.git

RUN useradd -m tecmint
RUN passwd -d tecmint
RUN printf 'tecmint ALL=(ALL) ALL\n' | tee -a /etc/sudoers

WORKDIR /opt/yay-git
RUN chown -R tecmint /opt/yay-git
USER tecmint
RUN makepkg -si --noconfirm

# AUR
RUN yay -S rustscan --noconfirm
RUN rustscan 127.0.0.1 -q -b 250 -t 1
RUN yay -R rustscan --noconfirm

# homebrew
RUN yes | /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
ENV PATH="/home/linuxbrew/.linuxbrew/bin:${PATH}"
RUN yes | brew tap brandonskerritt/rustscan
RUN yes | brew install rustscan
RUN rustscan 127.0.0.1 -q -b 250 -t 1
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ git pull --force

#amd64
cargo deb
yes | dpkg -i *.deb

#arm64
rustup target add arm-unknown-linux-gnueabihf
Expand Down
File renamed without changes.
90 changes: 83 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,20 @@ use colorful::Colorful;
use futures::executor::block_on;
use rlimit::Resource;
use rlimit::{getrlimit, setrlimit};
use serde_derive::Deserialize;
use std::collections::HashMap;
use std::net::ToSocketAddrs;
use std::process::Command;
use std::str::FromStr;
use std::{net::IpAddr, net::ToSocketAddrs, time::Duration};
use std::{
fs::File,
io::{Read, Write},
net::IpAddr,
path::PathBuf,
time::Duration,
};
use structopt::{clap::arg_enum, StructOpt};
use ureq;

extern crate colorful;
extern crate dirs;
Expand Down Expand Up @@ -97,6 +106,10 @@ struct Opts {
#[structopt(short, long)]
accessible: bool,

///Config file usage
#[structopt(short, long)]
config: bool,

/// The batch size for port scanning, it increases or slows the speed of
/// scanning. Depends on the open file limit of your OS. If you do 65535
/// it will do every port at the same time. Although, your OS may not
Expand Down Expand Up @@ -127,6 +140,11 @@ struct Opts {
command: Vec<String>,
}

#[derive(Deserialize)]
struct ConfigFile {
ip: IpAddr,
}

#[cfg(not(tarpaulin_include))]
/// Faster Nmap scanning with Rust
/// If you're looking for the actual scanning, check out the module Scanner
Expand All @@ -149,6 +167,12 @@ fn main() {
print_opening();
}

/*
TODO when releasing the new config file feature
turn this on.
if opts.config {
let config = get_config_file();
}*/
let ips: Vec<IpAddr> = parse_ips(&opts);

if ips.is_empty() {
Expand Down Expand Up @@ -245,21 +269,65 @@ Faster Nmap scanning with Rust."#;
--------------------------------------"#;
println!("{}", info.gradient(Color::Yellow).bold());
funny_opening!();
// println!("{}", loaded_config_file);
}

fn get_config_file() {
let result_get_location = get_location_config();
let location = match result_get_location {
Ok(path) => path,
Err(_) => panic!("Your system does not have appdirs."),
};
info!("The location of config is {:?}", location);

detail!(format!("The config file is at {:?}", location));

load_and_parse_config_file(location);
}

fn get_location_config() -> Result<std::path::PathBuf, &'static str> {
info!("Getting location of config file");
let config_path = match dirs::config_dir() {
Some(mut path) => {
path.push("rustscan");
path.push("config.toml");
path
Ok(path)
}
None => panic!("Couldn't find config dir."),
None => Err("Your system does not have APPDIRS"),
};
config_path
}

fn load_and_parse_config_file(config_path: PathBuf) -> ConfigFile {
// Gets the file and returns the string of TOML
// TODO quiet mode

let mut file = File::open(&config_path);
let mut file: File = match file {
Ok(result) => result,
Err(_) => download_place_config_file(config_path),
};
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
let config: ConfigFile = toml::from_str(&contents).unwrap();

detail!(format!(
"{} {:?}",
"The config file is expected to be at", config_path
));
config
}
// Downloads config file
// Places into Appdirs

fn download_place_config_file(config_location: PathBuf) -> std::fs::File {
info!("Placing config file");
let body = ureq::get("https://raw.githubusercontent.com/RustScan/RustScan/master/config.toml")
.call()
.into_string()
.unwrap();

let mut file = File::create(config_location).unwrap();
file.write(body.as_bytes()).unwrap();
file
}

#[cfg(not(tarpaulin_include))]
fn build_nmap_arguments<'a>(
addr: &'a str,
Expand Down Expand Up @@ -373,6 +441,7 @@ mod tests {
timeout: 1_000,
ulimit: Some(2_000),
command: Vec::new(),
config: false,
accessible: false,
scan_order: ScanOrder::Serial,
};
Expand All @@ -392,6 +461,7 @@ mod tests {
timeout: 1_000,
ulimit: Some(2_000),
command: Vec::new(),
config: false,
accessible: false,
scan_order: ScanOrder::Serial,
};
Expand All @@ -412,6 +482,7 @@ mod tests {
timeout: 1_000,
ulimit: Some(2_000),
command: Vec::new(),
config: false,
accessible: false,
scan_order: ScanOrder::Serial,
};
Expand All @@ -431,6 +502,7 @@ mod tests {
timeout: 1_000,
ulimit: Some(2_000),
command: Vec::new(),
config: false,
accessible: false,
scan_order: ScanOrder::Serial,
};
Expand All @@ -455,6 +527,7 @@ mod tests {
timeout: 1_000,
ulimit: None,
command: Vec::new(),
config: false,
accessible: true,
scan_order: ScanOrder::Serial,
};
Expand All @@ -477,6 +550,7 @@ mod tests {
command: Vec::new(),
accessible: false,
scan_order: ScanOrder::Serial,
config: false,
};
let ips = parse_ips(&opts);

Expand All @@ -496,6 +570,7 @@ mod tests {
command: Vec::new(),
accessible: false,
scan_order: ScanOrder::Serial,
config: false,
};
let ips = parse_ips(&opts);

Expand All @@ -515,6 +590,7 @@ mod tests {
command: Vec::new(),
accessible: false,
scan_order: ScanOrder::Serial,
config: false,
};
let ips = parse_ips(&opts);

Expand Down

0 comments on commit c509172

Please sign in to comment.