T1546.005 Elastic Security · Elastic

Detect Trap in Elastic Security

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/

Elastic Detection Query

Elastic Security (Elastic)
eql
process where event.type == "start"
  and process.name in ("bash", "sh", "zsh", "ksh", "dash")
  and process.args : "trap"
  and process.command_line : ("*EXIT*", "*SIGTERM*", "*SIGHUP*", "*SIGINT*", "*ERR*")
  and process.command_line : ("*curl*", "*wget*", "*nc *", "*ncat*", "*bash -i*", "*/dev/tcp*", "*python*", "*perl*", "*ruby*", "*base64*", "*eval*")
high severity high confidence

Detects shell processes (bash, sh, zsh, ksh, dash) executing trap commands tied to process-exit or error signals while also containing suspicious payload indicators such as reverse shells, downloaders, or code execution utilities. This pattern is indicative of T1546.005 where adversaries embed malicious commands in trap handlers to achieve persistence or execute code on session termination.

Data Sources

Endpoint process telemetry via Elastic Agent or Auditbeat

Required Tables

logs-endpoint.events.process-*auditbeat-*

False Positives & Tuning

  • Legitimate shell scripts that use trap for cleanup routines (e.g., removing temp files on EXIT) that happen to invoke curl or wget as part of a post-execution reporting step
  • DevOps automation scripts that register signal handlers to notify external monitoring systems via HTTP calls on script termination
  • Security tooling or backup scripts that use trap with base64-encoded configuration data for cleanup operations
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