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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
use super::*;
use comfy_table::*;
use comfy_table::presets::UTF8_FULL;
use comfy_table::modifiers::UTF8_ROUND_CORNERS;
pub const LEFT_PAD: usize = 40;
pub const TBL_LEN: usize = 190;
pub const URL_LEN: usize = 75;
pub fn print_active_alerts(checks:Vec<ActiveChecks>){
let mut table = Table::new();
table
.load_preset(UTF8_FULL)
.apply_modifier(UTF8_ROUND_CORNERS)
.set_content_arrangement(ContentArrangement::Dynamic)
.set_header(vec!["Check", "Top Severity", "Alerts"]);
for check in checks {
table.add_row(vec![
Cell::new(check.name()).add_attribute(Attribute::Bold),
check.top_severity().printable(),
check.alerts_text(),
]);
}
println!("{table}");
}
pub fn print_active_alerts_verbose(checks: Vec<ActiveChecks>) {
let mut table = Table::new();
table
.load_preset(UTF8_FULL)
.apply_modifier(UTF8_ROUND_CORNERS)
.set_content_arrangement(ContentArrangement::Dynamic)
.set_header(vec![
"Check",
"Severity",
"Description",
"Location",
"Certainty",
]);
for check in checks {
for alert in check.inner() {
table.add_row(vec![
Cell::new(check.name()).add_attribute(Attribute::Bold),
alert.level.printable(),
Cell::new(alert.description).add_attribute(Attribute::Bold),
Cell::new(alert.location).add_attribute(Attribute::Bold),
alert.certainty.printable(),
]);
}
}
println!("{table}");
}
pub fn print_passive_alerts(checks: Vec<PassiveChecks>) {
let mut table = Table::new();
table
.load_preset(UTF8_FULL)
.apply_modifier(UTF8_ROUND_CORNERS)
.set_content_arrangement(ContentArrangement::Dynamic)
.set_header(vec!["Check", "Top Severity", "Alerts", "Description"]);
for check in checks {
table.add_row(vec![
Cell::new(check.name()).add_attribute(Attribute::Bold),
check.top_severity().printable(),
check.alerts_text(),
Cell::new(check.description()).add_attribute(Attribute::Bold),
]);
}
println!("{table}");
}
pub fn print_passive_alerts_verbose(checks: Vec<PassiveChecks>) {
let mut table = Table::new();
table
.load_preset(UTF8_FULL)
.apply_modifier(UTF8_ROUND_CORNERS)
.set_content_arrangement(ContentArrangement::Dynamic)
.set_header(vec!["Check", "Severity", "Description", "Location"]);
for check in checks {
for alert in check.inner() {
table.add_row(vec![
Cell::new(check.name()).add_attribute(Attribute::Bold),
alert.level.printable(),
Cell::new(alert.description).add_attribute(Attribute::Bold),
Cell::new(trim_location(alert.location)).add_attribute(Attribute::Bold),
]);
}
}
println!("{table}");
}
fn prep_param(param:&ParamForTable)->(String,String,String,String,String,String,String){
let max = if let Some(m) = param.max { m.to_string() } else { "NULL".to_string() };
let min = if let Some(m) = param.min { m.to_string() } else { "NULL".to_string() };
let mut statuses = String::new();
let mut dms = String::new();
let mut eps = String::new();
let mut parents = String::new();
let mut children = String::new();
for status in ¶m.statuses{
statuses.push_str(status);
statuses.push('\n');
}
for dm in ¶m.dms{
dms.push_str(&format!("{:?}",dm));
dms.push('\n');
}
for ep in ¶m.eps{
eps.push_str(ep);
eps.push('\n');
}
for parent in ¶m.parents{
parents.push_str(parent);
parents.push('\n');
}
for child in ¶m.children{
children.push_str(child);
children.push('\n');
}
(min,max,statuses,dms,eps,parents,children)
}
// NOT YET USABLE SINCE THERE IS NO WAY FOR MULTICOLORING ONE CELL
pub fn print_param_table(params:&Vec<ParamForTable>){
let mut table = Table::new();
table
.load_preset(UTF8_FULL)
.apply_modifier(UTF8_ROUND_CORNERS)
.set_content_arrangement(ContentArrangement::Dynamic)
.set_header(vec!["Name","Type","Statuses","Delivery Methods","Endpoints","Parents","Children","Min-Max"]);
for param in params{
let (min,max,statuses,dms,eps,parents,children) = prep_param(param);
table.add_row(vec![
Cell::new(param.name.clone()).add_attribute(Attribute::Bold),
Cell::new(param.param_type.clone()).add_attribute(Attribute::Bold),
Cell::new(statuses).add_attribute(Attribute::Bold),
Cell::new(dms).add_attribute(Attribute::Bold),
Cell::new(eps).add_attribute(Attribute::Bold),
Cell::new(parents).add_attribute(Attribute::Bold),
Cell::new(children).add_attribute(Attribute::Bold),
Cell::new(format!("{}-{}",min,max)).add_attribute(Attribute::Bold),
]);
}
println!("{table}");
}
fn prep_ep(eps:&EpForTable)->(String,String,String,String,String,String){
let mut q_p = String::new();
let mut h_p = String::new();
let mut r_b_p = String::new();
let mut r_p = String::new();
let mut statuses = String::new();
let mut methods = String::new();
for method in &eps.ops{
methods.push_str(&method.to_string());
methods.push('\n');
}
for status in &eps.statuses{
statuses.push_str(status);
statuses.push('\n');
}
for p in &eps.query_params{
q_p.push_str(&p.to_string());
q_p.push('\n');
}
for p in &eps.headers_params{
h_p.push_str(p);
h_p.push('\n');
}
for p in &eps.req_body_params{
r_b_p.push_str(p);
r_b_p.push('\n');
}
for p in &eps.res_params{
r_p.push_str(p);
r_p.push('\n');
}
(methods,q_p,h_p,r_b_p,r_p,statuses)
}
// NOT YET USABLE SINCE THERE IS NO WAY FOR MULTICOLORING ONE CELL
pub fn print_ep_table(eps:&Vec<EpForTable>){
let mut table = Table::new();
table
.load_preset(UTF8_FULL)
.apply_modifier(UTF8_ROUND_CORNERS)
.set_content_arrangement(ContentArrangement::Dynamic)
.set_header(vec!["Path","Methods","Query Params","Header Params","Body Params","Response Params","Statuses"]);
for ep in eps{
let (methods,q_p,h_p,r_b_p,r_p,statuses) = prep_ep(ep);
table.add_row(vec![
Cell::new(ep.path.clone()).add_attribute(Attribute::Bold),
Cell::new(methods).add_attribute(Attribute::Bold),
Cell::new(q_p).add_attribute(Attribute::Bold),
Cell::new(h_p).add_attribute(Attribute::Bold),
Cell::new(r_b_p).add_attribute(Attribute::Bold),
Cell::new(r_p).add_attribute(Attribute::Bold),
Cell::new(statuses).add_attribute(Attribute::Bold),
]);
}
println!("{table}");
}
fn trim_location(loc:String)->String{
loc
.replace("swagger root", "")
.replace("swagger rooot", "")
.replace("swagger", "")
.replace("media type:application/json", "")
.replace("response status", "status")
}
impl Level {
pub fn printable(&self) -> Cell {
match self {
Self::Info => Cell::new("INFO")
.fg(Color::Blue)
.add_attribute(Attribute::Bold),
Self::Low => Cell::new("LOW")
.fg(Color::Yellow)
.add_attribute(Attribute::Bold),
Self::Medium => Cell::new("MEDIUM")
.fg(Color::Rgb {
r: 255,
g: 167,
b: 38,
})
.add_attribute(Attribute::Bold),
Self::High => Cell::new("HIGH")
.fg(Color::Red)
.add_attribute(Attribute::Bold),
Self::Critical => Cell::new("CRITICAL")
.fg(Color::Black)
.add_attribute(Attribute::Bold),
}
}
}
impl Certainty {
pub fn printable(&self) -> Cell {
match self {
Self::Low => Cell::new("LOW")
.fg(Color::DarkGrey)
.add_attribute(Attribute::Bold),
Self::Medium => Cell::new("MEDIUM")
.fg(Color::DarkGrey)
.add_attribute(Attribute::Bold),
Self::High => Cell::new("HIGH")
.fg(Color::DarkGrey)
.add_attribute(Attribute::Bold),
Self::Certain => Cell::new("CERTAIN")
.fg(Color::DarkGrey)
.add_attribute(Attribute::Bold),
Self::Passive => Cell::new(""),
}
}
}
/*
//pub const LEFT_PAD: usize = 40;
//pub const TBL_LEN: usize = 190;
//pub const URL_LEN: usize = 75;
pub fn print_checks_table<T>(checks: &[T])
where T:fmt::Display+Check{
println!(
"{:pad$}| RESULT | TOP SEVERITY | ALERTS |DESCRIPTION\n{:-<table_len$}",
"CHECK",
"",
pad = LEFT_PAD,
table_len = TBL_LEN
);
for check in checks {
println!("{}", check);
}
}
pub fn print_failed_checks_table<T>(checks: &[T])
where T:fmt::Display+Check{
println!(
"{:pad$}| RESULT | TOP SEVERITY | ALERTS |DESCRIPTION\n{:-<table_len$}",
"CHECK",
"",
pad = LEFT_PAD,
table_len = TBL_LEN
);
for check in checks {
if check.result() == "FAILED" {
println!("{}", check);
}
}
}
fn split_text_to_lines(string:&str)->Vec<String>{
let mut new_vec = vec![];
let mut new_str = String::new();
let line_len = 75;
let mut c = 0;
for t in string.split(' '){
if !t.trim().is_empty(){
c+=t.len()+1;
if c>line_len{
c = t.len();
new_str.pop();
new_vec.push(new_str);
new_str = format!(" {}",t);
}else{
new_str.push_str(&format!("{} ",t.trim()));
}
}
}
new_vec.push(new_str);
new_vec
}
pub fn print_alerts_table(checks: &[PassiveChecks]) {
println!(
"{:pad$}| LEVEL |{:75}|DESCRIPTION\n{:-<table_len$}",
"CHECK",
"LOCATION",
pad = 30,
table_len = TBL_LEN
);
for check in checks {
if check.result() == "FAILED" {
for alert in check.inner() {
println!("{:pad$}|{}", check.name().cyan().bold(), alert, pad = 30)
}
}
}
}
pub fn print_attack_alerts_table(checks: &[ActiveChecks]) {
println!(
"{:pad$}| SEVERITY | CERTAINTY |{:thing$}|DESCRIPTION\n{:-<table_len$}",
"CHECK",
"LOCATION",
"",
table_len = TBL_LEN,
pad = 30,
thing = URL_LEN
);
for check in checks {
if check.result() == "FAILED" {
for _ in check.inner() {
// println!("{:pad$}|{}", check.name().cyan().bold(), alert, pad = 30)
println!("{}",serde_json::to_string(&check).unwrap());
}
}
}
}
impl fmt::Display for PassiveChecks {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.result() == "PASSED" {
write!(
f,
"{:pad$}| {} | {:8} | {:5} |{}\n{:-<table_len$}",
self.name().bold(),
self.result().green().bold().underline(),
"NONE".blue().bold(),
self.alerts_text(),
self.description(),
"",
pad = LEFT_PAD,
table_len = TBL_LEN
)
} else if self.result() == "FAILED" {
write!(
f,
"{:pad$}| {} | {} | {:5} |{}\n{:-<table_len$}",
self.name().bold(),
self.result().red().bold().underline(),
self.top_severity(),
self.alerts_text(),
self.description(),
"",
pad = LEFT_PAD,
table_len = TBL_LEN
)
} else {
write!(f, "")
}
}
}
impl fmt::Display for ActiveChecks {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.result() == "PASSED" {
write!(
f,
"{:pad$}| {} | {:8} | {:5} |{}\n{:-<table_len$}",
self.name().bold(),
self.result().green().bold().underline(),
"NONE".blue().bold(),
self.alerts_text(),
self.description(),
"",
pad = LEFT_PAD,
table_len = TBL_LEN
)
} else if self.result() == "FAILED" {
write!(
f,
"{:pad$}| {} | {} | {:5} |{}\n{:-<table_len$}",
self.name().bold(),
self.result().red().bold().underline(),
self.top_severity(),
self.alerts_text(),
self.description(),
"",
pad = LEFT_PAD,
table_len = TBL_LEN
)
} else {
write!(f, "")
}
}
}
impl fmt::Display for Alert {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.certainty==Certainty::Passive{
let location = self
.location
.replace("swagger root", "")
.replace("swagger rooot", "")
.replace("swagger", "")
.replace("media type:application/json", "")
.replace("response status", "status");
let mut string = String::new();
let location = split_text_to_lines(&location);
string.push_str(&format!(
" {:10}|{:75}| {}\n",
self.level,
location[0].bright_magenta().bold(),
self.description.bright_red().bold(),
));
for loc in location.iter().skip(1){
string.push_str(&format!(
"{:30}|{:9}|{:75}| {}\n",
"",
"",
loc.bright_magenta().bold(),
""
));
}
string.push_str(&format!("\n{:-<190}",""));
write!(f,"{}",string)
}else{
/*write!(
f,
" {}| {} |{:thing$}| {:}\n{:-<table_len$}",
self.level,
self.certainty,
self.location.bright_magenta().bold(),
self.description.bright_red().bold(),
"",
thing=URL_LEN,
table_len = TBL_LEN
)*/
write!(f,"")
}
}
}
impl fmt::Display for Level {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Info => write!(f, "{:8}", "INFO".blue().bold()),
Self::Low => write!(f, "{:8}", "LOW".yellow().bold()),
Self::Medium => write!(f, "{:8}", "MEDIUM".truecolor(255, 167, 38).bold()),
Self::High => write!(f, "{:8}", "HIGH".red().bold()),
Self::Critical => write!(f, "{:8}", "CRITICAL".red().bold().blink()),
}
}
}
impl fmt::Display for Certainty {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Low => write!(f, "{:8}", "LOW".bright_black().bold()),
Self::Medium => write!(f, "{:8}", "MEDIUM".bright_black()/*.truecolor(255, 167, 38)*/.bold()),
Self::High => write!(f, "{:8}", "HIGH".bright_black().bold()),
Self::Certain => write!(f, "{:8}", "CERTAIN".bright_black().bold()),
Self::Passive=> write!(f, ""),
}
}
}
*/