Detection rules › Elastic
Managed .NET Code Execution via PowerShell
Identifies the compilation of managed code via Powershell with suspicious command-line patterns, this could indicate code injection or other form of suspicious code execution via Windows PowerShell.
MITRE ATT&CK coverage
| Tactic | Techniques |
|---|---|
| Execution | |
| Stealth |
Rule body
[rule]
description = """
Identifies the compilation of managed code via Powershell with suspicious command-line patterns, this could indicate
code injection or other form of suspicious code execution via Windows PowerShell.
"""
id = "cd886776-8790-4724-9484-3f0008b87da7"
license = "Elastic License v2"
name = "Managed .NET Code Execution via PowerShell"
os_list = ["windows"]
version = "1.0.32"
query = '''
process where event.type == "start" and
process.name : "csc.exe" and process.parent.name : "powershell.exe" and
not user.id : ("S-1-5-18", "S-1-5-19", "S-1-5-20") and
(
/* long command-line potentially encoded */
(length(process.parent.command_line) >= 200 and process.parent.args : ("-e", "-enc", "-ep", "-encoded", "*;iex")) or
/* suspicious args */
process.parent.command_line : ("*HKCU*", "* IEX*", "*^*^*^*^*^*^*^*^*^*", "*.replace*", "*Reflection.Assembly*",
"*set *set *set *", "*;iex*", "*IEX (*", "*FromBase64String*", "*.downloadstring*", "*.downloadfile*", "*.downloaddata*",
"*.webclient)*")
) and
not process.parent.args : ("?:\\Program Files (x86)\\*", "?:\\Program Files\\*", "visualstudio20??-workload-vctools;") and
not process.args : "@?:\\Windows\\TEMP\\*.cmdline" and
not (process.parent.args : "-ExecutionPolicy" and process.parent.command_line : "*.vscode\\extensions*") and
not process.parent.command_line : ("*http://localhost:9900/CaptureManager?wsdl*", "*KMS_VL_ALL_AIO.cmd*",
"*ServerURI*https*.office365.com*", "*Invoke-ps2exe -inputFile*",
"*amazonaws.com/scripts/FleetSweep.ps1*",
"*EnterpriseServices/Integrations/IntegrationHelper.svc*",
"*chocolatey.org*", "*amazonaws.com/scripts/Leaf-Agent_*",
"*Activator*MAS_*", "*:\\Program Files (x86)\\Universal\\Host\\host.dll*",
"*E:\\3Activator.cmd*", "*https://install.telivy.com/scan-passwords.ps1*",
"*C:\\temp\\PS2EXE-GUI\\ps2exe.ps1*",
"*repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper*") and
not (process.parent.args == "$execWrapper" and process.parent.args == "$execWrapper.Split(@(\"`0`0`0`0\")," and process.parent.args == "\"ANSIBLE_BOOTSTRAP_ERROR:") and
not process.parent.args : ("JABtAD0AZgBvAHIAZQBhAGMAaAAoACQAaQAgAGkAbgAgACQAaQBuAHAAdQB0ACkAewAKA*",
"& { [Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12; $registryPath='HKCU:\\SOFTWARE\\SOTI\\XSight'; New-Item -Path $registryPath -Force; $isPSNuGetProviderExist=(Get-PackageProvider -Name 'NuGet' -ea Ignore).Version -ge [System.Version]'2.8.5.201'; New-ItemProperty 'IsPSNuGetProviderExist' -Path $registryPath -Value $isPSNuGetProviderExist -PropertyType DWORD -Force; $isPSSqlServerModuleExist=(Get-InstalledModule -Name 'SqlServer' -ea Ignore).Version -ge [System.Version]'21.1.18256'; New-ItemProperty 'IsPSSqlServerModuleExist' -Path $registryPath -Value $isPSSqlServerModuleExist -PropertyType DWORD -Force } ")
'''
min_endpoint_version = "7.15.0"
optional_actions = []
[[actions]]
action = "kill_process"
field = "process.entity_id"
state = 0
[[actions]]
action = "kill_process"
field = "process.parent.entity_id"
state = 0
[[threat]]
framework = "MITRE ATT&CK"
[[threat.technique]]
id = "T1059"
name = "Command and Scripting Interpreter"
reference = "https://attack.mitre.org/techniques/T1059/"
[[threat.technique.subtechnique]]
id = "T1059.001"
name = "PowerShell"
reference = "https://attack.mitre.org/techniques/T1059/001/"
[threat.tactic]
id = "TA0002"
name = "Execution"
reference = "https://attack.mitre.org/tactics/TA0002/"
[[threat]]
framework = "MITRE ATT&CK"
[[threat.technique]]
id = "T1055"
name = "Process Injection"
reference = "https://attack.mitre.org/techniques/T1055/"
[threat.tactic]
id = "TA0005"
name = "Defense Evasion"
reference = "https://attack.mitre.org/tactics/TA0005/"
[internal]
min_endpoint_version = "7.15.0"
Stages and Predicates
Stage 1: process
process where event.type == "start" and
process.name : "csc.exe" and process.parent.name : "powershell.exe" and
not user.id : ("S-1-5-18", "S-1-5-19", "S-1-5-20") and
(
(length(process.parent.command_line) >= 200 and process.parent.args : ("-e", "-enc", "-ep", "-encoded", "*;iex")) or
process.parent.command_line : ("*HKCU*", "* IEX*", "*^*^*^*^*^*^*^*^*^*", "*.replace*", "*Reflection.Assembly*",
"*set *set *set *", "*;iex*", "*IEX (*", "*FromBase64String*", "*.downloadstring*", "*.downloadfile*", "*.downloaddata*",
"*.webclient)*")
) and
not process.parent.args : ("?:\\Program Files (x86)\\*", "?:\\Program Files\\*", "visualstudio20??-workload-vctools;") and
not process.args : "@?:\\Windows\\TEMP\\*.cmdline" and
not (process.parent.args : "-ExecutionPolicy" and process.parent.command_line : "*.vscode\\extensions*") and
not process.parent.command_line : ("*http://localhost:9900/CaptureManager?wsdl*", "*KMS_VL_ALL_AIO.cmd*",
"*ServerURI*https*.office365.com*", "*Invoke-ps2exe -inputFile*",
"*amazonaws.com/scripts/FleetSweep.ps1*",
"*EnterpriseServices/Integrations/IntegrationHelper.svc*",
"*chocolatey.org*", "*amazonaws.com/scripts/Leaf-Agent_*",
"*Activator*MAS_*", "*:\\Program Files (x86)\\Universal\\Host\\host.dll*",
"*E:\\3Activator.cmd*", "*https://install.telivy.com/scan-passwords.ps1*",
"*C:\\temp\\PS2EXE-GUI\\ps2exe.ps1*",
"*repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper*") and
not (process.parent.args == "$execWrapper" and process.parent.args == "$execWrapper.Split(@(\"`0`0`0`0\")," and process.parent.args == "\"ANSIBLE_BOOTSTRAP_ERROR:") and
not process.parent.args : ("JABtAD0AZgBvAHIAZQBhAGMAaAAoACQAaQAgAGkAbgAgACQAaQBuAHAAdQB0ACkAewAKA*",
"& { [Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12; $registryPath='HKCU:\\SOFTWARE\\SOTI\\XSight'; New-Item -Path $registryPath -Force; $isPSNuGetProviderExist=(Get-PackageProvider -Name 'NuGet' -ea Ignore).Version -ge [System.Version]'2.8.5.201'; New-ItemProperty 'IsPSNuGetProviderExist' -Path $registryPath -Value $isPSNuGetProviderExist -PropertyType DWORD -Force; $isPSSqlServerModuleExist=(Get-InstalledModule -Name 'SqlServer' -ea Ignore).Version -ge [System.Version]'21.1.18256'; New-ItemProperty 'IsPSSqlServerModuleExist' -Path $registryPath -Value $isPSSqlServerModuleExist -PropertyType DWORD -Force } ")
Exclusions
The rule actively suppresses these predicates.
| Field | Kind | Excluded values | Search |
|---|---|---|---|
process.parent.args | eq | $execWrapper | excludes:process.parent.args field:"process.parent.args" value:"$execWrapper" |
process.parent.args | eq | $execWrapper.Split(@("`0`0`0`0"), | excludes:process.parent.args |
process.parent.args | eq | "ANSIBLE_BOOTSTRAP_ERROR: | excludes:process.parent.args |
process.parent.args | eq | -ExecutionPolicy | excludes:process.parent.args field:"process.parent.args" value:"-ExecutionPolicy" |
process.parent.command_line | match | .vscode\extensions | excludes:process.parent.command_line field:"process.parent.command_line" value:".vscode\extensions" |
process.args | wildcard | @?:\Windows\TEMP\*.cmdline | excludes:process.args field:"process.args" value:"@?:\Windows\TEMP\*.cmdline" |
process.parent.args | wildcard | ?:\Program Files (x86)\*, ?:\Program Files\*, visualstudio20??-workload-vctools; | excludes:process.parent.args field:"process.parent.args" value:"?:\Program Files (x86)\*" field:"process.parent.args" value:"?:\Program Files\*" field:"process.parent.args" value:"visualstudio20??-workload-vctools;" |
process.parent.args | wildcard | JABtAD0AZgBvAHIAZQBhAGMAaAAoACQAaQAgAGkAbgAgACQAaQBuAHAAdQB0ACkAewAKA*, & { [Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12; $registryPath='HKCU:\SOFTWARE\SOTI\XSight'; New-Item -Path $registryPath -Force; $isPSNuGetProviderExist=(Get-PackageProvider -Name 'NuGet' -ea Ignore).Version -ge [System.Version]'2.8.5.201'; New-ItemProperty 'IsPSNuGetProviderExist' -Path $registryPath -Value $isPSNuGetProviderExist -PropertyType DWORD -Force; $isPSSqlServerModuleExist=(Get-InstalledModule -Name 'SqlServer' -ea Ignore).Version -ge [System.Version]'21.1.18256'; New-ItemProperty 'IsPSSqlServerModuleExist' -Path $registryPath -Value $isPSSqlServerModuleExist -PropertyType DWORD -Force } | excludes:process.parent.args field:"process.parent.args" value:"JABtAD0AZgBvAHIAZQBhAGMAaAAoACQAaQAgAGkAbgAgACQAaQBuAHAAdQB0ACkAewAKA*" field:"process.parent.args" value:"& { [Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12; $registryPath='HKCU:\SOFTWARE\SOTI\XSight'; New-Item -Path $registryPath -Force; $isPSNuGetProviderExist=(Get-PackageProvider -Name 'NuGet' -ea Ignore).Version -ge [System.Version]'2.8.5.201'; New-ItemProperty 'IsPSNuGetProviderExist' -Path $registryPath -Value $isPSNuGetProviderExist -PropertyType DWORD -Force; $isPSSqlServerModuleExist=(Get-InstalledModule -Name 'SqlServer' -ea Ignore).Version -ge [System.Version]'21.1.18256'; New-ItemProperty 'IsPSSqlServerModuleExist' -Path $registryPath -Value $isPSSqlServerModuleExist -PropertyType DWORD -Force } " |
process.parent.command_line | wildcard | *http://localhost:9900/CaptureManager?wsdl*, *KMS_VL_ALL_AIO.cmd*, *ServerURI*https*.office365.com*, *Invoke-ps2exe -inputFile*, *amazonaws.com/scripts/FleetSweep.ps1*, *EnterpriseServices/Integrations/IntegrationHelper.svc*, *chocolatey.org*, *amazonaws.com/scripts/Leaf-Agent_*, *Activator*MAS_*, *:\Program Files (x86)\Universal\Host\host.dll*, *E:\3Activator.cmd*, *https://install.telivy.com/scan-passwords.ps1*, *C:\temp\PS2EXE-GUI\ps2exe.ps1*, *repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper* | excludes:process.parent.command_line |
user.id | eq | S-1-5-18, S-1-5-19, S-1-5-20 | excludes:user.id field:"user.id" value:"S-1-5-18" field:"user.id" value:"S-1-5-19" field:"user.id" value:"S-1-5-20" |
Indicators
These rows show field, operator, and value matches.
| Field | Kind | Values | Search |
|---|---|---|---|
event.type | eq |
| field:"event.type" kind:eq value:"start" |
process.name | wildcard |
| field:"process_name" kind:wildcard value:"csc.exe" |
process.parent.args | wildcard |
| field:"process.parent.args" kind:wildcard |
process.parent.command_line | wildcard |
| field:"ParentCommandLine" kind:wildcard |
process.parent.name | wildcard |
| field:"parent_process_name" kind:wildcard value:"powershell.exe" |