Skip to content

Commit

Permalink
Merge pull request #1310 from Yamato-Security/1308-output-numbers-wit…
Browse files Browse the repository at this point in the history
…h-commas

Formatted output numbers
  • Loading branch information
hitenkoku authored Mar 15, 2024
2 parents e1f7469 + b3f8615 commit 91c8cf5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 21 deletions.
13 changes: 8 additions & 5 deletions src/detections/detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use chrono::{TimeZone, Utc};
use compact_str::CompactString;
use itertools::Itertools;
use nested::Nested;
use num_format::{Locale, ToFormattedString};
use std::default::Default;
use termcolor::{BufferWriter, Color, ColorChoice};
use yaml_rust::Yaml;
Expand Down Expand Up @@ -1082,7 +1083,7 @@ impl Detection {
let output_str = format!(
"{} rules: {}{}",
make_ascii_titlecase(key),
value,
value.to_formatted_string(&Locale::en),
disable_flag
);
println!("{output_str}");
Expand Down Expand Up @@ -1127,7 +1128,7 @@ impl Detection {
let output_str = format!(
"{} rules: {} ({:.2}%){}",
make_ascii_titlecase(key),
value,
value.to_formatted_string(&Locale::en),
rate,
disabled_flag
);
Expand All @@ -1149,7 +1150,7 @@ impl Detection {
let mut sorted_rc: Vec<(&CompactString, &u128)> = rc.iter().collect();
sorted_rc.sort_by(|a, b| a.0.cmp(b.0));
sorted_rc.into_iter().for_each(|(key, value)| {
let output_str = format!("{key} rules: {value}");
let output_str = format!("{key} rules: {}", value.to_formatted_string(&Locale::en));
write_color_buffer(
&BufferWriter::stdout(ColorChoice::Always),
None,
Expand All @@ -1162,8 +1163,10 @@ impl Detection {
}
});

let tmp_total_detect_output =
format!("Total enabled detection rules: {total_loaded_rule_cnt}");
let tmp_total_detect_output = format!(
"Total enabled detection rules: {}",
total_loaded_rule_cnt.to_formatted_string(&Locale::en)
);
println!("{tmp_total_detect_output}");
println!();
output_profile_name(&stored_static.output_option, true);
Expand Down
51 changes: 38 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use itertools::Itertools;
use libmimalloc_sys::mi_stats_print_out;
use mimalloc::MiMalloc;
use nested::Nested;
use num_format::{Locale, ToFormattedString};
use serde_json::{Map, Value};
use std::borrow::BorrowMut;
use std::ffi::{OsStr, OsString};
Expand Down Expand Up @@ -993,7 +994,10 @@ impl App {
write_color_buffer(
&BufferWriter::stdout(ColorChoice::Always),
None,
&format!("Total event log files: {:?}", evtx_files.len()),
&format!(
"Total event log files: {}",
evtx_files.len().to_formatted_string(&Locale::en)
),
true,
)
.ok();
Expand Down Expand Up @@ -1135,11 +1139,11 @@ impl App {
})
.collect_vec();
let selection_status_items = &[
format!("1. Core ({} rules) ( status: test, stable | level: high, critical )", sections_rule_cnt[0].iter().map(|(_, cnt)| cnt).sum::<i128>() - sections_rule_cnt[0].get("excluded").unwrap_or(&0)),
format!("2. Core+ ({} rules) ( status: test, stable | level: medium, high, critical )", sections_rule_cnt[1].iter().map(|(_, cnt)| cnt).sum::<i128>() - sections_rule_cnt[1].get("excluded").unwrap_or(&0)),
format!("3. Core++ ({} rules) ( status: experimental, test, stable | level: medium, high, critical )", sections_rule_cnt[2].iter().map(|(_, cnt)| cnt).sum::<i128>() - sections_rule_cnt[2].get("excluded").unwrap_or(&0)),
format!("4. All alert rules ({} rules) ( status: * | level: low+ )", sections_rule_cnt[3].iter().map(|(_, cnt)| cnt).sum::<i128>() - sections_rule_cnt[3].get("excluded").unwrap_or(&0)),
format!("5. All event and alert rules ({} rules) ( status: * | level: informational+ )", sections_rule_cnt[4].iter().map(|(_, cnt)| cnt).sum::<i128>() - sections_rule_cnt[4].get("excluded").unwrap_or(&0))
format!("1. Core ({} rules) ( status: test, stable | level: high, critical )", (sections_rule_cnt[0].iter().map(|(_, cnt)| cnt).sum::<i128>() - sections_rule_cnt[0].get("excluded").unwrap_or(&0)).to_formatted_string(&Locale::en)),
format!("2. Core+ ({} rules) ( status: test, stable | level: medium, high, critical )", (sections_rule_cnt[1].iter().map(|(_, cnt)| cnt).sum::<i128>() - sections_rule_cnt[1].get("excluded").unwrap_or(&0)).to_formatted_string(&Locale::en)),
format!("3. Core++ ({} rules) ( status: experimental, test, stable | level: medium, high, critical )", (sections_rule_cnt[2].iter().map(|(_, cnt)| cnt).sum::<i128>() - sections_rule_cnt[2].get("excluded").unwrap_or(&0)).to_formatted_string(&Locale::en)),
format!("4. All alert rules ({} rules) ( status: * | level: low+ )", (sections_rule_cnt[3].iter().map(|(_, cnt)| cnt).sum::<i128>() - sections_rule_cnt[3].get("excluded").unwrap_or(&0)).to_formatted_string(&Locale::en)),
format!("5. All event and alert rules ({} rules) ( status: * | level: informational+ )", (sections_rule_cnt[4].iter().map(|(_, cnt)| cnt).sum::<i128>() - sections_rule_cnt[4].get("excluded").unwrap_or(&0)).to_formatted_string(&Locale::en))
];

let color_theme = if stored_static.common_options.no_color {
Expand Down Expand Up @@ -1212,7 +1216,10 @@ impl App {
// If anything other than "4. All alert rules" or "5. All event and alert rules" was selected, ask questions about tags.
if selected_index < 3 {
if let Some(et_cnt) = tags_cnt.get("detection.emerging_threats") {
let prompt_fmt = format!("Include Emerging Threats rules? ({} rules)", et_cnt);
let prompt_fmt = format!(
"Include Emerging Threats rules? ({} rules)",
et_cnt.to_formatted_string(&Locale::en)
);
let et_rules_load_flag = Confirm::with_theme(&color_theme)
.with_prompt(prompt_fmt)
.default(true)
Expand All @@ -1225,7 +1232,10 @@ impl App {
}
}
if let Some(th_cnt) = tags_cnt.get("detection.threat_hunting") {
let prompt_fmt = format!("Include Threat Hunting rules? ({} rules)", th_cnt);
let prompt_fmt = format!(
"Include Threat Hunting rules? ({} rules)",
th_cnt.to_formatted_string(&Locale::en)
);
let th_rules_load_flag = Confirm::with_theme(&color_theme)
.with_prompt(prompt_fmt)
.default(false)
Expand All @@ -1241,7 +1251,10 @@ impl App {
// If "4. All alert rules" or "5. All event and alert rules" was selected, ask questions about deprecated and unsupported rules.
if let Some(dep_cnt) = exclude_noisy_cnt.get("deprecated") {
// deprecated rules load prompt
let prompt_fmt = format!("Include deprecated rules? ({} rules)", dep_cnt);
let prompt_fmt = format!(
"Include deprecated rules? ({} rules)",
dep_cnt.to_formatted_string(&Locale::en)
);
let dep_rules_load_flag = Confirm::with_theme(&color_theme)
.with_prompt(prompt_fmt)
.default(false)
Expand All @@ -1258,7 +1271,10 @@ impl App {
}
if let Some(unsup_cnt) = exclude_noisy_cnt.get("unsupported") {
// unsupported rules load prompt
let prompt_fmt = format!("Include unsupported rules? ({} rules)", unsup_cnt);
let prompt_fmt = format!(
"Include unsupported rules? ({} rules)",
unsup_cnt.to_formatted_string(&Locale::en)
);
let unsupported_rules_load_flag = Confirm::with_theme(&color_theme)
.with_prompt(prompt_fmt)
.default(false)
Expand All @@ -1283,7 +1299,10 @@ impl App {

if let Some(noisy_cnt) = exclude_noisy_cnt.get("noisy") {
// noisy rules load prompt
let prompt_fmt = format!("Include noisy rules? ({} rules)", noisy_cnt);
let prompt_fmt = format!(
"Include noisy rules? ({} rules)",
noisy_cnt.to_formatted_string(&Locale::en)
);
let noisy_rules_load_flag = Confirm::with_theme(&color_theme)
.with_prompt(prompt_fmt)
.default(false)
Expand All @@ -1300,7 +1319,10 @@ impl App {
}

if let Some(sysmon_cnt) = tags_cnt.get("sysmon") {
let prompt_fmt = format!("Include sysmon rules? ({} rules)", sysmon_cnt);
let prompt_fmt = format!(
"Include sysmon rules? ({} rules)",
sysmon_cnt.to_formatted_string(&Locale::en)
);
let sysmon_rules_load_flag = Confirm::with_theme(&color_theme)
.with_prompt(prompt_fmt)
.default(true)
Expand All @@ -1325,7 +1347,10 @@ impl App {
if stored_static.html_report_flag {
let mut output_data = Nested::<String>::new();
let mut html_report_data = Nested::<String>::from_iter(vec![
format!("- Analyzed event files: {}", evtx_files.len()),
format!(
"- Analyzed event files: {}",
evtx_files.len().to_formatted_string(&Locale::en)
),
format!("- {total_size_output}"),
]);
if let Some(status_report) = status_append_output {
Expand Down
6 changes: 3 additions & 3 deletions src/options/htmlreport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,12 @@ mod tests {
"- Total file size: 148.5 MB".to_string(),
"- Excluded rules: 12".to_string(),
"- Noisy rules: 5 (Disabled)".to_string(),
"- Experimental rules: 1935 (65.97%)".to_string(),
"- Experimental rules: 1,935 (65.97%)".to_string(),
"- Stable rules: 215 (7.33%)".to_string(),
"- Test rules: 783 (26.70%)".to_string(),
"- Hayabusa rules: 138".to_string(),
"- Sigma rules: 2795".to_string(),
"- Total enabled detection rules: 2933".to_string(),
"- Sigma rules: 2,795".to_string(),
"- Total enabled detection rules: 2,933".to_string(),
"- Elapsed time: 00:00:29.035".to_string(),
"".to_string(),
]);
Expand Down

0 comments on commit 91c8cf5

Please sign in to comment.