Linux PrivEsc

Date: 24, May, 2021

Author: Dhilip Sanjay S


Click Herearrow-up-right to go to the TryHackMe room.

Deploy the Vulnerable Debian VM

ssh [email protected]
The authenticity of host '10.10.95.239 (10.10.95.239)' can't be established.
RSA key fingerprint is SHA256:JwwPVfqC+8LPQda0B9wFLZzXCXcoAho6s8wYGjktAnk.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.10.95.239' (RSA) to the list of known hosts.
[email protected]'s password: 
Linux debian 2.6.32-5-amd64 #1 SMP Tue May 13 16:34:35 UTC 2014 x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Fri May 15 06:41:23 2020 from 192.168.1.125
user@debian:~$ whoami
user

Run the "id" command. What is the result?

  • Answer: uid=1000(user) gid=1000(user) groups=1000(user),24(cdrom),25(floppy),29(audio),30(dip),44(video),46(plugdev)


Service exploits

  • Run the MySQL commands:

  • Gain root shell using the rootbash.


Weak File Permissions - Readable /etc/shadow

What is the root user's password hash?

  • Answer: $6$Tb/euwmK$OXA.dwMeOAcopwBl68boTG5zi65wIHsc84OWAIye5VITLLtVlaXvRDJXET..it8r.jbrlpfZeMdwD3B0fGxJI0

  • Steps to Reproduce:

    • Read the /etc/shadow file:

What hashing algorithm was used to produce the root user's password hash?

  • Answer: sha512crypt

What is the root user's password?

  • Answer: password123

  • Steps to Reproduce:

    • Crack the hash using john:


Weak File Permissions - Writable /etc/shadow

  • Edit /etc/shadow and su using the new password:


Weak File Permissions - Writable /etc/passwd

  • Generate a new encrypted password using openssl

  • Replace the x inside /etc/passwd of the root user to the newly generated encrypted password:

Run the "id" command as the newroot user. What is the result?

  • Answer: uid=0(root) gid=0(root) groups=0(root)


Sudo - Shell Escape Sequences

How many programs is "user" allowed to run via sudo?

  • Answer: 11

Using GTFO bins

One program on the list doesn't have a shell escape sequence on GTFOBins. Which is it?

  • Answer: apache2

Consider how you might use this program with sudo to gain root privileges without a shell escape sequence.

  • We can use the -f option to provide the config file.

  • If the config file contains invalid commands, it'll error out the first line of the file.

  • We can use this to read the first line of /etc/passwd:


Sudo - Environment Variables

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

LD_PRELOAD

LD_LIBRARY_PATH


Cron Jobs - File Permissions

  • Reverse Shell on target machine:


Cron Jobs - PATH Environment Variable

What is the value of the PATH variable in /etc/crontab?

  • Answer: /home/user:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

  • Steps to Reproduce:

  • Exploiting PATH environment varibale:


Cron Jobs - Wildcards

  • View the compress.sh file:

  • Transfer shell.elf using scp

  • Creating file with appropriate names to exploit Wildcards:

  • Obtaining Reverse Shell as root:


SUID / SGID Executables - Known Exploits

  • Locate files with SUID/SGID set:

  • Running the exploit to gain root shell:


SUID / SGID Executables - Shared Object Injection

  • Running strace on the SUID executable suid-so:

  • Creating a malicious shared object file:

  • Gaining root access by running the vulnerable SUID binary:


SUID / SGID Executables - Environment Variables

  • Running strings on the SUID binary:

  • Compile the malicious C code and storing the executable in the home directory.

  • Append the current directory to the PATH variable.

  • Run the binary to gain root shell


SUID / SGID Executables - Abusing Shell Features (#1)

  • Running strings on SUID executable:

  • Checking the version of bash. (Bash < 4.2-028)

  • We can create functions which take precedence of the binary executables.

  • Running the SUID executable to gain root access:


SUID / SGID Executables - Abusing Shell Features (#2)

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

  • Gaining root access:


Passwords & Keys - History Files

  • Check out the history files:

What is the full mysql command the user executed?

  • Answer: mysql -h somehost.local -uroot -ppassword123

  • Gain root access using that password:


Passwords & Keys - Config Files

  • Cat the openvpn config file:

  • Look into the auth.txt

  • Gain root access using that password


Passwords & Keys - SSH Keys

  • Look for ssh private keys:

  • Copy the code to your local machine.

  • Change the file Permissions to 600.

  • Login using ssh to gain root shell:


NFS

  • View the /etc/exports file.

  • We can see no_root_squash

What is the name of the option that disables root squashing?

  • Answer: no_root_squash

  • Mount and insert some malicious SUID binary as root user.

  • Gaining root access on the target machine:


Kernel Exploits

  • Run Linux Exploit Suggester

  • It'll suggest dirty cow.

  • Compile and run the exploit code to gain root access:


Privilege Escalation Scripts

  • Listing all the Privilege Escalation Scripts:

  • Experiment with all three tools, running them with different options!


Last updated