Cron Job Created or Modified
This rule monitors for (ana)cron jobs being created or renamed. Linux cron jobs are scheduled tasks that can be leveraged by system administrators to set up scheduled tasks, but may be abused by malicious actors for persistence, privilege escalation and command execution. By creating or modifying cron job configurations, attackers can execute malicious commands or scripts at predefined intervals, ensuring their continued presence and enabling unauthorized activities.
Elastic rule (View on GitHub)
1[metadata]
2creation_date = "2023/06/09"
3integration = ["endpoint"]
4maturity = "production"
5updated_date = "2026/09/18"
6[transform]
7[[transform.osquery]]
8label = "Osquery - Retrieve File Listing Information"
9query = """
10SELECT * FROM file WHERE (path LIKE '/etc/cron.allow.d/%' OR path LIKE '/etc/cron.d/%' OR path LIKE '/etc/cron.hourly/%'
11OR path LIKE '/etc/cron.daily/%' OR path LIKE '/etc/cron.weekly/%' OR path LIKE '/etc/cron.monthly/%' OR path LIKE
12'/var/spool/cron/crontabs/%')
13"""
14
15[[transform.osquery]]
16label = "Osquery - Retrieve Cron File Information"
17query = "SELECT * FROM file WHERE (path = '/etc/cron.allow' OR path = '/etc/cron.deny' OR path = '/etc/crontab')\n"
18
19[[transform.osquery]]
20label = "Osquery - Retrieve Additional File Listing Information"
21query = """
22SELECT f.path, u.username AS file_owner, g.groupname AS group_owner, datetime(f.atime, 'unixepoch') AS
23file_last_access_time, datetime(f.mtime, 'unixepoch') AS file_last_modified_time, datetime(f.ctime, 'unixepoch') AS
24file_last_status_change_time, datetime(f.btime, 'unixepoch') AS file_created_time, f.size AS size_bytes FROM file f LEFT
25JOIN users u ON f.uid = u.uid LEFT JOIN groups g ON f.gid = g.gid WHERE ( path LIKE '/etc/cron.allow.d/%' OR path LIKE
26'/etc/cron.d/%' OR path LIKE '/etc/cron.hourly/%' OR path LIKE '/etc/cron.daily/%' OR path LIKE '/etc/cron.weekly/%' OR
27path LIKE '/etc/cron.monthly/%' OR path LIKE '/var/spool/cron/crontabs/%')
28"""
29
30[[transform.osquery]]
31label = "Osquery - Retrieve Running Processes by User"
32query = "SELECT pid, username, name FROM processes p JOIN users u ON u.uid = p.uid ORDER BY username"
33
34[[transform.osquery]]
35label = "Osquery - Retrieve Listening Ports"
36query = "SELECT pid, address, port, socket, protocol, path FROM listening_ports"
37
38[[transform.osquery]]
39label = "Osquery - Retrieve Open Sockets"
40query = "SELECT pid, family, remote_address, remote_port, socket, state FROM process_open_sockets"
41
42[[transform.osquery]]
43label = "Osquery - Retrieve Information for a Specific User"
44query = "SELECT * FROM users WHERE username = {{user.name}}"
45
46[[transform.osquery]]
47label = "Osquery - Investigate the Account Authentication Status"
48query = "SELECT * FROM logged_in_users WHERE user = {{user.name}}"
49
50[rule]
51author = ["Elastic"]
52description = """
53This rule monitors for (ana)cron jobs being created or renamed. Linux cron jobs are scheduled tasks that can be
54leveraged by system administrators to set up scheduled tasks, but may be abused by malicious actors for persistence,
55privilege escalation and command execution. By creating or modifying cron job configurations, attackers can execute
56malicious commands or scripts at predefined intervals, ensuring their continued presence and enabling unauthorized
57activities.
58"""
59from = "now-9m"
60index = ["logs-endpoint.events.file*"]
61language = "eql"
62license = "Elastic License v2"
63name = "Cron Job Created or Modified"
64note = """## Triage and analysis
65
66### Investigating Cron Job Created or Modified
67Linux cron jobs are scheduled tasks that run at specified intervals or times, managed by the cron daemon.
68
69By creating or modifying cron job configurations, attackers can execute malicious commands or scripts at predefined intervals, ensuring their continued presence and enabling unauthorized activities.
70
71This rule monitors the creation of cron jobs by monitoring for file creation and rename events in the most common cron job task location directories.
72
73> **Note**:
74> This investigation guide uses the [Osquery Markdown Plugin](https://www.elastic.co/guide/en/security/current/invest-guide-run-osquery.html) introduced in Elastic Stack version 8.5.0. Older Elastic Stack versions will display unrendered Markdown in this guide.
75> This investigation guide uses [placeholder fields](https://www.elastic.co/guide/en/security/current/osquery-placeholder-fields.html) to dynamically pass alert data into Osquery queries. Placeholder fields were introduced in Elastic Stack version 8.7.0. If you're using Elastic Stack version 8.6.0 or earlier, you'll need to manually adjust this investigation guide's queries to ensure they properly run.
76
77#### Possible Investigation Steps
78
79- Investigate the cron job file that was created or modified.
80- Investigate whether any other files in any of the available cron job directories have been altered through OSQuery.
81 - $osquery_0
82 - $osquery_1
83 - $osquery_2
84- Investigate the script execution chain (parent process tree) for unknown processes. Examine their executable files for prevalence and whether they are located in expected locations.
85 - $osquery_3
86- Investigate other alerts associated with the user/host during the past 48 hours.
87- Validate the activity is not related to planned patches, updates, network administrator activity, or legitimate software installations.
88- Investigate whether the altered scripts call other malicious scripts elsewhere on the file system.
89 - If scripts or executables were dropped, retrieve the files and determine if they are malicious:
90 - Use a private sandboxed malware analysis system to perform analysis.
91 - Observe and collect information about the following activities:
92 - Attempts to contact external domains and addresses.
93 - Check if the domain is newly registered or unexpected.
94 - Check the reputation of the domain or IP address.
95 - File access, modification, and creation activities.
96- Investigate abnormal behaviors by the subject process/user such as network connections, file modifications, and any other spawned child processes.
97 - Investigate listening ports and open sockets to look for potential command and control traffic or data exfiltration.
98 - $osquery_4
99 - $osquery_5
100 - Identify the user account that performed the action, analyze it, and check whether it should perform this kind of action.
101 - $osquery_6
102- Investigate whether the user is currently logged in and active.
103 - $osquery_7
104
105### False Positive Analysis
106
107- If this activity is related to new benign software installation activity, consider adding exceptions — preferably with a combination of user and command line conditions.
108- If this activity is related to a system administrator who uses cron jobs for administrative purposes, consider adding exceptions for this specific administrator user account.
109- Try to understand the context of the execution by thinking about the user, machine, or business purpose. A small number of endpoints, such as servers with unique software, might appear unusual but satisfy a specific business need.
110
111### Related Rules
112
113- Suspicious File Creation in /etc for Persistence - 1c84dd64-7e6c-4bad-ac73-a5014ee37042
114- Potential Persistence Through Run Control Detected - 0f4d35e4-925e-4959-ab24-911be207ee6f
115- Potential Persistence Through init.d Detected - 474fd20e-14cc-49c5-8160-d9ab4ba16c8b
116- Systemd Timer Created - 7fb500fa-8e24-4bd1-9480-2a819352602c
117- Systemd Service Created - 17b0a495-4d9f-414c-8ad0-92f018b8e001
118
119### Response and remediation
120
121- Initiate the incident response process based on the outcome of the triage.
122- Isolate the involved host to prevent further post-compromise behavior.
123- If the triage identified malware, search the environment for additional compromised hosts.
124 - Implement temporary network rules, procedures, and segmentation to contain the malware.
125 - Stop suspicious processes.
126 - Immediately block the identified indicators of compromise (IoCs).
127 - Inspect the affected systems for additional malware backdoors like reverse shells, reverse proxies, or droppers that attackers could use to reinfect the system.
128- Investigate credential exposure on systems compromised or used by the attacker to ensure all compromised accounts are identified. Reset passwords for these accounts and other potentially compromised credentials, such as email, business systems, and web services.
129- Delete the service/timer or restore its original configuration.
130- Run a full antimalware scan. This may reveal additional artifacts left in the system, persistence mechanisms, and malware components.
131- Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
132- Leverage the incident response data and logging to improve the mean time to detect (MTTD) and the mean time to respond (MTTR).
133"""
134references = [
135 "https://pberba.github.io/security/2022/01/30/linux-threat-hunting-for-persistence-systemd-timers-cron/",
136 "https://www.elastic.co/security-labs/primer-on-persistence-mechanisms",
137]
138risk_score = 47
139rule_id = "ff10d4d8-fea7-422d-afb1-e5a2702369a9"
140setup = """## Setup
141
142This rule requires data coming in from Elastic Defend.
143
144### Elastic Defend Integration Setup
145Elastic Defend is integrated into the Elastic Agent using Fleet. Upon configuration, the integration allows the Elastic Agent to monitor events on your host and send data to the Elastic Security app.
146
147#### Prerequisite Requirements:
148- Fleet is required for Elastic Defend.
149- To configure Fleet Server refer to the [documentation](https://www.elastic.co/guide/en/fleet/current/fleet-server.html).
150
151#### The following steps should be executed in order to add the Elastic Defend integration on a Linux System:
152- Go to the Kibana home page and click "Add integrations".
153- In the query bar, search for "Elastic Defend" and select the integration to see more details about it.
154- Click "Add Elastic Defend".
155- Configure the integration name and optionally add a description.
156- Select the type of environment you want to protect, either "Traditional Endpoints" or "Cloud Workloads".
157- Select a configuration preset. Each preset comes with different default settings for Elastic Agent, you can further customize these later by configuring the Elastic Defend integration policy. [Helper guide](https://www.elastic.co/guide/en/security/current/configure-endpoint-integration-policy.html).
158- We suggest selecting "Complete EDR (Endpoint Detection and Response)" as a configuration setting, that provides "All events; all preventions"
159- Enter a name for the agent policy in "New agent policy name". If other agent policies already exist, you can click the "Existing hosts" tab and select an existing policy instead.
160For more details on Elastic Agent configuration settings, refer to the [helper guide](https://www.elastic.co/guide/en/fleet/8.10/agent-policy.html).
161- Click "Save and Continue".
162- To complete the integration, select "Add Elastic Agent to your hosts" and continue to the next section to install the Elastic Agent on your hosts.
163For more details on Elastic Defend refer to the [helper guide](https://www.elastic.co/guide/en/security/current/install-endpoint.html).
164"""
165severity = "medium"
166tags = [
167 "Domain: Endpoint",
168 "OS: Linux",
169 "Use Case: Threat Detection",
170 "Tactic: Persistence",
171 "Tactic: Privilege Escalation",
172 "Tactic: Execution",
173 "Data Source: Elastic Defend",
174 "Resources: Investigation Guide",
175 "Noise: High",
176 "Performance: Normal",
177 "Profile: Aggressive",
178 "Rule Type: Event Correlation (EQL)",
179 "Platform: Linux",
180 "Resources: Osquery",
181]
182timestamp_override = "event.ingested"
183type = "eql"
184query = '''
185file where host.os.type == "linux" and event.action in ("rename", "creation") and file.path like (
186 "/etc/cron.allow", "/etc/cron.deny", "/etc/cron.d/*", "/etc/cron.hourly/*", "/etc/cron.daily/*", "/etc/cron.weekly/*",
187 "/etc/cron.monthly/*", "/etc/crontab", "/var/spool/cron/crontabs/*", "/var/spool/anacron/*"
188) and not (
189 process.executable in (
190 "/bin/dpkg", "/usr/bin/dpkg", "/bin/dockerd", "/usr/bin/dockerd", "/usr/sbin/dockerd", "/bin/microdnf",
191 "/usr/bin/microdnf", "/bin/rpm", "/usr/bin/rpm", "/bin/snapd", "/usr/bin/snapd", "/bin/yum", "/usr/bin/yum",
192 "/bin/dnf", "/usr/bin/dnf", "/bin/podman", "/usr/bin/podman", "/bin/dnf-automatic", "/usr/bin/dnf-automatic",
193 "/bin/pacman", "/usr/bin/pacman", "/usr/bin/dpkg-divert", "/bin/dpkg-divert", "/sbin/apk", "/usr/sbin/apk",
194 "/usr/local/sbin/apk", "/usr/bin/apt", "/usr/sbin/pacman", "/bin/podman", "/usr/bin/podman", "/usr/bin/puppet",
195 "/bin/puppet", "/opt/puppetlabs/puppet/bin/puppet", "/usr/bin/chef-client", "/bin/chef-client",
196 "/bin/autossl_check", "/usr/bin/autossl_check", "/proc/self/exe", "/usr/bin/pamac-daemon",
197 "/bin/pamac-daemon", "/usr/local/bin/dockerd", "/opt/elasticbeanstalk/bin/platform-engine",
198 "/opt/puppetlabs/puppet/bin/ruby", "/usr/libexec/platform-python", "/opt/imunify360/venv/bin/python3",
199 "/opt/eset/efs/lib/utild", "/usr/sbin/anacron", "/usr/bin/podman", "/kaniko/kaniko-executor",
200 "/usr/bin/pvedaemon", "./usr/bin/podman", "/usr/lib/systemd/systemd", "./usr/bin/podman", "/usr/bin/coreutils",
201 "/usr/sbin/univention-config-registry", "/usr/bin/dnf5", "./usr/lib/snapd/snap-update-ns"
202 ) or
203 file.path like ("/var/spool/cron/crontabs/tmp.*", "/etc/cron.d/jumpcloud-updater") or
204 file.extension in ("swp", "swpx", "swx", "dpkg-remove") or
205 file.Ext.original.extension == "dpkg-new" or
206 process.executable like (
207 "/nix/store/*", "/var/lib/dpkg/*", "/tmp/vmis.*", "/snap/*", "/dev/fd/*", "/usr/libexec/platform-python*",
208 "/var/lib/waagent/Microsoft*"
209 ) or
210 process.executable == null or
211 process.name in (
212 "crond", "executor", "puppet", "droplet-agent.postinst", "cf-agent", "schedd", "imunify-notifier",
213 "jumpcloud-agent", "crio", "dnf_install", "utild"
214 ) or
215 (process.name == "sed" and file.name like "sed*") or
216 (process.name == "perl" and file.name like "e2scrub_all.tmp*") or
217 (process.name in ("vi", "vim") and file.name like "*~")
218)
219'''
220
221[[rule.threat]]
222framework = "MITRE ATT&CK"
223
224[[rule.threat.technique]]
225id = "T1053"
226name = "Scheduled Task/Job"
227reference = "https://attack.mitre.org/techniques/T1053/"
228
229[[rule.threat.technique.subtechnique]]
230id = "T1053.003"
231name = "Cron"
232reference = "https://attack.mitre.org/techniques/T1053/003/"
233
234[rule.threat.tactic]
235id = "TA0003"
236name = "Persistence"
237reference = "https://attack.mitre.org/tactics/TA0003/"
238
239[[rule.threat]]
240framework = "MITRE ATT&CK"
241
242[[rule.threat.technique]]
243id = "T1053"
244name = "Scheduled Task/Job"
245reference = "https://attack.mitre.org/techniques/T1053/"
246
247[[rule.threat.technique.subtechnique]]
248id = "T1053.003"
249name = "Cron"
250reference = "https://attack.mitre.org/techniques/T1053/003/"
251
252[rule.threat.tactic]
253id = "TA0004"
254name = "Privilege Escalation"
255reference = "https://attack.mitre.org/tactics/TA0004/"
256
257[[rule.threat]]
258framework = "MITRE ATT&CK"
259
260[[rule.threat.technique]]
261id = "T1053"
262name = "Scheduled Task/Job"
263reference = "https://attack.mitre.org/techniques/T1053/"
264
265[[rule.threat.technique.subtechnique]]
266id = "T1053.003"
267name = "Cron"
268reference = "https://attack.mitre.org/techniques/T1053/003/"
269
270[rule.threat.tactic]
271id = "TA0002"
272name = "Execution"
273reference = "https://attack.mitre.org/tactics/TA0002/"
Triage and analysis
Investigating Cron Job Created or Modified
Linux cron jobs are scheduled tasks that run at specified intervals or times, managed by the cron daemon.
By creating or modifying cron job configurations, attackers can execute malicious commands or scripts at predefined intervals, ensuring their continued presence and enabling unauthorized activities.
This rule monitors the creation of cron jobs by monitoring for file creation and rename events in the most common cron job task location directories.
Note: This investigation guide uses the Osquery Markdown Plugin introduced in Elastic Stack version 8.5.0. Older Elastic Stack versions will display unrendered Markdown in this guide. This investigation guide uses placeholder fields to dynamically pass alert data into Osquery queries. Placeholder fields were introduced in Elastic Stack version 8.7.0. If you're using Elastic Stack version 8.6.0 or earlier, you'll need to manually adjust this investigation guide's queries to ensure they properly run.
Possible Investigation Steps
- Investigate the cron job file that was created or modified.
- Investigate whether any other files in any of the available cron job directories have been altered through OSQuery.
- $osquery_0
- $osquery_1
- $osquery_2
- Investigate the script execution chain (parent process tree) for unknown processes. Examine their executable files for prevalence and whether they are located in expected locations.
- $osquery_3
- Investigate other alerts associated with the user/host during the past 48 hours.
- Validate the activity is not related to planned patches, updates, network administrator activity, or legitimate software installations.
- Investigate whether the altered scripts call other malicious scripts elsewhere on the file system.
- If scripts or executables were dropped, retrieve the files and determine if they are malicious:
- Use a private sandboxed malware analysis system to perform analysis.
- Observe and collect information about the following activities:
- Attempts to contact external domains and addresses.
- Check if the domain is newly registered or unexpected.
- Check the reputation of the domain or IP address.
- File access, modification, and creation activities.
- Attempts to contact external domains and addresses.
- Observe and collect information about the following activities:
- Use a private sandboxed malware analysis system to perform analysis.
- If scripts or executables were dropped, retrieve the files and determine if they are malicious:
- Investigate abnormal behaviors by the subject process/user such as network connections, file modifications, and any other spawned child processes.
- Investigate listening ports and open sockets to look for potential command and control traffic or data exfiltration.
- $osquery_4
- $osquery_5
- Identify the user account that performed the action, analyze it, and check whether it should perform this kind of action.
- $osquery_6
- Investigate listening ports and open sockets to look for potential command and control traffic or data exfiltration.
- Investigate whether the user is currently logged in and active.
- $osquery_7
False Positive Analysis
- If this activity is related to new benign software installation activity, consider adding exceptions — preferably with a combination of user and command line conditions.
- If this activity is related to a system administrator who uses cron jobs for administrative purposes, consider adding exceptions for this specific administrator user account.
- Try to understand the context of the execution by thinking about the user, machine, or business purpose. A small number of endpoints, such as servers with unique software, might appear unusual but satisfy a specific business need.
Related Rules
- Suspicious File Creation in /etc for Persistence - 1c84dd64-7e6c-4bad-ac73-a5014ee37042
- Potential Persistence Through Run Control Detected - 0f4d35e4-925e-4959-ab24-911be207ee6f
- Potential Persistence Through init.d Detected - 474fd20e-14cc-49c5-8160-d9ab4ba16c8b
- Systemd Timer Created - 7fb500fa-8e24-4bd1-9480-2a819352602c
- Systemd Service Created - 17b0a495-4d9f-414c-8ad0-92f018b8e001
Response and remediation
- Initiate the incident response process based on the outcome of the triage.
- Isolate the involved host to prevent further post-compromise behavior.
- If the triage identified malware, search the environment for additional compromised hosts.
- Implement temporary network rules, procedures, and segmentation to contain the malware.
- Stop suspicious processes.
- Immediately block the identified indicators of compromise (IoCs).
- Inspect the affected systems for additional malware backdoors like reverse shells, reverse proxies, or droppers that attackers could use to reinfect the system.
- Investigate credential exposure on systems compromised or used by the attacker to ensure all compromised accounts are identified. Reset passwords for these accounts and other potentially compromised credentials, such as email, business systems, and web services.
- Delete the service/timer or restore its original configuration.
- Run a full antimalware scan. This may reveal additional artifacts left in the system, persistence mechanisms, and malware components.
- Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
- Leverage the incident response data and logging to improve the mean time to detect (MTTD) and the mean time to respond (MTTR).
References
Related rules
- Unusual D-Bus Daemon Child Process
- Git Hook Child Process
- Kernel Module Load via Built-in Utility
- Privilege Escalation via SUID/SGID
- Process Capability Set via setcap Utility