T1546.005 Microsoft Sentinel · KQL

Detect Trap in Microsoft Sentinel

Adversaries may establish persistence by executing malicious content triggered by an interrupt signal using the trap command. The trap command is used in Unix/Linux shell scripting to specify commands that will execute when the shell receives a particular signal. Adversaries can abuse the trap command to establish persistence that executes when the shell or system receives specific signals such as EXIT (when the shell exits), ERR (on error), or signal numbers like SIGINT (2), SIGHUP (1), or SIGTERM (15). The trap command can be placed in shell initialization files to execute malicious commands whenever a user session ends or encounters an error.

MITRE ATT&CK

Tactic
Privilege Escalation Persistence
Technique
T1546 Event Triggered Execution
Sub-technique
T1546.005 Trap
Canonical reference
https://attack.mitre.org/techniques/T1546/005/

KQL Detection Query

Microsoft Sentinel (KQL)
kusto
DeviceProcessEvents
| where Timestamp > ago(24h)
| where FileName in~ ("bash", "sh", "zsh", "ksh", "dash")
| where ProcessCommandLine has "trap"
| where ProcessCommandLine has_any (
    "EXIT", "SIGTERM", "SIGHUP", "SIGINT", "ERR",
    "0", "1", "2", "15"  // signal numbers
  )
| extend TrapPayload = extract(@"trap\s+['\"]?(.+?)['\"]?\s+(EXIT|SIGTERM|SIGHUP|SIGINT|ERR|[0-9]+)", 1, ProcessCommandLine)
| extend SuspiciousTrap = ProcessCommandLine has_any (
    "curl", "wget", "nc", "ncat", "bash -i", "/dev/tcp",
    "python", "perl", "ruby", "base64", "eval"
  )
| where SuspiciousTrap
| project Timestamp, DeviceName, AccountName, FileName, ProcessCommandLine,
         TrapPayload, SuspiciousTrap,
         InitiatingProcessFileName, InitiatingProcessCommandLine
| sort by Timestamp desc
medium severity medium confidence

Detects suspicious use of the bash/zsh trap command with potentially malicious payloads. Monitors shell process command lines for trap invocations targeting common signals (EXIT, SIGTERM, SIGHUP, SIGINT, ERR) combined with indicators of malicious activity such as download tools, reverse shell patterns, or scripting languages. Focuses on trap commands that include network utilities, encoding functions, or execution of external interpreters.

Data Sources

Process: Process CreationCommand: Command ExecutionMicrosoft Defender for Endpoint

Required Tables

DeviceProcessEvents

False Positives & Tuning

  • Shell scripts that use trap for legitimate cleanup operations (removing temp files on exit, resetting terminal settings on SIGINT)
  • Database backup scripts that use trap EXIT to ensure connection cleanup and lock release on unexpected termination
  • CI/CD pipeline scripts that use trap ERR for error handling and rollback operations
  • System administration scripts that use trap SIGTERM/SIGHUP for graceful shutdown of services or daemons
Download portable Sigma rule (.yml)

Other platforms for T1546.005


Testing Methodology

Validate this detection against 3 adversary techniques from Atomic Red Team. Each test below lists the behaviour to exercise and the telemetry you should expect to see. Executable commands and cleanup steps are available with Pro.

  1. Test 1Establish EXIT Trap in Interactive Shell

    Expected signal: Process creation event for bash with command line containing 'trap' and 'EXIT'. The curl invocation in the trap payload. When the shell exits, a child process for curl is spawned by the shell process.

  2. Test 2Add SIGTERM Trap to Shell Config

    Expected signal: File modification event for ~/.bashrc. Process creation for bash writing the trap via echo. The trap payload in the file contains wget and chmod+execute pattern — high severity indicator.

  3. Test 3Trap ERR Signal for Stealthy Execution

    Expected signal: Process creation for bash -c with trap ERR in command. The ls command fails with non-zero exit (directory doesn't exist), triggering the ERR trap. Child process (echo) spawned from the bash session. File creation event for /tmp/trap_test.txt.

Unlock Pro Content

Get the full detection package for T1546.005 including response playbook, investigation guide, and atomic red team tests.

Response PlaybookInvestigation GuideHunting QueriesAtomic Red Team TestsTuning Guidance

Related Detections