Trap
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.
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 Data Sources
Required Tables
False Positives
- 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
Unlock Pro Content
Get the full detection package for T1546.005 including response playbook, investigation guide, and atomic red team tests.