Linux Privilege Escalation

Service Exploits

The MySQL service is running as root and the "root" user for the service does not have a password assigned. We can use a popular exploit that takes advantage of User Defined Functions (UDFs) to run system commands as root via the MySQL service.

Compile the exploit code

Connect Mysql user as root without password:

mysql -u root

Execute the following commands on the MySQL shell to create a User Defined Function (UDF) "do_system" using our compiled exploit.

Copy /bin/bash to /tmp/rootbash and set SUID permission.

Exit from mysql shell and type this /tmp/rootbash -p.

Weak File Permissions

Reable /etc/shodow

The /etc/shadow file contains user password hashes and is usually readable only by the root user.

Crack the hashes with john

Writable /etc/passwd

The /etc/passwd file contains information about user accounts only writable by the root user.

Edit the /etc/passwd file and place the generated password hash between the first and second colon (:) of the root user's row (replacing the "x").

Sudo

Shell Escape Sequences

Always should try sudo -l first.

Go to GTFObin and file the programs name on the list, Eg.nmap

Type nmap in GTFObin.

Environment Variables

Cron Jobs

File Permissions

Cron jobs are programs or scripts which users can schedule to run at specific times or intervals. Cron table files (crontabs) store the configuration for cron jobs. The system-wide crontab is located at /etc/crontab.

View the contents of the system-wide crontab: cat /etc/crontab

You can also replace bash script to get reverse shell.

PATH Environment Variable

cat /etc/crontab

You can see that there is no file path at overwrite.sh . PATH also has /home/user . If we can create a malicious file to that directory ,it will execute as root.

Wildcards

tar command is being run with a wildcard (*). Note that tar has command line options that let you run other commands as part of a checkpoint feature.

Generate a reverse elf file with msfvenom .

Create two folder:

When cronjob is run, tar will recognize them as command line options rather than filenames

Set up a netcat listener and wait for the cron job to run.

SUID / SGID

Known Exploits

Find all the SUID/SGID executables file.

There is a file called /usr/sbin/exim-4.84-3 .To find exploit ExploitDB, Google, Github are good places to search.

Exploit

Shared Object Injection

The /usr/local/bin/suid-so SUID executable is vulnerable to shared object injection

First, execute the file /usr/local/bin/suid-so

Run strace on the file and search the output for open/access calls and for "no such file" errors:

The executable tries to load the /home/user/.config/libcalc.so shared object within our home directory, but it cannot be found.

Create the .config directory for the libcalc.so file.

mkdir /home/user/.config.

libcalc.c

Execute /usr/local/bin/suid-so again and now we will get root shell.

Environment Variables

The /usr/local/bin/suid-env executable can be exploited due to it inheriting the user's PATH environment variable and attempting to execute programs without specifying an absolute path.

Run the execute file /usr/local/bin/suid-env

It trying to runapache. we can check with strings command.

The service executable is being called to start webserver but it has not the full path. The Path should be like this/usr/bin/service .

Make a file name as service.c

Compile the code name with service

Now give the PATH to the currently directory or where the new service file is located.

Passwords & Keys

If a user accidentally types their password on the command line instead of into a password prompt, it may get recorded in a history file.

View the contents of all the hidden history files in the user's home directory:

cat ~/.history | less

Config Files

Config files often contain passwords in plaintext or other reversible formats.

SSH Keys

Sometimes users make backups of important files but fail to secure them with the correct permissions.

Copy to our machine and give the right permission and login with ssh

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.

To check the NFS share configuration cat /etc/exports

the /tmp share has root squashing disabled. On your linux , switch to root if you are not root user.Create a mount point on your Kali box and mount the /tmp share and generate reverse shell with msfvenom .

Now on the victim account, execute the file to gain a root shell. /tmp/shell.elf

Kernel Exploits

The popular Linux kernel exploit is Dirty Cow.

Reference : https://tryhackme.com/room/linuxprivesc

Last updated