Detection rules › Elastic

Suspicious Task for Pid System Call

Source
github.com/elastic/protections-artifacts

Identifies when a process invoke the task_for_pid system service which gives a process the capability to control another process. A process needs root privileges to call task_for_pid or possesses the com.apple.security.get-task-allow entitlement. This type of behavior may indicate inter-process code injection or legit debugging activity. Adversaries may inject code into processes in order to evade process-based defenses as well as possibly elevate privileges.

MITRE ATT&CK coverage

TacticTechniques
Stealth

Rule body

[rule]
description = """
Identifies when a process invoke the task_for_pid system service which gives a process the capability to control another
process. A process needs root privileges to call task_for_pid or possesses the com.apple.security.get-task-allow
entitlement. This type of behavior may indicate inter-process code injection or legit debugging activity. Adversaries
may inject code into processes in order to evade process-based defenses as well as possibly elevate privileges.
"""
id = "572ec53e-fc79-43ad-8bdd-573999ff70d7"
license = "Elastic License v2"
name = "Suspicious Task for Pid System Call"
os_list = ["macos"]
reference = [
    "http://os-tres.net/blog/2010/02/17/mac-os-x-and-task-for-pid-mach-call/",
    "https://knight.sc/malware/2019/03/15/code-injection-on-macos.html",
    "https://blog.xpnsec.com/macos-injection-via-third-party-frameworks/",
    "https://wojciechregula.blog/post/learn-xpc-exploitation-part-3-code-injections/",
]
version = "1.0.38"

query = '''
process where event.action == "process_open" and
 (process.code_signature.trusted == false or process.code_signature.exists == false) and 
 not process.executable like~ ("/private/var/folders/*", 
                               "/Users/*/.pyenv/*",
                               "/opt/homebrew/*",
                               "/nix/store/*",
                               "/Users/*/.homebrew/*",
                               "/Users/*/miniconda3/envs/*",
                               "/usr/local/Cellar/*",
                               "/usr/local/Caskroom/*",
                               "/System/*") and 
  not process.name == "htop" and 
  not Target.process.executable like "/Users/*/anaconda3/envs/python*" and
  not (startswith~(process.parent.executable, Target.process.executable) or 
   startswith~(process.executable, Target.process.executable)) and 
  not process.hash.sha256 : ("d41e02b5366b186a237f33150baa7c5ef4747ba06646a193ab9ba5125fc6ef7a",
                             "bee1a00ffb1c4b7c2c4784a1fdcb849dc7853b9deb5173ff4c5a08209c84bc26",
                             "6ac6f81d80afff9b6ee9d3608d9964566a39188d0166ae867df97c624e632b98",
                             "8698f4e54c64667c4a4c7ccde3fbcd7babc6478a5e8e22732732f633cc2fc499") and 
  not (process.executable like "/Users/*/micromamba/envs/grid/bin/python*" and Target.process.executable in ("/bin/bash", "/usr/bin/which", "/usr/bin/uname")) and
  not (process.executable like "/Users/*/.local/share/uv/python/cpython-*-macos-aarch64-none/bin/python*" and Target.process.executable like "/Users/*/Code/eis-ray/.venv/lib/python*/site-packages/ray/core/src/ray/gcs/gcs_server") and 
  not (process.executable like "/Users/*/anaconda?/bin/python*" and Target.process.executable like "/Users/*/anaconda?/envs/python*/bin/python*")
'''

min_endpoint_version = "8.4.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 = "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.4.0"

Stages and Predicates

Stage 1: process

process where event.action == "process_open" and
 (process.code_signature.trusted == false or process.code_signature.exists == false) and 
 not process.executable like~ ("/private/var/folders/*", 
                               "/Users/*/.pyenv/*",
                               "/opt/homebrew/*",
                               "/nix/store/*",
                               "/Users/*/.homebrew/*",
                               "/Users/*/miniconda3/envs/*",
                               "/usr/local/Cellar/*",
                               "/usr/local/Caskroom/*",
                               "/System/*") and 
  not process.name == "htop" and 
  not Target.process.executable like "/Users/*/anaconda3/envs/python*" and
  not (startswith~(process.parent.executable, Target.process.executable) or 
   startswith~(process.executable, Target.process.executable)) and 
  not process.hash.sha256 : ("d41e02b5366b186a237f33150baa7c5ef4747ba06646a193ab9ba5125fc6ef7a",
                             "bee1a00ffb1c4b7c2c4784a1fdcb849dc7853b9deb5173ff4c5a08209c84bc26",
                             "6ac6f81d80afff9b6ee9d3608d9964566a39188d0166ae867df97c624e632b98",
                             "8698f4e54c64667c4a4c7ccde3fbcd7babc6478a5e8e22732732f633cc2fc499") and 
  not (process.executable like "/Users/*/micromamba/envs/grid/bin/python*" and Target.process.executable in ("/bin/bash", "/usr/bin/which", "/usr/bin/uname")) and
  not (process.executable like "/Users/*/.local/share/uv/python/cpython-*-macos-aarch64-none/bin/python*" and Target.process.executable like "/Users/*/Code/eis-ray/.venv/lib/python*/site-packages/ray/core/src/ray/gcs/gcs_server") and 
  not (process.executable like "/Users/*/anaconda?/bin/python*" and Target.process.executable like "/Users/*/anaconda?/envs/python*/bin/python*")

Exclusions

The rule actively suppresses these predicates.

FieldKindExcluded valuesSearch
Target.process.executablein/bin/bash, /usr/bin/uname, /usr/bin/whichexcludes:Target.process.executable field:"Target.process.executable" value:"/bin/bash" field:"Target.process.executable" value:"/usr/bin/uname" field:"Target.process.executable" value:"/usr/bin/which"
process.executablewildcard/Users/*/micromamba/envs/grid/bin/python*excludes:process.executable field:"process.executable" value:"/Users/*/micromamba/envs/grid/bin/python*"
Target.process.executablewildcard/Users/*/Code/eis-ray/.venv/lib/python*/site-packages/ray/core/src/ray/gcs/gcs_serverexcludes:Target.process.executable field:"Target.process.executable" value:"/Users/*/Code/eis-ray/.venv/lib/python*/site-packages/ray/core/src/ray/gcs/gcs_server"
process.executablewildcard/Users/*/.local/share/uv/python/cpython-*-macos-aarch64-none/bin/python*excludes:process.executable field:"process.executable" value:"/Users/*/.local/share/uv/python/cpython-*-macos-aarch64-none/bin/python*"
Target.process.executablewildcard/Users/*/anaconda?/envs/python*/bin/python*excludes:Target.process.executable field:"Target.process.executable" value:"/Users/*/anaconda?/envs/python*/bin/python*"
process.executablewildcard/Users/*/anaconda?/bin/python*excludes:process.executable field:"process.executable" value:"/Users/*/anaconda?/bin/python*"
process.executablestarts_withTarget.process.executableexcludes:process.executable field:"process.executable" value:"Target.process.executable"
process.parent.executablestarts_withTarget.process.executableexcludes:process.parent.executable field:"process.parent.executable" value:"Target.process.executable"
Target.process.executablewildcard/Users/*/anaconda3/envs/python*excludes:Target.process.executable field:"Target.process.executable" value:"/Users/*/anaconda3/envs/python*"
process.executablewildcard/private/var/folders/*, /Users/*/.pyenv/*, /opt/homebrew/*, /nix/store/*, /Users/*/.homebrew/*, /Users/*/miniconda3/envs/*, /usr/local/Cellar/*, /usr/local/Caskroom/*, /System/*excludes:process.executable
process.hash.sha256eqd41e02b5366b186a237f33150baa7c5ef4747ba06646a193ab9ba5125fc6ef7a, bee1a00ffb1c4b7c2c4784a1fdcb849dc7853b9deb5173ff4c5a08209c84bc26, 6ac6f81d80afff9b6ee9d3608d9964566a39188d0166ae867df97c624e632b98, 8698f4e54c64667c4a4c7ccde3fbcd7babc6478a5e8e22732732f633cc2fc499excludes:process.hash.sha256
process.nameeqhtopexcludes:process.name field:"process.name" value:"htop"

Indicators

These rows show field, operator, and value matches.