Class: Msf::RPC::Client
- Inherits:
-
Object
- Object
- Msf::RPC::Client
- Defined in:
- lib/msfrpc-client/client.rb
Instance Attribute Summary collapse
-
#info ⇒ Object
Returns the value of attribute info.
-
#token ⇒ Object
Returns the value of attribute token.
Class Method Summary collapse
-
.option_handler(options = {}) ⇒ Object
Load options from the command-line, environment.
-
.option_parser(options) ⇒ Object
Provides a parser object that understands the RPC specific options.
Instance Method Summary collapse
-
#call(meth, *args) ⇒ Object
Prepend the authentication token as the first parameter of every call except auth.login.
-
#initialize(config = {}) ⇒ Client
constructor
Create a new RPC Client instance.
-
#login(user, pass) ⇒ Object
Authenticate using a username and password.
Constructor Details
#initialize(config = {}) ⇒ Client
Create a new RPC Client instance
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/msfrpc-client/client.rb', line 27 def initialize(config={}) self.info = { :host => '127.0.0.1', :port => 3790, :uri => '/api/' + Msf::RPC::API_VERSION, :ssl => true, :ssl_version => 'TLS1', :context => {} }.merge(config) # Set the token self.token = self.info[:token] if not self.token and (info[:user] and info[:pass]) login(info[:user], info[:pass]) end end |
Instance Attribute Details
#info ⇒ Object
Returns the value of attribute info.
22 23 24 |
# File 'lib/msfrpc-client/client.rb', line 22 def info @info end |
#token ⇒ Object
Returns the value of attribute token.
22 23 24 |
# File 'lib/msfrpc-client/client.rb', line 22 def token @token end |
Class Method Details
.option_handler(options = {}) ⇒ Object
Load options from the command-line, environment. and any configuration files specified
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/msfrpc-client/client.rb', line 167 def self.option_handler(={}) [:host] ||= ENV['MSFRPC_HOST'] [:port] ||= ENV['MSFRPC_PORT'] [:uri] ||= ENV['MSFRPC_URI'] [:user] ||= ENV['MSFRPC_USER'] [:pass] ||= ENV['MSFRPC_PASS'] [:ssl] ||= ENV['MSFRPC_SSL'] [:token] ||= ENV['MSFRPC_TOKEN'] [:config] ||= ENV['MSFRPC_CONFIG'] empty_keys = .keys.select{|k| [k].nil? } empty_keys.each { |k| .delete(k) } config_file = .delete(:config) if config_file yaml_data = ::File.read(config_file) rescue nil if yaml_data yaml = ::YAML.load(yaml_data) rescue nil if yaml and yaml.kind_of?(::Hash) and yaml['options'] yaml['options'].each_pair do |k,v| case k when 'ssl' [k.intern] = !!(v.to_s =~ /^(t|y|1)/i) when 'port' [k.intern] = v.to_i else [k.intern] = v end end else $stderr.puts "[-] Could not parse configuration file: #{config_file}" exit(1) end else $stderr.puts "[-] Could not read configuration file: #{config_file}" exit(1) end end if [:port] [:port] = [:port].to_i end if [:ssl] [:ssl] = !!([:ssl].to_s =~ /^(t|y|1)/i) end end |
.option_parser(options) ⇒ Object
Provides a parser object that understands the RPC specific options
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/msfrpc-client/client.rb', line 114 def self.option_parser() parser = OptionParser.new parser. = "Usage: #{$0} [options]" parser.separator('') parser.separator('RPC Options:') parser.on("--rpc-host HOST") do |v| [:host] = v end parser.on("--rpc-port PORT") do |v| [:port] = v.to_i end parser.on("--rpc-ssl <true|false>") do |v| [:ssl] = v end parser.on("--rpc-uri URI") do |v| [:uri] = v end parser.on("--rpc-user USERNAME") do |v| [:user] = v end parser.on("--rpc-pass PASSWORD") do |v| [:pass] = v end parser.on("--rpc-token TOKEN") do |v| [:token] = v end parser.on("--rpc-config CONFIG-FILE") do |v| [:config] = v end parser.on("--rpc-help") do $stderr.puts parser exit(1) end parser.separator('') parser end |
Instance Method Details
#call(meth, *args) ⇒ Object
Prepend the authentication token as the first parameter of every call except auth.login. This simplifies the calling API.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/msfrpc-client/client.rb', line 63 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 Pro 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) 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(res.code, resp['error_message'] || resp['error_string'], resp['error_class'], resp['error_backtrace']) end return resp else raise RuntimeError, res.inspect end end |
#login(user, pass) ⇒ Object
Authenticate using a username and password
49 50 51 52 53 54 55 56 |
# File 'lib/msfrpc-client/client.rb', line 49 def login(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 |