Detection rules › Elastic

Shellcode Execution via Python Script

Source
github.com/elastic/protections-artifacts

Identifies attempt to allocate or execute shellcode from a Python script.

MITRE ATT&CK coverage

TacticTechniques
Stealth

Rule body

[rule]
description = "Identifies attempt to allocate or execute shellcode from a Python script."
id = "1d0a1b39-a29e-4370-a712-546ed047f5f5"
license = "Elastic License v2"
name = "Shellcode Execution via Python Script"
os_list = ["windows"]
version = "1.0.8"

query = '''
api where process.code_signature.subject_name == "Python Software Foundation" and
 not process.Ext.api.name in ("TpAllocWork", "TpCallbackStart") and
 process.Ext.api.behaviors in ("shellcode", "allocate_shellcode", "execute_shellcode") and
 not process.thread.Ext.call_stack_final_user_module.protection_provenance like ("Kernel", "Kernel|*", "Unknown", "Unknown|*", "clr.dll") and
 not process.thread.Ext.call_stack_final_user_module.name in ("Unknown", "Kernel") and
 not _arraysearch(process.thread.Ext.call_stack, $entry, $entry.protection_provenance like "conemuhk64.dll*") and
 not (process.Ext.api.name == "VirtualProtect" and _arraysearch(process.thread.Ext.call_stack, $entry, $entry.symbol_info like "c:\\windows\\system32\\ntdll.dll!LdrLoadDll*")) and
 not (process.parent.executable : "C:\\Program Files\\pgAdmin ?\\runtime\\pgAdmin?.exe" and process.executable : "C:\\Program Files\\pgAdmin ?\\python\\python.exe") and
 not process.Ext.api.metadata.target_address_path : "c:\\windows\\system32\\tmumh\\20019\\tmmon\\2.9.0.1181\\tmmon64.dll" and
 not (process.thread.Ext.call_stack_final_user_module.name == "Unbacked" and process.thread.Ext.call_stack_final_user_module.protection_provenance == "Unbacked") and
 not (process.Ext.api.name in ("NtQueueApcThread", "SetThreadContext") and process.Ext.api.behaviors == "execute_shellcode" and
      _arraysearch(process.thread.Ext.call_stack, $entry, $entry.symbol_info like ("c:\\windows\\sys*\\kernelbase.dll!CreateProcess*", "c:\\program files\\eset\\eset security\\ebehmoni.dll*"))) and
 not _arraysearch(process.thread.Ext.call_stack_final_user_module.code_signature, $entry, $entry.trusted == true and
                  $entry.subject_name in ("Microsoft Windows Hardware Compatibility Publisher", "Microsoft Windows Software Compatibility Publisher", "Bromium UK Limited"))
'''

min_endpoint_version = "8.10.0"
[[actions]]
action = "kill_process"
field = "process.entity_id"
state = 0

[[optional_actions]]
action = "rollback"
field = "process.entity_id"
state = 0

[[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 = "8.10.0"

Stages and Predicates

Stage 1: api

api where process.code_signature.subject_name == "Python Software Foundation" and
 not process.Ext.api.name in ("TpAllocWork", "TpCallbackStart") and
 process.Ext.api.behaviors in ("shellcode", "allocate_shellcode", "execute_shellcode") and
 not process.thread.Ext.call_stack_final_user_module.protection_provenance like ("Kernel", "Kernel|*", "Unknown", "Unknown|*", "clr.dll") and
 not process.thread.Ext.call_stack_final_user_module.name in ("Unknown", "Kernel") and
 not _arraysearch(process.thread.Ext.call_stack, $entry, $entry.protection_provenance like "conemuhk64.dll*") and
 not (process.Ext.api.name == "VirtualProtect" and _arraysearch(process.thread.Ext.call_stack, $entry, $entry.symbol_info like "c:\\windows\\system32\\ntdll.dll!LdrLoadDll*")) and
 not (process.parent.executable : "C:\\Program Files\\pgAdmin ?\\runtime\\pgAdmin?.exe" and process.executable : "C:\\Program Files\\pgAdmin ?\\python\\python.exe") and
 not process.Ext.api.metadata.target_address_path : "c:\\windows\\system32\\tmumh\\20019\\tmmon\\2.9.0.1181\\tmmon64.dll" and
 not (process.thread.Ext.call_stack_final_user_module.name == "Unbacked" and process.thread.Ext.call_stack_final_user_module.protection_provenance == "Unbacked") and
 not (process.Ext.api.name in ("NtQueueApcThread", "SetThreadContext") and process.Ext.api.behaviors == "execute_shellcode" and
      _arraysearch(process.thread.Ext.call_stack, $entry, $entry.symbol_info like ("c:\\windows\\sys*\\kernelbase.dll!CreateProcess*", "c:\\program files\\eset\\eset security\\ebehmoni.dll*"))) and
 not _arraysearch(process.thread.Ext.call_stack_final_user_module.code_signature, $entry, $entry.trusted == true and
                  $entry.subject_name in ("Microsoft Windows Hardware Compatibility Publisher", "Microsoft Windows Software Compatibility Publisher", "Bromium UK Limited"))

Exclusions

The rule actively suppresses these predicates.

FieldKindExcluded valuesSearch
process.thread.Ext.call_stackarray_any(no value, null check)excludes:process.thread.Ext.call_stack
process.Ext.api.nameeqVirtualProtectexcludes:process.Ext.api.name field:"process.Ext.api.name" value:"VirtualProtect"
process.Ext.api.behaviorseqexecute_shellcodeexcludes:process.Ext.api.behaviors field:"process.Ext.api.behaviors" value:"execute_shellcode"
process.Ext.api.nameinNtQueueApcThread, SetThreadContextexcludes:process.Ext.api.name field:"process.Ext.api.name" value:"NtQueueApcThread" field:"process.Ext.api.name" value:"SetThreadContext"
process.thread.Ext.call_stack_final_user_module.code_signaturearray_any(no value, null check)excludes:process.thread.Ext.call_stack_final_user_module.code_signature
process.executableeqC:\Program Files\pgAdmin ?\python\python.exeexcludes:process.executable field:"process.executable" value:"C:\Program Files\pgAdmin ?\python\python.exe"
process.parent.executableeqC:\Program Files\pgAdmin ?\runtime\pgAdmin?.exeexcludes:process.parent.executable field:"process.parent.executable" value:"C:\Program Files\pgAdmin ?\runtime\pgAdmin?.exe"
process.thread.Ext.call_stack_final_user_module.nameeqUnbackedexcludes:process.thread.Ext.call_stack_final_user_module.name field:"process.thread.Ext.call_stack_final_user_module.name" value:"Unbacked"
process.thread.Ext.call_stack_final_user_module.protection_provenanceeqUnbackedexcludes:process.thread.Ext.call_stack_final_user_module.protection_provenance field:"process.thread.Ext.call_stack_final_user_module.protection_provenance" value:"Unbacked"
process.Ext.api.metadata.target_address_patheqc:\windows\system32\tmumh\20019\tmmon\2.9.0.1181\tmmon64.dllexcludes:process.Ext.api.metadata.target_address_path field:"process.Ext.api.metadata.target_address_path" value:"c:\windows\system32\tmumh\20019\tmmon\2.9.0.1181\tmmon64.dll"
process.Ext.api.nameinTpAllocWork, TpCallbackStartexcludes:process.Ext.api.name field:"process.Ext.api.name" value:"TpAllocWork" field:"process.Ext.api.name" value:"TpCallbackStart"
process.thread.Ext.call_stack_final_user_module.nameinKernel, Unknownexcludes:process.thread.Ext.call_stack_final_user_module.name field:"process.thread.Ext.call_stack_final_user_module.name" value:"Kernel" field:"process.thread.Ext.call_stack_final_user_module.name" value:"Unknown"
process.thread.Ext.call_stack_final_user_module.protection_provenancewildcardKernel, Kernel|*, Unknown, Unknown|*, clr.dllexcludes:process.thread.Ext.call_stack_final_user_module.protection_provenance

Indicators

These rows show field, operator, and value matches.

FieldKindValuesSearch
process.Ext.api.behaviorsin
  • allocate_shellcode corpus 9 (elastic 9)
  • execute_shellcode corpus 3 (elastic 3)
  • shellcode corpus 2 (elastic 2)
field:"process.Ext.api.behaviors" kind:in
process.code_signature.subject_nameeq
  • Python Software Foundation
field:"Signature" kind:eq value:"Python Software Foundation"