1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
pub mod config;
mod info;
mod scan;
use crate::config::Verbosity;
use crate::info::eps::EpTable;
use crate::info::params::ParamTable;
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::passive::passive_scanner;
use scan::*;
use serde_json::{json, Value};
use std::collections::HashMap;
fn verbose_print(config: &Config, required: Option<Verbosity>, message: &str) {
let required = required.unwrap_or(Verbosity::Normal);
if config.verbosity >= required {
println!("{message}");
}
}
pub async fn run(config: &Config) -> anyhow::Result<Value> {
verbose_print(config, None, "Starting Cherrybomb...");
verbose_print(config, None, "Opening OAS file...");
let oas_file = match std::fs::read_to_string(&config.file) {
Ok(file) => file,
Err(e) => {
return Err(anyhow::anyhow!("Error reading OAS file: {}", e));
}
};
verbose_print(config, None, "Parsing OAS file...");
let oas_json: Value = match serde_json::from_str(&oas_file) {
Ok(json) => json,
Err(e) => {
return Err(anyhow::anyhow!("Error parsing OAS file: {}", e));
}
};
verbose_print(config, Some(Verbosity::Debug), "Creating OAS struct...");
let oas: OAS3_1 = match serde_json::from_value(oas_json.clone()) {
Ok(oas) => oas,
Err(e) => {
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::Full => run_full_profile(config, &oas, &oas_json).await,
}
}
fn run_profile_info(config: &Config, oas: &OAS3_1, oas_json: &Value) -> anyhow::Result<Value> {
verbose_print(config, None, "Creating param list...");
let param_scan = ParamTable::new::<OAS3_1>(oas_json);
let param_result: HashMap<&str, Value> = param_scan
.params
.iter()
.map(|param| (param.name.as_str(), json!(param)))
.collect();
verbose_print(config, None, "Create endpoint list");
let ep_table = EpTable::new::<OAS3_1>(oas_json);
let endpoint_result: HashMap<&str, Value> = ep_table
.eps
.iter()
.map(|param| (param.path.as_str(), json!(param)))
.collect();
verbose_print(config, None, "Creating report...");
let report = json!({
"params": param_result,
"endpoints": endpoint_result,
});
Ok(report)
}
async fn run_active_profile(
config: &Config,
oas: &OAS3_1,
oas_json: &Value,
) -> anyhow::Result<Value> {
verbose_print(
config,
Some(Verbosity::Debug),
"Creating active scan struct...",
);
let mut active_scan = match active_scanner::ActiveScan::new(oas.clone(), oas_json.clone()) {
Ok(scan) => scan,
Err(e) => {
return Err(anyhow::anyhow!("Error creating active scan struct: {}", e));
}
};
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)
}
fn run_passive_profile(config: &Config, oas: &OAS3_1, oas_json: &Value) -> anyhow::Result<Value> {
verbose_print(
config,
Some(Verbosity::Debug),
"Creating passive scan struct...",
);
let mut passive_scan = passive_scanner::PassiveSwaggerScan {
swagger: oas.clone(),
swagger_value: oas_json.clone(),
passive_checks: vec![], verbosity: 0,
};
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 }))
}
async fn run_normal_profile(
config: &Config,
oas: &OAS3_1,
oas_json: &Value,
) -> anyhow::Result<Value> {
let mut report = json!({});
let mut results = HashMap::from([
("passive", run_passive_profile(config, oas, oas_json)),
("active", run_active_profile(config, oas, oas_json).await),
]);
for (key, value) in results.iter_mut() {
match value {
Ok(result) => {
if let Some(val) = result.get(key) {
report[key] = val.clone();
}
}
Err(e) => {
verbose_print(
config,
None,
&format!("WARNING: Error running {key} scan: {e}"),
);
}
}
}
Ok(report)
}
async fn run_full_profile(config: &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),
("passive", run_passive_profile(config, oas, oas_json)),
("params", run_profile_info(config, oas, oas_json)),
("endpoints", run_profile_info(config, oas, oas_json)),
]);
for (key, value) in results.iter_mut() {
match value {
Ok(result) => {
if let Some(val) = result.get(key) {
report[key] = val.clone();
}
}
Err(e) => {
verbose_print(
config,
None,
&format!("WARNING: Error running {} scan: {}", key, e),
);
}
}
}
Ok(report)
}