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 = {}) ⇒ Client


18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/msf/core/rpc/v10/client.rb', line 18

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

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

Instance Attribute Details

#infoObject

Returns the value of attribute info


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

def info
  @info
end

#tokenObject

Returns the value of attribute token


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

def token
  @token
end

Instance Method Details

#call(meth, *args) ⇒ Object

Prepend the authentication token as the first parameter of every call except auth.login. Requires the


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/msf/core/rpc/v10/client.rb', line 43

def call(meth, *args)
  if(meth != "auth.login")
    if(not self.token)
      raise RuntimeError, "client not authenticated"
    end
    args.unshift(self.token)
  end

  args.unshift(meth)

  if not @cli
    @cli = Rex::Proto::Http::Client.new(info[:host], info[:port], info[:context], info[:ssl], info[:ssl_version])
    @cli.set_config(
      :vhost => info[:host],
      :agent => "Metasploit RPC Client/#{API_VERSION}",
      :read_max_data => (1024*1024*512)
    )
  end

  req = @cli.request_cgi(
    'method' => 'POST',
    'uri'    => self.info[:uri],
    'ctype'  => 'binary/message-pack',
    'data'   => args.to_msgpack
  )

  res = @cli.send_recv(req)
  @cli.close

  if res and [200, 401, 403, 500].include?(res.code)
    resp = MessagePack.unpack(res.body)

    if resp and resp.kind_of?(::Hash) and resp['error'] == true
      raise Msf::RPC::ServerException.new(resp['error_code'] || res.code, resp['error_message'] || resp['error_string'], resp['error_class'], resp['error_backtrace'])
    end

    return resp
  else
    raise RuntimeError, res.inspect
  end
end

#closeObject


85
86
87
88
89
90
# File 'lib/msf/core/rpc/v10/client.rb', line 85

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

#login(user, pass) ⇒ Object


32
33
34
35
36
37
38
39
# File 'lib/msf/core/rpc/v10/client.rb', line 32

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