Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude include #134

Merged
merged 7 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,765 changes: 1,765 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ clap = { version = "4.0.29", features = ["derive"] }
const_format = "0.2.30"
serde = { version = "^1.0", features = ["derive"] }
serde_json = "^1.0"
cherrybomb-engine = "^0.1"
#cherrybomb-engine = "^0.1"
cherrybomb-engine = {path="cherrybomb-engine"}
serde_yaml = "^0.9.0"
uuid = {version = "1.2.2", features = ["v4"] }
dirs = "4.0.0"
Expand Down
2 changes: 0 additions & 2 deletions cherrybomb-engine/Cargo.lock

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

3 changes: 2 additions & 1 deletion cherrybomb-engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cherrybomb-oas = "^0.1"
#cherrybomb-oas = "^0.1"
cherrybomb-oas={path = "../cherrybomb-oas"}
GuyL99 marked this conversation as resolved.
Show resolved Hide resolved
anyhow = "1.0.66"
thiserror = "1.0.37"
serde_json = "^1.0"
Expand Down
19 changes: 17 additions & 2 deletions cherrybomb-engine/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ pub enum Profile {
Info,
#[default]
Normal,
Intrusive,
Active,
Passive,
Full,
OWASP
}

#[derive(Deserialize, Debug, Default)]
#[derive(Deserialize, Debug, Default, Clone)]
#[serde(default)]
pub struct Config {
pub file: std::path::PathBuf,
Expand All @@ -25,6 +26,20 @@ pub struct Config {
pub security: Vec<Auth>,
pub ignore_tls_errors: bool,
pub no_color: bool,
pub active_checks: Vec<String>,
pub passive_checks: Vec<String>,
}
impl Config {
pub fn update_checks_passive(&mut self, mut vec_checks: Vec<String>) {
//get a list of passive checks and remove exclude
vec_checks.retain(|check| !self.passive_exclude.contains(check));
self.passive_checks = vec_checks;
}
pub fn update_checks_active(&mut self, mut vec_checks: Vec<String>) {
//get a list of active checks and remove exclude
vec_checks.retain(|check| !self.active_exclude.contains(check));
self.active_checks = vec_checks;
}
}

#[derive(ValueEnum, Deserialize, Clone, Debug, Default, PartialOrd, PartialEq)]
Expand Down
155 changes: 124 additions & 31 deletions cherrybomb-engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ use crate::scan::active::active_scanner;
use crate::scan::active::http_client::auth::Authorization;
use cherrybomb_oas::legacy::legacy_oas::*;
use config::Config;
use scan::checks::{ActiveChecks, PassiveChecks};
use scan::passive::passive_scanner;
use scan::*;
use serde_json::{json, Value};
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::vec;
use strum::IntoEnumIterator;

fn verbose_print(config: &Config, required: Option<Verbosity>, message: &str) {
let required = required.unwrap_or(Verbosity::Normal);
Expand All @@ -21,7 +24,7 @@ fn verbose_print(config: &Config, required: Option<Verbosity>, message: &str) {
}
}

pub async fn run(config: &Config) -> anyhow::Result<Value> {
pub async fn run(config: &mut Config) -> anyhow::Result<Value> {
verbose_print(config, None, "Starting Cherrybomb...");

// Reading OAS file to string
Expand Down Expand Up @@ -50,17 +53,32 @@ pub async fn run(config: &Config) -> anyhow::Result<Value> {
return Err(anyhow::anyhow!("Error creating OAS struct: {}", e));
}
};

match config.profile {
config::Profile::Info => run_profile_info(&config, &oas, &oas_json),
config::Profile::Normal => run_normal_profile(&config, &oas, &oas_json).await,
config::Profile::Intrusive => todo!("Not implemented!"),
config::Profile::Passive => run_passive_profile(&config, &oas, &oas_json),
config::Profile::Info => run_profile_info(config, &oas, &oas_json),
config::Profile::Normal => run_normal_profile(config, &oas, &oas_json).await,
config::Profile::Active => {
if !&config.passive_include.is_empty() {
GuyL99 marked this conversation as resolved.
Show resolved Hide resolved
config.passive_checks = config.passive_include.clone();
run_normal_profile(config, &oas, &oas_json).await
} else {
run_active_profile(config, &oas, &oas_json).await
}
}

config::Profile::Passive => {
if !&config.active_include.is_empty() {
GuyL99 marked this conversation as resolved.
Show resolved Hide resolved
config.active_checks = config.active_include.clone();
run_normal_profile(config, &oas, &oas_json).await
} else {
run_passive_profile(config, &oas, &oas_json)
}
}
config::Profile::Full => run_full_profile(config, &oas, &oas_json).await,
config::Profile::OWASP => todo!("not implemented yet!"),
}
}

fn run_profile_info(config: &Config, oas: &OAS3_1, oas_json: &Value) -> anyhow::Result<Value> {
fn run_profile_info(config: &mut Config, oas: &OAS3_1, oas_json: &Value) -> anyhow::Result<Value> {
// Creating parameter list
verbose_print(config, None, "Creating param list...");
let param_scan = ParamTable::new::<OAS3_1>(oas_json);
Expand Down Expand Up @@ -89,7 +107,7 @@ fn run_profile_info(config: &Config, oas: &OAS3_1, oas_json: &Value) -> anyhow::
}

async fn run_active_profile(
config: &Config,
config: &mut Config,
oas: &OAS3_1,
oas_json: &Value,
) -> anyhow::Result<Value> {
Expand All @@ -109,44 +127,115 @@ async fn run_active_profile(
// Running active scan
verbose_print(config, None, "Running active scan...");
let temp_auth = Authorization::None;
active_scan
.run(active_scanner::ActiveScanType::Full, &temp_auth)
.await;
let active_result: HashMap<&str, Vec<Alert>> = active_scan
.checks
.iter()
.map(|check| (check.name(), check.inner()))
.collect();
let report = json!({ "active": active_result });
Ok(report)

let active_result: HashMap<&str, Vec<Alert>> = match config.active_checks.is_empty() {
true => {
// if empty, active profile and exclude active checks is set

let all_active_checks = ActiveChecks::iter().map(|x| x.name().to_string()).collect();
config.update_checks_active(all_active_checks);

//create a vec of active scan checks
let active_checks = ActiveChecks::create_checks(config.active_checks.clone());

active_scan
.run(
active_scanner::ActiveScanType::Partial(active_checks),
&temp_auth,
)
.await;
active_scan
.checks
.iter()
.map(|check| (check.name(), check.inner()))
.collect()
}
false => {
// if the active_checks not empty, active include_checks and passive profile is set
let active_checks_to_run = ActiveChecks::iter()
.filter(|check| config.active_checks.contains(&check.name().to_string()))
.collect();
active_scan
.run(
active_scanner::ActiveScanType::Partial(active_checks_to_run),
&temp_auth,
)
.await;
active_scan
.checks
.iter()
.map(|check| (check.name(), check.inner()))
.collect()
}
};


Ok(json!({ "active": active_result }))
}

fn run_passive_profile(config: &Config, oas: &OAS3_1, oas_json: &Value) -> anyhow::Result<Value> {
// Creating passive scan struct
fn run_passive_profile(
config: &mut Config,
oas: &OAS3_1,
oas_json: &Value,
) -> anyhow::Result<Value> {
verbose_print(
config,
Some(Verbosity::Debug),
"Creating passive scan struct...",
);
// Creating passive scan struct
let mut passive_scan = passive_scanner::PassiveSwaggerScan {
swagger: oas.clone(),
swagger_value: oas_json.clone(),
passive_checks: vec![], //TODO create check list from config
passive_checks: vec![],
verbosity: 0,
};

// Running passive scan
verbose_print(config, None, "Running passive scan...");
passive_scan.run(passive_scanner::PassiveScanType::Full);
let passive_result: HashMap<&str, Vec<Alert>> = passive_scan
.passive_checks
.iter()
.map(|check| (check.name(), check.inner()))
.collect();
Ok(json!({ "passive": passive_result }))

let passive_result: HashMap<&str, Vec<Alert>> = match config.passive_checks.is_empty() {
true => {
// if empty, passive profile and exclude passive checks is set
let all_passive_checks = PassiveChecks::iter()
.map(|x| x.name().to_string())
.collect(); //collect all passive checks
config.update_checks_passive(all_passive_checks);

//create vector of passive checks to run
let passive_checks_to_run = PassiveChecks::iter()
.filter(|check| config.active_checks.contains(&check.name().to_string()))
.collect();
passive_scan.run(passive_scanner::PassiveScanType::Partial(
passive_checks_to_run,
));
passive_scan
.passive_checks
.iter()
.map(|check| (check.name(), check.inner()))
.collect()
}
false => {
// if the passive_checks not empty,so passive include_checks and active profile is set
let passive_checks_to_run = PassiveChecks::iter()
.filter(|check| config.passive_checks.contains(&check.name().to_string()))
.collect();
passive_scan.run(passive_scanner::PassiveScanType::Partial(
passive_checks_to_run,
));
passive_scan
.passive_checks
.iter()
.map(|check| (check.name(), check.inner()))
.collect()
}
};
Ok(json!({"passive": passive_result}))
}


async fn run_normal_profile(
config: &Config,
config: &mut Config,
oas: &OAS3_1,
oas_json: &Value,
) -> anyhow::Result<Value> {
Expand Down Expand Up @@ -174,7 +263,11 @@ async fn run_normal_profile(
Ok(report)
}

async fn run_full_profile(config: &Config, oas: &OAS3_1, oas_json: &Value) -> anyhow::Result<Value> {
async fn run_full_profile(
config: &mut Config,
oas: &OAS3_1,
oas_json: &Value,
) -> anyhow::Result<Value> {
let mut report = json!({});
let mut results = HashMap::from([
("active", run_active_profile(config, oas, oas_json).await),
Expand Down
1 change: 0 additions & 1 deletion cherrybomb-engine/src/scan/active/active_scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ impl<T: OAS + Serialize + for<'de> Deserialize<'de>> ActiveScan<T> {
};
}


fn payloads_generator(oas: &T, oas_value: &Value) -> Vec<OASMap> {
let mut payloads = vec![];
for (path, path_item) in oas.get_paths() {
Expand Down
13 changes: 13 additions & 0 deletions cherrybomb-engine/src/scan/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ macro_rules! impl_passive_checks{
)*
}
}
pub fn create_checks( vec_checks: Vec<String>) ->Vec<ActiveChecks>{ //get a vec of checks name and create check active struct
vec_checks
.into_iter()
.filter_map(|name| ActiveChecks::from_string(&name))
.collect()
}

}
impl <T:OAS+Serialize>PassiveSwaggerScan<T>{
Expand Down Expand Up @@ -90,7 +96,14 @@ macro_rules! impl_active_checks{
)*
}
}
pub fn create_checks( vec_checks: Vec<String>) ->Vec<ActiveChecks>{ //get a vec of checks name and create passive check struct
vec_checks
.into_iter()
.filter_map(|name| ActiveChecks::from_string(&name))
.collect()
}
}

impl <T:OAS+Serialize>ActiveScan<T>{
pub async fn run_check(&self,check:ActiveChecks,auth:&Authorization)->ActiveChecks{
match check{
Expand Down
5 changes: 4 additions & 1 deletion cherrybomb-oas/src/legacy/refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ impl Reference {
}
serde_json::from_value(val.clone()).unwrap()
} else {
todo!("external references are not supported yet: {:?}", self.param_ref)
todo!(
"external references are not supported yet: {:?}",
self.param_ref
)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async fn main() -> anyhow::Result<ExitCode> {
if !opt.no_telemetry {
telemetry::send(config.profile.clone(), config.verbosity.clone()).await?;
}
let json_val = cherrybomb_engine::run(&config).await?;
let json_val = cherrybomb_engine::run(&mut config).await?;
match print_tables(json_val, &opt) {
Ok(exit_code) => Ok(exit_code),
Err(e) => Err(anyhow::anyhow!("Error printing tables: {}", e)),
Expand Down
2 changes: 1 addition & 1 deletion src/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub async fn send(profile: Profile, verbosity: Verbosity) -> anyhow::Result<()>
let profile = match profile {
Profile::Info => "ep_table",
Profile::Normal => "passive and active oas scan",
Profile::Intrusive => "passive and active oas scan",
Profile::Active => "active oas scan",
Profile::Passive => "passive oas scan",
Profile::Full => "passive and active oas scan",
};
Expand Down