-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexception_list_generator.py
More file actions
1205 lines (1047 loc) · 52.8 KB
/
exception_list_generator.py
File metadata and controls
1205 lines (1047 loc) · 52.8 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
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import pyperclip
import sys
import os
import re
import requests
import time
import traceback
import google.generativeai as genai
from datetime import datetime, timedelta
from dotenv import load_dotenv
# --- Load Environment Variables ---
load_dotenv()
# --- Configuration ---
GEMINI_API_KEY = os.getenv('GEMINI_API_KEY')
GEMINI_MODEL_NAME = "gemini-2.5-flash"
# --- Helper Functions ---
def extract_customer_info(hostname, user_domain, host_domain):
"""
Derive a concise org/customer label for use before "user".
Priority: user.domain (if not generic like AUTH) -> host.domain -> hostname patterns.
Examples:
- user.domain "ENGR" -> label "ENGR"
- user.domain "engr.tamu.edu" -> label "ENGR" (first subdomain uppercased)
- else host.domain "engr.tamu.edu" -> label "ENGR"
Returns a label string (e.g., "ENGR", "CSISD") or None if not identifiable.
"""
if not hostname or hostname == "N/A":
hostname = ""
if not user_domain or user_domain == "N/A":
user_domain = ""
if not host_domain or host_domain == "N/A":
host_domain = ""
# Common customer patterns (expand as needed)
customer_patterns = {
'csisd': 'CSISD',
'uiw': 'UIW',
'tamuk': 'TAMUK',
'tamucc': 'TAMU-CC',
'tamu-cc': 'TAMU-CC',
'tamuds': 'TAMU-DS',
'tamusa': 'TAMUSA',
'corpus': 'Corpus Christi',
'ccad': 'Corpus Christi Army Depot',
'ait': 'AIT',
'agnet': 'AGNET',
'engr': 'ENGR',
# Add more customer patterns as discovered
}
# Generic/non-meaningful domain values to skip
generic_domains = {'auth', 'nt authority', 'workgroup', 'builtin', ''}
# PRIORITY 1: user.domain
user_domain_lower = user_domain.lower()
if user_domain_lower not in generic_domains and user_domain_lower:
# If user.domain contains dots, take first label; else use whole value
try:
first_label = user_domain_lower.split('.')[0] if '.' in user_domain_lower else user_domain_lower
if first_label and first_label not in generic_domains and first_label not in {'www'}:
mapped = customer_patterns.get(first_label)
return mapped if mapped else first_label.upper()
except Exception:
pass
# Fallback to pattern mapping on user.domain
for pattern, customer_name in customer_patterns.items():
if pattern in user_domain_lower:
return customer_name
# PRIORITY 2: host.domain -> first subdomain uppercased
host_domain_lower = host_domain.lower()
if host_domain_lower not in generic_domains and host_domain_lower:
try:
first_label = host_domain_lower.split('.')[0]
if first_label and first_label not in generic_domains and first_label not in {'www'}:
mapped = customer_patterns.get(first_label)
return mapped if mapped else first_label.upper()
except Exception:
pass
# Pattern mapping fallback on host.domain
for pattern, customer_name in customer_patterns.items():
if pattern in host_domain_lower:
return customer_name
# Fall back to hostname mapping last
hostname_lower = hostname.lower()
for pattern, customer_name in customer_patterns.items():
if pattern in hostname_lower:
return customer_name
return None
def generate_observation_statement(context, api_key):
"""
Uses the Gemini API to generate a concise, natural-language observation statement
following the "Actors, Assets, and Actions" framework with customer information included.
Key entities are enclosed in backticks.
"""
def determine_action(rule_name: str) -> str:
rn = (rule_name or "").lower()
if any(k in rn for k in ("download", "downloader")):
return "downloaded"
if any(k in rn for k in ("execution", "execut")):
return "executed"
if any(k in rn for k in ("prevent", "blocked", "block", "prevention")):
return "triggered a prevention alert for"
if "memory" in rn:
return "triggered memory protection for"
if any(k in rn for k in ("malware", "trojan", "worm", "virus", "adware", "pup")):
return "encountered malware"
if any(k in rn for k in ("child process", "spawn", "parent")):
return "spawned suspicious process"
if any(k in rn for k in ("phish", "phishing")):
return "interacted with suspected phishing content"
if "ransomware" in rn:
return "triggered ransomware-related behavior"
if any(k in rn for k in ("credential", "creds", "mimikatz")):
return "triggered credential access behavior"
if "lateral" in rn:
return "triggered lateral movement behavior"
if "persistence" in rn:
return "established persistence-related behavior"
if any(k in rn for k in ("defense evasion", "obfuscat", "tamper")):
return "triggered defense evasion behavior"
return "triggered alert involving"
# Extract customer information
customer = extract_customer_info(
context.get('hostname', 'N/A'),
context.get('user_domain', 'N/A'),
context.get('host_domain', 'N/A')
)
primary_actor_fallback = context.get('proc_name', "N/A") if context.get('proc_name', "N/A") != "N/A" else context.get('parent_proc_name', 'N/A')
rule_name_val = context.get('rule_name', 'N/A')
action_phrase = determine_action(rule_name_val)
# Check if we have a distinct file entity to focus on
file_val = context.get('file_name', 'N/A')
proc_val = context.get('proc_name', 'N/A')
has_distinct_file = file_val != "N/A" and file_val != proc_val
subject_is_file = has_distinct_file or \
'file' in rule_name_val.lower() or \
'download' in rule_name_val.lower() or \
'execution' in rule_name_val.lower() or \
'malware' in rule_name_val.lower() or \
'prevented' in rule_name_val.lower()
subject_fallback = context.get('file_name', 'N/A') if subject_is_file and context.get('file_name', 'N/A') != "N/A" else primary_actor_fallback
# Build deterministic fallback with customer info included and natural alert type integration
sentence_parts_fb = []
if customer:
sentence_parts_fb.append(f"{customer} user `{context.get('user_name', 'N/A')}`")
else:
sentence_parts_fb.append(f"User `{context.get('user_name', 'N/A')}`")
# Describe the action with process/file context
proc_name = context.get('proc_name', 'N/A')
parent_proc_name = context.get('parent_proc_name', 'N/A')
# Action (+ inline alert name when using generic phrasing)
def _article(word: str) -> str:
if not isinstance(word, str) or not word:
return "a"
return "an" if word[0].lower() in "aeiou" else "a"
# Helper to convert rule name to natural language
def _naturalize_alert(rule_name: str) -> str:
"""Convert a rule name like 'Malicious File Prevention Alert: Trojan' to 'malicious file prevention alert'"""
if not rule_name or rule_name == 'N/A':
return ""
# Remove common noise words and colons
cleaned = re.sub(r'(?i)(alert|prevention|threat|memory|endpoint|event)(?=\s*:)', '', rule_name)
cleaned = cleaned.replace(":", "").strip()
# Remove trailing descriptors after colon if any remain
if ':' in cleaned:
cleaned = cleaned.split(':')[0].strip()
return cleaned.lower()
inline_alert = False
if rule_name_val and rule_name_val != 'N/A':
# Always inline the alert naturally after user
natural_alert = _naturalize_alert(rule_name_val)
if natural_alert:
sentence_parts_fb.append(f"triggered {_article(natural_alert)} {natural_alert} alert for")
inline_alert = True
else:
sentence_parts_fb.append(action_phrase)
else:
sentence_parts_fb.append(action_phrase)
# Asset
if subject_is_file and subject_fallback != "N/A":
sentence_parts_fb.append(f"`{subject_fallback}`")
if proc_name != "N/A" and proc_name != subject_fallback:
sentence_parts_fb.append(f"via process `{proc_name}`")
if parent_proc_name != "N/A" and parent_proc_name != proc_name:
sentence_parts_fb.append(f"from parent `{parent_proc_name}`")
elif primary_actor_fallback != "N/A":
sentence_parts_fb.append(f"`{primary_actor_fallback}`")
if parent_proc_name != "N/A" and parent_proc_name != primary_actor_fallback:
sentence_parts_fb.append(f"from parent `{parent_proc_name}`")
# Target detail if present
unique_fn = context.get('unique_arg_filename', 'N/A')
if unique_fn != "N/A":
sentence_parts_fb.append(f"targeting `{unique_fn}`")
# Host (omit domain)
_host_name = context.get('hostname', 'N/A')
sentence_parts_fb.append(f"on host `{_host_name}`")
fallback_sentence = " ".join(sentence_parts_fb).strip()
if not fallback_sentence.endswith('.'):
fallback_sentence += '.'
if not api_key:
return f"***Observation Statement***\n\n{fallback_sentence}\n(Observation generated locally due to missing GEMINI_API_KEY)"
try:
genai.configure(api_key=api_key)
# Configure model with strict instruction to preserve semantics
system_instruction = (
"You are a precise text formatter. Return exactly one sentence, "
"fixing grammar minimally. Do NOT remove or alter any backticked tokens, "
"parenthetical segments, or the action phrase enclosed in quotes below. "
"Do NOT paraphrase verbs. Do NOT add or remove entities."
)
model = genai.GenerativeModel(
model_name=GEMINI_MODEL_NAME,
system_instruction=system_instruction
)
data_for_prompt = {
"customer": customer if customer else "N/A",
"rule_name": context.get('rule_name', 'N/A'),
"process_name": context.get('proc_name', 'N/A'),
"parent_name": context.get('parent_proc_name', 'N/A'),
"file_name": context.get('file_name', 'N/A'),
"user_name": context.get('user_name', 'N/A'),
"host_name": context.get('hostname', 'N/A'),
"host_domain": context.get('host_domain', 'N/A'),
"unique_arg_filename": context.get('unique_arg_filename', 'N/A')
}
# --- Ultra-Direct Prompt with guarded tokens ---
# Build the base sentence with process context, then ask the model to minimally polish.
prompt_parts = []
# Pre-resolved action verb
action = action_phrase
# Build detailed sentence with process context
sentence_parts = []
# Start with customer and user
if data_for_prompt['customer'] != "N/A":
sentence_parts.append(f"{data_for_prompt['customer']} user `{data_for_prompt['user_name']}`")
else:
sentence_parts.append(f"User `{data_for_prompt['user_name']}`")
# Integrate alert naturally after user
def _naturalize_alert_gen(rule_name: str) -> str:
"""Convert a rule name to natural language description"""
if not rule_name or rule_name == 'N/A':
return ""
# Remove common noise words and colons
cleaned = re.sub(r'(?i)(alert|prevention|threat|memory|endpoint|event)(?=\s*:)', '', rule_name)
cleaned = cleaned.replace(":", "").strip()
# Remove trailing descriptors after colon if any remain
if ':' in cleaned:
cleaned = cleaned.split(':')[0].strip()
return cleaned.lower()
# Add alert integration or action
if data_for_prompt['rule_name'] != "N/A":
natural_alert = _naturalize_alert_gen(data_for_prompt['rule_name'])
if natural_alert:
art = "an" if natural_alert[0].lower() in "aeiou" else "a"
sentence_parts.append(f"triggered {art} {natural_alert} alert for")
else:
sentence_parts.append(action)
else:
sentence_parts.append(action)
# Determine what to highlight based on alert type
file_name = data_for_prompt['file_name']
process_name = data_for_prompt['process_name']
parent_name = data_for_prompt['parent_name']
# Build asset description with process context
if file_name != "N/A" and file_name != process_name:
# File is distinct from process
sentence_parts.append(f"`{file_name}`")
if process_name != "N/A":
sentence_parts.append(f"via process `{process_name}`")
elif process_name != "N/A":
# Process-focused alert
sentence_parts.append(f"`{process_name}`")
# Add parent process context when available and different
if parent_name != "N/A" and parent_name != process_name:
sentence_parts.append(f"from parent `{parent_name}`")
# Add host only (omit domain in observation statement)
sentence_parts.append(f"on host `{data_for_prompt['host_name']}`")
# Construct the rewrite instruction - preserve tokens and natural alert integration
base_sentence = " ".join(sentence_parts)
prompt_parts.append(
"Polish minimally for grammar and readability. Keep all backticked entities (processes, files, usernames, hostnames) "
"exactly as-is. The alert type has already been naturally integrated after the user. "
"Preserve the alert description in natural language form. "
"Maintain the existing structure: customer+user → alert type → asset → parent context → host.\n"
f"Sentence: {base_sentence}"
)
final_prompt = "\n".join(prompt_parts)
# Aggressive token configuration - giving model maximum headroom
# The MAX_TOKENS issue suggests internal reasoning is consuming tokens
generation_config = genai.types.GenerationConfig(
temperature=0.0,
max_output_tokens=8192, # Maximum allowed - give plenty of headroom
candidate_count=1
)
safety_settings=[
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
]
print(" Sending request to Gemini API for Observation Statement...")
response = model.generate_content(final_prompt, generation_config=generation_config, safety_settings=safety_settings)
# Try to extract text even if finish_reason is MAX_TOKENS
# The model may have produced valid output before hitting the limit
generated_sentence = None
try:
if hasattr(response, 'text') and response.text:
generated_sentence = response.text.strip()
except Exception as e:
print(f" DEBUG: Could not extract text: {e}")
# Check finish reason - but allow MAX_TOKENS if we got valid text
finish_reason = None
if hasattr(response, 'candidates') and response.candidates:
finish_reason = response.candidates[0].finish_reason.name
# If we have no text or it's empty, use fallback
if not generated_sentence:
reason = finish_reason if finish_reason else "Unknown (no text generated)"
print(f"Warning: Observation generation failed. Reason: {reason}. Using fallback.")
return f"***Observation Statement***\n\n{fallback_sentence}\n(Observation generation failed: {reason})"
# If finish_reason is MAX_TOKENS but we have text, that's actually OK for a single sentence
# Just warn if it seems incomplete
if finish_reason == "MAX_TOKENS":
# Check if sentence looks complete (ends with period, backtick, or similar)
if not generated_sentence.rstrip().endswith(('.', '`', ')', ']')):
print(f" WARNING: Sentence may be incomplete. Using fallback.")
return f"***Observation Statement***\n\n{fallback_sentence}\n(Observation may be incomplete due to MAX_TOKENS)"
generated_sentence = response.text.strip()
if len(generated_sentence.splitlines()) > 1:
print("Warning: Observation statement generation returned multiple lines. Using first line.")
generated_sentence = generated_sentence.splitlines()[0]
# Simplified validation - just check for critical elements
missing_elements = []
def check_element(element_name, element_value):
if element_value != "N/A" and f"`{element_value}`" not in generated_sentence:
missing_elements.append(element_name)
# Validate critical elements: user, host, verb/alert presence
check_element("user name", context.get('user_name', 'N/A'))
check_element("hostname", context.get('hostname', 'N/A'))
# Check for either action verb or "alert" keyword (since we're integrating it naturally)
verb_root = (action_phrase or '').split(' ')[0].lower()
has_action = (verb_root and verb_root in generated_sentence.lower()) or 'alert' in generated_sentence.lower()
if not has_action:
missing_elements.append("action verb or alert keyword")
# Domain is intentionally omitted from observation; no domain validation
# Check if customer info should be present
if customer and customer.lower() not in generated_sentence.lower():
missing_elements.append(f"customer identifier ({customer})")
# Extra validation to avoid awkward duplications
if generated_sentence.lower().count("alert") > 2:
missing_elements.append("excessive 'alert' repetition")
# Avoid dangling prepositions
if generated_sentence.rstrip().lower().endswith(("involving", "via", "from", "with")):
missing_elements.append("dangling phrase")
if missing_elements:
print(f"Warning: Generated sentence might be missing elements: {', '.join(missing_elements)}. Using fallback.")
return f"***Observation Statement***\n\n{fallback_sentence}\n(Observation generation missing elements)"
else:
return f"***Observation Statement***\n\n{generated_sentence}"
except Exception as e:
error_msg = f"Observation Generation Error: {e}"
print(f"Error during observation generation: {error_msg}")
err_str = str(e).lower()
if "429" in err_str: error_msg = "API Rate Limit Exceeded"
elif "permission_denied" in err_str or "api key not valid" in err_str: error_msg = "Invalid API Key or Permissions Issue"
elif "deadline exceeded" in err_str: error_msg = "API Request Timeout"
return f"***Observation Statement***\n\n{fallback_sentence}\n(Observation generated locally due to API error: {error_msg})"
def get_nested_value(data, key_path):
"""
Safely retrieves a nested value from a dictionary using a dot-separated key path.
"""
keys = key_path.split('.')
current_data = data
for key in keys:
if isinstance(current_data, dict) and key in current_data:
current_data = current_data[key]
else:
return None
# Handle final value if it's a list (e.g., event.type: ["info", "start"])
if isinstance(current_data, list):
return ' '.join([str(item) for item in current_data if item is not None])
# Format boolean values to lowercase string (true/false) as required by EQL
if isinstance(current_data, bool):
return str(current_data).lower()
# Return raw string or other non-dict type
return current_data
def parse_multiple_json(text):
"""
Parses text that might contain a single JSON object, a list, or NDJSON.
"""
text = text.strip()
results = []
# Attempt 1: Try parsing as a standard JSON (Dict or List)
try:
parsed = json.loads(text)
if isinstance(parsed, list):
return parsed
if isinstance(parsed, dict):
return [parsed]
except json.JSONDecodeError:
pass
# Attempt 2: Parse concatenated JSON objects (NDJSON)
decoder = json.JSONDecoder()
pos = 0
while pos < len(text):
while pos < len(text) and text[pos].isspace():
pos += 1
if pos >= len(text):
break
try:
obj, json_len = decoder.raw_decode(text[pos:])
results.append(obj)
pos += json_len
except json.JSONDecodeError:
break
return results
def extract_context(data):
"""Extracts relevant security fields from a single alert dict."""
if '_source' in data and isinstance(data['_source'], dict):
ecs_data = data['_source']
else:
ecs_data = data
FIELD_MAP = {
'event.code': 'event.code',
'event.action': 'event.action',
'process.name': 'process.name',
'process.executable': 'process.executable',
'process.pe.original_file_name': 'process.pe.original_file_name',
'process.command_line': 'process.command_line',
'process.args': 'process.args',
'process.working_directory': 'process.working_directory',
'process.parent.name': 'process.parent.name',
'process.parent.executable': 'process.parent.executable',
'process.parent.executable.text': 'process.parent.executable.text',
'process.parent.command_line': 'process.parent.command_line',
'process.parent.args': 'process.parent.args',
'process.parent.working_directory': 'process.parent.working_directory',
'process.code_signature.subject_name': 'process.code_signature.subject_name',
'process.code_signature.trusted': 'process.code_signature.trusted',
'process.hash.sha256': 'process.hash.sha256',
'process.hash.sha1': 'process.hash.sha1',
'process.hash.md5': 'process.hash.md5',
'file.name': 'file.name',
'file.path': 'file.path',
'file.code_signature.subject_name': 'file.code_signature.subject_name',
'file.code_signature.trusted': 'file.code_signature.trusted',
'file.hash.sha256': 'file.hash.sha256',
'file.hash.sha1': 'file.hash.sha1',
'file.hash.md5': 'file.hash.md5',
'rule.id': 'rule.id',
'rule.uuid': 'rule.uuid',
'kibana.alert.rule.uuid': 'kibana.alert.rule.uuid',
'user.name': 'user.name', # Extracted for context only
}
context = {}
for eql_field, json_path in FIELD_MAP.items():
value = get_nested_value(ecs_data, json_path)
if value is not None and value != '':
context[eql_field] = value
# --- Additional Context for Observation Statement ---
# Map to keys expected by generate_observation_statement
# Hostname
context['hostname'] = (
get_nested_value(ecs_data, 'host.name') or
get_nested_value(ecs_data, 'host.hostname') or
get_nested_value(ecs_data, 'observer.hostname') or
get_nested_value(ecs_data, 'agent.hostname') or
"N/A"
)
# Domains
context['user_domain'] = get_nested_value(ecs_data, 'user.domain') or "N/A"
context['host_domain'] = get_nested_value(ecs_data, 'host.domain') or "N/A"
# Rule Name
context['rule_name'] = (
get_nested_value(ecs_data, 'kibana.alert.rule.name') or
get_nested_value(ecs_data, 'rule.name') or
"N/A"
)
context['rule.name'] = context['rule_name']
# Process/File Names (already in context but with dot notation, map to underscore for compatibility)
context['proc_name'] = context.get('process.name', "N/A")
context['parent_proc_name'] = context.get('process.parent.name', "N/A")
# If parent.name is missing, extract from parent.executable or parent.executable.text
if context['parent_proc_name'] == "N/A":
parent_exec = context.get('process.parent.executable') or context.get('process.parent.executable.text')
if parent_exec and parent_exec != "N/A":
# Extract basename from full path
context['parent_proc_name'] = os.path.basename(parent_exec)
context['file_name'] = context.get('file.name', "N/A")
context['user_name'] = context.get('user.name', "N/A")
# Unique Arg Filename (simplified extraction)
context['unique_arg_filename'] = "N/A"
if context['rule_name'] == "Suspicious MS Office Child Process" and context['proc_name'] == 'explorer.exe':
# Try to extract from args or command line
cmd = get_nested_value(ecs_data, 'process.command_line')
target_arg = None
if cmd and "/select," in str(cmd).lower():
parts = str(cmd).split()
for i, part in enumerate(parts):
if part.lower().startswith("/select,"):
target_arg = part
if part.lower() == "/select," and i + 1 < len(parts):
target_arg = "/select," + parts[i+1]
break
if target_arg:
try:
full_path = target_arg[len("/select,"):].strip().strip('"')
if full_path:
context['unique_arg_filename'] = os.path.basename(full_path)
except:
pass
return context
def _build_fallback_exception_list(alerts=None, contexts=None, min_conditions=3):
"""
Build a stronger EQL exception list without LLM by intersecting or voting on stable fields across alerts.
Uses tiered approach: signer+trusted+name > hashes+name > process+parent+patterns > behavioral.
Automatically generalizes paths (strips versions, detects WindowsApps), pattern-matches versioned scripts.
Uses frequency (>=60% of alerts, min 2) instead of strict equality, then fills from the first alert if needed.
"""
if not alerts and not contexts:
return None
# Reuse already-extracted contexts when provided to avoid duplicate work
ctx_list = contexts if contexts is not None else []
if not ctx_list and alerts:
for alert in alerts:
ctx = extract_context(alert)
if ctx:
ctx_list.append(ctx)
if not ctx_list:
return None
# --- Helper Functions ---
def strip_version_from_path(path):
"""Strip version numbers and hashes from WindowsApps/versioned paths."""
if not path or path in (None, '', 'N/A'):
return None
# Match WindowsApps pattern: CompanyName.AppName_version_arch__hash
match = re.search(r'WindowsApps[/\\]([^/\\]+?)_[0-9.]+_[^/\\]+__[^/\\]+[/\\](.+)$', path, re.IGNORECASE)
if match:
return match.group(2) # Return just the filename
# Match other versioned patterns: tool-1.2.3.exe or tool_v1.2.exe
match = re.search(r'([^/\\]+?)[-_]v?[0-9]+\.[0-9.]+', path, re.IGNORECASE)
if match:
return os.path.basename(path)
return None
def is_windows_app_path(path):
"""Check if path is from WindowsApps (versioned store apps)."""
return bool(path and 'WindowsApps' in str(path))
def extract_script_pattern(text):
"""Extract generalized script pattern from command/file (e.g., deleteusers1.4.ps1 -> deleteusers1.[0-9]+.ps1)."""
if not text:
return None
# Match script.1.2.ps1 or tool-1.2.py patterns
match = re.search(r'([a-zA-Z_-]+[0-9]*)\.[0-9]+(?:\.[0-9]+)*\.(ps1|py|vbs|bat|cmd|js)', str(text), re.IGNORECASE)
if match:
base = match.group(1)
ext = match.group(2)
return f"{base}\\.[0-9]+(?:\\.[0-9]+)*\\.{ext}"
return None
def extract_executable_from_command(cmd):
"""Best-effort extraction of the leading executable path from a command line."""
if not cmd:
return None
txt = str(cmd).strip()
m = re.match(r'^"([^"]+)"', txt)
if m:
return m.group(1)
parts = txt.split()
return parts[0] if parts else None
def generalize_path_pattern(path):
"""Generalize a path to a MATCHES-friendly pattern (wildcards for versions/roots) while keeping filename."""
if not path:
return None
p = str(path)
# Remove drive letter
p = re.sub(r'^[A-Za-z]:', '', p)
# Normalize slashes
p = p.replace('\\', '/')
# Generalize user profile folder
p = re.sub(r'/Users/[^/]+/', r'/Users/[^/]+/', p, flags=re.IGNORECASE)
# WindowsApps version blobs
p = re.sub(r'WindowsApps/([^/]+?)_[0-9.]+_[^/]+__[^/]+/', r'WindowsApps/\1_*/', p, flags=re.IGNORECASE)
# Replace version numbers with wildcard class
p = re.sub(r'[0-9]+(?:\.[0-9]+)+', r'[0-9.]+', p)
# Restore escaped backslashes for EQL MATCHES
p = p.replace('/', r'\\')
# Ensure we anchor loosely but keep filename
if not p.startswith('.*'):
p = '.*' + p
return p
def frequent_value(key):
"""Find a representative value across contexts.
- If there is only a single context, just return its value (if present).
- Otherwise, return a high-frequency value present in >=60% of alerts with at least 2 occurrences.
"""
# Single-alert fast path: treat the lone value as authoritative
if len(ctx_list) == 1:
val = ctx_list[0].get(key)
return val if val not in (None, '', 'N/A') else None
freq = {}
for ctx in ctx_list:
val = ctx.get(key)
if val in (None, '', 'N/A'):
continue
freq[val] = freq.get(val, 0) + 1
if not freq:
return None
max_count = max(freq.values())
threshold = max(2, int(len(ctx_list) * 0.6 + 0.5))
if max_count < threshold:
return None
# Return the most frequent value
for v, c in freq.items():
if c == max_count:
return v
return None
conditions = []
seen = set()
# --- TIER 1: Code Signature (Strongest - works across versions/hosts) ---
signer = frequent_value('process.code_signature.subject_name')
trusted = frequent_value('process.code_signature.trusted')
proc_name = frequent_value('process.name')
parent_name = frequent_value('process.parent.name')
pe_original = frequent_value('process.pe.original_file_name')
if signer and proc_name:
conditions.append(f"process.code_signature.subject_name IS \"{signer}\"")
seen.add(('process.code_signature.subject_name', signer))
if trusted:
conditions.append(f"process.code_signature.trusted IS \"{trusted}\"")
seen.add(('process.code_signature.trusted', trusted))
if pe_original:
conditions.append(f"process.pe.original_file_name IS \"{pe_original}\"")
seen.add(('process.pe.original_file_name', pe_original))
conditions.append(f"process.name IS \"{proc_name}\"")
seen.add(('process.name', proc_name))
# ALWAYS add parent if available - critical for preventing false positives
if parent_name:
conditions.append(f"process.parent.name IS \"{parent_name}\"")
seen.add(('process.parent.name', parent_name))
# --- TIER 2: Hash + Name (Precise for unsigned binaries) ---
elif frequent_value('process.hash.sha256') and proc_name:
sha256 = frequent_value('process.hash.sha256')
conditions.append(f"process.hash.sha256 IS \"{sha256}\"")
seen.add(('process.hash.sha256', sha256))
if pe_original:
conditions.append(f"process.pe.original_file_name IS \"{pe_original}\"")
seen.add(('process.pe.original_file_name', pe_original))
conditions.append(f"process.name IS \"{proc_name}\"")
seen.add(('process.name', proc_name))
# ALWAYS add parent if available
if parent_name:
conditions.append(f"process.parent.name IS \"{parent_name}\"")
seen.add(('process.parent.name', parent_name))
# --- TIER 3: Process + Parent + Pattern Matching ---
else:
# Add process name if available
if proc_name:
conditions.append(f"process.name IS \"{proc_name}\"")
seen.add(('process.name', proc_name))
# ALWAYS add parent process if available
if parent_name:
conditions.append(f"process.parent.name IS \"{parent_name}\"")
seen.add(('process.parent.name', parent_name))
# Detect script patterns in command line or file name
script_pattern = None
for ctx in ctx_list:
cmdl = ctx.get('process.command_line') or ctx.get('process.args') or ''
fname = ctx.get('file.name') or ''
# Try command line first
pattern = extract_script_pattern(cmdl)
if pattern and ('process.command_line', pattern) not in seen:
conditions.append(f"process.command_line MATCHES \"(?i).*{pattern}.*\"")
seen.add(('process.command_line', pattern))
script_pattern = pattern
break
# Fall back to file name
pattern = extract_script_pattern(fname)
if pattern and ('file.name', pattern) not in seen:
conditions.append(f"file.name MATCHES \"(?i){pattern}\"")
seen.add(('file.name', pattern))
script_pattern = pattern
break
# If no pattern detected but we have command line, check for WindowsApps paths
if not script_pattern:
for ctx in ctx_list:
cmdl = ctx.get('process.command_line') or ctx.get('process.args') or ''
exec_path = ctx.get('process.executable') or extract_executable_from_command(cmdl) or ''
# Generalize executable path to a wildcard pattern if present
pattern = generalize_path_pattern(exec_path)
if pattern and ('process.executable', pattern) not in seen:
conditions.append(f"process.executable MATCHES \"(?i){pattern}\"")
seen.add(('process.executable', pattern))
break
# WindowsApps: avoid exact paths; prefer signer already added. No extra condition here.
# --- LoLBin-specific tightening: require command line scoping when possible ---
lolbins = {
"powershell.exe", "pwsh.exe", "cmd.exe", "wscript.exe", "cscript.exe",
"rundll32.exe", "mshta.exe", "msiexec.exe", "regsvr32.exe",
"schtasks.exe", "wmic.exe"
}
if proc_name and proc_name.lower() in lolbins:
cmd_example = None
for ctx in ctx_list:
name = ctx.get('process.name')
if name and name.lower() == proc_name.lower():
cmd_example = ctx.get('process.command_line') or ctx.get('process.args')
if cmd_example:
break
if cmd_example:
cl_pattern = re.escape(str(cmd_example))
if ('process.command_line', cl_pattern) not in seen:
conditions.append(
f"process.command_line MATCHES \"(?i).*{cl_pattern}.*\""
)
seen.add(('process.command_line', cl_pattern))
# --- Add rule name for scoping (always useful) ---
rule_name = frequent_value('rule.name') or frequent_value('rule_name')
if not rule_name and ctx_list:
# Fall back to the first context if frequency voting fails
first_ctx = ctx_list[0]
rule_name = first_ctx.get('rule.name') or first_ctx.get('rule_name')
if rule_name and ('rule.name', rule_name) not in seen:
conditions.append(f"rule.name IS \"{rule_name}\"")
seen.add(('rule.name', rule_name))
# --- Fallback: ensure min_conditions by adding from first context ---
if len(conditions) < min_conditions:
first_ctx = ctx_list[0]
fallback_keys = [
'process.name',
'process.parent.name',
'file.name',
'event.code',
'event.action',
]
for key in fallback_keys:
val = first_ctx.get(key)
if val not in (None, '', 'N/A') and (key, val) not in seen:
conditions.append(f"{key} IS \"{val}\"")
seen.add((key, val))
if len(conditions) >= min_conditions:
break
# --- Safety gate: require at least one strong indicator ---
strong_tokens = (
"hash.sha256",
"code_signature.",
"executable MATCHES",
"file.path MATCHES",
"file.name MATCHES",
"command_line MATCHES",
)
has_strong = any(
any(tok in cond for tok in strong_tokens)
for cond in conditions
)
if not has_strong:
# Too broad to be safely automated; force the caller to handle manually
return None
if not conditions:
return None
result = conditions[0]
for cond in conditions[1:]:
result += f"\nAND {cond}"
return result
def generate_llm_exception_list(alerts, api_key, model_name):
"""Generate a consolidated exception list from alert objects.
Returns (exception_list, meta) where meta contains:
warning: non-fatal message (e.g., fallback used)
error: fatal message when no exception list is returned
used_fallback: True when the deterministic builder was used
"""
meta = {"warning": None, "error": None, "used_fallback": False}
if not api_key:
meta["error"] = "Error: GEMINI_API_KEY is not set."
return None, meta
# Extract context from ALL accumulated alerts - COMPACT FORMAT
all_contexts = []
context_per_alert = []
for alert in alerts:
ctx = extract_context(alert)
if ctx:
context_per_alert.append(ctx)
# Use compact representation to save tokens and keep host/user out
compact = {k: v for k, v in ctx.items() if v and v != "N/A"}
for drop_key in ('hostname', 'host_domain', 'user_domain', 'user.name', 'user_name'):
compact.pop(drop_key, None)
all_contexts.append(json.dumps(compact))
if not all_contexts:
meta["error"] = "No relevant security fields found in the collected alerts."
return None, meta
# Compact combined context - one per line
combined_context_str = "\n".join(all_contexts)
system_instruction = (
"Create Elastic EQL exception list. ONLY output EQL query. "
"Return 3-8 lines. Every line after the first MUST start with AND. "
"TIER 1 (preferred): process.code_signature.subject_name + trusted + process.pe.original_file_name + process.name + process.parent.name (works across versions/hosts). "
"TIER 2: process.hash.sha256 + process.pe.original_file_name + process.name + process.parent.name (for unsigned tools). "
"TIER 3: process.executable (generalized) + process.name + process.parent.name + MATCHES for scripts. "
"Generalize paths: strip version numbers and WindowsApps blobs; use MATCHES \"(?i)...\" for case-insensitive regex. "
"For LOLBins (cmd.exe, powershell.exe, etc.), YOU MUST INCLUDE process.command_line. "
"ALWAYS include process.parent.name when available - critical for preventing false positives. "
"ALWAYS add rule.name for scoping. Format booleans as strings: 'true' or 'false'. "
"Do NOT include host or user fields. Avoid returning only event.action. No markdown, no prose."
)
prompt = (
f"Consolidate {len(alerts)} alerts into ONE robust EQL exception for the triggered rule.\n"
f"You MUST emit at least 3 conditions (more if available).\n"
f"PRIORITY ORDER:\n"
f"1. If code signer exists: process.code_signature.subject_name + trusted + process.pe.original_file_name + process.name + process.parent.name\n"
f"2. If hash exists: process.hash.sha256 + process.pe.original_file_name + process.name + process.parent.name\n"
f"3. Otherwise: process.executable (generalized) + process.name + process.parent.name + MATCHES pattern for scripts\n"
f"CRITICAL: ALWAYS include process.parent.name when available - prevents false positives.\n"
f"CRITICAL: For system tools (cmd, powershell, rundll32), you MUST include process.command_line to avoid broad exceptions.\n"
f"Generalize paths with wildcards: use MATCHES \"(?i)...\" for case-insensitivity. Strip versions/hashes.\n"
f"For scripts like 'tool1.4.ps1', use MATCHES '(?i)tool1\\.[0-9]+\\.ps1'.\n"
f"ALWAYS add rule.name (not rule.id or rule.uuid) for scoping. Format booleans as strings: 'true' not true.\n"
f"Exclude host/user fields. Target only processes directly involved in the alert (parent+child).\n"
f"Source alerts (one per line):\n{combined_context_str}\n"
"Output format (no prose, no markdown):\n"
"field1 IS \"value1\"\n"
"AND field2 IS \"value2\"\n"
"AND field3 IS \"value3\""
)
try:
genai.configure(api_key=api_key)
model = genai.GenerativeModel(
model_name=model_name,
system_instruction=system_instruction
)
generation_config = genai.types.GenerationConfig(
temperature=0.0,
max_output_tokens=1024,
)
response = model.generate_content(
prompt,
generation_config=generation_config
)
# Robustly extract textual content from the Gemini response.
def _extract_response_text(resp):
try:
txt = getattr(resp, 'text', None)
if txt and txt.strip():
return txt
except Exception:
pass
try:
candidates = getattr(resp, 'candidates', None) or getattr(resp, 'candidate', None)
if candidates:
parts = []
if not isinstance(candidates, (list, tuple)):
candidates = [candidates]
for c in candidates:
if isinstance(c, dict):
if 'content' in c:
cont = c['content']
if isinstance(cont, list):
for p in cont:
if isinstance(p, dict) and 'text' in p and p['text'].strip():
parts.append(p['text'])
elif isinstance(p, str) and p.strip():
parts.append(p)
elif isinstance(cont, str) and cont.strip():
parts.append(cont)
elif 'text' in c and c['text'].strip():
parts.append(c['text'])
else:
for attr in ('content', 'text', 'output'):
val = getattr(c, attr, None)
if val:
if isinstance(val, list):
for p in val:
if isinstance(p, dict):
if 'text' in p and p['text'].strip():
parts.append(p['text'])
elif 'content' in p and p['content'].strip():
parts.append(p['content'])
elif isinstance(p, str) and p.strip():
parts.append(p)
elif isinstance(val, str) and val.strip():
parts.append(val)
if parts:
break
if parts:
return "\n".join(parts)
except Exception: