Detection rules › YARA-L
MITRE ATT&CK T1140 Encoded Powershell Command
Detects encoded powershell commands
MITRE ATT&CK coverage
| Tactic | Techniques |
|---|---|
| Stealth |
Telemetry coverage
| Provider | Record / event type |
|---|---|
| Sysmon | Event ID 1: Process creation |
| Security-Auditing | Event ID 4688: A new process has been created. |
Rule body
// Copyright 2023 Google LLC. Licensed under Apache-2.0.
rule mitre_attack_T1140_encoded_powershell_command {
meta:
author = "Google Cloud Security"
description = "Detects encoded powershell commands"
rule_id = "mr_c01d7d92-db49-4044-a2c5-5a16c74a141d"
rule_name = "MITRE ATT&CK T1140 Encoded Powershell Command"
tactic = "TA0005"
technique = "T1140"
type = "hunt"
platform = "Windows"
data_source = "microsoft sysmon, microsoft windows events"
severity = "Info"
priority = "Info"
events:
$process.metadata.event_type = "PROCESS_LAUNCH"
$process.principal.hostname = $hostname
re.regex($process.target.process.file.full_path, `(system32|syswow64)\\WindowsPowerShell\\v1\.0\\powershell(|\_ise)\.exe`) nocase
re.regex($process.target.process.command_line, `(?i)(?:-enc|-ec|-en)\s*\S*`)
$encoded_value = re.capture($process.target.process.command_line, `(?i)(?:-enc|-ec|-en)\s*(\S*)`)
$decoded_value = re.replace(strings.base64_decode(re.capture($process.target.process.command_line, `(?i)(?:-enc|-ec|-en)\s*(\S*)`)),`\0`, "")
match:
$hostname over 5m
outcome:
$risk_score = 10
$event_count = count_distinct($process.metadata.id)
$encoded_powershell = array_distinct($encoded_value)
$decoded_powershell = array_distinct($decoded_value)
// added to populate alert graph with additional context
// Commented out principal.hostname because it is already represented in graph as match variable. If match changes, can uncomment to add to results
$principal_hostname = array_distinct($process.principal.hostname)
$principal_process_pid = array_distinct($process.principal.process.pid)
$principal_process_command_line = array_distinct($process.principal.process.command_line)
$principal_process_file_sha256 = array_distinct($process.principal.process.file.sha256)
$principal_process_file_full_path = array_distinct($process.principal.process.file.full_path)
$principal_process_product_specific_process_id = array_distinct($process.principal.process.product_specific_process_id)
$principal_process_parent_process_product_specific_process_id = array_distinct($process.principal.process.parent_process.product_specific_process_id)
$target_process_pid = array_distinct($process.target.process.pid)
$target_process_command_line = array_distinct($process.target.process.command_line)
$target_process_file_sha256 = array_distinct($process.target.process.file.sha256)
$target_process_file_full_path = array_distinct($process.target.process.file.full_path)
$target_process_product_specific_process_id = array_distinct($process.target.process.product_specific_process_id)
$principal_user_userid = array_distinct($process.principal.user.userid)
condition:
$process
}
YARA-L rule structure
Condition
Fires when at least one $process event in the 5m window.
Events
$process
target.process.file.full_path matches "(system32|syswow64)\\WindowsPowerShell\\v1\.0\\powershell(|\_ise)\.exe"target.process.command_line matches "(?i)(?:-enc|-ec|-en)\s*\S*"
Match and correlation
Outcome
Outcome variables add context to a detection. When the rule generates an alert, $risk_score is displayed with it.
| Field | Expression |
|---|---|
risk_score | 10 |
event_count | count_distinct($process.metadata.id) |
encoded_powershell | array_distinct($encoded_value) |
decoded_powershell | array_distinct($decoded_value) |
principal_hostname | array_distinct($process.principal.hostname) |
principal_process_pid | array_distinct($process.principal.process.pid) |
principal_process_command_line | array_distinct($process.principal.process.command_line) |
principal_process_file_sha256 | array_distinct($process.principal.process.file.sha256) |
principal_process_file_full_path | array_distinct($process.principal.process.file.full_path) |
principal_process_product_specific_process_id | array_distinct($process.principal.process.product_specific_process_id) |
principal_process_parent_process_product_specific_process_id | array_distinct($process.principal.process.parent_process.product_specific_process_id) |
target_process_pid | array_distinct($process.target.process.pid) |
target_process_command_line | array_distinct($process.target.process.command_line) |
target_process_file_sha256 | array_distinct($process.target.process.file.sha256) |
target_process_file_full_path | array_distinct($process.target.process.file.full_path) |
target_process_product_specific_process_id | array_distinct($process.target.process.product_specific_process_id) |
principal_user_userid | array_distinct($process.principal.user.userid) |