Skip to content

Commit 8dccaa0

Browse files
Add date and maturity logic
1 parent ade05d1 commit 8dccaa0

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

detection_rules/config.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
ROOT_DIR = Path(__file__).parent.parent
2121
CUSTOM_RULES_DIR = os.getenv('CUSTOM_RULES_DIR', None)
22-
CUSTOM_RULES_DIR = "/home/forteea1/Code/dac_demo/detection-rules/demo"
2322

2423

2524
@dataclass

detection_rules/kbwrap.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import sys
99
from pathlib import Path
1010
from typing import Iterable, List, Optional
11+
from datetime import datetime
1112

1213
import click
1314

@@ -237,10 +238,30 @@ def kibana_export_rules(ctx: click.Context, directory: Path, action_connectors_d
237238
rule_resource["author"] = rule_resource.get("author") or default_author or [rule_resource.get("created_by")]
238239
if isinstance(rule_resource["author"], str):
239240
rule_resource["author"] = [rule_resource["author"]]
240-
contents = TOMLRuleContents.from_rule_resource(rule_resource, maturity="production")
241-
threat = contents.data.get("threat")
242-
first_tactic = threat[0].tactic.name if threat else ""
243-
rule_name = rulename_to_filename(contents.data.name, tactic_name=first_tactic)
241+
# NOTE we may want to remove the date logic, should the date match kibana or match rules repo?
242+
# Inherit maturity from the rule already exists
243+
maturity = "development"
244+
updated_date = None
245+
created_date = None
246+
threat = rule_resource.get("threat")
247+
first_tactic = threat[0].get("tactic").get("name") if threat else ""
248+
rule_name = rulename_to_filename(rule_resource.get("name"), tactic_name=first_tactic)
249+
# check if directory / f"{rule_name}" exists
250+
if (directory / f"{rule_name}").exists():
251+
rules = RuleCollection()
252+
rules.load_file(directory / f"{rule_name}")
253+
if rules:
254+
maturity = rules.rules[0].contents.metadata.maturity
255+
updated_date = datetime.strptime(rule_resource.get("updated_at"), "%Y-%m-%dT%H:%M:%S.%fZ").strftime(
256+
"%Y/%m/%d"
257+
)
258+
created_date = datetime.strptime(rule_resource.get("created_at"), "%Y-%m-%dT%H:%M:%S.%fZ").strftime(
259+
"%Y/%m/%d"
260+
)
261+
262+
contents = TOMLRuleContents.from_rule_resource(
263+
rule_resource, creation_date=created_date, updated_date=updated_date, maturity=maturity
264+
)
244265
rule = TOMLRule(contents=contents, path=directory / f"{rule_name}")
245266
except Exception as e:
246267
if skip_errors:

0 commit comments

Comments
 (0)