# Malicious Package Techniques

## The `setup.py` Install Script

Always check for a setup.py file in a package.&#x20;

When you `pip install` a package, during the install process, Python will automatically execute the setup.py file if one exists. This is why you should never run `pip` with sudo or administrator rights. The `setup.py` is an easy target for attackers, so always review the file contents. This attack vector is becoming more known, so you will most likely only catch script kiddies using it.

## The `__init__.py` File

Every package contains an `__init__.py` file, because it is used to mark directories on disk as a Python package. When you import a package, the  `__init__.py` file is automatically executed. This is how the malicious package `ascii2text` was able to run code to steal passwords and upload them to a web hook.

## Package Name Typo Squatting

Adversaries will create package names that mimic popular packages or remove a letter so that you accidentally download their package.&#x20;

For example, the malicious package `chekov` typo squatting on the legitimate package `checkov` .

<figure><img src="/files/A6Uo2icY8EBeNBhtkcK9" alt=""><figcaption><p>checkov vs. chekov</p></figcaption></figure>

Not only are the package names similar, but the project description is the exact same.&#x20;

## Package Name Squatting

Package name squatting occurs when a popular python package exists on another site (like Github) and not on pypi.org. Adversaries see this as an opportunity to claim the name on pypi.org and upload a malicious package.&#x20;

This happened to PyTorch back in [December of 2022](https://adamcysec.github.io/PyTorch-dependency-confusion/). PyTorch’s preview build had a dependency called `torchtriton` that was hosted only on their [3rd party package indexer site](https://download.pytorch.org/whl/), therefore an attacker was able to claim this name on pypi.org.&#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://adamcysec.gitbook.io/csapp/threat-hunting/malicious-package-techniques.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
