Malicious Python Code Gains Execution

https://blog.phylum.io/how-malicious-python-code-gains-execution/

The primary vector for malicious code running in software developer environments (e.g., local system, CI/CD runners, production servers, etc.) is software dependencies. This is third-party code which often means open-source software, also known as running code from strangers on the internet.

The prized goal for attackers is arbitrary code execution. It’s the stuff high CVE scores are made of and often the topic of how vulnerabilities can turn into exploits. It’s the foothold needed to run cryptominers, steal secrets, or encrypt data for ransom. It’s no wonder why threat actors want it, but how do they get it? Sutton’s Law makes it obvious why they go after open-source software: because executing arbitrary code is easy there.

This is a series examining the methods malicious Python code gains execution. Some of the methods are obvious and some are potentially undiscovered or at least not found in the wild, yet. What they all mostly have in common is the reliance on a software dependency in the form of a Python package, which is where we begin.

Threat modeling is a useful defensive exercise to predict and prevent future attacks. By thinking like a malicious actor, we can identify the attack surface, enumerate possible compromise vectors, and neutralize them with considered countermeasures. As security researchers, we’ll don a hat of a darker color to put ourselves in the right mindset. Certainly not a black hat, but maybe more of a gray thinking cap. The remainder of the series documents these findings.

Putting our white hat back on, there are countermeasures to protect developers from these attacks. First, use a lockfile every time an environment is created to ensure reproducibility. Then, guard against any changes to that lockfile by automatically monitoring the health of the lockfile and the dependencies contained therein. Finally, don’t allow arbitrary code to run anywhere in your development process.

Phylum can detect, report, and block malicious packages. Other solutions are merely looking for known vulnerabilities and will therefore miss this entire risk domain. Use Phylum to analyze dependencies. Integrations exist to guard PRs with a free GitHub app or a GitHub action. There is also a CLI and pre-commit hook for local development, as well as a phylum Python package that can be pip/pipx installed. Additional supported CI platforms include GitLab CI, Azure Pipelines, and Bitbucket Pipelines, with more coming.

At the time of this writing, Phylum offers Python lockfile and manifest support for pip, pipenv, and poetry. A free community edition is available for everyone to automate software supply chain security to block new risks, prioritize existing issues, and only use trusted open-source code.

--cta--

{
"by": "louislang",
"descendants": 0,
"id": 40211569,
"score": 6,
"time": 1714488635,
"title": "Malicious Python Code Gains Execution",
"type": "story",
"url": "https://blog.phylum.io/how-malicious-python-code-gains-execution/"
}
{
"author": "Charles Coggins",
"date": "2024-05-16T16:47:00.000Z",
"description": "The primary vector for malicious code running in software developer environments (e.g., local system, CI/CD runners, production servers, etc.) is software dependencies. This is third-party code which often means open-source software, also known as running code from strangers on the internet. The prized goal for attackers is arbitrary",
"image": "https://blog.phylum.io/content/images/size/w1200/2024/04/photo-1690683790356-c1edb75e3df7.jpeg",
"logo": "https://logo.clearbit.com/phylum.io",
"publisher": "Phylum",
"title": "Series: How Malicious Python Code Gains Execution",
"url": "https://blog.phylum.io/how-malicious-python-code-gains-execution/"
}
{
"url": "https://blog.phylum.io/how-malicious-python-code-gains-execution/",
"title": "Series: How Malicious Python Code Gains Execution",
"description": "The primary vector for malicious code running in software developer environments (e.g., local system, CI/CD runners, production servers, etc.) is software dependencies. This is third-party code which often means open-source software, also known as running code from strangers on the internet.\n\nThe prized goal for attackers is arbitrary",
"links": [
"https://blog.phylum.io/how-malicious-python-code-gains-execution/"
],
"image": "https://blog.phylum.io/content/images/size/w1200/2024/04/photo-1690683790356-c1edb75e3df7.jpeg",
"content": "<div>\n <p>The primary vector for malicious code running in software developer environments (e.g., local system, CI/CD runners, production servers, etc.) is software dependencies. This is third-party code which often means open-source software, also known as running code from strangers on the internet.</p><p>The prized goal for attackers is arbitrary code execution. It’s the stuff high CVE scores are made of and often the topic of how vulnerabilities can turn into exploits. It’s the foothold needed to run <a target=\"_blank\" href=\"https://blog.phylum.io/cryptocurrency-miner-masquerading-as-gcc-compiler-found-in-npm-package/\">cryptominers</a>, <a target=\"_blank\" href=\"https://blog.phylum.io/a-deep-dive-into-powerat-a-newly-discovered-stealer-rat-combo-polluting-pypi/\">steal secrets</a>, or <a target=\"_blank\" href=\"https://blog.phylum.io/phylum-detects-active-typosquatting-campaign-in-pypi/\">encrypt data for ransom</a>. It’s no wonder why threat actors want it, but how do they get it? <a target=\"_blank\" href=\"https://en.wikipedia.org/wiki/Sutton%27s_law\">Sutton’s Law</a> makes it obvious why they go after open-source software: because executing arbitrary code is easy there.</p><p>This is a series examining the methods malicious Python code gains execution. Some of the methods are obvious and some are potentially undiscovered or at least not found in the wild, yet. What they all mostly have in common is the reliance on a software dependency in the form of a Python package, which is where we begin.</p><ul><li><a target=\"_blank\" href=\"https://blog.phylum.io/python-package-spoofing/\">Python Package Spoofing</a></li></ul><p>Threat modeling is a useful defensive exercise to predict and prevent future attacks. By thinking like a malicious actor, we can identify the attack surface, enumerate possible compromise vectors, and neutralize them with considered countermeasures. As security researchers, we’ll don a hat of a darker color to put ourselves in the right mindset. Certainly not a black hat, but maybe more of a gray thinking cap. The remainder of the series documents these findings.</p><ul><li><a target=\"_blank\" href=\"https://blog.phylum.io/python-trojan-functions-and-imports/\">Python Trojan Functions and Imports</a></li><li><a target=\"_blank\" href=\"https://blog.phylum.io/python-package-installation-attacks/\">Python Package Installation Attacks</a></li><li><a target=\"_blank\" href=\"https://blog.phylum.io/devious-python-build-requirements/\">Devious Python Build Requirements</a></li><li><a target=\"_blank\" href=\"https://blog.phylum.io/modern-python-build-hooks/\">Modern Python Build Hooks</a></li><li><a target=\"_blank\" href=\"https://blog.phylum.io/adding-spurious-wheels-to-pypi/\">Adding Spurious Wheels to PyPI</a></li><li><a target=\"_blank\" href=\"https://blog.phylum.io/python-executable-hooks/\">Python Executable Hooks</a></li><li><a target=\"_blank\" href=\"https://blog.phylum.io/compiled-python-files/\">Compiled Python Files</a></li><li>(More links will be added here as new posts in the series are published)</li></ul><p>Putting our white hat back on, there are countermeasures to protect developers from these attacks. First, <a target=\"_blank\" href=\"https://blog.phylum.io/pick-a-python-lockfile-and-improve-security/\">use a lockfile</a> every time an environment is created to ensure reproducibility. Then, guard against any changes to that lockfile by automatically monitoring the health of the lockfile and the dependencies contained therein. Finally, don’t allow arbitrary code to run anywhere in your development process.</p><p><a target=\"_blank\" href=\"https://www.phylum.io/\">Phylum</a> can detect, report, and block <strong>malicious</strong> packages. Other solutions are merely looking for known vulnerabilities and will therefore miss this entire risk domain. Use Phylum to analyze dependencies. <a target=\"_blank\" href=\"https://docs.phylum.io/integrations/integrations_overview\">Integrations exist</a> to guard PRs with <a target=\"_blank\" href=\"https://github.com/marketplace/phylum-io\">a free GitHub app</a> or <a target=\"_blank\" href=\"https://docs.phylum.io/phylum-ci/github_actions\">a GitHub action</a>. There is also a CLI and <code>pre-commit</code> hook for local development, as well as a <code>phylum</code> Python package that can be <code>pip</code>/<code>pipx</code> installed. Additional supported CI platforms include GitLab CI, Azure Pipelines, and Bitbucket Pipelines, with more coming.</p><p>At the time of this writing, Phylum offers Python <a target=\"_blank\" href=\"https://docs.phylum.io/cli/supported_lockfiles\">lockfile</a> and <a target=\"_blank\" href=\"https://docs.phylum.io/cli/lockfile_generation\">manifest</a> support for <code>pip</code>, <code>pipenv</code>, and <code>poetry</code>. A <a target=\"_blank\" href=\"https://app.phylum.io/register\">free community edition</a> is available for everyone to automate software supply chain security to block new risks, prioritize existing issues, and only use trusted open-source code.</p><p>--cta--</p>\n </div>",
"author": "",
"favicon": "https://blog.phylum.io/content/images/size/w256h256/2023/03/phylum-logo.png",
"source": "blog.phylum.io",
"published": "2024-05-16T16:47:00.000Z",
"ttr": 93,
"type": "article"
}