Skip to content

Commit 011f249

Browse files
[BUG] test_all_rule_queries_optimized does not run on rules (#2823)
* Fixed kql -> kuery in test_all_rule_queries_opt... * all queries optimized * manually reconciled all rules that failed due to toml escaped chars * merge rules from main * Rules needing optimization * Fix optimized note * fix another note * another note fix * fixing whitespace * Updated for readability --------- Co-authored-by: terrancedejesus <[email protected]> Co-authored-by: Mika Ayenson <[email protected]> Removed changes from: - rules/cross-platform/guided_onboarding_sample_rule.toml - rules/linux/command_and_control_suspicious_network_activity_from_unknown_executable.toml - rules/linux/persistence_shared_object_creation.toml (selectively cherry picked from commit aaa4ce2)
1 parent 2053502 commit 011f249

11 files changed

+118
-122
lines changed

rules/cross-platform/execution_pentest_eggshell_remote_admin_tool.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ timestamp_override = "event.ingested"
2323
type = "query"
2424

2525
query = '''
26-
event.category:process and event.type:(start or process_started) and process.name:espl and process.args:eyJkZWJ1ZyI6*
26+
event.category:process and event.type:(process_started or start) and process.name:espl and process.args:eyJkZWJ1ZyI6*
2727
'''
2828

2929

rules/integrations/kubernetes/initial_access_anonymous_request_authorized.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ timestamp_override = "event.ingested"
3838
type = "query"
3939

4040
query = '''
41-
event.dataset : "kubernetes.audit_logs"
42-
and kubernetes.audit.annotations.authorization_k8s_io/decision:"allow"
43-
and (kubernetes.audit.user.username:("system:anonymous" or "system:unauthenticated") or not kubernetes.audit.user.username:*)
44-
and not kubernetes.audit.objectRef.resource:("healthz" or "livez" or "readyz")
41+
event.dataset:kubernetes.audit_logs
42+
and kubernetes.audit.annotations.authorization_k8s_io/decision:allow
43+
and kubernetes.audit.user.username:("system:anonymous" or "system:unauthenticated" or not *)
44+
and not kubernetes.audit.objectRef.resource:(healthz or livez or readyz)
4545
'''
4646

4747

rules/linux/persistence_credential_access_modify_ssh_binaries.toml

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ timestamp_override = "event.ingested"
2929
type = "query"
3030

3131
query = '''
32-
event.category:file and host.os.type:linux and event.type:change and
33-
process.name:* and
34-
(file.path:(/usr/sbin/sshd or /usr/bin/ssh or /usr/bin/sftp or /usr/bin/scp) or file.name:libkeyutils.so) and
35-
not process.name:("dpkg" or "yum" or "dnf" or "dnf-automatic")
32+
event.category:file and host.os.type:linux and event.type:change and
33+
process.name:(* and not (dnf or dnf-automatic or dpkg or yum)) and
34+
(file.path:(/usr/bin/scp or
35+
/usr/bin/sftp or
36+
/usr/bin/ssh or
37+
/usr/sbin/sshd) or
38+
file.name:libkeyutils.so)
3639
'''
3740

3841

@@ -48,7 +51,6 @@ reference = "https://attack.mitre.org/techniques/T1543/"
4851
id = "TA0003"
4952
name = "Persistence"
5053
reference = "https://attack.mitre.org/tactics/TA0003/"
51-
5254
[[rule.threat]]
5355
framework = "MITRE ATT&CK"
5456
[[rule.threat.technique]]
@@ -61,12 +63,18 @@ reference = "https://attack.mitre.org/techniques/T1556/"
6163
id = "TA0006"
6264
name = "Credential Access"
6365
reference = "https://attack.mitre.org/tactics/TA0006/"
64-
65-
66-
67-
6866
[[rule.threat]]
6967
framework = "MITRE ATT&CK"
68+
[[rule.threat.technique]]
69+
id = "T1021"
70+
name = "Remote Services"
71+
reference = "https://attack.mitre.org/techniques/T1021/"
72+
[[rule.threat.technique.subtechnique]]
73+
id = "T1021.004"
74+
name = "SSH"
75+
reference = "https://attack.mitre.org/techniques/T1021/004/"
76+
77+
7078
[[rule.threat.technique]]
7179
id = "T1563"
7280
name = "Remote Service Session Hijacking"
@@ -76,16 +84,10 @@ id = "T1563.001"
7684
name = "SSH Hijacking"
7785
reference = "https://attack.mitre.org/techniques/T1563/001/"
7886

79-
[[rule.threat.technique]]
80-
id = "T1021"
81-
name = "Remote Services"
82-
reference = "https://attack.mitre.org/techniques/T1021/"
83-
[[rule.threat.technique.subtechnique]]
84-
id = "T1021.004"
85-
name = "SSH"
86-
reference = "https://attack.mitre.org/techniques/T1021/004/"
87+
8788

8889
[rule.threat.tactic]
8990
id = "TA0008"
9091
name = "Lateral Movement"
9192
reference = "https://attack.mitre.org/tactics/TA0008/"
93+

rules/macos/defense_evasion_modify_environment_launchctl.toml

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,20 @@ timestamp_override = "event.ingested"
2929
type = "query"
3030

3131
query = '''
32-
event.category:process and host.os.type:macos and event.type:start and
33-
process.name:launchctl and
34-
process.args:(setenv and not (JAVA*_HOME or
35-
RUNTIME_JAVA_HOME or
36-
DBUS_LAUNCHD_SESSION_BUS_SOCKET or
37-
ANT_HOME or
38-
LG_WEBOS_TV_SDK_HOME or
39-
WEBOS_CLI_TV or
40-
EDEN_ENV)
41-
) and
42-
not process.parent.executable:("/Applications/NoMachine.app/Contents/Frameworks/bin/nxserver.bin" or
43-
"/usr/local/bin/kr" or
44-
"/Applications/NoMachine.app/Contents/Frameworks/bin/nxserver.bin" or
45-
"/Applications/IntelliJ IDEA CE.app/Contents/jbr/Contents/Home/lib/jspawnhelper") and
46-
not process.args : "*.vmoptions"
32+
event.category:process and host.os.type:macos and event.type:start and
33+
process.name:launchctl and
34+
process.args:(setenv and not (ANT_HOME or
35+
DBUS_LAUNCHD_SESSION_BUS_SOCKET or
36+
EDEN_ENV or
37+
LG_WEBOS_TV_SDK_HOME or
38+
RUNTIME_JAVA_HOME or
39+
WEBOS_CLI_TV or
40+
JAVA*_HOME) and
41+
not *.vmoptions) and
42+
not process.parent.executable:("/Applications/IntelliJ IDEA CE.app/Contents/jbr/Contents/Home/lib/jspawnhelper" or
43+
/Applications/NoMachine.app/Contents/Frameworks/bin/nxserver.bin or
44+
/Applications/NoMachine.app/Contents/Frameworks/bin/nxserver.bin or
45+
/usr/local/bin/kr)
4746
'''
4847

4948

rules/macos/defense_evasion_sandboxed_office_app_suspicious_zip_file.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ timestamp_override = "event.ingested"
3131
type = "query"
3232

3333
query = '''
34-
event.category:file and host.os.type:macos and not event.type:deletion and file.name:~$*.zip and host.os.type:macos
34+
event.category:file and host.os.type:(macos and macos) and not event.type:deletion and file.name:~$*.zip
3535
'''
3636

3737

rules/windows/collection_posh_clipboard_capture.toml

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ reg add "hklm\\SOFTWARE\\Policies\\Microsoft\\Windows\\PowerShell\\ScriptBlockLo
7474
```
7575
"""
7676
references = [
77-
"https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-clipboard",
78-
"https://github.com/EmpireProject/Empire/blob/master/data/module_source/collection/Get-ClipboardContents.ps1",
77+
"https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-clipboard",
78+
"https://github.com/EmpireProject/Empire/blob/master/data/module_source/collection/Get-ClipboardContents.ps1",
7979
]
8080
risk_score = 47
8181
rule_id = "92984446-aefb-4d5e-ad12-598042ca80ba"
@@ -85,17 +85,14 @@ timestamp_override = "event.ingested"
8585
type = "query"
8686

8787
query = '''
88-
event.category:process and host.os.type:windows and
89-
(powershell.file.script_block_text : (
90-
"Windows.Clipboard" or
91-
"Windows.Forms.Clipboard" or
92-
"Windows.Forms.TextBox"
93-
) and
94-
powershell.file.script_block_text : (
95-
"]::GetText" or
96-
".Paste()"
97-
)) or powershell.file.script_block_text : "Get-Clipboard"
98-
and not user.id : "S-1-5-18"
88+
event.category:process and host.os.type:windows and
89+
powershell.file.script_block_text:((Windows.Clipboard or
90+
Windows.Forms.Clipboard or
91+
Windows.Forms.TextBox) and
92+
(".Paste()" or
93+
"]::GetText")) or
94+
powershell.file.script_block_text:Get-Clipboard and
95+
not user.id:S-1-5-18
9996
'''
10097

10198

rules/windows/collection_posh_keylogger.toml

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,23 @@ timestamp_override = "event.ingested"
8585
type = "query"
8686

8787
query = '''
88-
event.category:process and host.os.type:windows and
89-
(
90-
powershell.file.script_block_text : (GetAsyncKeyState or NtUserGetAsyncKeyState or GetKeyboardState or "Get-Keystrokes") or
91-
powershell.file.script_block_text : (
92-
(SetWindowsHookA or SetWindowsHookW or SetWindowsHookEx or SetWindowsHookExA or NtUserSetWindowsHookEx) and
93-
(GetForegroundWindow or GetWindowTextA or GetWindowTextW or "WM_KEYBOARD_LL")
94-
)
95-
) and not user.id : "S-1-5-18"
88+
event.category:process and host.os.type:windows and
89+
powershell.file.script_block_text : (
90+
Get-Keystrokes or GetAsyncKeyState or GetKeyboardState or NtUserGetAsyncKeyState or
91+
(
92+
NtUserSetWindowsHookEx or
93+
SetWindowsHookA or
94+
SetWindowsHookEx or
95+
SetWindowsHookExA or
96+
SetWindowsHookW
97+
) and
98+
(
99+
GetForegroundWindow or
100+
GetWindowTextA or
101+
GetWindowTextW or
102+
WM_KEYBOARD_LL)
103+
)
104+
and not user.id:S-1-5-18
96105
'''
97106

98107

rules/windows/collection_posh_mailbox.toml

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,13 @@ timestamp_override = "event.ingested"
8686
type = "query"
8787

8888
query = '''
89-
event.category:process and host.os.type:windows and
90-
(
91-
powershell.file.script_block_text : (
92-
"Microsoft.Office.Interop.Outlook" or
93-
"Interop.Outlook.olDefaultFolders" or
94-
"::olFolderInBox"
95-
) or
96-
powershell.file.script_block_text : (
97-
"Microsoft.Exchange.WebServices.Data.Folder" or
98-
"Microsoft.Exchange.WebServices.Data.FileAttachment"
99-
)
100-
)
89+
event.category:process and host.os.type:windows and
90+
powershell.file.script_block_text : (
91+
"::olFolderInBox" or
92+
Interop.Outlook.olDefaultFolders or
93+
Microsoft.Exchange.WebServices.Data.FileAttachment or
94+
Microsoft.Exchange.WebServices.Data.Folder or
95+
Microsoft.Office.Interop.Outlook)
10196
'''
10297

10398

@@ -107,7 +102,6 @@ framework = "MITRE ATT&CK"
107102
id = "T1114"
108103
name = "Email Collection"
109104
reference = "https://attack.mitre.org/techniques/T1114/"
110-
111105
[[rule.threat.technique.subtechnique]]
112106
id = "T1114.001"
113107
name = "Local Email Collection"
@@ -118,26 +112,27 @@ id = "T1114.002"
118112
name = "Remote Email Collection"
119113
reference = "https://attack.mitre.org/techniques/T1114/002/"
120114

115+
116+
121117
[rule.threat.tactic]
122118
id = "TA0009"
123119
name = "Collection"
124120
reference = "https://attack.mitre.org/tactics/TA0009/"
125-
126121
[[rule.threat]]
127122
framework = "MITRE ATT&CK"
128-
129123
[[rule.threat.technique]]
130124
id = "T1059"
131125
name = "Command and Scripting Interpreter"
132126
reference = "https://attack.mitre.org/techniques/T1059/"
133-
134127
[[rule.threat.technique.subtechnique]]
135128
id = "T1059.001"
136129
name = "PowerShell"
137130
reference = "https://attack.mitre.org/techniques/T1059/001/"
138131

139132

133+
140134
[rule.threat.tactic]
141135
id = "TA0002"
142136
name = "Execution"
143137
reference = "https://attack.mitre.org/tactics/TA0002/"
138+

rules/windows/defense_evasion_amsi_bypass_powershell.toml

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -104,26 +104,25 @@ timestamp_override = "event.ingested"
104104
type = "query"
105105

106106
query = '''
107-
event.category:"process" and host.os.type:windows and
108-
(powershell.file.script_block_text :
109-
("System.Management.Automation.AmsiUtils" or
110-
amsiInitFailed or
111-
"Invoke-AmsiBypass" or
112-
"Bypass.AMSI" or
113-
"amsi.dll" or
114-
AntimalwareProvider or
115-
amsiSession or
116-
amsiContext or
117-
"System.Management.Automation.ScriptBlock" or
118-
AmsiInitialize or
119-
unloadobfuscated or
120-
unloadsilent or
121-
AmsiX64 or
122-
AmsiX32 or
123-
FindAmsiFun) or
124-
powershell.file.script_block_text:("[System.Runtime.InteropServices.Marshal]::Copy" and "VirtualProtect") or
125-
powershell.file.script_block_text:("[Ref].Assembly.GetType(('System.Management.Automation" and ".SetValue(")
126-
)
107+
event.category:process and host.os.type:windows and
108+
powershell.file.script_block_text : (
109+
AmsiInitialize or
110+
AmsiX32 or
111+
AmsiX64 or
112+
AntimalwareProvider or
113+
Bypass.AMSI or
114+
FindAmsiFun or
115+
Invoke-AmsiBypass or
116+
System.Management.Automation.AmsiUtils or
117+
System.Management.Automation.ScriptBlock or
118+
amsi.dll or
119+
amsiContext or
120+
amsiInitFailed or
121+
amsiSession or
122+
unloadobfuscated or
123+
unloadsilent or
124+
VirtualProtect and "[System.Runtime.InteropServices.Marshal]::Copy" or
125+
".SetValue(" and "[Ref].Assembly.GetType(('System.Management.Automation")
127126
'''
128127

129128

rules/windows/defense_evasion_posh_encryption.toml

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,44 +26,39 @@ timestamp_override = "event.ingested"
2626
type = "query"
2727

2828
query = '''
29-
event.category:process and host.os.type:windows and
30-
powershell.file.script_block_text : (
31-
(
32-
"Cryptography.AESManaged" or
33-
"Cryptography.RijndaelManaged" or
34-
"Cryptography.SHA1Managed" or
35-
"Cryptography.SHA256Managed" or
36-
"Cryptography.SHA384Managed" or
37-
"Cryptography.SHA512Managed" or
38-
"Cryptography.SymmetricAlgorithm" or
39-
"PasswordDeriveBytes" or
40-
"Rfc2898DeriveBytes"
41-
) and
42-
(
43-
CipherMode and PaddingMode
44-
) and
45-
(
46-
".CreateEncryptor" or
47-
".CreateDecryptor"
48-
)
49-
) and not user.id : "S-1-5-18"
29+
event.category:process and host.os.type:windows and
30+
powershell.file.script_block_text : (
31+
CipherMode and
32+
PaddingMode and
33+
(
34+
Cryptography.AESManaged or
35+
Cryptography.RijndaelManaged or
36+
Cryptography.SHA1Managed or
37+
Cryptography.SHA256Managed or
38+
Cryptography.SHA384Managed or
39+
Cryptography.SHA512Managed or
40+
Cryptography.SymmetricAlgorithm or
41+
PasswordDeriveBytes or
42+
Rfc2898DeriveBytes
43+
) and (.CreateDecryptor or .CreateEncryptor)) and not user.id:S-1-5-18
5044
'''
5145

5246

5347
[[rule.threat]]
5448
framework = "MITRE ATT&CK"
55-
[[rule.threat.technique]]
56-
id = "T1140"
57-
name = "Deobfuscate/Decode Files or Information"
58-
reference = "https://attack.mitre.org/techniques/T1140/"
59-
6049
[[rule.threat.technique]]
6150
id = "T1027"
6251
name = "Obfuscated Files or Information"
6352
reference = "https://attack.mitre.org/techniques/T1027/"
6453

54+
[[rule.threat.technique]]
55+
id = "T1140"
56+
name = "Deobfuscate/Decode Files or Information"
57+
reference = "https://attack.mitre.org/techniques/T1140/"
58+
6559

6660
[rule.threat.tactic]
6761
id = "TA0005"
6862
name = "Defense Evasion"
6963
reference = "https://attack.mitre.org/tactics/TA0005/"
64+

tests/test_all_rules.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def test_file_names(self):
5959
def test_all_rule_queries_optimized(self):
6060
"""Ensure that every rule query is in optimized form."""
6161
for rule in self.production_rules:
62-
if rule.contents.data.get("language") == "kql":
62+
if rule.contents.data.get("language") == "kuery":
6363
source = rule.contents.data.query
6464
tree = kql.parse(source, optimize=False)
6565
optimized = tree.optimize(recursive=True)

0 commit comments

Comments
 (0)