Skip to content
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

[Rule Tuning] Tighten Up Elastic Defend Indexes - Linux #4446

Merged
merged 2 commits into from
Feb 5, 2025
Merged

Conversation

w0rk3r
Copy link
Contributor

@w0rk3r w0rk3r commented Feb 4, 2025

Issues

Resolves #4430

Summary

This PR tightens the index patterns related to Elastic Defend to improve performance and solve issues with exceptions like this.

Here is the code used to generate the changes:

Expand me for a very bad-looking script
import os
import pickle
import tomlkit
import eql

# Pickle file just speeds up testing. You can modify this to work with detection-rules RuleCollection()
# rules = RuleCollection().default()

with open(os.path.join('pickles', 'rules.pkl'), 'rb') as prod:
    rules = pickle.load(prod)

def adjust_rule_index(file_path, index_arr):
    with open(file_path, 'r', encoding='utf-8') as h:
        toml_data = tomlkit.parse(h.read())
    
    toml_data['rule']['index'] = index_arr
    toml_data['metadata']['updated_date'] = "2025/02/04"

    with open(file_path, 'w', encoding='utf-8') as file:
        tomlkit.dump(toml_data, file)

def return_category(rules):
    categories = set()

    if not isinstance(rules, list):
        rules = [rules]

    for rule in rules:

        for instance in rule.contents.data.ast:
            if isinstance(instance, eql.ast.EventQuery):
                category = str(instance).split()[0]
                if category == "any":
                    return "manual_check"
                else:
                    categories.add(category)
    return categories

def return_metadata(rules):
    if not isinstance(rules, list):
        rules = [rules]
    return {
        rule.contents.data.name: {
            "path": rule.path,
            "index": rule.contents.data.index,
            "uuid": rule.contents.data.rule_id
        }
        for rule in rules
    }

def return_eql_rules(rules, os):
    return [
        rule
        for rule in rules
        if rule.contents.data.type == "eql" and rule.path.parent.name == os
    ]

rules = return_eql_rules(rules, "linux")
metadata = return_metadata(rules)

for rule in rules:
    rule_name = rule.contents.data.name
    indexes = metadata[rule_name]["index"]
    path = metadata[rule_name]["path"]
    categories = return_category(rule)

    if any(index.startswith("logs-endpoint.events.*") for index in indexes) and categories != "manual_check":
        #print(f"\n* {rule_name}")
        indexes.remove("logs-endpoint.events.*")
        for category in categories:
            indexes.append(f"logs-endpoint.events.{category}*")
        indexes = sorted(indexes)
        
        adjust_rule_index(path, indexes)

Copy link
Contributor

github-actions bot commented Feb 4, 2025

Rule: Tuning - Guidelines

These guidelines serve as a reminder set of considerations when tuning an existing rule.

Documentation and Context

  • Detailed description of the suggested changes.
  • Provide example JSON data or screenshots.
  • Provide evidence of reducing benign events mistakenly identified as threats (False Positives).
  • Provide evidence of enhancing detection of true threats that were previously missed (False Negatives).
  • Provide evidence of optimizing resource consumption and execution time of detection rules (Performance).
  • Provide evidence of specific environment factors influencing customized rule tuning (Contextual Tuning).
  • Provide evidence of improvements made by modifying sensitivity by changing alert triggering thresholds (Threshold Adjustments).
  • Provide evidence of refining rules to better detect deviations from typical behavior (Behavioral Tuning).
  • Provide evidence of improvements of adjusting rules based on time-based patterns (Temporal Tuning).
  • Provide reasoning of adjusting priority or severity levels of alerts (Severity Tuning).
  • Provide evidence of improving quality integrity of our data used by detection rules (Data Quality).
  • Ensure the tuning includes necessary updates to the release documentation and versioning.

Rule Metadata Checks

  • updated_date matches the date of tuning PR merged.
  • min_stack_version should support the widest stack versions.
  • name and description should be descriptive and not include typos.
  • query should be inclusive, not overly exclusive. Review to ensure the original intent of the rule is maintained.

Testing and Validation

  • Validate that the tuned rule's performance is satisfactory and does not negatively impact the stack.
  • Ensure that the tuned rule has a low false positive rate.

Copy link
Contributor

@Aegrah Aegrah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work. Thanks a lot for running this to completion!

@w0rk3r w0rk3r merged commit 0268daa into main Feb 5, 2025
9 checks passed
@w0rk3r w0rk3r deleted the lnx_index_rt branch February 5, 2025 18:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Rule Tuning] Linux Index Patterns
3 participants