Detection rules › Sigma

Python Inline Command Execution

Status
test
Severity
medium
Log source
product windows, category process_creation
Author
Nasreddine Bencherchali (Nextron Systems)
Source
github.com/SigmaHQ/sigma

Detects execution of python using the "-c" flag. This is could be used as a way to launch a reverse shell or execute live python code.

MITRE ATT&CK coverage

Event coverage

ProviderEventTitle
SysmonEvent ID 1Process creation

Rule body yaml

title: Python Inline Command Execution
id: 899133d5-4d7c-4a7f-94ee-27355c879d90
status: test
description: Detects execution of python using the "-c" flag. This is could be used as a way to launch a reverse shell or execute live python code.
references:
    - https://docs.python.org/3/using/cmdline.html#cmdoption-c
    - https://www.revshells.com/
    - https://pentestmonkey.net/cheat-sheet/shells/reverse-shell-cheat-sheet
author: Nasreddine Bencherchali (Nextron Systems)
date: 2023-01-02
modified: 2025-10-07
tags:
    - attack.execution
    - attack.t1059
logsource:
    category: process_creation
    product: windows
detection:
    selection_img:
        - OriginalFileName: 'python.exe'
        - Image|endswith:
              - 'python.exe' # no \ bc of e.g. ipython.exe
              - 'python3.exe'
              - 'python2.exe'
    selection_cli:
        CommandLine|contains: ' -c'
    filter_main_python_1: # Based on baseline
        ParentImage|startswith:
            - 'C:\Program Files\Python'
            - 'C:\Program Files (x86)\Python'
        ParentImage|endswith: '\python.exe'
        ParentCommandLine|contains: '-E -s -m ensurepip -U --default-pip'
    filter_main_python_trace: # Based on baseline
        ParentImage|startswith:
            - 'C:\Program Files\Python'
            - 'C:\Program Files (x86)\Python'
        CommandLine|contains|all:
            # CommandLine: \"C:\\Program Files\\Python312\\python.exe\" -W ignore::DeprecationWarning -c \"\nimport runpy\nimport sys\nsys.path = ['C:\\\\Users\\\\User\\\\AppData\\\\Local\\\\Temp\\\\tmpdakwn6aj\\\\pip-23.2.1-py3-none-any.whl'] + sys.path\nsys.argv[1:] = ['install', '--no-cache-dir', '--no-index', '--find-links', 'C:\\\\Users\\\\User\\\\AppData\\\\Local\\\\Temp\\\\tmpdakwn6aj', '--upgrade', 'pip']\nrunpy.run_module(\\\"pip\\\", run_name=\\\"__main__\\\", alter_sys=True)\n\
            - '-W ignore::DeprecationWarning'
            - "['install', '--no-cache-dir', '--no-index', '--find-links',"
            - "'--upgrade', 'pip'"
    filter_optional_vscode:
        - ParentImage|endswith: '\AppData\Local\Programs\Microsoft VS Code\Code.exe'
        - ParentImage:
              - 'C:\Program Files\Microsoft VS Code\Code.exe'
              - 'C:\Program Files (x86)\Microsoft VS Code\Code.exe'
    filter_optional_pip:
        CommandLine|contains|all:
            - '<pip-setuptools-caller>'
            - 'exec(compile('
    condition: all of selection_* and not 1 of filter_main_* and not 1 of filter_optional_*
falsepositives:
    - Python libraries that use a flag starting with "-c". Filter according to your environment
level: medium

Stages and Predicates

Stage 0: condition

all of selection_* and not 1 of filter_main_* and not 1 of filter_optional_*

Stage 1: selection_img

selection_img:
    - OriginalFileName: 'python.exe'
    - Image|endswith:
          - 'python.exe'
          - 'python3.exe'
          - 'python2.exe'

Stage 2: selection_cli

selection_cli:
    CommandLine|contains: ' -c'

Stage 3: not filter_main_*

filter_main_python_1:
    ParentImage|startswith:
        - 'C:\Program Files\Python'
        - 'C:\Program Files (x86)\Python'
    ParentImage|endswith: '\python.exe'
    ParentCommandLine|contains: '-E -s -m ensurepip -U --default-pip'
filter_main_python_trace:
    ParentImage|startswith:
        - 'C:\Program Files\Python'
        - 'C:\Program Files (x86)\Python'
    CommandLine|contains|all:
        - '-W ignore::DeprecationWarning'
        - "['install', '--no-cache-dir', '--no-index', '--find-links',"
        - "'--upgrade', 'pip'"

Stage 4: not filter_optional_*

filter_optional_vscode:
    - ParentImage|endswith: '\AppData\Local\Programs\Microsoft VS Code\Code.exe'
    - ParentImage:
          - 'C:\Program Files\Microsoft VS Code\Code.exe'
          - 'C:\Program Files (x86)\Microsoft VS Code\Code.exe'
filter_optional_pip:
    CommandLine|contains|all:
        - '<pip-setuptools-caller>'
        - 'exec(compile('

Exclusions

Top-level NOT(...) conjuncts: predicates this rule actively suppresses.

FieldKindExcluded values
ParentImagestarts_withC:\Program Files (x86)\Python
ParentImagestarts_withC:\Program Files\Python
CommandLinematch'--upgrade', 'pip'
CommandLinematch-W ignore::DeprecationWarning
CommandLinematch['install', '--no-cache-dir', '--no-index', '--find-links',
ParentImagestarts_withC:\Program Files (x86)\Python
ParentImagestarts_withC:\Program Files\Python
ParentCommandLinematch-E -s -m ensurepip -U --default-pip
ParentImageends_with\python.exe
CommandLinematch<pip-setuptools-caller>
CommandLinematchexec(compile(
ParentImageends_with\AppData\Local\Programs\Microsoft VS Code\Code.exe
ParentImageeqC:\Program Files (x86)\Microsoft VS Code\Code.exe
ParentImageeqC:\Program Files\Microsoft VS Code\Code.exe

Indicators

Each row is a field, operator, and value that the rule matches. The corpus column counts how many other rules in the catalog look for the same combination: high numbers point to widely-used, community-vetted indicators. Blank or 1 shows that the indicator is specific to this rule.

FieldKindValues
CommandLinematch
  • -c corpus 5 (sigma 5)
Imageends_with
  • python.exe corpus 2 (sigma 2)
  • python2.exe corpus 2 (sigma 2)
  • python3.exe corpus 2 (sigma 2)
OriginalFileNameeq
  • python.exe