8000 chore: give back list of promoted rules by ariel-anieli · Pull Request #5420 · SigmaHQ/sigma · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

chore: give back list of promoted rules #5420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
8000 Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions .github/workflows/sigma-rule-promoter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,19 @@ jobs:
uses: actions/setup-python@v4.5.0
with:
python-version: 3.11
- name: Install dependencies
run: pip install --force-reinstall pySigma; pip install --force-reinstall sigma-cli
- name: Install a compatible backend
run: |
BACKEND=`sigma plugin list -t backend -s stable | awk '/\| yes / {print $2}' | xargs shuf -n1 -e`
sigma plugin install -f $BACKEND
- name: Execute Rule Promoter Script
run: python tests/promote_rules_status.py | tee --append promoted_rules
- name: Convert artifacts
run: |
pip install pySigma
python tests/promote_rules_status.py
TARGET=`sigma list targets | awk '!/Identifier/ && /\| [a-zA-Z]/ {print $2}' | xargs shuf -n1 -e`
PIPELINE=`sigma list pipelines | awk '!/Identifier/ && /\| [a-zA-Z]/ {print $2}' | xargs shuf -n1 -e`
cat promoted_rules | xargs -I {} sh -c "echo 'RULE {}, TARGET $TARGET, PIPELINE $PIPELINE'; sigma convert -t $TARGET --pipeline $PIPELINE {}"
- name: Create Pull Request
uses: peter-evans/create-pull-request@v5
with:
Expand Down
49 changes: 26 additions & 23 deletions tests/promote_rules_status.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,46 @@
from datetime import datetime
from sigma.collection import SigmaCollection
from typing import Iterator

path_to_rules = [
PATH_TO_RULES = [
"rules",
"rules-emerging-threats",
"rules-placeholder",
"rules-threat-hunting",
"rules-compliance",
]
nb_days = 300

NB_DAYS = 300

def get_rules_to_promote():
today = datetime.now().date()
rules_to_promote = []

rule_paths = SigmaCollection.resolve_paths(path_to_rules)
def is_experimental_and_older_than_ref(sigmaHQrule: "sigma.rule.SigmaRule") -> bool:
last_update = sigmaHQrule.modified if sigmaHQrule.modified else sigmaHQrule.date

return (
str(sigmaHQrule.status) == "experimental"
and (datetime.now().date() - last_update).days >= NB_DAYS
)


def get_rules_to_promote() -> Iterator[str]:
rule_paths = SigmaCollection.resolve_paths(PATH_TO_RULES)
rule_collection = SigmaCollection.load_ruleset(rule_paths, collect_errors=True)
for sigmaHQrule in rule_collection:
if str(sigmaHQrule.status) == "experimental":
last_update = (
sigmaHQrule.modified if sigmaHQrule.modified else sigmaHQrule.date
)
difference = (today - last_update).days
if difference >= nb_days:
rules_to_promote.append(sigmaHQrule.source.path)

return rules_to_promote
return (
rule.source.path
for rule in filter(is_experimental_and_older_than_ref, rule_collection)
)


def promote_rule(rule: str) -> str:
with open(rule, "r", encoding="utf8") as f:
data = f.read().replace("\nstatus: experimental", "\nstatus: test")

def promote_rules(rules_to_promote):
for file_ in rules_to_promote:
with open(file_, "r", encoding="utf8") as f:
data = f.read().replace("\nstatus: experimental", "\nstatus: test")
with open(rule, "w", encoding="utf8") as f:
f.write(data)

with open(file_, "w", encoding="utf8") as f:
f.write(data)
return rule


if __name__ == "__main__":
rules_to_promote = get_rules_to_promote()
promote_rules(rules_to_promote)
print("\n".join(str(promote_rule(rule)) for rule in get_rules_to_promote()))
0