Linux Privilege Escalation

Masterpiece

Enumeration Scripts

LinEnum.sh

Linux Smart Enumeration

LinPEAS

Tar Wildcard

Condition

  • A script running by root has a tar command with * at the end

  • The script folder is writable by the current user

Exploit

Reference:

Example:

  • TryHackMe: Skynet

If you see a root script running tar command with Wildcard, you may inject command! For example, root is running a cron job with the following script:

As you see, there is a wildcard at the end of the tar command.

The exploit condition is having Write permission on /var/www/html. If so, follow the following step to get a root reverse shell:

  1. Write a bash script (Reverse Shell command) and save as /var/www/html/shell.sh

2. Navigate to /var/www/html and do:

3. Launch a netcat listener locally

Next time when root runs the cron, a reverse shell will call back.

MySQL User-defined Function

Condition

  • MySQL is running as root (Use ps aux | grep mysql to check)

  • MySQL root account access to mysql database

Exploit

Compile the library

Get into mysql as mysql root user

Then use the created bash with SUID to escalate the privilege

Readable /etc/shadow

Condition

  • /etc/shadow is readable (Use ls -l /etc/shadow to check)

Exploit

Read /etc/shadow and copy the hashes into a file

Then use john / hashcat to crack the hash

Note that john will detect and show the hash algorithm used.

In hashcat, we need to find the module to be used. For example, if it is a sha512crypt ($6$), we can find out the according module using:

Then we can use -m 1800 to crack the hash.

Writable /etc/shadow

Condition

  • /etc/shadow is writable (Use ls -l /etc/shadow to check)

Exploit

The target is to change the root password. First generate a salted hash using mkpasswd

Replace root salted hash by the above output in /etc/shadow

Writable /etc/passwd

Condition

  • /etc/passwd is writable (Use ls -l /etc/passwd to check)

Exploit

One-liner:

Alternatively, generate a password hash using openssl

Then add it to /etc/passwd

Sudo apache2

Condition

  • Have SUDO privilege to run apache2 (sudo -l to check)

Exploit

Read the first line of /etc/shadow to obtain the salted hash.

Sudo - Environment Variables

Condition

  • Sudo privilege to run anything (sudo -l to check)

  • LD_PRELOAD and LD_LIBRARY_PATH are inherited from the user's environment

    • LD_PRELOAD loads a shared object before any others when a program is run.

    • LD_LIBRARY_PATH provides a list of directories where shared libraries are searched for first.

Exploit - Preload

preload.c

Compile proload.c:

Run one of the programs you are allowed to run via sudo (listed when running sudo -l) (apache2 as an example here), while setting the LD_PRELOAD environment variable to the full path of the new shared object:

Then the current user will become root.

Exploit - Library_PATH

library_path.c:

First you have to find which a sudo program will call, for example:

Then we can hijack any one of the library above, libcrypto.so.1 for example. To do so, compile:

Then point LD_LIBRARY_PATH to tmp folder when running sudo apache2

SUID / GUID bits

Condition

  • Some of the results could be ab-usable

    • nmap

    • sh / bash

    • less / more

    • man

    • vim / nano

    • find

    • iftop

    • awk

  • Use https://gtfobins.github.io/ to see help to make use of them

  • Some of them could be vulnerable

    • exim-4.84-3 --> Search for exploit in exploit-db (CVE-2016-1531)

SUID / SGID - suid-so Shared Object Injection

Condition

  • suid-so has root SUID set

Exploit

First run:

Then use strace on suid-so and search the output for open/access calls and for no such file errors:

Find a directory that is writable by the current user. For example:

Then compile the following library:

libcalc.c

Compile and output to the directory:

Finally execute suid-so:

SUID / SGID - suid-env

Condition

  • suid-env has root suid set

Exploit

First run:

Likely to be used to run apache server. Then try to use strings

It is vulnerable since the full path of service (/usr/sbin/service) is not used. Then we can hijack the PATH.

service.c

Compile service.c:

Then refine the PATH variable:

Run suid-env

SUID / SGID - Abuse Shell Feature (Bash < 4.2-048)

Condition

  • Root SUID set for a binary

  • Bash < 4.2.048

Exploit

Use strings to inspect the SUID binary

In Bash versions <4.2-048 it is possible to define shell functions with names that resemble file paths, then export those functions so that they are used instead of any actual executable at that file path.

Then we can create a Bash function with the name /usr/sbin/service

When running the SUID binary, we will get a root shell:

SUID / SGID - Abuse Shell Features (Bash < 4.4)

Condition

  • Root SUID binary

  • Bash < 4.4

Exploit

When in debugging mode, Bash uses the environment variable PS4 to display an extra prompt for debugging statements.

Run the SUID binary with bash debugging enabled and the PS4 variable set to an embedded command which create an SUID version of bash:

Then run rootbash:

History File

Exploit

Trivial ...

Config File

Exploit

  • Find common files which has password ...

    • /var/www/html/wp-config.php

    • xxx.ovpn

    • ...

Exposed SSH Key

Exploit

First retrieve the SSH key. Then

Finally use it to logon:

Remote NFS

Files created via NFS inherit the remote user's ID. If the user is root, and root squashing is enabled, the ID will instead be set to the "nobody" user.

Exploit

On the target, check NFS configuration:

  • rw

  • insecure

  • no_root_squash

Then on Kali, as root, do the following:

Then generate a bash binary with root SUID:

Then on the target, just do

(Side note: Never use no_root_squash! Use root_squash instead and so things put by the nfs user will have both UID and GID set to nobody)

Kernel Exploit

hostnamectl

uname -a

...

Last updated