Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
938 lines (798 loc) · 35 KB
/
Copy pathmain.py
File metadata and controls
938 lines (798 loc) · 35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
# or more contributor license agreements. Licensed under the Elastic License
# 2.0; you may not use this file except in compliance with the Elastic License
# 2.0.
"""CLI commands for detection_rules."""
import dataclasses
import json
import os
import time
from collections.abc import Iterable
from datetime import UTC, datetime
from pathlib import Path
from typing import TYPE_CHECKING, Any, Literal, get_args
from uuid import uuid4
import click
import pytoml # type: ignore[reportMissingTypeStubs]
from marshmallow_dataclass import class_schema
from semver import Version
from .action_connector import (
TOMLActionConnector,
TOMLActionConnectorContents,
build_action_connector_objects,
parse_action_connector_results_from_api,
)
from .attack import build_threat_map_entry
from .cli_utils import multi_collection, rule_prompt
from .config import load_current_package_version, parse_rules_config
from .exception import TOMLException, TOMLExceptionContents, build_exception_objects, parse_exceptions_results_from_api
from .generic_loader import GenericCollection
from .misc import (
add_client,
nested_set,
parse_user_config,
raise_client_error,
)
from .rule import DeprecatedRule, QueryRuleData, TOMLRule, TOMLRuleContents
from .rule_formatter import toml_write
from .rule_loader import RawRuleCollection, RuleCollection, update_metadata_from_file
from .schemas import all_versions, definitions, get_incompatible_fields, get_schema_file
from .utils import (
Ndjson,
clear_caches,
get_etc_path,
get_path,
load_dump, # type: ignore[reportUnknownVariableType]
load_rule_contents,
rulename_to_filename,
)
if TYPE_CHECKING:
from elasticsearch import Elasticsearch
RULES_CONFIG = parse_rules_config()
RULES_DIRS = RULES_CONFIG.rule_dirs
@click.group(
"detection-rules",
context_settings={
"help_option_names": ["-h", "--help"],
"max_content_width": int(os.getenv("DR_CLI_MAX_WIDTH", 240)), # noqa: PLW1508
},
)
@click.option(
"--debug/--no-debug",
"-D/-N",
is_flag=True,
default=None,
help="Print full exception stacktrace on errors",
)
@click.pass_context
def root(ctx: click.Context, debug: bool) -> None:
"""Commands for detection-rules repository."""
debug = debug if debug else parse_user_config().get("debug")
ctx.obj = {"debug": debug, "rules_config": RULES_CONFIG}
if debug:
click.secho("DEBUG MODE ENABLED", fg="yellow")
@root.command("create-rule")
@click.argument("path", type=Path)
@click.option(
"--config", "-c", type=click.Path(exists=True, dir_okay=False, path_type=Path), help="Rule or config file"
)
@click.option("--required-only", is_flag=True, help="Only prompt for required fields")
@click.option(
"--rule-type", "-t", type=click.Choice(sorted(TOMLRuleContents.all_rule_types())), help="Type of rule to create"
)
def create_rule(path: Path, config: Path, required_only: bool, rule_type: str): # noqa: ANN201
"""Create a detection rule."""
contents: dict[str, Any] = load_rule_contents(config, single_only=True)[0] if config else {}
return rule_prompt(path, rule_type=rule_type, required_only=required_only, save=True, **contents)
@root.command("generate-rules-index")
@click.option("--query", "-q", help="Optional KQL query to limit to specific rules")
@click.option("--overwrite", is_flag=True, help="Overwrite files in an existing folder")
@click.pass_context
def generate_rules_index(
ctx: click.Context,
query: str,
overwrite: bool,
save_files: bool = True,
) -> tuple[Ndjson, Ndjson]:
"""Generate enriched indexes of rules, based on a KQL search, for indexing/importing into elasticsearch/kibana."""
from .packaging import Package
if query:
rule_paths = [r["file"] for r in ctx.invoke(search_rules, query=query, verbose=False)]
rules = RuleCollection()
rules.load_files(Path(p) for p in rule_paths)
else:
rules = RuleCollection.default()
rule_count = len(rules)
package = Package(rules, name=load_current_package_version(), verbose=False)
package_hash = package.get_package_hash()
bulk_upload_docs, importable_rules_docs = package.create_bulk_index_body()
if save_files:
path = get_path(["enriched-rule-indexes", package_hash])
path.mkdir(parents=True, exist_ok=overwrite)
bulk_upload_docs.dump(path.joinpath("enriched-rules-index-uploadable.ndjson"), sort_keys=True)
importable_rules_docs.dump(path.joinpath("enriched-rules-index-importable.ndjson"), sort_keys=True)
click.echo(f"files saved to: {path}")
click.echo(f"{rule_count} rules included")
return bulk_upload_docs, importable_rules_docs
@root.command("import-rules-to-repo")
@click.argument("input-file", type=click.Path(dir_okay=False, exists=True, path_type=Path), nargs=-1, required=False)
@click.option("--action-connector-import", "-ac", is_flag=True, help="Include action connectors in export")
@click.option("--exceptions-import", "-e", is_flag=True, help="Include exceptions in export")
@click.option("--required-only", is_flag=True, help="Only prompt for required fields")
@click.option(
"--directory",
"-d",
type=click.Path(file_okay=False, exists=True, path_type=Path),
help="Load files from a directory",
)
@click.option(
"--save-directory",
"-s",
type=click.Path(file_okay=False, exists=True, path_type=Path),
help="Save imported rules to a directory",
)
@click.option(
"--exceptions-directory",
"-se",
type=click.Path(file_okay=False, exists=True, path_type=Path),
help="Save imported exceptions to a directory",
)
@click.option(
"--action-connectors-directory",
"-sa",
type=click.Path(file_okay=False, exists=True, path_type=Path),
help="Save imported actions to a directory",
)
@click.option("--skip-errors", "-ske", is_flag=True, help="Skip rule import errors")
@click.option("--default-author", "-da", type=str, required=False, help="Default author for rules missing one")
@click.option("--strip-none-values", "-snv", is_flag=True, help="Strip None values from the rule")
@click.option("--local-creation-date", "-lc", is_flag=True, help="Preserve the local creation date of the rule")
@click.option("--local-updated-date", "-lu", is_flag=True, help="Preserve the local updated date of the rule")
@click.option("--dates-import", "-di", is_flag=True, help="Parse created_at and updated_at from the rule content")
@click.option(
"--use-existing-rule-dirs",
"--load-rule-loading",
"-lr",
"use_existing_rule_dirs",
is_flag=True,
help=(
"Enable arbitrary rule loading from the rules directories (Can be very slow!). "
"This option was previously named --load-rule-loading; that name is kept as an alias "
"for backwards compatibility."
),
)
def import_rules_into_repo( # noqa: PLR0912, PLR0913, PLR0915, PLR0917
input_file: tuple[Path, ...] | None,
required_only: bool,
action_connector_import: bool,
exceptions_import: bool,
directory: Path | None,
save_directory: Path,
action_connectors_directory: Path | None,
exceptions_directory: Path | None,
skip_errors: bool,
default_author: str,
strip_none_values: bool,
local_creation_date: bool,
local_updated_date: bool,
dates_import: bool,
use_existing_rule_dirs: bool,
) -> None:
"""Import rules from json, toml, or yaml files containing Kibana exported rule(s)."""
errors: list[str] = []
if dates_import and (local_creation_date or local_updated_date):
click.echo("Error: --dates-import cannot be used with --local-creation-date or --local-updated-date.")
return
rule_files: list[Path] = []
if directory:
rule_files = list(directory.glob("**/*.*"))
if input_file:
rule_files = sorted({*rule_files, *input_file})
file_contents: list[Any] = []
for rule_file in rule_files:
file_contents.extend(load_rule_contents(Path(rule_file)))
if not file_contents:
click.echo("Must specify at least one file!")
raw_rule_collection = RawRuleCollection()
if use_existing_rule_dirs:
raw_rule_collection = raw_rule_collection.default()
exceptions_containers = {}
exceptions_items = {}
exceptions_containers, exceptions_items, _, unparsed_results = parse_exceptions_results_from_api(file_contents)
action_connectors, unparsed_results = parse_action_connector_results_from_api(unparsed_results)
file_contents = unparsed_results
exception_list_rule_table: dict[str, Any] = {}
action_connector_rule_table: dict[str, Any] = {}
rule_count = 0
for contents in file_contents:
# Don't load exceptions as rules
if contents.get("type") not in get_args(definitions.RuleType):
click.echo(f"Skipping - {contents.get('type')} is not a supported rule type")
continue
base_path = contents.get("name") or contents.get("rule", {}).get("name")
base_path = rulename_to_filename(base_path) if base_path else base_path
if base_path is None:
raise ValueError(f"Invalid rule file, please ensure the rule has a name field: {contents}")
rule_base_path = Path(save_directory or RULES_DIRS[0])
rule_path = rule_base_path / base_path
rule_id = contents.get("rule_id")
if rule_id in raw_rule_collection.id_map:
rule_path = raw_rule_collection.id_map[rule_id].path or rule_path
# handle both rule json formats loaded from kibana and toml
data_view_id = contents.get("data_view_id") or contents.get("rule", {}).get("data_view_id")
additional = ["index"] if not data_view_id else ["data_view_id"]
# Use additional to store all available fields for the rule
additional += [key for key in contents if key not in additional and contents.get(key) is not None]
# use default author if not provided
contents["author"] = contents.get("author") or default_author or [contents.get("created_by")]
if isinstance(contents["author"], str):
contents["author"] = [contents["author"]]
# Parse created_at and updated_at to creation_date and updated_date if they exist in contents
if dates_import:
now = datetime.now(UTC).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
contents["creation_date"] = (
datetime.strptime(contents.get("created_at", now), "%Y-%m-%dT%H:%M:%S.%fZ")
.replace(tzinfo=UTC)
.strftime("%Y/%m/%d")
)
contents["updated_date"] = (
datetime.strptime(contents.get("updated_at", now), "%Y-%m-%dT%H:%M:%S.%fZ")
.replace(tzinfo=UTC)
.strftime("%Y/%m/%d")
)
contents.update(
update_metadata_from_file(
rule_path, {"creation_date": local_creation_date, "updated_date": local_updated_date}
)
)
output = rule_prompt(
rule_path,
required_only=required_only,
save=True,
verbose=True,
additional_required=additional,
skip_errors=skip_errors,
strip_none_values=strip_none_values,
**contents,
)
# If output is not a TOMLRule
if isinstance(output, str):
errors.append(output)
else:
rule_count += 1
if contents.get("exceptions_list"):
# For each item in rule.contents.data.exceptions_list to the exception_list_rule_table under the list_id
for exception in contents["exceptions_list"]:
exception_id = exception["list_id"]
if exception_id not in exception_list_rule_table:
exception_list_rule_table[exception_id] = []
exception_list_rule_table[exception_id].append(
{
"id": contents.get("rule_id"),
"name": contents["name"],
}
)
if contents.get("actions"):
# If rule has actions with connectors, add them to the action_connector_rule_table under the action_id
for action in contents["actions"]:
action_id = action["id"]
if action_id not in action_connector_rule_table:
action_connector_rule_table[action_id] = []
action_connector_rule_table[action_id].append(
{
"id": contents.get("rule_id"),
"name": contents["name"],
}
)
# Build TOMLException Objects
if exceptions_import:
_, e_output, e_errors = build_exception_objects(
exceptions_containers,
exceptions_items,
exception_list_rule_table,
exceptions_directory,
save_toml=True,
skip_errors=skip_errors,
verbose=True,
)
for line in e_output:
click.echo(line)
errors.extend(e_errors)
# Build TOMLActionConnector Objects
if action_connector_import:
_, ac_output, ac_errors = build_action_connector_objects(
action_connectors,
action_connector_rule_table,
action_connectors_directory,
save_toml=True,
skip_errors=skip_errors,
verbose=True,
)
for line in ac_output:
click.echo(line)
errors.extend(ac_errors)
exceptions_count = 0 if not exceptions_import else len(exceptions_containers) + len(exceptions_items)
click.echo(f"{rule_count + exceptions_count + len(action_connectors)} results exported")
click.echo(f"{rule_count} rules converted")
click.echo(f"{exceptions_count} exceptions exported")
click.echo(f"{len(action_connectors)} actions connectors exported")
if errors:
_dir = save_directory if save_directory else RULES_DIRS[0]
err_file = _dir / "_errors.txt"
_ = err_file.write_text("\n".join(errors))
click.echo(f"{len(errors)} errors saved to {err_file}")
@root.command("build-limited-rules")
@click.option(
"--stack-version",
type=click.Choice(all_versions()),
required=True,
help="Version to downgrade to be compatible with the older instance of Kibana",
)
@click.option("--output-file", "-o", type=click.Path(dir_okay=False, exists=False), required=True)
def build_limited_rules(stack_version: str, output_file: str) -> None:
"""
Import rules from json, toml, or Kibana exported rule file(s),
filter out unsupported ones, and write to output NDJSON file.
"""
# Schema generation and incompatible fields detection
query_rule_data = class_schema(QueryRuleData)()
fields = getattr(query_rule_data, "fields", {})
incompatible_fields = get_incompatible_fields(
list(fields.values()), Version.parse(stack_version, optional_minor_and_patch=True)
)
# Load all rules
rules = RuleCollection.default()
# Define output path
output_path = Path(output_file)
# Define ndjson instance for output
ndjson_output = Ndjson()
# Get API schema for rule type
api_schema = get_schema_file(stack_version, "base")["properties"]["type"]["enum"]
# Function to process each rule
def process_rule(rule: TOMLRule, incompatible_fields: list[str]) -> dict[str, Any] | None:
if rule.contents.type not in api_schema:
click.secho(
f"{rule.contents.name} - Skipping unsupported rule type: {rule.contents.get('type')}", fg="yellow"
)
return None
# Remove unsupported fields from rule
rule_contents = rule.contents.to_api_format()
for field in incompatible_fields:
rule_contents.pop(field, None)
return rule_contents
# Process each rule and add to ndjson_output
for rule in rules.rules:
processed_rule = process_rule(rule, incompatible_fields)
if processed_rule is not None:
ndjson_output.append(processed_rule)
# Write ndjson_output to file
ndjson_output.dump(output_path)
click.echo(f"Success: Rules written to {output_file}")
@root.command("toml-lint")
@click.option(
"--rule-file",
"-f",
multiple=True,
type=click.Path(exists=True, path_type=Path),
help="Specify one or more rule files.",
)
def toml_lint(rule_file: list[Path]) -> None:
"""Cleanup files with some simple toml formatting."""
if rule_file:
rules = RuleCollection()
rules.load_files(Path(p) for p in rule_file)
else:
rules = RuleCollection.default()
# re-save the rules to force TOML reformatting
for rule in rules:
rule.save_toml()
click.echo("TOML file linting complete")
@root.command("mass-update")
@click.argument("query")
@click.option("--metadata", "-m", is_flag=True, help="Make an update to the rule metadata rather than contents.")
@click.option("--language", type=click.Choice(["eql", "kql"]), default="kql")
@click.option(
"--field",
type=(str, str),
multiple=True,
help="Use rule-search to retrieve a subset of rules and modify values "
"(ex: --field management.ecs_version 1.1.1).\n"
"Note this is limited to string fields only. Nested fields should use dot notation.",
)
@click.pass_context
def mass_update(
ctx: click.Context,
query: str,
metadata: bool,
language: Literal["eql", "kql"],
field: tuple[str, str],
) -> Any:
"""Update multiple rules based on eql results."""
rules = RuleCollection().default()
results = ctx.invoke(search_rules, query=query, language=language, verbose=False)
matching_ids = {r["rule_id"] for r in results}
rules = rules.filter(lambda r: r.id in matching_ids)
for rule in rules:
for key, value in field:
nested_set(rule.metadata if metadata else rule.contents, key, value) # type: ignore[reportAttributeAccessIssue]
rule.validate(as_rule=True) # type: ignore[reportAttributeAccessIssue]
rule.save(as_rule=True) # type: ignore[reportAttributeAccessIssue]
return ctx.invoke(
search_rules,
query=query,
language=language,
columns=["rule_id", "name"] + [k[0].split(".")[-1] for k in field],
)
@root.command("view-rule")
@click.argument("rule-file", type=Path)
@click.option("--api-format/--rule-format", default=True, help="Print the rule in final api or rule format")
@click.pass_context
def view_rule(_: click.Context, rule_file: Path, api_format: str) -> TOMLRule | DeprecatedRule:
"""View an internal rule or specified rule file."""
rule = RuleCollection().load_file(rule_file)
if api_format:
click.echo(json.dumps(rule.contents.to_api_format(), indent=2, sort_keys=True))
else:
click.echo(toml_write(rule.contents.to_dict())) # type: ignore[reportAttributeAccessIssue]
return rule
def _export_rules_as_yaml( # noqa: PLR0913, PLR0917
rules: RuleCollection,
yaml_directory: Path,
downgrade_version: definitions.SemVer | None = None,
verbose: bool = True,
skip_unsupported: bool = False,
include_metadata: bool = False,
include_action_connectors: bool = False,
include_exceptions: bool = False,
) -> None:
"""Export rules and exceptions into a directory of YAML files."""
from .rule import downgrade_contents_from_rule
unsupported: list[str] = []
for rule in rules:
contents_override = None
if downgrade_version:
try:
contents_override = downgrade_contents_from_rule(
rule, downgrade_version, include_metadata=include_metadata
)
except ValueError as e:
if skip_unsupported:
unsupported.append(f"{e}: {rule.id} - {rule.name}")
continue
raise
rule_path = yaml_directory / rulename_to_filename(rule.name)
rule_path.parent.mkdir(parents=True, exist_ok=True)
rule.save_yaml(rule_path, contents_override=contents_override)
export_types: list[type[Any]] = []
if include_exceptions:
export_types.append(TOMLException)
if include_action_connectors:
export_types.append(TOMLActionConnector)
if export_types:
cl = GenericCollection.default()
for d in cl.items:
if any(isinstance(d, export_type) for export_type in export_types):
save_yaml = getattr(d, "save_yaml", None)
if callable(save_yaml) and isinstance(d.path, Path):
_ = save_yaml(yaml_directory / d.path.name)
if verbose:
click.echo(f"Exported {len(rules) - len(unsupported)} rules into {yaml_directory}")
if skip_unsupported and unsupported:
unsupported_str = "\n- ".join(unsupported)
click.echo(f"Skipped {len(unsupported)} unsupported rules: \n- {unsupported_str}")
def _export_rules( # noqa: PLR0913, PLR0917
rules: RuleCollection,
outfile: Path,
downgrade_version: definitions.SemVer | None = None,
verbose: bool = True,
skip_unsupported: bool = False,
include_metadata: bool = False,
include_action_connectors: bool = False,
include_exceptions: bool = False,
) -> None:
"""Export rules and exceptions into a consolidated ndjson file."""
from .rule import downgrade_contents_from_rule
outfile = outfile.with_suffix(".ndjson")
unsupported: list[str] = []
output_lines: list[str] = []
if downgrade_version:
for rule in rules:
try:
output_lines.append(
json.dumps(
downgrade_contents_from_rule(rule, downgrade_version, include_metadata=include_metadata),
sort_keys=True,
)
)
except ValueError as e:
if skip_unsupported:
unsupported.append(f"{e}: {rule.id} - {rule.name}")
else:
raise
else:
output_lines = [
json.dumps(r.contents.to_api_format(include_metadata=include_metadata), sort_keys=True) for r in rules
]
# Add exceptions to api format here and add to output_lines
if include_exceptions or include_action_connectors:
cl = GenericCollection.default()
rule_ids = {r.id for r in rules}
# Get exceptions in API format (only those linked to the exported rules)
if include_exceptions:
exceptions_raw: list[list[dict[str, Any]]] = [
d.contents.to_api_format() # type: ignore[reportAttributeAccessIssue, reportUnknownMemberType]
for d in cl.items_matching(TOMLExceptionContents, rule_ids)
]
exceptions: list[dict[str, Any]] = [e for sublist in exceptions_raw for e in sublist]
output_lines.extend(json.dumps(e, sort_keys=True) for e in exceptions)
if include_action_connectors:
action_connectors: list[list[dict[str, Any]]] = [
d.contents.to_api_format() # type: ignore[reportAttributeAccessIssue, reportUnknownMemberType]
for d in cl.items_matching(TOMLActionConnectorContents, rule_ids)
]
actions: list[dict[str, Any]] = [a for sublist in action_connectors for a in sublist]
output_lines.extend(json.dumps(a, sort_keys=True) for a in actions)
_ = outfile.write_text("\n".join(output_lines) + "\n")
if verbose:
click.echo(f"Exported {len(rules) - len(unsupported)} rules into {outfile}")
if skip_unsupported and unsupported:
unsupported_str = "\n- ".join(unsupported)
click.echo(f"Skipped {len(unsupported)} unsupported rules: \n- {unsupported_str}")
@root.command("export-rules-from-repo")
@multi_collection
@click.option(
"--outfile",
"-o",
default=Path(get_path(["exports", f"{time.strftime('%Y%m%dT%H%M%SL')}.ndjson"])),
type=Path,
help="Name of file for exported rules",
)
@click.option("--replace-id", "-r", is_flag=True, help="Replace rule IDs with new IDs before export")
@click.option(
"--stack-version",
type=click.Choice(all_versions()),
help="Downgrade a rule version to be compatible with older instances of Kibana",
)
@click.option(
"--skip-unsupported",
"-s",
is_flag=True,
help="If `--stack-version` is passed, skip rule types which are unsupported (an error will be raised otherwise)",
)
@click.option("--include-metadata", type=bool, is_flag=True, default=False, help="Add metadata to the exported rules")
@click.option(
"--include-action-connectors",
"-ac",
type=bool,
is_flag=True,
default=False,
help="Include Action Connectors in export",
)
@click.option(
"--include-exceptions", "-e", type=bool, is_flag=True, default=False, help="Include Exceptions Lists in export"
)
@click.option(
"--save-yaml-dir",
"-syd",
type=Path,
required=False,
help="Optional directory to export individual YAML files instead of NDJSON",
)
def export_rules_from_repo( # noqa: PLR0913, PLR0917
rules: RuleCollection,
outfile: Path,
replace_id: bool,
stack_version: str,
skip_unsupported: bool,
include_metadata: bool,
include_action_connectors: bool,
include_exceptions: bool,
save_yaml_dir: Path | None,
) -> RuleCollection:
"""Export rule(s) and exception(s) into an importable ndjson file."""
if len(rules) == 0:
raise ValueError("No rules found")
if replace_id:
# if we need to replace the id, take each rule object and create a copy
# of it, with only the rule_id field changed
old_rules = rules
rules = RuleCollection()
for rule in old_rules:
new_data = dataclasses.replace(rule.contents.data, rule_id=str(uuid4()))
new_contents = dataclasses.replace(rule.contents, data=new_data)
rules.add_rule(TOMLRule(contents=new_contents))
if save_yaml_dir:
save_yaml_dir.mkdir(parents=True, exist_ok=True)
_export_rules_as_yaml(
rules=rules,
yaml_directory=save_yaml_dir,
downgrade_version=stack_version,
skip_unsupported=skip_unsupported,
include_metadata=include_metadata,
include_action_connectors=include_action_connectors,
include_exceptions=include_exceptions,
)
else:
outfile.parent.mkdir(exist_ok=True)
_export_rules(
rules=rules,
outfile=outfile,
downgrade_version=stack_version,
skip_unsupported=skip_unsupported,
include_metadata=include_metadata,
include_action_connectors=include_action_connectors,
include_exceptions=include_exceptions,
)
return rules
@root.command("validate-rule")
@click.argument("path")
@click.pass_context
def validate_rule(_: click.Context, path: str) -> TOMLRule | DeprecatedRule:
"""Check if a rule staged in rules dir validates against a schema."""
rule = RuleCollection().load_file(Path(path))
click.echo("Rule validation successful")
return rule
@root.command("validate-all")
def validate_all() -> None:
"""Check if all rules validates against a schema."""
_ = RuleCollection.default()
click.echo("Rule validation successful")
@root.command("rule-search")
@click.argument("query", required=False)
@click.option("--columns", "-c", multiple=True, help="Specify columns to add the table")
@click.option("--language", type=click.Choice(["eql", "kql"]), default="kql")
@click.option("--count", is_flag=True, help="Return a count rather than table")
def search_rules( # noqa: PLR0913, PLR0917
query: str | None,
columns: list[str],
language: Literal["eql", "kql"],
count: bool,
verbose: bool = True,
rules: dict[str, TOMLRule] | None = None,
pager: bool = False,
) -> list[dict[str, Any]]:
"""Use KQL or EQL to find matching rules."""
from eql import parse_query # type: ignore[reportMissingTypeStubs]
from eql.build import get_engine # type: ignore[reportMissingTypeStubs]
from eql.pipes import CountPipe # type: ignore[reportMissingTypeStubs]
from eql.table import Table # type: ignore[reportMissingTypeStubs]
from kql import get_evaluator # type: ignore[reportMissingTypeStubs]
from .rule import get_unique_query_fields
flattened_rules: list[dict[str, Any]] = []
rules = rules or {str(rule.path): rule for rule in RuleCollection.default()}
for file_name, rule in rules.items():
flat: dict[str, Any] = {"file": os.path.relpath(file_name)}
flat.update(rule.contents.to_dict())
flat.update(flat["metadata"])
flat.update(flat["rule"])
tactic_names: list[str] = []
technique_ids: list[str] = []
subtechnique_ids: list[str] = []
for entry in flat["rule"].get("threat", []):
if entry["framework"] != "MITRE ATT&CK":
continue
techniques = entry.get("technique", [])
tactic_names.append(entry["tactic"]["name"])
technique_ids.extend([t["id"] for t in techniques])
subtechnique_ids.extend([st["id"] for t in techniques for st in t.get("subtechnique", [])])
flat.update(
techniques=technique_ids,
tactics=tactic_names,
subtechniques=subtechnique_ids,
unique_fields=get_unique_query_fields(rule),
)
flattened_rules.append(flat)
flattened_rules.sort(key=lambda dct: dct["name"])
filtered: list[dict[str, Any]] = []
if language == "kql":
evaluator = get_evaluator(query) if query else lambda _: True # type: ignore[reportUnknownLambdaType]
filtered = list(filter(evaluator, flattened_rules)) # type: ignore[reportCallIssue]
elif language == "eql":
parsed = parse_query(query, implied_any=True, implied_base=True) # type: ignore[reportUnknownVariableType]
evaluator = get_engine(parsed) # type: ignore[reportUnknownVariableType]
filtered = [result.events[0].data for result in evaluator(flattened_rules)] # type: ignore[reportUnknownVariableType]
if not columns and any(isinstance(pipe, CountPipe) for pipe in parsed.pipes): # type: ignore[reportAttributeAccessIssue]
columns = ["key", "count", "percent"]
if count:
click.echo(f"{len(filtered)} rules")
return filtered
columns = ",".join(columns).split(",") if columns else ["rule_id", "file", "name"]
table: Table = Table.from_list(columns, filtered) # type: ignore[reportUnknownMemberType]
if verbose:
click.echo_via_pager(table) if pager else click.echo(table)
return filtered
@root.command("build-threat-map-entry")
@click.argument("tactic")
@click.argument("technique-ids", nargs=-1)
def build_threat_map(tactic: str, technique_ids: Iterable[str]) -> dict[str, Any]:
"""Build a threat map entry."""
entry = build_threat_map_entry(tactic, *technique_ids)
rendered = pytoml.dumps({"rule": {"threat": [entry]}}) # type: ignore[reportUnknownMemberType]
# strip out [rule]
cleaned = "\n".join(rendered.splitlines()[2:])
print(cleaned)
return entry
@root.command("test")
@click.pass_context
def test_rules(ctx: click.Context) -> None:
"""Run unit tests over all of the rules."""
import pytest
rules_config = ctx.obj["rules_config"]
test_config = rules_config.test_config
tests, skipped = test_config.get_test_names(formatted=True)
if skipped:
click.echo(f"Tests skipped per config ({len(skipped)}):")
click.echo("\n".join(skipped))
clear_caches()
if tests:
ctx.exit(pytest.main(["-v", *tests]))
else:
click.echo("No tests found to execute!")
@root.group("typosquat")
def typosquat_group() -> None:
"""Commands for generating typosquat detections."""
@typosquat_group.command("create-dnstwist-index")
@click.argument("input-file", type=click.Path(exists=True, dir_okay=False), required=True)
@click.pass_context
@add_client(["elasticsearch"], add_func_arg=False)
def create_dnstwist_index(ctx: click.Context, input_file: click.Path) -> None:
"""Create a dnstwist index in Elasticsearch to work with a threat match rule."""
es_client: Elasticsearch = ctx.obj["es"]
click.echo(f"Attempting to load dnstwist data from {input_file}")
dnstwist_data: list[dict[str, Any]] = load_dump(str(input_file)) # type: ignore[reportAssignmentType]
click.echo(f"{len(dnstwist_data)} records loaded")
original_domain = next(r["domain-name"] for r in dnstwist_data if r.get("fuzzer", "") == "original*") # type: ignore[reportAttributeAccessIssue]
click.echo(f"Original domain name identified: {original_domain}")
domain = original_domain.split(".")[0]
domain_index = f"dnstwist-{domain}"
# If index already exists, prompt user to confirm if they want to overwrite
if es_client.indices.exists(index=domain_index) and click.confirm(
f"dnstwist index: {domain_index} already exists for {original_domain}. Do you want to overwrite?",
abort=True,
):
_ = es_client.indices.delete(index=domain_index)
fields = [
"dns-a",
"dns-aaaa",
"dns-mx",
"dns-ns",
"banner-http",
"fuzzer",
"original-domain",
"dns.question.registered_domain",
]
timestamp_field = "@timestamp"
mappings = {"mappings": {"properties": {f: {"type": "keyword"} for f in fields}}}
mappings["mappings"]["properties"][timestamp_field] = {"type": "date"}
_ = es_client.indices.create(index=domain_index, body=mappings)
# handle dns.question.registered_domain separately
_ = fields.pop()
es_updates: list[dict[str, Any]] = []
now = datetime.now(UTC)
for item in dnstwist_data:
if item["fuzzer"] == "original*":
continue
record = item.copy()
record.setdefault("dns", {}).setdefault("question", {}).setdefault("registered_domain", item.get("domain-name"))
for field in fields:
_ = record.setdefault(field, None)
record["@timestamp"] = now
es_updates.extend([{"create": {"_index": domain_index}}, record])
click.echo(f"Indexing data for domain {original_domain}")
results = es_client.bulk(body=es_updates)
if results["errors"]:
error = {r["create"]["result"] for r in results["items"] if r["create"]["status"] != 201} # noqa: PLR2004
raise_client_error(f"Errors occurred during indexing:\n{error}")
click.echo(f"{len(results['items'])} watchlist domains added to index")
click.echo("Run `prep-rule` and import to Kibana to create alerts on this index")
@typosquat_group.command("prep-rule")
@click.argument("author")
def prep_rule(author: str) -> None:
"""Prep the detection threat match rule for dnstwist data with a rule_id and author."""
rule_template_file = get_etc_path(["rule_template_typosquatting_domain.json"])
template_rule = json.loads(rule_template_file.read_text())
template_rule.update(author=[author], rule_id=str(uuid4()))
updated_rule = get_path(["rule_typosquatting_domain.ndjson"])
_ = updated_rule.write_text(json.dumps(template_rule, sort_keys=True))
click.echo(f"Rule saved to: {updated_rule}. Import this to Kibana to create alerts on all dnstwist-* indexes")
click.echo("Note: you only need to import and enable this rule one time for all dnstwist-* indexes")