Skip to content

Commit

Permalink
fix: fixed rule authors load error by used embbeded rules.
Browse files Browse the repository at this point in the history
  • Loading branch information
hitenkoku committed Jun 15, 2024
1 parent 9d8de52 commit 44df83b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 42 deletions.
58 changes: 17 additions & 41 deletions src/afterfact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::detections::utils::{
};
use crate::options::htmlreport;
use crate::options::profile::Profile;
use crate::yaml::ParseYaml;
use aho_corasick::{AhoCorasick, AhoCorasickBuilder, MatchKind};
use chrono::{DateTime, Local, TimeZone, Utc};
use comfy_table::modifiers::UTF8_ROUND_CORNERS;
Expand All @@ -20,9 +19,7 @@ use csv::{QuoteStyle, Writer, WriterBuilder};
use itertools::Itertools;
use krapslog::{build_sparkline, build_time_markers};
use nested::Nested;
use std::path::Path;
use std::str::FromStr;
use yaml_rust::YamlLoader;

use comfy_table::*;
use hashbrown::{HashMap, HashSet};
Expand Down Expand Up @@ -511,7 +508,7 @@ fn calc_statistic_info(
let author_list = afterfact_info
.author_list_cache
.entry(detect_info.rulepath.clone())
.or_insert_with(|| extract_author_name(&detect_info.rulepath))
.or_insert_with(|| extract_author_name(&detect_info.authors))
.clone();
let author_str = author_list.iter().join(", ");
afterfact_info
Expand Down Expand Up @@ -2133,45 +2130,24 @@ fn output_detected_rule_authors(
println!("{tb}");
}

/// 与えられたyaml_pathからauthorの名前を抽出して配列で返却する関数
fn extract_author_name(yaml_path: &str) -> Nested<String> {
let contents = match ParseYaml::read_file(Path::new(&yaml_path).to_path_buf()) {
Ok(yaml) => Some(yaml),
Err(e) => {
AlertMessage::alert(&e).ok();
None
}
};
if contents.is_none() {
// 対象のファイルが存在しなかった場合は空配列を返す(検知しているルールに対して行うため、ここは通る想定はないが、ファイルが検知途中で削除された場合などを考慮して追加)
return Nested::new();
/// 与えられたauthorsの文字列からそれぞれの名前を抽出して配列で返却する関数
fn extract_author_name(authors: &str) -> Nested<String> {
let mut ret = Nested::<String>::new();
for author in authors.split(',').map(|s| {
// 各要素の括弧以降の記載は名前としないためtmpの一番最初の要素のみを参照する
// データの中にdouble quote と single quoteが入っているためここで除外する
s.split('(').next().unwrap_or_default().to_string()
}) {
ret.extend(author.split(';'));
}
for yaml in YamlLoader::load_from_str(&contents.unwrap())
.unwrap_or_default()
.into_iter()
{
if let Some(author) = yaml["author"].as_str() {
let mut ret = Nested::<String>::new();
for author in author.split(',').map(|s| {
// 各要素の括弧以降の記載は名前としないためtmpの一番最初の要素のみを参照する
// データの中にdouble quote と single quoteが入っているためここで除外する
s.split('(').next().unwrap_or_default().to_string()
}) {
ret.extend(author.split(';'));
}

return ret
.iter()
.map(|r| {
r.split('/')
.map(|p| p.trim().replace(['"', '\''], ""))
.collect::<String>()
})
.collect();
};
}
// ここまで来た場合は要素がない場合なので空配列を返す
Nested::new()
ret.iter()
.map(|r| {
r.split('/')
.map(|p| p.trim().replace(['"', '\''], ""))
.collect::<String>()
})
.collect()
}

///MITRE ATTCKのTacticsの属性を持つルールに検知したコンピュータ名をhtml出力するための文字列をhtml_output_stockに追加する関数
Expand Down
2 changes: 2 additions & 0 deletions src/detections/detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@ impl Detection {
let detect_info = DetectInfo {
detected_time: time,
rulepath: CompactString::from(&rule.rulepath),
authors: CompactString::from(rule.yaml["author"].as_str().unwrap_or("-")),
ruleid: CompactString::from(rule.yaml["id"].as_str().unwrap_or("-")),
ruletitle: CompactString::from(rule.yaml["title"].as_str().unwrap_or("-")),
level: CompactString::from(
Expand Down Expand Up @@ -953,6 +954,7 @@ impl Detection {
let detect_info = DetectInfo {
detected_time: agg_result.start_timedate,
rulepath: CompactString::from(&rule.rulepath),
authors: CompactString::default(),
ruleid: CompactString::from(rule.yaml["id"].as_str().unwrap_or("-")),
ruletitle: CompactString::from(rule.yaml["title"].as_str().unwrap_or("-")),
level: CompactString::from(
Expand Down
1 change: 1 addition & 0 deletions src/detections/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use super::utils::remove_sp_char;
pub struct DetectInfo {
pub detected_time: DateTime<Utc>,
pub rulepath: CompactString,
pub authors: CompactString,
pub ruleid: CompactString,
pub ruletitle: CompactString,
pub level: CompactString,
Expand Down
10 changes: 9 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1439,7 +1439,15 @@ impl App {
|| stored_static.search_flag
|| stored_static.computer_metrics_flag
|| stored_static.metrics_flag;
if !unused_rules_option && rule_files.is_empty() {
if !unused_rules_option
&& rule_files.is_empty()
&& stored_static
.output_option
.as_ref()
.unwrap()
.rules
.is_some()
{
AlertMessage::alert(
"No rules were loaded. Please download the latest rules with the update-rules command.\r\n",
)
Expand Down

0 comments on commit 44df83b

Please sign in to comment.