-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsigma-lookup.py
170 lines (144 loc) · 6.5 KB
/
sigma-lookup.py
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
import os
import yaml
import re
import argparse
import json
import sys
import yaml
from rich.console import Console
from rich.syntax import Syntax
from rich.table import Table
from rich.text import Text
from src.parser import sigma_parser
if __name__ == "__main__":
console = Console()
parsed_file = 'cache/cache.json'
if not os.path.isfile(parsed_file):
console.print("> [orange3]Cache file is missing. Attempting to create a new cache...")
os.makedirs('/'.join(parsed_file.split('/')[:-1]), exist_ok=True)
if sigma_parser(('sigma/rules', 'sigma/rules-threat-hunting', 'sigma/rules-emerging-threats'), parsed_file):
console.print("> [green]Cache file created.")
with open(parsed_file, 'r') as fd:
rules = json.load(fd)
products = set( r['product'] for _, r in rules.items() if r['product'])
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--id", type=str, help="Get Sigma Rule by Rule ID. The Rule ID is an internal reference used only by this tool.")
parser.add_argument("-t", "--technique", type=str, help="Search by MITRE ATT&CK Technique.")
tactis_choices = ["collection", \
"command-and-control", \
"credential-access", \
"defense-evasion", \
"discovery", \
"execution", \
"exfiltration", \
"impact", \
"initial-access", \
"lateral-movement", \
"persistence", \
"privilege-escalation", \
"reconnaissance", \
"resource-development"]
parser.add_argument("-T", "--tactic", type=str, help=("Search by MITRE ATT&CK Tactic. Allowed values are: "+', '.join(tactis_choices)), choices=tactis_choices, metavar='')
parser.add_argument("-p", "--product", type=str,
help=("Search by Product. Allowed values are: "+', '.join(products)),
choices=list(products), metavar='')
status_values = ['stable', \
'test', \
'experimental', \
'deprecated', \
'unsupported']
parser.add_argument("-S", "--status", nargs="+", help="Filter by status. Allowed values are: "+', '.join(status_values), choices=status_values, metavar='')
parser.add_argument("-s", "--search", type=str, help="Search for free text in rule titles and descriptions (RegEx are supported).")
parser.add_argument("-F", "--force-caching", help="Force the regeneration of the detection rule cache.", action="store_true")
args = parser.parse_args()
technique = args.technique
tactic = args.tactic
status = args.status
product = args.product
search_text = args.search
rule_id = args.id
if args.force_caching:
console.print("> [orange3]Caching the detection rules...")
if sigma_parser(('sigma/rules', 'sigma/rules-threat-hunting', 'sigma/rules-emerging-threats'), parsed_file):
console.print("> [green]Cache file created.")
sys.exit(0)
else:
console.print("[bold][red]An error occurred during the operation. Exiting...")
sys.exit(1)
if rule_id:
with open(rules[rule_id]['filepath'], 'r') as fd:
console.print(Syntax(fd.read(), 'yaml'))
sys.exit(0)
if not any((technique, tactic, search_text, product, status)):
parser.print_help()
console.print("[bold][red] Error: You must provide at least one argument")
sys.exit(1)
table = Table(title="Sigma Rules", show_lines=True)
table.add_column("Rule ID", style="cyan", no_wrap=True)
table.add_column("Title", no_wrap=True)
table.add_column("Status", no_wrap=True)
if search_text:
table.add_column("Description", no_wrap=False)
table.add_column("Product", no_wrap=True)
table.add_column("Category", no_wrap=True)
table.add_column("Tactics", style="magenta", no_wrap=True)
table.add_column("Techniques", style="green", no_wrap=True)
for rid, rule in rules.items():
technique_match = False
tactic_match = False
search_match = False
status_match = False
product_match = False
rule_title = Text(rule['title'])
rule_description = Text(rule['description'])
if technique:
for t in rule['techniques']:
if re.match(f'{technique.lower()}(?=$|\\.)', t):
technique_match=True
else:
technique_match = True
if tactic:
for t in rule['tactics']:
if t == tactic:
tactic_match = True
else:
tactic_match = True
if search_text:
title_match = re.search(search_text, rule['title'], re.IGNORECASE) or False
if title_match:
rule_title.highlight_regex(title_match.group(0), style="red")
descr_match = re.search(search_text, rule['description'], re.IGNORECASE) or False
if descr_match:
rule_description.highlight_regex(descr_match.group(0), style="red")
search_match = any((title_match, descr_match))
else:
search_match = True
if product:
if product == rule['product']:
product_match = True
else:
product_match = True
if status:
if any((s == rule['status'] for s in status)):
status_match = True
else:
status_match = True
if all((technique_match, tactic_match, search_match, product_match, status_match)):
if search_text:
table.add_row(rid,
rule_title,
rule['status'],
rule_description,
rule['product'],
rule['category'],
'\n'.join(rule['tactics']),
'\n'.join(rule['techniques']))
else:
table.add_row(rid,
rule_title,
rule['status'],
rule['product'],
rule['category'],
'\n'.join(rule['tactics']),
'\n'.join(rule['techniques']))
console.print(table)