githubEdit

Python Privilege Escalation

Python Privilege Escalation

There are multiple ways to escalate with python

EXAMPLES:

MODULE HIJACKING

1)

SETENV: NOPASSWD /usr/bin/python /home/whatever/script.py

2) Check if you can overwrite an already existing module in python, or simply create the missing import module that the script asks

E.g.: ImportError: No module named pwned

3) Create the malicious binary in any directory we have write access to (/tmp directory as usual)

4) The payload can be

import os; os.system('/bin/bash') 

or a python reverse shell.

5)

chmod +x pwned.py

6)

7) Enjoy root!

SUDO PRIVILEGE ESCALATION

1)

2) If the python script is under the current user's home directory, we can remove the script and create the new one with the same name.

3) Insert payload

4) GGEZ

MODULE OVERRIDING

If the Python script contains a module that can be modified by current user, we can inject arbitrary code into the module.

1) First, check what modules the Python script uses.

2)

3)

4) If we have write permissions on the file, we can inject our own malicious code for privesc

5) Execute for root shell ;)

OS COMMANDS IN INPUT()

1) If the python executable asks for input, you can try to enter OS commands like:

2) Input:

BLACKLISTED PYTHON MODULES

Examples:

eval, exec, import, open, os, read, system, write

Bypass techniques: String obfuscation

Payload:

Bypass techniques: Input

If the "eval" or "exec" modules are accepted, we can input arbitrary code.

eval(input()) or exec(input())

PYTHON YAML PRIVILEGE ESCALATION

Python yaml package is vulnerable to execute arbitrary commands

1)

2) Payload:

3)

Base64 Encoding RCE

1)

Reverse Shell

1) start listener:

2) Execute python script that contains YAML code as root:

Last updated