Class: Msf::RPC::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/msf/core/rpc/v10/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(info = {}) ⇒ void

Initializes the RPC client to connect to: 127.0.0.1:3790 (TLS1) The connection information is overridden through the optional info hash.

Parameters:

  • info (Hash) (defaults to: {})

    Information needed for the initialization.

Options Hash (info):

  • :token (String)

    A token used by the client.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/msf/core/rpc/v10/client.rb', line 29

def initialize(info={})
  @user = nil
  @pass = nil

  self.info = {
    :host => '127.0.0.1',
    :port => 3790,
    :uri  => '/api/',
    :ssl  => true,
    :ssl_version => 'TLS1.2',
    :context     => {}
  }.merge(info)

  self.token = self.info[:token]
end

Instance Attribute Details

#infoHash

Returns Login information.

Returns:

  • (Hash)

    Login information.



20
21
22
# File 'lib/msf/core/rpc/v10/client.rb', line 20

def info
  @info
end

#tokenString

Returns A login token.

Returns:

  • (String)

    A login token.



16
17
18
# File 'lib/msf/core/rpc/v10/client.rb', line 16

def token
  @token
end

Instance Method Details

#call(meth, *args) ⇒ Hash

Calls an API.

Examples:

# This will return something like this:
# {"version"=>"4.11.0-dev", "ruby"=>"2.1.5 x86_64-darwin14.0 2014-11-13", "api"=>"1.0"}
rpc.call('core.version')

Parameters:

  • meth (String)

    The RPC API to call.

  • args (Array<string>)

    The arguments to pass.

Returns:

  • (Hash)

    The API response. It contains the following keys:

    • 'version' [String] Framework version.

    • 'ruby' [String] Ruby version.

    • 'api' [String] API version.

Raises:

  • (RuntimeError)

    Something is wrong while calling the remote API, including:

    • A missing token (your client needs to authenticate).

    • A unexpected response from the server, such as a timeout or unexpected HTTP code.

  • (Msf::RPC::ServerException)

    The RPC service returns an error.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/msf/core/rpc/v10/client.rb', line 89

def call(meth, *args)
  if meth == 'auth.logout'
    do_logout_cleanup
  end

  if meth != 'auth.login' && meth != 'health.check'
    unless self.token
      raise RuntimeError, "client not authenticated"
    end
    args.unshift(self.token)
  end

  args.unshift(meth)

  begin
    send_rpc_request(args)
  rescue Msf::RPC::ServerException => e
    if e.message =~ /Invalid Authentication Token/i && meth != 'auth.login' && @user && @pass
      
      args[1] = self.token
      retry
    else
      raise e
    end
  ensure
    @cli.close if @cli
  end

end

#closevoid

This method returns an undefined value.

Closes the client.



123
124
125
126
127
128
# File 'lib/msf/core/rpc/v10/client.rb', line 123

def close
  if @cli && @cli.conn?
    @cli.close
  end
  @cli = nil
end

#login(user, pass) ⇒ TrueClass

Logs in by calling the ‘auth.login’ API. The authentication token will expire after 5 minutes, but will automatically be rewnewed when you make a new RPC request.

Parameters:

  • user (String)

    Username.

  • pass (String)

    Password.

Returns:

  • (TrueClass)

    Indicating a successful login.

Raises:

  • RuntimeError Indicating a failed authentication.



53
54
55
56
57
58
59
60
61
62
# File 'lib/msf/core/rpc/v10/client.rb', line 53

def (user,pass)
  @user = user
  @pass = pass
  res = self.call("auth.login", user, pass)
  unless (res && res['result'] == "success")
    raise RuntimeError, "authentication failed"
  end
  self.token = res['token']
  true
end

#re_loginTrueClass

Attempts to login again with the last known user name and password.

Returns:

  • (TrueClass)

    Indicating a successful login.



68
69
70
# File 'lib/msf/core/rpc/v10/client.rb', line 68

def 
  (@user, @pass)
end