Linux Privilege Escalation - Cron Jobs for CTF Creators
Cron Jobs
Cron jobs are scheduled task which are used to automate specific task at specific time intervals. Cron tables (crontabs) store the configuration of these cron jobs. These can be configured to run as high privileged users or groups. However, if they are misconfigured, it can lead to elevation of privilege.
Cron Jobs Syntax
Here is the syntax of a cron job:
|
|
Cron Jobs Symbols
These are the meaning of each symbol:
Symbol | Value |
---|---|
* | Any value |
, | Value list separator |
- | Range of values |
/ | Step of values |
Cron Jobs Generator
Crontab generators can be used to automate the process of creating a cron job:
Crontabs Tables
Crontab table syntax:
Minute | Hour | Day Of Month | Month | Day Of Week |
---|---|---|---|---|
0-59 | 0-23 | 1-31 | 1-12 | 0-7 |
* | * | * | * | * |
12,46 | 1,2,20 | 7,29 | MAR,AUG | 3,5 |
34-56/2 | 6-12 | 7-14 | 3-8 | MON-FRI |
Run every minute:
Minute | Hour | Day Of Month | Month | Day Of Week |
---|---|---|---|---|
* | * | * | * | * |
Run every hour on the hour:
Minute | Hour | Day Of Month | Month | Day Of Week |
---|---|---|---|---|
0 | * | * | * | * |
Run every five minutes:
Minute | Hour | Day Of Month | Month | Day Of Week |
---|---|---|---|---|
*/5 | * | * | * | * |
Run at 9:30 AM on every Monday and Friday:
Minute | Hour | Day Of Month | Month | Day Of Week |
---|---|---|---|---|
30 | 9 | * | * | MON-FRI |
Configuring Crontabs
We can list the current crontabs with the following command:
|
|
We can edit the default crontab text editor with the following:
|
|
The select-editor
can also be used:
|
|
We can remove a crontab with the following:
|
|
We can add a crontab from a specific user:
|
|
We could also remove a crontab for a specific user:
|
|
Restarting Crontabs
After making changes to the cron configuration file, we need to restart the cron daemon for the changes to take effect. The command to restart the cron daemon varies based on the specific Unix-like system you are using. Common commands include:
service cron restart
systemctl restart cron
/etc/init.d/cron restart
Allow/Deny Users & Groups
Open the cron configuration file. This file is typically located in /etc
and may be named cron.allow
or cron.deny
as shown below:
|
|
Then we’ll need to add the usernames or group names that you want to allow in the appropriate file. Each entry should be placed on a separate line. Then we can save the changes and exit the file.
System-Wide Crontab
The system-wide crontab is located at /etc/crontab
. We can view the contents of the system-wide crontab, and notice how the user column is available:
|
|
User-Wide Crontab
Each crontab that we create is located in the /var/spool/cron/crontabs
directory and by default will be named as the username of the user who owns the crontab:
|
|
Then we could delete the crontab file:
|
|
Crontab Syslog
Depending on the system distribution and the system configuration. The logs may stored in one of the following locations:
/var/log/syslog
/var/log/cron
/var/log/messages
/var/log/cron.log
Note: The actual log file and its location can be customized based on system configurations. Therefore, it’s recommended to consult the system’s documentation.
Crontabs events are logged with the following syntax:
|
|
Privilege Escalation via System-Wide Crontab
Make a script that outputs the user’s security context:
|
|
To extract user information, add the following code:
|
|
Add execution rights to the script so that it can be run by the cron job:
|
|
Edit the system-wide crontab:
|
|
Add the following code to the crontab:
|
|
In summary, this may end up as the following:
|
|
Wait one minute:
|
|
Read the user_security_context.txt
file:
|
|
Add a setuid
bit to the bash script:
|
|
Wait one minute until the SUID bit is set to the bash executable:
|
|
Then we can bash shell the setuid
bit as the root
user by passing the -p
flag:
|
|
We can confirm this by using id
:
|
|
Privelege Escalation via PATH Environment Variable
Create a script in your home directory that adds an SUID bit to the bash executable:
|
|
Add execution permissions to the script:
|
|
Create a custom cron job to run the elevate.sh
script as the root user once every minute:
|
|
Set up the cron job:
|
|
Note: The PATH variable starts with /home/user which is our user’s home directory.
Wait a minute, then “root” will run the script “elevate.sh”:
|
|
This output is what we want to get:
|
|
Then we can bash shell the setuid
bit as the root
user by passing the -p
flag:
|
|
We can confirm this by using id
:
|
|