Skip to content

Commit

Permalink
added clap args
Browse files Browse the repository at this point in the history
  • Loading branch information
Gogs committed Jun 20, 2020
1 parent e41f0e2 commit 3c8e991
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 35 deletions.
63 changes: 63 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ edition = "2018"
[dependencies]
pcap = "0.7.0"
hex = "0.4"
clap = "2.33.1"
reqwest = { version = "0.10.6", features = ["blocking", "json"] }
91 changes: 56 additions & 35 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,72 @@ use pcap::Device;
use std::collections::HashMap;
use std::time::Instant;
use std::env;
use std::process;
use clap::ArgMatches;

mod utils;

static mut CONFIG: Option<Config> = None;

struct Config {
threshold: u32,
wait: u32,
use_discord: bool,
discord_channel_id: String,
discord_token: String
}

impl Config {
fn parse() -> Result<Config, &'static str> {


let discord_channel_id = match env::var("DISCORD_CHANNEL_ID") {
Ok(id) => id,
_ => {
println!("Not notifying with discord. Not DISCORD_CHANNEL_ID env set.");
let value = String::from("");
value
fn parse(matches: &ArgMatches) -> Result<Config, &'static str> {

let mut use_discord = false;

let discord_channel_id = match matches.value_of("channel") {
Some(channel) => {
use_discord = true;
channel.to_string()
},
None => match env::var("DISCORD_CHANNEL_ID") {
Ok(channel) => {
use_discord = true;
channel.to_string()
},
Err(_) => {
use_discord = false;
String::from("")
}
}
};

let discord_token = match env::var("DISCORD_TOKEN") {
Ok(token) => token,
_ => {
println!("Not notifying with discord. Not DISCORD_TOKEN env set.");
let value = String::from("");
value
}
let discord_token = match matches.value_of("token") {
Some(value) => {
use_discord = true;
value.to_string()
},
None => match env::var("DISCORD_TOKEN") {
Ok(value) => {
use_discord = true;
value.to_string()
},
Err(_) => {
use_discord = false;
String::from("")
}
}
};

// Instantiate Offender HitCounter with Threshold of 10 packets.
let threshold = match env::args().nth(1) {
Some(threshold) => threshold.parse().expect("Threshold must be a integer"),
_ => {
println!("Using default packet threshold of 5");
5
}
};
let threshold: u32 = matches.value_of("threshold").unwrap_or("5").parse()
.expect("threshold must be an integer");

let wait: u32 = matches.value_of("wait").unwrap_or("10").parse()
.expect("wait must be an integer");

let wait = match env::args().nth(2) {
Some(wait) => wait.parse().expect("wait must be an integer"),
_ => {
println!("Using default discord notfication delay wait of 10 seconds");
10
}
};

Ok(Config {
threshold,
wait,
use_discord,
discord_channel_id,
discord_token,
})
Expand Down Expand Up @@ -190,8 +206,12 @@ fn notify_discord(intruder: &DecodedPacket) {

fn main() {

// Parse Config with Clap
let matches = utils::parse_args();

// Parse Config
let config = Config::parse().unwrap();
// CONFIG = Some(Config::parse(&matches).unwrap());
let config = Config::parse(&matches).unwrap();

// Instantiate Capture Device
let mut cap = Device::lookup().unwrap()
Expand All @@ -201,11 +221,10 @@ fn main() {
println!("===== Config ===========================================");
println!("Threshold: {} (Only triggers after this many packets)", config.threshold);
println!(" Wait: {} (Waits this many seconds before sending another Discord Message)", config.wait);
if config.discord_channel_id == "".to_string() || config.discord_token == "".to_string() {
println!(" Notify: None (set DISCORD_TOKEN and DISCORD_CHANNEL_ID env var to notify with Discord bot)");
} else {
if config.use_discord {
println!(" Notify: Discord");

} else {
println!(" Notify: None (set DISCORD_TOKEN and DISCORD_CHANNEL_ID env var to notify with Discord bot)");
}
println!("=========================================================");

Expand All @@ -227,3 +246,5 @@ fn main() {

}
}


44 changes: 44 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
extern crate clap;

pub fn parse_args() -> clap::ArgMatches<'static> {
let matches = clap::App::new("nform - Be nform'd of nmap scan")
.version("0.1.0")
.author("Ryan Plyler <[email protected]>")
.about("Detects stealth nmap scans and notifies via Discord Bot")
.arg(clap::Arg::with_name("threshold")
.short("t")
.long("threshold")
.takes_value(true)
.help("Number of scanning packets to capture before triggered"))
.arg(clap::Arg::with_name("discord")
.short("d")
.long("discord")
.takes_value(false)
.help("Use discord bot to notify of scanning activity"))
.arg(clap::Arg::with_name("token")
.short("k")
.long("token")
.takes_value(true)
.help("Discord Bot Auth token"))
.arg(clap::Arg::with_name("channel")
.short("c")
.long("channel")
.takes_value(true)
.help("Discord Bot channel ID"))
.arg(clap::Arg::with_name("wait")
.short("w")
.long("wait")
.takes_value(true)
.help("Delay in seconds between discord notifications"))
.get_matches();

matches
}








0 comments on commit 3c8e991

Please sign in to comment.