T1059.006 Microsoft Sentinel · KQL

Detect Python in Microsoft Sentinel

Adversaries may abuse Python commands and scripts for execution. Python is a cross-platform scripting language that can be executed interactively from the command-line (via python.exe/python3), via scripts (.py), or compiled into binary executables. Python's built-in libraries for file operations, networking (socket, urllib, requests), and system interaction make it a powerful tool for adversaries. Threat actors including APT31, APT37, MuddyWater, and Contagious Interview have used Python-based implants, reverse shells, and backdoors across Windows, Linux, macOS, and ESXi environments.

MITRE ATT&CK

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

KQL Detection Query

Microsoft Sentinel (KQL)
kusto
let SuspiciousPatterns = dynamic([
  "import socket", "import subprocess", "import os",
  "import urllib", "import requests", "import http.client",
  "socket.socket", "subprocess.call", "subprocess.Popen",
  "os.system(", "os.popen(", "exec(", "eval(",
  "compile(", "__import__",
  "base64.b64decode", "codecs.decode",
  "pty.spawn", "/bin/sh", "/bin/bash",
  "reverse_tcp", "reverse_shell",
  "pyinstaller", "py2exe", "nuitka"
]);
DeviceProcessEvents
| where Timestamp > ago(24h)
| where FileName in~ ("python.exe", "python3.exe", "python", "python3", "pythonw.exe")
| where ProcessCommandLine has_any (SuspiciousPatterns)
| extend ReverseShell = ProcessCommandLine has_any ("socket.socket", "pty.spawn", "/bin/sh", "reverse_shell")
| extend DownloadExec = ProcessCommandLine has_any ("urllib", "requests", "http.client")
| extend SubprocessExec = ProcessCommandLine has_any ("subprocess.call", "subprocess.Popen", "os.system", "os.popen")
| extend CodeExec = ProcessCommandLine has_any ("exec(", "eval(", "compile(", "__import__")
| project Timestamp, DeviceName, AccountName, FileName, ProcessCommandLine,
         InitiatingProcessFileName, InitiatingProcessCommandLine,
         ReverseShell, DownloadExec, SubprocessExec, CodeExec
| sort by Timestamp desc
high severity medium confidence

Detects suspicious Python execution patterns across Windows, Linux, and macOS using MDE. Identifies Python reverse shells, network download and execution via urllib/requests, subprocess spawning, and dynamic code execution (exec/eval). Covers python.exe, python3.exe, python, python3, and pythonw.exe (headless).

Data Sources

Process: Process CreationCommand: Command ExecutionMicrosoft Defender for Endpoint

Required Tables

DeviceProcessEvents

False Positives & Tuning

  • Developers and data scientists running Python scripts that import networking or subprocess libraries
  • DevOps automation tools (Ansible, SaltStack) that execute Python for system configuration
  • CI/CD pipelines running Python test suites with subprocess calls
  • Monitoring and observability agents written in Python (Datadog, Checkmk)
Download portable Sigma rule (.yml)

Other platforms for T1059.006


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 1Python Reverse Shell One-Liner

    Expected signal: Auditd: EXECVE record for python3 with -c flag and socket.socket in arguments. Network connection attempt to 127.0.0.1:4444. MDE DeviceProcessEvents and DeviceNetworkEvents on managed endpoints.

  2. Test 2Python Download and Execute

    Expected signal: Sysmon Event ID 1: Process Create for python.exe with urllib in CommandLine. Sysmon Event ID 3: Network Connection to 127.0.0.1:8080 (will fail without listener). The exec() call triggers even though download fails.

  3. Test 3Python Subprocess Command Execution

    Expected signal: Sysmon Event ID 1: Process Create for python.exe with subprocess.Popen in CommandLine. Child process event for cmd.exe spawned by python.exe.

Unlock Pro Content

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

Response PlaybookInvestigation GuideHunting QueriesAtomic Red Team TestsTuning Guidance

Related Detections