-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathparser.py
More file actions
426 lines (387 loc) · 14.6 KB
/
parser.py
File metadata and controls
426 lines (387 loc) · 14.6 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
"""
Command line handlers and helpers for SecOps CLI
"""
import argparse
import base64
import sys
from secops.cli.utils.common_args import add_pagination_args
from secops.cli.utils.formatters import output_formatter
from secops.exceptions import APIError, SecOpsError
def setup_parser_command(subparsers):
"""Set up the parser command parser."""
parser_parser = subparsers.add_parser("parser", help="Manage Parsers")
parser_subparsers = parser_parser.add_subparsers(
dest="parser_command", help="Parser command"
)
parser_parser.set_defaults(func=lambda args, _: parser_parser.print_help())
# --- Activate Parser Command ---
activate_parser_sub = parser_subparsers.add_parser(
"activate", help="Activate a custom parser."
)
activate_parser_sub.add_argument(
"--log-type", type=str, help="Log type of the parser."
)
activate_parser_sub.add_argument(
"--id", type=str, help="ID of the parser to activate."
)
activate_parser_sub.set_defaults(func=handle_parser_activate_command)
# --- Activate Release Candidate Parser Command ---
activate_rc_parser_sub = parser_subparsers.add_parser(
"activate-rc", help="Activate the release candidate parser."
)
activate_rc_parser_sub.add_argument(
"--log-type", type=str, help="Log type of the parser."
)
activate_rc_parser_sub.add_argument(
"--id", type=str, help="ID of the release candidate parser to activate."
)
activate_rc_parser_sub.set_defaults(func=handle_parser_activate_rc_command)
# --- Copy Parser Command ---
copy_parser_sub = parser_subparsers.add_parser(
"copy", help="Make a copy of a prebuilt parser."
)
copy_parser_sub.add_argument(
"--log-type", type=str, help="Log type of the parser to copy."
)
copy_parser_sub.add_argument(
"--id", type=str, help="ID of the parser to copy."
)
copy_parser_sub.set_defaults(func=handle_parser_copy_command)
# --- Create Parser Command ---
create_parser_sub = parser_subparsers.add_parser(
"create", help="Create a new parser."
)
create_parser_sub.add_argument(
"--log-type", type=str, help="Log type for the new parser."
)
create_parser_code_group = create_parser_sub.add_mutually_exclusive_group(
required=True
)
create_parser_code_group.add_argument(
"--parser-code", type=str, help="Content of the new parser (CBN code)."
)
create_parser_code_group.add_argument(
"--parser-code-file",
type=str,
help="Path to a file containing the parser code (CBN code).",
)
create_parser_sub.add_argument(
"--validated-on-empty-logs",
action="store_true",
help=(
"Whether the parser is validated on empty logs "
"(default: True if not specified, only use flag for True)."
),
)
create_parser_sub.set_defaults(func=handle_parser_create_command)
# --- Deactivate Parser Command ---
deactivate_parser_sub = parser_subparsers.add_parser(
"deactivate", help="Deactivate a custom parser."
)
deactivate_parser_sub.add_argument(
"--log-type", type=str, help="Log type of the parser."
)
deactivate_parser_sub.add_argument(
"--id", type=str, help="ID of the parser to deactivate."
)
deactivate_parser_sub.set_defaults(func=handle_parser_deactivate_command)
# --- Delete Parser Command ---
delete_parser_sub = parser_subparsers.add_parser(
"delete", help="Delete a parser."
)
delete_parser_sub.add_argument(
"--log-type", type=str, help="Log type of the parser."
)
delete_parser_sub.add_argument(
"--id", type=str, help="ID of the parser to delete."
)
delete_parser_sub.add_argument(
"--force",
action="store_true",
help="Forcefully delete an ACTIVE parser.",
)
delete_parser_sub.set_defaults(func=handle_parser_delete_command)
# --- Get Parser Command ---
get_parser_sub = parser_subparsers.add_parser(
"get", help="Get a parser by ID."
)
get_parser_sub.add_argument(
"--log-type", type=str, help="Log type of the parser."
)
get_parser_sub.add_argument(
"--id", type=str, help="ID of the parser to retrieve."
)
get_parser_sub.set_defaults(func=handle_parser_get_command)
# --- List Parsers Command ---
list_parsers_sub = parser_subparsers.add_parser(
"list", help="List parsers."
)
list_parsers_sub.add_argument(
"--log-type",
type=str,
default="-",
help="Log type to filter by (default: '-' for all).",
)
add_pagination_args(list_parsers_sub)
list_parsers_sub.add_argument(
"--filter",
type=str,
help="Filter expression to apply (e.g., 'state=ACTIVE').",
)
list_parsers_sub.set_defaults(func=handle_parser_list_command)
# --- Run Parser Command ---
run_parser_sub = parser_subparsers.add_parser(
"run",
help="Run parser against sample logs for evaluation.",
description=(
"Evaluate a parser by running it against sample log entries. "
"This helps test parser logic before deploying it."
),
epilog=(
"Examples:\n"
" # Run parser with inline code and logs:\n"
" secops parser run --log-type OKTA --parser-code 'filter {}' "
"--log 'log1' --log 'log2'\n\n"
" # Run parser using files:\n"
" secops parser run --log-type WINDOWS "
"--parser-code-file parser.conf --logs-file logs.txt\n\n"
" # Run parser with the active parser\n"
" secops parser run --log-type OKTA --log-file logs.txt\n\n"
" # Run parser with extension:\n"
" secops parser run --log-type CUSTOM --parser-code-file "
"parser.conf \\\n --parser-extension-code-file extension.conf "
"--logs-file logs.txt"
),
formatter_class=argparse.RawDescriptionHelpFormatter,
)
run_parser_sub.add_argument(
"--log-type",
type=str,
required=True,
help="Log type of the parser for evaluation (e.g., OKTA, WINDOWS_AD)",
)
run_parser_code_group = run_parser_sub.add_mutually_exclusive_group(
required=False
)
run_parser_code_group.add_argument(
"--parser-code",
type=str,
help="Content of the main parser (CBN code) to evaluate",
)
run_parser_code_group.add_argument(
"--parser-code-file",
type=str,
help="Path to a file containing the main parser code (CBN code)",
)
run_parser_ext_group = run_parser_sub.add_mutually_exclusive_group(
required=False
)
run_parser_ext_group.add_argument(
"--parser-extension-code",
type=str,
help="Content of the parser extension (CBN snippet)",
)
run_parser_ext_group.add_argument(
"--parser-extension-code-file",
type=str,
help=(
"Path to a file containing the parser extension code (CBN snippet)"
),
)
run_parser_logs_group = run_parser_sub.add_mutually_exclusive_group(
required=True
)
run_parser_logs_group.add_argument(
"--log",
action="append",
help=(
"Provide a raw log string to test. Can be specified multiple "
"times for multiple logs"
),
)
run_parser_logs_group.add_argument(
"--logs-file",
type=str,
help="Path to a file containing raw logs (one log per line)",
)
run_parser_sub.add_argument(
"--statedump-allowed",
action="store_true",
help="Enable statedump filter for the parser configuration",
)
run_parser_sub.add_argument(
"--parse-statedump",
"--parse_statedump",
action="store_true",
help=("Parse statedump results into readable format"),
)
run_parser_sub.set_defaults(func=handle_parser_run_command)
def handle_parser_activate_command(args, chronicle):
"""Handle parser activate command."""
try:
result = chronicle.activate_parser(args.log_type, args.id)
output_formatter(result, args.output)
except Exception as e: # pylint: disable=broad-exception-caught
print(f"Error activating parser: {e}", file=sys.stderr)
sys.exit(1)
def handle_parser_activate_rc_command(args, chronicle):
"""Handle parser activate-release-candidate command."""
try:
result = chronicle.activate_release_candidate_parser(
args.log_type, args.id
)
output_formatter(result, args.output)
except Exception as e: # pylint: disable=broad-exception-caught
print(
f"Error activating release candidate parser: {e}", file=sys.stderr
)
sys.exit(1)
def handle_parser_copy_command(args, chronicle):
"""Handle parser copy command."""
try:
result = chronicle.copy_parser(args.log_type, args.id)
output_formatter(result, args.output)
except Exception as e: # pylint: disable=broad-exception-caught
print(f"Error copying parser: {e}", file=sys.stderr)
sys.exit(1)
def handle_parser_create_command(args, chronicle):
"""Handle parser create command."""
try:
parser_code = ""
if args.parser_code_file:
try:
with open(args.parser_code_file, encoding="utf-8") as f:
parser_code = f.read()
except OSError as e:
print(f"Error reading parser code file: {e}", file=sys.stderr)
sys.exit(1)
elif args.parser_code:
parser_code = args.parser_code
else:
raise SecOpsError(
"Either --parser-code or --parser-code-file must be provided."
)
result = chronicle.create_parser(
args.log_type, parser_code, args.validated_on_empty_logs
)
output_formatter(result, args.output)
except Exception as e: # pylint: disable=broad-exception-caught
print(f"Error creating parser: {e}", file=sys.stderr)
sys.exit(1)
def handle_parser_deactivate_command(args, chronicle):
"""Handle parser deactivate command."""
try:
result = chronicle.deactivate_parser(args.log_type, args.id)
output_formatter(result, args.output)
except Exception as e: # pylint: disable=broad-exception-caught
print(f"Error deactivating parser: {e}", file=sys.stderr)
sys.exit(1)
def handle_parser_delete_command(args, chronicle):
"""Handle parser delete command."""
try:
result = chronicle.delete_parser(args.log_type, args.id, args.force)
output_formatter(result, args.output)
except Exception as e: # pylint: disable=broad-exception-caught
print(f"Error deleting parser: {e}", file=sys.stderr)
sys.exit(1)
def handle_parser_get_command(args, chronicle):
"""Handle parser get command."""
try:
result = chronicle.get_parser(args.log_type, args.id)
output_formatter(result, args.output)
except Exception as e: # pylint: disable=broad-exception-caught
print(f"Error getting parser: {e}", file=sys.stderr)
sys.exit(1)
def handle_parser_list_command(args, chronicle):
"""Handle parser list command."""
try:
result = chronicle.list_parsers(
args.log_type, args.page_size, args.page_token, args.filter
)
output_formatter(result, args.output)
except Exception as e: # pylint: disable=broad-exception-caught
print(f"Error listing parsers: {e}", file=sys.stderr)
sys.exit(1)
def handle_parser_run_command(args, chronicle):
"""Handle parser run (evaluation) command."""
try:
# Read parser code
parser_code = ""
if args.parser_code_file:
try:
with open(args.parser_code_file, encoding="utf-8") as f:
parser_code = f.read()
except OSError as e:
print(f"Error reading parser code file: {e}", file=sys.stderr)
sys.exit(1)
elif args.parser_code:
parser_code = args.parser_code
else:
# If no parser code provided,
# try to find an active parser for the log type
parser_list_response = chronicle.list_parsers(
args.log_type,
page_size=1,
filter="STATE=ACTIVE",
)
parsers = parser_list_response.get("parsers", [])
if len(parsers) < 1:
raise SecOpsError(
"No parser file provided and an active parser could not "
f"be found for log type '{args.log_type}'."
)
parser_code_encoded = parsers[0].get("cbn")
parser_code = base64.b64decode(parser_code_encoded).decode("utf-8")
# Read parser extension code (optional)
parser_extension_code = ""
if args.parser_extension_code_file:
try:
with open(
args.parser_extension_code_file, encoding="utf-8"
) as f:
parser_extension_code = f.read()
except OSError as e:
print(
f"Error reading parser extension code file: {e}",
file=sys.stderr,
)
sys.exit(1)
elif args.parser_extension_code:
parser_extension_code = args.parser_extension_code
# Read logs
logs = []
if args.logs_file:
try:
with open(args.logs_file, encoding="utf-8") as f:
logs = [line.strip() for line in f if line.strip()]
except OSError as e:
print(f"Error reading logs file: {e}", file=sys.stderr)
sys.exit(1)
elif args.log:
logs = args.log
if not logs:
print(
"Error: No logs provided. Use --log or --logs-file to provide "
"log entries.",
file=sys.stderr,
)
sys.exit(1)
# Call the API
result = chronicle.run_parser(
args.log_type,
parser_code,
parser_extension_code,
logs,
args.statedump_allowed,
args.parse_statedump,
)
output_formatter(result, args.output)
except ValueError as e:
print(f"Validation error: {e}", file=sys.stderr)
sys.exit(1)
except APIError as e:
print(f"API error: {e}", file=sys.stderr)
sys.exit(1)
except Exception as e: # pylint: disable=broad-exception-caught
print(f"Error running parser: {e}", file=sys.stderr)
sys.exit(1)