Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/post/hwbridge/client.rb
Views: 11783
# -*- coding: binary -*-12require 'rex/post/hwbridge/extension'3require 'rex/post/hwbridge/object_aliases'45module Rex6module Post7module HWBridge89# Used for merging constants from extensions10module Extensions11end1213class Client14@@ext_hash = {}1516#17# Checks the extension hash to see if a class has already been associated18# with the supplied extension name.19#20def self.check_ext_hash(name)21@@ext_hash[name]22end2324#25# Stores the name to class association for the supplied extension name.26#27def self.set_ext_hash(name, klass)28@@ext_hash[name] = klass29end3031#32# Initializes the client context33#34def initialize(sock,opts={})35init_hwbridge(sock,opts)36end3738#39# Initialize the hwbridge instance40#41def init_hwbridge(sock,opts={})42self.sock = sock43self.ext = ObjectAliases.new44self.ext_aliases = ObjectAliases.new45end4647#48# sends request through 'exploit' which is the hwbridge/connect49#50def send_request(uri)51if not exploit52$stdout.puts("Exploit module not connected")53return {}54end55exploit.fetch_json(uri)56end5758#59# Gets/refreshes HW status & capabilities60#61def get_status62send_request("/status")63end6465#66# Gets the devices statistics67#68def get_statistics69send_request("/statistics")70end7172#73# Fetches custom methods from HW, if any74#75def get_custom_methods76send_request("/custom_methods")77end7879#80# Sends a reset signal to the device to perform a software bounce or a full81# factory reset. Depends on how the device decided to handle it.82#83def reset84send_request("/control/factory_reset")85end8687#88# Sends a reboot signal to reboot the device.89#90def reboot91send_request("/control/reboot")92end9394##95#96# Alias processor97#98##99100#101# Translates unhandled methods into registered extension aliases102# if a matching extension alias exists for the supplied symbol.103#104def method_missing(symbol, *args)105self.ext_aliases.aliases[symbol.to_s]106end107108##109#110# Extension registration111#112##113114#115# Loads the client half of the supplied extension and initializes it as a116# registered extension that can be reached through client.ext.[extension].117#118def add_extension(name, commands=[])119self.commands |= commands120121# Check to see if this extension has already been loaded.122if ((klass = self.class.check_ext_hash(name.downcase)) == nil)123old = Rex::Post::HWBridge::Extensions.constants124require("rex/post/hwbridge/extensions/#{name.downcase}/#{name.downcase}")125new = Rex::Post::HWBridge::Extensions.constants126127# No new constants added?128if ((diff = new - old).empty?)129diff = [ name.capitalize ]130end131132klass = Rex::Post::HWBridge::Extensions.const_get(diff[0]).const_get(diff[0])133134# Save the module name to class association now that the code is135# loaded.136self.class.set_ext_hash(name.downcase, klass)137end138139# Create a new instance of the extension140inst = klass.new(self)141142self.ext.aliases[inst.name] = inst143144return true145end146147#148# Deregisters an extension alias of the supplied name.149#150def deregister_extension(name)151self.ext.aliases.delete(name)152end153154#155# Enumerates all of the loaded extensions.156#157def each_extension(&block)158self.ext.aliases.each(block)159end160161#162# Registers an aliased extension that can be referenced through163# client.name.164#165def register_extension_alias(name, ext)166self.ext_aliases.aliases[name] = ext167# Whee! Syntactic sugar, where art thou?168#169# Create an instance method on this object called +name+ that returns170# +ext+. We have to do it this way instead of simply171# self.class.class_eval so that other meterpreter sessions don't get172# extension methods when this one does173(class << self; self; end).class_eval do174define_method(name.to_sym) do175ext176end177end178ext179end180181#182# Registers zero or more aliases that are provided in an array.183#184def register_extension_aliases(aliases)185aliases.each { |a|186register_extension_alias(a['name'], a['ext'])187}188end189190#191# Deregisters a previously registered extension alias.192#193def deregister_extension_alias(name)194self.ext_aliases.aliases.delete(name)195end196197#198# Dumps the extension tree.199#200def dump_extension_tree()201items = []202items.concat(self.ext.dump_alias_tree('client.ext'))203items.concat(self.ext_aliases.dump_alias_tree('client'))204205return items.sort206end207208#209# Encodes (or not) a UTF-8 string210#211def unicode_filter_encode(str)212self.encode_unicode ? Rex::Text.unicode_filter_encode(str) : str213end214215#216# Decodes (or not) a UTF-8 string217#218def unicode_filter_decode(str)219self.encode_unicode ? Rex::Text.unicode_filter_decode(str) : str220end221222# A list of the commands223#224attr_reader :commands225attr_reader :ext, :sock226protected227attr_writer :commands # :nodoc:228attr_accessor :ext_aliases # :nodoc:229attr_writer :ext, :sock # :nodoc:230231end232233###234#235# Exception thrown when a request fails.236#237###238class RequestError < ArgumentError239def initialize(method, einfo, ecode=nil)240@method = method241@result = einfo242@code = ecode || einfo243end244245def to_s246"#{@method}: Operation failed: #{@result}"247end248249# The method that failed.250attr_reader :method251252# The error result that occurred, typically a windows error message.253attr_reader :result254255# The error result that occurred, typically a windows error code.256attr_reader :code257end258259end260end261end262263264