Skip to content

Commit

Permalink
Merge pull request #1265 from Yamato-Security/1264-json-output-does-n…
Browse files Browse the repository at this point in the history
…ot-output-in-original-order

Changed `Details` output in `json-timeline` output to original rule order
  • Loading branch information
YamatoSecurity authored Feb 4, 2024
2 parents 8c2a859 + 5678989 commit 6eb5c73
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-Japanese.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- `json-timeline`コマンドの標準出力でJSONフォーマットを出力するように修正した。 (#1197) (@hitenkoku)
- JSON入力でデータが配列内にある場合に解析できるようにした。 (#1248) (@hitenkoku)
- 古いターミナルでも正しく表示されるように、また読みやすくするために、``区切り文字を`·`区切り文字に変更した。(#1258) (@YamatoSecurity)
- `json-timeline`コマンドの`Details`の出力で、要素がアルファベット順に並んでいたのをルールに記載されているオリジナルの順番に変更した。 (#1264) (@hitenkoku)
- ルールをロードする必要のないコマンドを実行した場合、検出ルールのロードをスキップするようにした。 (#1263) (@hitenkoku)

**バグ修正:**
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- The `json-timeline` command now outputs in JSON format when outputting to the terminal. (#1197) (@hitenkoku)
- Added support for parsing JSON input when the data is inside an array. (#1248) (@hitenkoku)
- Changed the `` separator into a `·` separator to make it easier to read and render properly on older terminals. (#1258) (@YamatoSecurity)
- Changed the `Details` output in `json-timeline` command from alphabetical order to the original order.
- Skiped loading detection rules when running to command which is no need to load rule. (#1263) (@hitenkoku)

**Bug Fixes:**
Expand Down
17 changes: 15 additions & 2 deletions src/afterfact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use chrono::{DateTime, Local, TimeZone, Utc};
use comfy_table::modifiers::UTF8_ROUND_CORNERS;
use comfy_table::presets::UTF8_FULL;
use compact_str::CompactString;
use hashbrown::hash_map::RawEntryMut;
use terminal_size::terminal_size;

use csv::{QuoteStyle, WriterBuilder};
Expand Down Expand Up @@ -1633,20 +1634,32 @@ pub fn output_json_str(
};
let mut children_output_stock: HashMap<CompactString, Vec<CompactString>> =
HashMap::new();
let mut children_output_order = vec![];
for contents in details_target_stock.iter() {
let (key, value) = contents.split_once(':').unwrap_or_default();
let output_key = _convert_valid_json_str(&[key], false);
let fmted_val = _convert_valid_json_str(&[value.trim_start()], false);
if let RawEntryMut::Vacant(_) = children_output_stock
.raw_entry_mut()
.from_key(output_key.as_str())
{
children_output_order.push(output_key.clone());
}
children_output_stock
.entry(output_key.into())
.or_insert(vec![])
.push(fmted_val.into());
}
// ルール内での表示順に合わせた表示順を戻した配列
let mut sorted_children_output_stock: Vec<(
&CompactString,
&Vec<CompactString>,
)> = children_output_stock.iter().collect_vec();
sorted_children_output_stock.sort_by(|a, b| a.0.cmp(b.0));
for (k, v) in children_output_stock.iter() {
let index_in_rule =
children_output_order.iter().position(|x| x == k).unwrap();
sorted_children_output_stock[index_in_rule] = (k, v);
}
for (idx, (c_key, c_val)) in sorted_children_output_stock.iter().enumerate() {
let fmted_c_val = if c_val.len() == 1 {
c_val[0].to_string()
Expand All @@ -1656,7 +1669,7 @@ pub fn output_json_str(
c_val.iter().map(|x| { format!("\"{x}\"") }).join(", ")
)
};
if idx != sorted_children_output_stock.len() - 1 {
if idx != children_output_stock.len() - 1 {
output_stock.push(format!(
"{},",
_create_json_output_format(
Expand Down
13 changes: 6 additions & 7 deletions src/detections/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ pub fn parse_message(
field_data_map: &Option<FieldDataMap>,
) -> (CompactString, Vec<CompactString>) {
let mut return_message = output.clone();
let mut hash_map: HashMap<CompactString, Vec<CompactString>> = HashMap::new();
let mut hash_map: Vec<(CompactString, Vec<CompactString>)> = vec![];
let details_key: Vec<&str> = output.split(" ¦ ").collect();
for caps in ALIASREGEX.captures_iter(&return_message) {
let full_target_str = &caps[0];
Expand Down Expand Up @@ -337,19 +337,19 @@ pub fn parse_message(
converted_str.unwrap_or(hash_value)
};
if json_timeline_flag {
hash_map.insert(CompactString::from(full_target_str), [field_data].to_vec());
hash_map.push((CompactString::from(full_target_str), [field_data].to_vec()));
} else {
hash_map.insert(
hash_map.push((
CompactString::from(full_target_str),
[field_data.split_ascii_whitespace().join(" ").into()].to_vec(),
);
));
}
}
} else {
hash_map.insert(
hash_map.push((
CompactString::from(full_target_str),
["n/a".into()].to_vec(),
);
));
}
}
let mut details_key_and_value: Vec<CompactString> = vec![];
Expand All @@ -366,7 +366,6 @@ pub fn parse_message(
}
}
}
details_key_and_value.sort_unstable();
(return_message, details_key_and_value)
}

Expand Down

0 comments on commit 6eb5c73

Please sign in to comment.