Class: Msf::RPC::Client
- Inherits:
-
Object
- Object
- Msf::RPC::Client
- Defined in:
- lib/msfrpc-client/client.rb
Instance Attribute Summary collapse
-
#info ⇒ Hash
Login information.
-
#token ⇒ String
A login 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) ⇒ Hash
Calls an API.
-
#close ⇒ void
Closes the client.
-
#initialize(info = {}) ⇒ void
constructor
Initializes the RPC client to connect to: 127.0.0.1:3790 (TLS1) The connection information is overridden through the optional info hash.
-
#login(user, pass) ⇒ TrueClass
Logs in by calling the ‘auth.login’ API.
-
#re_login ⇒ TrueClass
Attempts to login again with the last known user name and password.
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.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/msfrpc-client/client.rb', line 36 def initialize(info = {}) @user = nil @pass = nil self.info = { host: '127.0.0.1', port: 3790, uri: '/api/', ssl: true, ssl_version: 'TLS1.2', }.merge(info) self.token = self.info[:token] end |
Instance Attribute Details
#info ⇒ Hash
Returns Login information.
27 28 29 |
# File 'lib/msfrpc-client/client.rb', line 27 def info @info end |
#token ⇒ String
Returns A login token.
23 24 25 |
# File 'lib/msfrpc-client/client.rb', line 23 def token @token end |
Class Method Details
.option_handler(options = {}) ⇒ Object
Load options from the command-line, environment. and any configuration files specified
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/msfrpc-client/client.rb', line 198 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 && yaml.is_a?(::Hash) && 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 [:port] = [:port].to_i if [:port] [:ssl] = !!([:ssl].to_s =~ /^(t|y|1)/i) if [:ssl] end |
.option_parser(options) ⇒ Object
Provides a parser object that understands the RPC specific options
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 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 |
# File 'lib/msfrpc-client/client.rb', line 145 def self.option_parser() parser = OptionParser.new parser. = "Usage: #{$PROGRAM_NAME} [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) ⇒ Hash
Calls an API.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/msfrpc-client/client.rb', line 102 def call(meth, *args) if meth == 'auth.logout' do_logout_cleanup end unless meth == 'auth.login' unless self.token raise Msf::RPC::Exception.new('Client not authenticated') end args.unshift(self.token) end args.unshift(meth) begin send_rpc_request(args) rescue Msf::RPC::ServerException => e if e. =~ /Invalid Authentication Token/i && meth != 'auth.login' && @user && @pass re_login args[1] = self.token retry else raise e end end end |
#close ⇒ void
This method returns an undefined value.
Closes the client.
133 134 135 |
# File 'lib/msfrpc-client/client.rb', line 133 def close @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.
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/msfrpc-client/client.rb', line 60 def login(user, pass) @user = user @pass = pass res = self.call('auth.login', user, pass) unless res && res['result'] == 'success' raise Msf::RPC::Exception.new('Authentication failed') end self.token = res['token'] true end |
#re_login ⇒ TrueClass
Attempts to login again with the last known user name and password.
76 77 78 |
# File 'lib/msfrpc-client/client.rb', line 76 def re_login login(@user, @pass) end |