lib/rex/post/meterpreter/ui/console/command_dispatcher/core.rb
#
# Migrates the server to the supplied process identifier.
#
# @param args [Array<String>] Commandline arguments, -h or a pid. On linux
#   platforms a path for the unix domain socket used for IPC.
# @return [void]
Migrates the server to the supplied process identifier.
Migrates the server to the supplied process identifier .
def cmd_migrate(*args)
    if ( args.length == 0 or args.include?("-h") )
      cmd_migrate_help
      return true
    end
    pid = args[0].to_i
    if(pid == 0)
      print_error("A process ID must be specified, not a process name")
      return
    end
    if client.platform =~ /linux/
      writable_dir = (args.length >= 2) ? args[1] : nil
    end
    begin
      server = client.sys.process.open
    rescue TimeoutError => e
      elog(e.to_s)
    rescue RequestError => e
      elog(e.to_s)
    end
    service = client.pfservice
    existing_relays = []
    if service
      service.each_tcp_relay do |lhost, lport, rhost, rport, opts|
        next unless opts['MeterpreterRelay']
        if existing_relays.empty?
          print_status("Removing existing TCP relays...")
        end
        if (service.stop_tcp_relay(lport, lhost))
          print_status("Successfully stopped TCP relay on #{lhost || '0.0.0.0'}:#{lport}")
          existing_relays << {
            :lport => lport,
            :opts => opts
          }
        else
          print_error("Failed to stop TCP relay on #{lhost || '0.0.0.0'}:#{lport}")
          next
        end
      end
      unless existing_relays.empty?
        print_status("#{existing_relays.length} TCP relay(s) removed.")
      end
    end
    server ? print_status("Migrating from #{server.pid} to #{pid}...") : print_status("Migrating to #{pid}")
    if client.platform =~ /linux/
      client.core.migrate(pid, writable_dir)
    else
      client.core.migrate(pid)
    end
    print_status("Migration completed successfully.")
    client.update_session_info
    unless existing_relays.empty?
      print_status("Recreating TCP relay(s)...")
      existing_relays.each do |r|
        client.pfservice.start_tcp_relay(r[:lport], r[:opts])
        print_status("Local TCP relay recreated: #{r[:opts]['LocalHost'] || '0.0.0.0'}:#{r[:lport]} <-> #{r[:opts]['PeerHost']}:#{r[:opts]['PeerPort']}")
      end
    end
  end
def cmd_migrate ( * args ) if ( args . length == 0 or args . include? ( "-h" ) ) cmd_migrate_help return true end pid = args [ 0 ] . to_i if ( pid == 0 ) print_error ( "A process ID must be specified, not a process name" ) return end if client . platform =~ / linux / writable_dir = ( args . length >= 2 ) ? args [ 1 ] : nil end begin server = client . sys . process . open rescue TimeoutError => e elog ( e . to_s ) rescue RequestError => e elog ( e . to_s ) end service = client . pfservice existing_relays = [ ] if service service . each_tcp_relay do | lhost , lport , rhost , rport , opts | next unless opts [ 'MeterpreterRelay' ] if existing_relays . empty? print_status ( "Removing existing TCP relays..." ) end if ( service . stop_tcp_relay ( lport , lhost ) ) print_status ( "Successfully stopped TCP relay on #{lhost || '0.0.0.0'}:#{lport}" ) existing_relays << { :lport => lport , :opts => opts } else print_error ( "Failed to stop TCP relay on #{lhost || '0.0.0.0'}:#{lport}" ) next end end unless existing_relays . empty? print_status ( "#{existing_relays.length} TCP relay(s) removed." ) end end server ? print_status ( "Migrating from #{server.pid} to #{pid}..." ) : print_status ( "Migrating to #{pid}" ) if client . platform =~ / linux / client . core . migrate ( pid , writable_dir ) else client . core . migrate ( pid ) end print_status ( "Migration completed successfully." ) client . update_session_info unless existing_relays . empty? print_status ( "Recreating TCP relay(s)..." ) existing_relays . each do | r | client . pfservice . start_tcp_relay ( r [ :lport ] , r [ :opts ] ) print_status ( "Local TCP relay recreated: #{r[:opts]['LocalHost'] || '0.0.0.0'}:#{r[:lport]} <-> #{r[:opts]['PeerHost']}:#{r[:opts]['PeerPort']}" ) end end end
Migrates the server to the supplied process identifier.
Migrates the server to the supplied process identifier .
# If we have any open port forwards, we need to close them down # otherwise we'll end up with local listeners which aren't connected # to valid channels in the migrated meterpreter instance. # Do this thang. # Update session info (we may have a new username)
None
void
args
Commandline arguments, -h or a pid. On linux platforms a path for the unix domain socket used for IPC.
Commandline arguments - h or a pid . On linux platforms a path for the unix domain socket used for IPC .