From 68276292bcec30357c1872fe9674a6cf26fcc008 Mon Sep 17 00:00:00 2001 From: DustInDark Date: Sun, 26 Jun 2022 00:21:07 +0900 Subject: [PATCH] to remove unnecessary newline in display output --- src/afterfact.rs | 11 ++++++++-- src/detections/detection.rs | 3 +++ src/detections/print.rs | 2 ++ src/detections/utils.rs | 7 +++++- src/main.rs | 44 ++++++++++++++++++++++++++++++++----- src/options/level_tuning.rs | 2 ++ 6 files changed, 61 insertions(+), 8 deletions(-) diff --git a/src/afterfact.rs b/src/afterfact.rs index 4d966ac5..1d699a60 100644 --- a/src/afterfact.rs +++ b/src/afterfact.rs @@ -287,6 +287,7 @@ fn emit_csv( &disp_wtr, get_writable_color(None), &_get_serialized_disp_output(None), + true, ) .ok(); plus_header = false; @@ -295,6 +296,7 @@ fn emit_csv( &disp_wtr, get_writable_color(_get_output_color(&color_map, &detect_info.level)), &_get_serialized_disp_output(Some(dispformat)), + false, ) .ok(); } else { @@ -383,6 +385,7 @@ fn emit_csv( &disp_wtr, get_writable_color(Some(Color::Green)), "Results Summary:", + true, ) .ok(); @@ -406,6 +409,7 @@ fn emit_csv( &disp_wtr, get_writable_color(None), &format!("Total events: {}", all_record_cnt), + true, ) .ok(); write_color_buffer( @@ -415,6 +419,7 @@ fn emit_csv( "Data reduction: {} events ({:.2}%)", reducted_record_cnt, reducted_percent ), + true, ) .ok(); println!(); @@ -471,7 +476,7 @@ fn _get_serialized_disp_output(dispformat: Option) -> String { if configs::CONFIG.read().unwrap().args.full_data { titles.push("RecordInformation"); } - return format!("{}\n", titles.join("|")); + return titles.join("|").to_string(); } let mut disp_serializer = csv::WriterBuilder::new() .double_quote(false) @@ -521,8 +526,9 @@ fn _print_unique_results( "{} {}: {}", head_word, tail_word, - counts_by_level.iter().sum::() + counts_by_level.iter().sum::(), ), + true, ) .ok(); @@ -538,6 +544,7 @@ fn _print_unique_results( &BufferWriter::stdout(ColorChoice::Always), _get_output_color(color_map, level_name), &output_raw_str, + true, ) .ok(); } diff --git a/src/detections/detection.rs b/src/detections/detection.rs index 597fce32..a507c2dc 100644 --- a/src/detections/detection.rs +++ b/src/detections/detection.rs @@ -394,6 +394,7 @@ impl Detection { &BufferWriter::stdout(ColorChoice::Always), Some(Color::Red), &format!("Rule parsing errors: {}", err_rc), + true, ) .ok(); } @@ -421,6 +422,7 @@ impl Detection { rate, deprecated_flag ), + true, ) .ok(); } @@ -434,6 +436,7 @@ impl Detection { &BufferWriter::stdout(ColorChoice::Always), None, &format!("{} rules: {}", key, value), + true, ) .ok(); }); diff --git a/src/detections/print.rs b/src/detections/print.rs index a63bd91e..29b943b0 100644 --- a/src/detections/print.rs +++ b/src/detections/print.rs @@ -327,6 +327,7 @@ impl AlertMessage { &BufferWriter::stderr(ColorChoice::Always), None, &format!("[ERROR] {}", contents), + true, ) } @@ -336,6 +337,7 @@ impl AlertMessage { &BufferWriter::stderr(ColorChoice::Always), None, &format!("[WARN] {}", contents), + true, ) } } diff --git a/src/detections/utils.rs b/src/detections/utils.rs index b9f9ba4a..9f919630 100644 --- a/src/detections/utils.rs +++ b/src/detections/utils.rs @@ -245,10 +245,15 @@ pub fn write_color_buffer( wtr: &BufferWriter, color: Option, output_str: &str, + newline_flag: bool, ) -> io::Result<()> { let mut buf = wtr.buffer(); buf.set_color(ColorSpec::new().set_fg(color)).ok(); - writeln!(buf, "{}", output_str).ok(); + if newline_flag { + writeln!(buf, "{}", output_str).ok(); + } else { + write!(buf, "{}", output_str).ok(); + } wtr.print(&buf) } diff --git a/src/main.rs b/src/main.rs index e3c95f2e..578f7727 100644 --- a/src/main.rs +++ b/src/main.rs @@ -120,6 +120,7 @@ impl App { &BufferWriter::stdout(ColorChoice::Always), None, "Rules updated successfully.", + true, ) .ok(); } @@ -173,6 +174,7 @@ impl App { &BufferWriter::stdout(ColorChoice::Always), None, "Generating Event ID Statistics", + true, ) .ok(); println!(); @@ -182,6 +184,7 @@ impl App { &BufferWriter::stdout(ColorChoice::Always), None, "Generating Logons Summary", + true, ) .ok(); println!(); @@ -265,6 +268,7 @@ impl App { &BufferWriter::stdout(ColorChoice::Always), None, &configs::CONFIG.read().unwrap().headless_help, + true, ) .ok(); return; @@ -277,6 +281,7 @@ impl App { &BufferWriter::stdout(ColorChoice::Always), None, &format!("Elapsed Time: {}", &analysis_duration.hhmmssxxx()), + true, ) .ok(); println!(); @@ -329,17 +334,30 @@ impl App { ) .ok(); }); - write_color_buffer(&BufferWriter::stdout(ColorChoice::Always), None, &output).ok(); + write_color_buffer( + &BufferWriter::stdout(ColorChoice::Always), + None, + &output, + true, + ) + .ok(); } else { //標準出力の場合 let output = "The following pivot keywords were found:".to_string(); - write_color_buffer(&BufferWriter::stdout(ColorChoice::Always), None, &output).ok(); + write_color_buffer( + &BufferWriter::stdout(ColorChoice::Always), + None, + &output, + true, + ) + .ok(); pivot_key_unions.iter().for_each(|(key, pivot_keyword)| { write_color_buffer( &BufferWriter::stdout(ColorChoice::Always), None, &create_output(String::default(), key, pivot_keyword), + true, ) .ok(); }); @@ -425,8 +443,13 @@ impl App { fn print_contributors(&self) { match fs::read_to_string("./contributors.txt") { Ok(contents) => { - write_color_buffer(&BufferWriter::stdout(ColorChoice::Always), None, &contents) - .ok(); + write_color_buffer( + &BufferWriter::stdout(ColorChoice::Always), + None, + &contents, + true, + ) + .ok(); } Err(err) => { AlertMessage::alert(&format!("{}", err)).ok(); @@ -445,6 +468,7 @@ impl App { &BufferWriter::stdout(ColorChoice::Always), None, &format!("Analyzing event files: {:?}", evtx_files.len()), + true, ) .ok(); @@ -671,6 +695,7 @@ impl App { &BufferWriter::stdout(ColorChoice::Always), output_color, &content, + true, ) .ok(); } @@ -687,7 +712,13 @@ impl App { None => {} Some(path) => { let content = fs::read_to_string(path).unwrap_or_default(); - write_color_buffer(&BufferWriter::stdout(ColorChoice::Always), None, &content).ok(); + write_color_buffer( + &BufferWriter::stdout(ColorChoice::Always), + None, + &content, + true, + ) + .ok(); } } } @@ -704,6 +735,7 @@ impl App { &BufferWriter::stdout(ColorChoice::Always), None, "Attempting to git clone the hayabusa-rules repository into the rules folder.", + true, ) .ok(); // execution git clone of hayabusa-rules repository when failed open hayabusa repository. @@ -886,6 +918,7 @@ impl App { "[Updated] {} (Modified: {} | Path: {})", tmp[0], tmp[1], tmp[2] ), + true, ) .ok(); } @@ -900,6 +933,7 @@ impl App { &BufferWriter::stdout(ColorChoice::Always), None, "You currently have the latest rules.", + true, ) .ok(); Ok("You currently have the latest rules.".to_string()) diff --git a/src/options/level_tuning.rs b/src/options/level_tuning.rs index c7a7bf80..797a0b3b 100644 --- a/src/options/level_tuning.rs +++ b/src/options/level_tuning.rs @@ -62,6 +62,7 @@ impl LevelTuning { &BufferWriter::stdout(ColorChoice::Always), None, &format!("path: {}", path), + true, ) .ok(); let mut content = match fs::read_to_string(&path) { @@ -101,6 +102,7 @@ impl LevelTuning { rule["level"].as_str().unwrap(), new_level ), + true, ) .ok(); }