A Hunt on Synthetic Data, End to End
This lesson runs one complete hunt the way the hunting loop describes it, on a synthetic week of process-creation events from 40 workstations: a written hypothesis, the data check, stacking to find the unusual, a pivot to confirm, a conclusion, and the hunt turned into a tested detection. The technique is scheduled tasks created from the command line (ATT&CK T1053.005), a persistence method seen in a large share of intrusions because it is built into Windows and needs no extra tools. The data contains only the traces such tasks leave in logs.
1. Hypothesis and scope
HUNT H7 Scheduled tasks created from the command line for persistence analyst: hunter on duty
Hypothesis : an adversary on a workstation creates a scheduled task with schtasks.exe that runs a payload
from a user-writable location at short intervals or at logon (T1053.005)
Evidence : process creation of schtasks.exe with /create, by an unusual parent or user, with an action
(/tr) pointing to \Users\, \ProgramData\, \Temp\ or a script/LOLBin host
Data : Sysmon Event ID 1 / EDR process events, 7 days, 40 workstations (kiosks excluded: no EDR)
Not covered: tasks created through the Task Scheduler API or COM without schtasks.exe (needs Security
event 4698 "scheduled task created" or the TaskScheduler operational log)Writing down what is not covered matters as much as the hypothesis: a negative result only means "not created with schtasks.exe on hosts with EDR".
2. Find and stack
import numpy as np, pandas as pd
rng = np.random.default_rng(21)
hosts = [f"ws{i:02d}" for i in range(1, 41)]
ev = []
for h in hosts: # software updaters register their tasks via the installer
for _ in range(int(rng.integers(0, 3))):
ev.append((h, "SYSTEM", "msiexec.exe", 'schtasks /create /tn "LabOfficeUpdate" /sc daily /tr "C:\\Program Files\\LabOffice\\update.exe" /ru SYSTEM /f'))
for h in rng.choice(hosts, 12, replace=False): # IT deploys an inventory task by script
ev.append((h, "svc-deploy", "powershell.exe", 'schtasks /create /tn "LabInventory" /sc weekly /tr "C:\\IT\\inventory.exe" /ru SYSTEM /f'))
ev.append(("ws08", "dave", "cmd.exe", 'schtasks /create /tn "backup-photos" /sc daily /st 18:00 /tr "C:\\Users\\dave\\tools\\sync.bat"'))
ev.append(("ws15", "erin", "rundll32.exe", 'schtasks /create /tn "WinUpdateSvcHelper" /sc minute /mo 30 /tr "rundll32.exe C:\\Users\\Public\\lib.dll,Start" /f'))
ev.append(("ws15", "erin", "rundll32.exe", 'schtasks /query /tn "WinUpdateSvcHelper"'))
ev += [(h, "SYSTEM", "svchost.exe", "schtasks /query /fo csv") for h in rng.choice(hosts, 25)]
ev = pd.DataFrame(ev, columns=["host", "user", "parent", "cmd"])
created = ev[ev.cmd.str.contains(r"\s/create\b", case=False)].copy()
created["task"] = created.cmd.str.extract(r'/tn\s+"([^"]+)"', expand=False)
created["action"] = created.cmd.str.extract(r'/tr\s+"([^"]+)"', expand=False)
print(f"{len(ev)} schtasks events, {len(created)} task creations on {created.host.nunique()} hosts\n")
for field in ["task", "parent", "user"]:
st = created.groupby(field).host.agg(n="size", hosts="nunique").sort_values("hosts")
print(f"stack by {field}:", "; ".join(f"{k} ({r.hosts} host{'s' if r.hosts > 1 else ''})" for k, r in st.iterrows()))88 schtasks events, 62 task creations on 33 hosts
stack by task: WinUpdateSvcHelper (1 host); backup-photos (1 host); LabInventory (12 hosts); LabOfficeUpdate (30 hosts)
stack by parent: cmd.exe (1 host); rundll32.exe (1 host); powershell.exe (12 hosts); msiexec.exe (30 hosts)
stack by user: dave (1 host); erin (1 host); svc-deploy (12 hosts); SYSTEM (30 hosts)
88 schtasks events, of which 62 create tasks on 33 hosts. Every stack has the same shape: two big groups and a tail of two. The big groups explain themselves: LabOfficeUpdate, created by msiexec.exe as SYSTEM on 30 hosts (an installer), and LabInventory, created by svc-deploy through PowerShell on 12 hosts (IT's deployment script). The tail is one task each from dave on ws08 (parent cmd.exe, a person typing a command) and from erin on ws15, whose parent is rundll32.exe. People don't type into rundll32.exe; a DLL running inside it created this task. Stacking by three different fields and getting the same two outliers each time is a good sign that they are worth the next step.
3. Narrow, then pivot
The second step applies the hypothesis's evidence criteria to the tail, then pivots on whatever survives: what else happened on that host, by that user, around that time?
import pandas as pd
created = pd.DataFrame([
("ws08", "dave", "cmd.exe", "backup-photos", "C:\\Users\\dave\\tools\\sync.bat", "daily 18:00"),
("ws15", "erin", "rundll32.exe", "WinUpdateSvcHelper", "rundll32.exe C:\\Users\\Public\\lib.dll,Start", "every 30 min"),
], columns=["host", "user", "parent", "task", "action", "schedule"])
USER_WRITABLE = r"\\Users\\|\\ProgramData\\|\\Temp\\|\\AppData\\"
LOLBINS = r"rundll32|regsvr32|mshta|wscript|cscript|powershell"
SHELL_PARENTS = {"cmd.exe", "powershell.exe", "explorer.exe"}
created["writable_path"] = created.action.str.contains(USER_WRITABLE, case=False)
created["lolbin_action"] = created.action.str.contains(LOLBINS, case=False)
created["odd_parent"] = ~created.parent.isin(SHELL_PARENTS | {"msiexec.exe"})
created["frequent"] = created.schedule.str.contains("min")
created["score"] = created[["writable_path", "lolbin_action", "odd_parent", "frequent"]].sum(axis=1)
print(created[["host", "user", "task", "writable_path", "lolbin_action", "odd_parent", "frequent", "score"]].to_string(index=False))
# Pivot: everything on ws15 in the EDR timeline around the task creation (synthetic extract)
timeline = [("09:58", "winword.exe", "opened invoice-0115.docm from Downloads"),
("09:59", "powershell.exe", "-nop -w hidden -enc ... (parent winword.exe)"),
("10:00", "rundll32.exe", "C:\\Users\\Public\\lib.dll,Start (parent powershell.exe)"),
("10:01", "whoami.exe", "/all (parent rundll32.exe)"),
("10:03", "schtasks.exe", "/create /tn WinUpdateSvcHelper ... (parent rundll32.exe)"),
("10:05", "dns", "upd.cdn-lab.example, then every ~5 min")]
print("\npivot: ws15 timeline")
for t, what, detail in timeline:
print(f" {t} {what:15} {detail}")host user task writable_path lolbin_action odd_parent frequent score
ws08 dave backup-photos True False False False 1
ws15 erin WinUpdateSvcHelper True True True True 4
pivot: ws15 timeline
09:58 winword.exe opened invoice-0115.docm from Downloads
09:59 powershell.exe -nop -w hidden -enc ... (parent winword.exe)
10:00 rundll32.exe C:\Users\Public\lib.dll,Start (parent powershell.exe)
10:01 whoami.exe /all (parent rundll32.exe)
10:03 schtasks.exe /create /tn WinUpdateSvcHelper ... (parent rundll32.exe)
10:05 dns upd.cdn-lab.example, then every ~5 min
4. Conclusion
The hypothesis is confirmed on one host: on ws15 a task disguised with a Windows-like name runs a DLL from a world-writable folder every 30 minutes, created by rundll32.exe, which itself was started by PowerShell from a Word macro; the same host beacons to upd.cdn-lab.example. This is an intrusion, so the hunt hands over to incident response with the timeline (Triage, Escalation and Case Management): isolate ws15, preserve evidence, remove the task after evidence is collected, reset erin's credentials, and scope (search every host for the task name, the DLL hash and the domain). dave's task is personal software in his profile: not malicious, but a policy question for the desktop team.
5. Turn the hunt into a detection
The scoring criteria become a Sigma rule, tested against the hunt's own data (positive: the ws15 event; negatives: the installer, the IT deployment and dave's task):
title: Scheduled Task Created by Unusual Parent with User-Writable or LOLBin Action
id: 7b0c4b1e-2f6d-4c55-8a7e-9e3d2a1f6c40
status: test
tags: [attack.persistence, attack.execution, attack.t1053.005]
logsource: {category: process_creation, product: windows}
detection:
selection_create:
Image|endswith: '\schtasks.exe'
CommandLine|contains: '/create'
selection_action:
- CommandLine|contains: ['\Users\Public\', '\ProgramData\', '\AppData\Local\Temp\']
- CommandLine|contains: ['rundll32', 'regsvr32', 'mshta', 'wscript', 'cscript']
filter_parents:
ParentImage|endswith: ['\msiexec.exe', '\explorer.exe']
condition: all of selection_* and not filter_parents
falsepositives: [Software that registers tasks from a custom updater (add exact filters with an owner)]
level: highThe hunt report records the outputs: the incident, the new rule (status: test, then production after a week in silent mode), a hunting query for the same pattern via Security event 4698 to cover tasks created without schtasks.exe, and a data-gap ticket for the kiosks.
How scheduled-task persistence is prevented
- Application control (WDAC or AppLocker) that blocks execution from user-writable folders such as
C:\Users\Publicstops the task's action even if the task is created. - Least privilege: standard users can create tasks that run as themselves; tasks running as SYSTEM require admin rights, so fewer admins means less damaging persistence.
- Attack surface reduction: blocking Office from creating child processes and blocking Internet macros removes the first link of this chain.
- Audit: enable "Audit Other Object Access Events" so Windows logs 4698/4702 (task created/updated) independently of the tool used.
Pitfalls
- Ending the hunt at the first hit. One confirmed host means: search all hosts for the same indicators and behaviour before calling scope.
- Deleting the task before collecting evidence. The task definition (XML under
C:\Windows\System32\Tasks), the DLL and the timeline are evidence; collect first (Evidence Handoff to DFIR). - Writing the rule from the single positive only. Variants (
regsvr32, scripts in%TEMP%,/sc onlogon) belong in the rule's tests.
Practice
1. Score dave's task by hand with the four criteria. Why is its score not zero?
writable_path: yes (C:\Users\dave\tools) = 1; lolbin_action: no; odd_parent: no (cmd.exe is a normal shell); frequent: no (daily). Score 1: user-writable paths alone are common for personal tools, which is why the rule requires a second criterion.
2. Does the Sigma rule alert on dave's task?
selection_create matches; selection_action's first map needs \Users\Public\, \ProgramData\ or \AppData\Local\Temp\ and his path is \Users\dave\tools\, the second map needs a LOLBin name: neither matches, so no alert. Note that the rule's path list is narrower than the hunt's USER_WRITABLE pattern; that trade-off is documented in the rule's blind spots.
3. Which query covers tasks created without schtasks.exe?
Security event 4698 (a scheduled task was created) on all workstations, stacked by task name and by the <Command> in the task XML included in the event, filtered on the same path and LOLBin criteria. It requires the audit setting to be on.
Test yourself
Why does the hunt's scope statement list what is not covered?
- For formatting
- A negative result only holds for the data examined; tasks created without schtasks.exe or on hosts without EDR were not checked
- It is required by law
- To make it longer
Which single field in the stacks pointed most clearly to ws15?
- The user erin
- The parent process rundll32.exe, seen on one host
- The number of hosts with LabOfficeUpdate
- The task count
What is the right order after confirming the malicious task on ws15?
- Delete the task, then investigate
- Escalate to incident response, isolate, collect evidence, then remove the task and scope all hosts
- Ignore it until next week
- Reinstall every workstation
What should a hunt produce besides its finding?
- Nothing else
- A tested detection, documented blind spots, data-gap tickets and a repeatable query
- A blog post
- A new SIEM