T1059.004 Microsoft Sentinel · KQL

Detect Unix Shell in Microsoft Sentinel

Adversaries may abuse Unix shell commands and scripts for execution. Unix shells are the primary command prompt on Linux, macOS, and ESXi systems, though many variations exist (sh, ash, bash, zsh, etc.). Unix shells can control every aspect of a system, with certain commands requiring elevated privileges. Adversaries may abuse Unix shells to execute various commands or payloads, access interactive shells through C2 channels, leverage shell scripts for persistence, or use stripped-down shells via Busybox on embedded devices and ESXi servers.

MITRE ATT&CK

Tactic
Execution
Technique
T1059 Command and Scripting Interpreter
Sub-technique
T1059.004 Unix Shell
Canonical reference
https://attack.mitre.org/techniques/T1059/004/

KQL Detection Query

Microsoft Sentinel (KQL)
kusto
let SuspiciousPatterns = dynamic([
  "/dev/tcp/", "/dev/udp/",
  "bash -i >& /dev/tcp",
  "nc -e /bin/", "ncat -e", "socat exec:",
  "curl | bash", "curl | sh", "wget | bash", "wget | sh",
  "curl -s | bash", "wget -q | bash",
  "base64 -d", "base64 --decode",
  "python -c 'import socket", "python3 -c 'import socket",
  "perl -e 'use Socket",
  "mkfifo /tmp/", "mknod /tmp/",
  "chmod +s", "chmod 4755", "chmod u+s",
  "crontab -", "/etc/cron",
  "useradd", "usermod -aG",
  "iptables -F", "iptables -P ACCEPT"
]);
DeviceProcessEvents
| where Timestamp > ago(24h)
| where FileName in~ ("bash", "sh", "zsh", "dash", "ash", "ksh", "csh", "tcsh", "busybox")
| where ProcessCommandLine has_any (SuspiciousPatterns)
| extend ReverseShell = ProcessCommandLine has_any ("/dev/tcp/", "nc -e", "ncat -e", "socat exec:")
| extend CurlPipe = ProcessCommandLine has_any ("curl | bash", "curl | sh", "wget | bash", "wget | sh", "curl -s | bash")
| extend Base64Decode = ProcessCommandLine has_any ("base64 -d", "base64 --decode")
| extend PrivEsc = ProcessCommandLine has_any ("chmod +s", "chmod 4755", "useradd", "usermod -aG")
| project Timestamp, DeviceName, AccountName, FileName, ProcessCommandLine,
         InitiatingProcessFileName, InitiatingProcessCommandLine,
         ReverseShell, CurlPipe, Base64Decode, PrivEsc
| sort by Timestamp desc
high severity high confidence

Detects suspicious Unix shell execution patterns on Linux, macOS, and ESXi endpoints using MDE. Identifies reverse shells (/dev/tcp, netcat, socat), curl/wget pipe-to-shell patterns, base64 decoding (obfuscation), and privilege escalation attempts (SUID bit, user creation). Covers bash, sh, zsh, dash, ash, ksh, csh, and busybox.

Data Sources

Process: Process CreationCommand: Command ExecutionMicrosoft Defender for Endpoint

Required Tables

DeviceProcessEvents

False Positives & Tuning

  • DevOps tools and CI/CD pipelines that use curl|bash patterns for software installation (e.g., install.sh scripts)
  • System administrators running legitimate setup scripts that decode base64-encoded configuration
  • Configuration management tools (Ansible, Chef, Puppet, SaltStack) executing shell commands remotely
Download portable Sigma rule (.yml)

Other platforms for T1059.004


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 1Bash Reverse Shell via /dev/tcp

    Expected signal: Auditd: EXECVE record for bash with /dev/tcp in arguments. Syslog: bash process creation. Network connection attempt to 127.0.0.1:4444 (will fail without listener). MDE DeviceProcessEvents on managed Linux endpoints.

  2. Test 2Curl Pipe to Bash

    Expected signal: Auditd: EXECVE records for curl and bash. Process tree shows curl piped to bash. Network connection attempt to 127.0.0.1:8080 (will fail without listener). The curl failure means no content reaches bash.

  3. Test 3Base64 Encoded Command Execution

    Expected signal: Auditd: EXECVE records for echo, base64, and bash. The decoded content 'whoami' will be executed. Syslog captures the process chain.

Unlock Pro Content

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

Response PlaybookInvestigation GuideHunting QueriesAtomic Red Team TestsTuning Guidance

Related Detections