Class: Rex::Post::HWBridge::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/post/hwbridge/client.rb

Direct Known Subclasses

Msf::Sessions::HWBridge

Constant Summary collapse

@@ext_hash =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sock, opts = {}) ⇒ Client

Initializes the client context



35
36
37
# File 'lib/rex/post/hwbridge/client.rb', line 35

def initialize(sock,opts={})
  init_hwbridge(sock,opts)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object

Translates unhandled methods into registered extension aliases if a matching extension alias exists for the supplied symbol.



105
106
107
# File 'lib/rex/post/hwbridge/client.rb', line 105

def method_missing(symbol, *args)
  self.ext_aliases.aliases[symbol.to_s]
end

Instance Attribute Details

#commandsObject

A list of the commands



225
226
227
# File 'lib/rex/post/hwbridge/client.rb', line 225

def commands
  @commands
end

#extObject

Returns the value of attribute ext.



226
227
228
# File 'lib/rex/post/hwbridge/client.rb', line 226

def ext
  @ext
end

#ext_aliasesObject (protected)

:nodoc:



229
230
231
# File 'lib/rex/post/hwbridge/client.rb', line 229

def ext_aliases
  @ext_aliases
end

#sockObject

Returns the value of attribute sock.



226
227
228
# File 'lib/rex/post/hwbridge/client.rb', line 226

def sock
  @sock
end

Class Method Details

.check_ext_hash(name) ⇒ Object

Checks the extension hash to see if a class has already been associated with the supplied extension name.



21
22
23
# File 'lib/rex/post/hwbridge/client.rb', line 21

def self.check_ext_hash(name)
  @@ext_hash[name]
end

.set_ext_hash(name, klass) ⇒ Object

Stores the name to class association for the supplied extension name.



28
29
30
# File 'lib/rex/post/hwbridge/client.rb', line 28

def self.set_ext_hash(name, klass)
  @@ext_hash[name] = klass
end

Instance Method Details

#add_extension(name, commands = []) ⇒ Object

Loads the client half of the supplied extension and initializes it as a registered extension that can be reached through client.ext..



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
# File 'lib/rex/post/hwbridge/client.rb', line 119

def add_extension(name, commands=[])
  self.commands |= commands

  # Check to see if this extension has already been loaded.
  if ((klass = self.class.check_ext_hash(name.downcase)) == nil)
    old = Rex::Post::HWBridge::Extensions.constants
    require("rex/post/hwbridge/extensions/#{name.downcase}/#{name.downcase}")
    new = Rex::Post::HWBridge::Extensions.constants

    # No new constants added?
    if ((diff = new - old).empty?)
      diff = [ name.capitalize ]
    end

    klass = Rex::Post::HWBridge::Extensions.const_get(diff[0]).const_get(diff[0])

    # Save the module name to class association now that the code is
    # loaded.
    self.class.set_ext_hash(name.downcase, klass)
  end

  # Create a new instance of the extension
  inst = klass.new(self)

  self.ext.aliases[inst.name] = inst

  return true
end

#deregister_extension(name) ⇒ Object

Deregisters an extension alias of the supplied name.



151
152
153
# File 'lib/rex/post/hwbridge/client.rb', line 151

def deregister_extension(name)
  self.ext.aliases.delete(name)
end

#deregister_extension_alias(name) ⇒ Object

Deregisters a previously registered extension alias.



194
195
196
# File 'lib/rex/post/hwbridge/client.rb', line 194

def deregister_extension_alias(name)
  self.ext_aliases.aliases.delete(name)
end

#dump_extension_treeObject

Dumps the extension tree.



201
202
203
204
205
206
207
# File 'lib/rex/post/hwbridge/client.rb', line 201

def dump_extension_tree()
  items = []
  items.concat(self.ext.dump_alias_tree('client.ext'))
  items.concat(self.ext_aliases.dump_alias_tree('client'))

  return items.sort
end

#each_extension(&block) ⇒ Object

Enumerates all of the loaded extensions.



158
159
160
# File 'lib/rex/post/hwbridge/client.rb', line 158

def each_extension(&block)
  self.ext.aliases.each(block)
end

#get_custom_methodsObject

Fetches custom methods from HW, if any



76
77
78
# File 'lib/rex/post/hwbridge/client.rb', line 76

def get_custom_methods
  send_request("/custom_methods")
end

#get_statisticsObject

Gets the devices statistics



69
70
71
# File 'lib/rex/post/hwbridge/client.rb', line 69

def get_statistics
  send_request("/statistics")
end

#get_statusObject

Gets/refreshes HW status & capabilities



62
63
64
# File 'lib/rex/post/hwbridge/client.rb', line 62

def get_status
  send_request("/status")
end

#init_hwbridge(sock, opts = {}) ⇒ Object

Initialize the hwbridge instance



42
43
44
45
46
# File 'lib/rex/post/hwbridge/client.rb', line 42

def init_hwbridge(sock,opts={})
  self.sock        = sock
  self.ext         = ObjectAliases.new
  self.ext_aliases = ObjectAliases.new
end

#rebootObject

Sends a reboot signal to reboot the device.



91
92
93
# File 'lib/rex/post/hwbridge/client.rb', line 91

def reboot
  send_request("/control/reboot")
end

#register_extension_alias(name, ext) ⇒ Object

Registers an aliased extension that can be referenced through client.name.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/rex/post/hwbridge/client.rb', line 166

def register_extension_alias(name, ext)
  self.ext_aliases.aliases[name] = ext
  # Whee!  Syntactic sugar, where art thou?
  #
  # Create an instance method on this object called +name+ that returns
  # +ext+.  We have to do it this way instead of simply
  # self.class.class_eval so that other meterpreter sessions don't get
  # extension methods when this one does
  (class << self; self; end).class_eval do
    define_method(name.to_sym) do
      ext
    end
  end
  ext
end

#register_extension_aliases(aliases) ⇒ Object

Registers zero or more aliases that are provided in an array.



185
186
187
188
189
# File 'lib/rex/post/hwbridge/client.rb', line 185

def register_extension_aliases(aliases)
  aliases.each { |a|
    register_extension_alias(a['name'], a['ext'])
  }
end

#resetObject

Sends a reset signal to the device to perform a software bounce or a full factory reset. Depends on how the device decided to handle it.



84
85
86
# File 'lib/rex/post/hwbridge/client.rb', line 84

def reset
  send_request("/control/factory_reset")
end

#send_request(uri) ⇒ Object

sends request through ‘exploit’ which is the hwbridge/connect



51
52
53
54
55
56
57
# File 'lib/rex/post/hwbridge/client.rb', line 51

def send_request(uri)
  if not exploit
    $stdout.puts("Exploit module not connected")
    return {}
  end
  exploit.fetch_json(uri)
end

#unicode_filter_decode(str) ⇒ Object

Decodes (or not) a UTF-8 string



219
220
221
# File 'lib/rex/post/hwbridge/client.rb', line 219

def unicode_filter_decode(str)
  self.encode_unicode ? Rex::Text.unicode_filter_decode(str) : str
end

#unicode_filter_encode(str) ⇒ Object

Encodes (or not) a UTF-8 string



212
213
214
# File 'lib/rex/post/hwbridge/client.rb', line 212

def unicode_filter_encode(str)
  self.encode_unicode ? Rex::Text.unicode_filter_encode(str) : str
end