T1059.006 Elastic Security · Elastic

Detect Python in Elastic Security

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/

Elastic Detection Query

Elastic Security (Elastic)
eql
process where event.type == "start"
  and process.name in~ ("python", "python3", "python.exe", "python3.exe", "pythonw.exe")
  and (
    process.args : ("*import socket*", "*import subprocess*", "*import os*", "*import urllib*", "*import requests*", "*import http.client*")
    or process.args : ("*socket.socket*", "*subprocess.call*", "*subprocess.Popen*", "*os.system(*", "*os.popen(*")
    or process.args : ("*exec(*", "*eval(*", "*compile(*", "*__import__*")
    or process.args : ("*base64.b64decode*", "*codecs.decode*", "*pty.spawn*", "*/bin/sh*", "*/bin/bash*")
    or process.args : ("*reverse_tcp*", "*reverse_shell*", "*pyinstaller*", "*py2exe*", "*nuitka*")
    or process.args : ("*urllib*", "*requests*", "*http.client*")
  )
high severity medium confidence

Detects suspicious Python interpreter invocations indicative of MITRE ATT&CK T1059.006. Flags command-line arguments associated with reverse shells (socket.socket, pty.spawn), download-and-execute patterns (urllib, requests), subprocess abuse (os.system, subprocess.Popen), and dynamic code execution (exec, eval, compile, __import__). Covers Windows and Linux hosts via ECS process events.

Data Sources

Elastic Endpoint Security (endpoint.events.process)Auditbeat (auditd module)Winlogbeat (Sysmon Event ID 1)

Required Tables

logs-endpoint.events.process-*logs-system.security-*winlogbeat-*auditbeat-*

False Positives & Tuning

  • Legitimate developer tooling (Jupyter notebooks, Flask/Django dev servers, Ansible playbooks) that import socket or subprocess as part of normal operation
  • System automation scripts run by ops teams using requests or urllib for health checks and API polling
  • Security tooling such as SIEM agents, vulnerability scanners, or EDR components written in Python that use subprocess or socket internally
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