Python Inline Command Execution

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.

Sigma rule (View on GitHub)

 1title: Python Inline Command Execution
 2id: 899133d5-4d7c-4a7f-94ee-27355c879d90
 3status: test
 4description: 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.
 5references:
 6    - https://docs.python.org/3/using/cmdline.html#cmdoption-c
 7    - https://www.revshells.com/
 8    - https://pentestmonkey.net/cheat-sheet/shells/reverse-shell-cheat-sheet
 9author: Nasreddine Bencherchali (Nextron Systems)
10date: 2023/01/02
11modified: 2023/02/17
12tags:
13    - attack.execution
14    - attack.t1059
15logsource:
16    category: process_creation
17    product: windows
18detection:
19    selection_img:
20        - OriginalFileName: 'python.exe'
21        - Image|endswith:
22              - 'python.exe' # no \ bc of e.g. ipython.exe
23              - 'python3.exe'
24              - 'python2.exe'
25    selection_cli:
26        CommandLine|contains: ' -c'
27    filter_python: # Based on baseline
28        ParentImage|startswith: 'C:\Program Files\Python'
29        ParentImage|endswith: '\python.exe'
30        ParentCommandLine|contains: '-E -s -m ensurepip -U --default-pip'
31    filter_vscode:
32        ParentImage|endswith: '\AppData\Local\Programs\Microsoft VS Code\Code.exe'
33    condition: all of selection_* and not 1 of filter_*
34falsepositives:
35    - Python libraries that use a flag starting with "-c". Filter according to your environment
36level: medium

References

Related rules

to-top