T1059.006

Python

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.

Microsoft Sentinel / Defender
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

Data Sources

Process: Process Creation Command: Command Execution Microsoft Defender for Endpoint

Required Tables

DeviceProcessEvents

False Positives

  • 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)

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