Windows Scheduled Task Persistence
Scheduled tasks are one of the most reliable persistence mechanisms on Windows. They blend in with the dozens of legitimate tasks already present on every installation. Advantages over other methods:
Survive reboots and run as any user including SYSTEM
Support complex triggers (time-based, event-based, at logon)
Tasks running as SYSTEM execute even when no user is logged in
Can be created remotely for persistence on other hosts
Tasks created with admin rights that run as SYSTEM are the most flexible option.
Creating tasks with schtasks
The built-in schtasks command works on all Windows versions and requires no additional tools.
Task names that resemble legitimate Windows tasks blend in better. Common patterns: WindowsUpdate, MicrosoftEdgeUpdate, OneDriveRemediation. Place tasks in a subfolder to further reduce visibility: use /tn "\Microsoft\Windows\UpdateOrchestrator\TaskName".
Creating tasks with PowerShell
The PowerShell task cmdlets give more control over trigger types and task properties than schtasks.
XML task import
Tasks can be defined in XML and imported. This is useful for creating tasks with trigger types or properties that schtasks does not expose through command-line flags, and the XML can be staged on disk and imported in one step.
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<Triggers>
<BootTrigger>
<Enabled>true</Enabled>
</BootTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>S-1-5-18</UserId>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<Hidden>true</Hidden>
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
</Settings>
<Actions>
<Exec>
<Command>C:\Windows\Temp\payload.exe</Command>
</Exec>
</Actions>
</Task>
Remote task creation
Tasks can be created on remote hosts directly from your current session if you have admin credentials. This is useful for establishing persistence on a host you have moved to without needing an interactive shell.
Cleaning up
References
-
Task Scheduler XML Schema: Microsoft DocsDOCS: Task Scheduler XML Schema: Microsoft Docs, learn.microsoft.com/en-us/windows/win32/taskschd/task-scheduler-schema (opens in new tab)
Full XML schema reference for task definitions
-
schtasks create: Microsoft DocsDOCS: schtasks create: Microsoft Docs, learn.microsoft.com/en-us/windows-server/administration/windows-commands/schtasks-create (opens in new tab)
All schtasks /create flags and trigger types