Detect Trap in Google Chronicle
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/
YARA-L Detection Query
rule t1546_005_trap_persistence {
meta:
author = "Argus Detection Engineering"
description = "Detects T1546.005 - Trap command abuse for persistence via signal handlers containing suspicious payloads"
mitre_attack_tactic = "Persistence"
mitre_attack_technique = "T1546.005"
severity = "HIGH"
confidence = "HIGH"
events:
$e.metadata.event_type = "PROCESS_LAUNCH"
$e.principal.process.file.full_path = /\/(bash|sh|zsh|ksh|dash)$/
$e.target.process.command_line = /\btrap\b/
$e.target.process.command_line = /(EXIT|SIGTERM|SIGHUP|SIGINT|\bERR\b)/
$e.target.process.command_line = /(curl|wget|\bnc\s|ncat|\/dev\/tcp|bash\s+-i|python[23]?\s+-c|perl\s+-e|base64|\beval\b)/i
condition:
$e
} Chronicle YARA-L 2.0 rule detecting process launch events where a shell interpreter (bash, sh, zsh, ksh, dash) executes a trap command referencing EXIT, SIGTERM, SIGHUP, SIGINT, or ERR signals while the command line also contains suspicious payload indicators associated with reverse shells, remote code execution, or data exfiltration. Maps to MITRE ATT&CK T1546.005.
Data Sources
Required Tables
False Positives & Tuning
- Legitimate system administration scripts that trap signals for proper cleanup and happen to use wget to download updated configuration files before exiting
- Monitoring agents that register trap handlers to send telemetry via curl to internal SIEM or observability platforms
- Python-based orchestration tools that wrap shell scripts with trap-based error handlers that invoke python for logging
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.
- 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.
- 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.
- 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.