7. Cron jobs
Introduction
Cron jobs are programs or scripts which users can schedule to run at specific times or intervals.
Cron jobs run with the security level of the user who owns them.
By default, cron jobs are run using the /bin/sh shell, with limited environment variables.
Crontabs
Cron table files (crontabs) store the configuration for cron jobs.
User crontabs are usually located in /var/spool/cron/ or /var/spool/cron/crontabs/
The system-wide crontab is located at /etc/crontab.
File Permissions
Misconfiguration of file permissions associated with cron jobs can lead to easy privilege escalation.
If we can write to a program or script which gets run as part of a cron job, we can replace it with our own code.
View the contents of the system-wide crontab:
Locate the overwrite.sh file on the server:
Check the file’s permissions:
Note that the file is world writable
Replace the contents of the overwrite.sh file with the following:
Run a netcat listener on your local machine and wait for the cron job to run. A reverse shell running as the root user should be caught:
PATH Environment Variable
The crontab PATH environment variable is by default set to /usr/bin:/bin
The PATH variable can be overwritten in the crontab file.
If a cron job program/script does not use an absolute path, and one of the PATH directories is writable by our user, we may be able to create a program/script with the same name as the cron job.
View the contents of the system-wide crontab:
Note that the /home/user directory (which we can write to) is at the start of the PATH variable, and the first cron job does not use an absolute path.
Create the file overwrite.sh in /home/user with the following contents:
Ensure that overwrite.sh is executable:
Wait for the cronjob to run (this job in particular runs every minute).
Once the /tmp/rootbash file is created, execute it (with -p to preserve the effective UID) to gain a root shell:
Wildcards
When a wildcard character (*) is provided to a command as part of an argument, the shell will first perform filename expansion (also known as globbing) on the wildcard.
This process replaces the wildcard with a space-separated list of the file and directory names in the current directory.
An easy way to see this in action is to run the following command from your home directory:
Wildcards and Filenames
Since filesystems in Linux are generally very permissive with filenames, and filename expansion happens before the command is executed, it is possible to pass command line options (e.g. -h, --help) to commands by creating files with these names.
The following commands should show how this works:
Filenames are not simply restricted to simple options like -h or --help.
In fact we can create filenames that match complex options:
GTFOBins can help determine whether a command has command line options which will be useful for our purposes.
View the contents of the system-wide crontab
View the contents of the /usr/local/bin/compress.sh file
Note that the tar command is run with a wildcard in the /home/user directory
GTFOBins shows that tar has command line options which can be used to run other commands as part of a checkpoint feature.
Use msfvenom to create a reverse shell ELF payload:
Copy the file to the /home/user directory on the remote host and make it executable:
Create two files in the /home/user directory
Run a netcat listener on your local machine and wait for the cron job to run. A reverse shell running as the root user should be caught:
Last updated