def __init__(self): self.types = Types() self.badges = Badges() self.sessions = Sessions() self.payloads = Payloads() self.local_storage = LocalStorage() self.importer = Importer()
class PayloadsTests: def __init__(self): self.badges = Badges() self.importer = Importer() self.local_storage = LocalStorage() self.payloads = Payloads() def perform_test(self): fail = False all_payloads = self.local_storage.get("payloads") if all_payloads: for database in all_payloads.keys(): self.badges.output_process("Testing payloads from " + database + " database...") payloads = all_payloads[database] for platform in payloads.keys(): for architecture in payloads[platform].keys(): for payload in payloads[platform][architecture].keys(): try: _ = self.importer.import_payload( payloads[platform][architecture][payload] ['Path']) self.badges.output_success( self.payloads.get_full_name( platform, architecture, payload) + ': OK') except Exception: self.badges.output_error( self.payloads.get_full_name( platform, architecture, payload) + ': FAIL') fail = True return fail
class HatSploitCommand(Command): local_storage = LocalStorage() modules = Modules() payloads = Payloads() details = { 'Category': "core", 'Name': "show", 'Authors': ['Ivan Nikolsky (enty8080)'], 'Description': "Show specified information.", 'Usage': "show <information>", 'MinArgs': 1 } def show_plugins(self): plugins = self.local_storage.get("plugins") headers = ("Number", "Name", "Description") for database in plugins.keys(): number = 0 plugins_data = list() plugins = plugins[database] for plugin in sorted(plugins.keys()): plugins_data.append( (number, plugin, plugins[plugin]['Description'])) number += 1 self.print_table("Plugins (" + database + ")", headers, *plugins_data) def show_modules(self, information): modules = self.local_storage.get("modules") headers = ("Number", "Module", "Risk", "Description") for database in modules.keys(): number = 0 modules_data = list() modules = modules[database][information] for platform in sorted(modules.keys()): for module in sorted(modules[platform].keys()): modules_data.append( (number, modules[platform][module]['Module'], modules[platform][module]['Risk'], modules[platform][module]['Description'])) number += 1 self.print_table( information.title() + " Modules (" + database + ")", headers, *modules_data) def show_payloads(self): payloads = self.local_storage.get("payloads") headers = ("Number", "Category", "Payload", "Risk", "Description") for database in sorted(payloads.keys()): number = 0 payloads_data = list() for platform in sorted(payloads[database].keys()): for architecture in sorted( payloads[database][platform].keys()): for payload in sorted( payloads[database][platform][architecture].keys()): current_payload = payloads[database][platform][ architecture][payload] payloads_data.append( (number, current_payload['Category'], current_payload['Payload'], current_payload['Risk'], current_payload['Description'])) number += 1 self.print_table("Payloads (" + database + ")", headers, *payloads_data) def show_options(self): current_module = self.modules.get_current_module_object() if not hasattr(current_module, "options") and not hasattr( current_module, "payload"): self.badges.output_warning("Module has no options.") return if not hasattr(current_module, "options") and not hasattr( self.payloads.get_current_payload(), "options"): self.badges.output_warning("Module has no options.") return if hasattr(current_module, "options"): options_data = list() headers = ("Option", "Value", "Required", "Description") options = current_module.options.copy() if hasattr(current_module, "payload"): options['PAYLOAD'] = dict() options['PAYLOAD']['Description'] = current_module.payload[ 'Description'] options['PAYLOAD']['Value'] = current_module.payload['Value'] options['PAYLOAD']['Type'] = None options['PAYLOAD']['Required'] = True for option in sorted(options.keys()): value, required = options[option]['Value'], options[option][ 'Required'] if required: required = "yes" else: required = "no" if not value and value != 0: value = "" options_data.append( (option, value, required, options[option]['Description'])) self.print_table( "Module Options (" + current_module.details['Module'] + ")", headers, *options_data) if hasattr(current_module, "payload"): if hasattr(self.payloads.get_current_payload(), "options"): options_data = list() headers = ("Option", "Value", "Required", "Description") current_payload = self.payloads.get_current_payload() if current_payload: for option in sorted(current_payload.options.keys()): value, required = current_payload.options[option]['Value'], \ current_payload.options[option]['Required'] if required: required = "yes" else: required = "no" if not value and value != 0: value = "" options_data.append( (option, value, required, current_payload.options[option]['Description'])) self.print_table( "Payload Options (" + current_payload.details['Payload'] + ")", headers, *options_data) def print_usage(self, informations, plugins, options): if informations or plugins or options: usage = "Informations: " if self.local_storage.get("payloads"): usage += "payloads, " for information in informations: usage += information + ", " if plugins: usage += "plugins, " if options: usage += "options" else: usage = usage[:-2] self.output_information(usage) else: self.output_warning("No informations available!") def run(self, argc, argv): information = argv[0] options = self.modules.check_current_module() payloads = self.local_storage.get("payloads") modules = self.local_storage.get("modules") plugins = self.local_storage.get("plugins") informations = list() if modules: for database in sorted(modules.keys()): for category in sorted(modules[database].keys()): informations.append(category) if payloads: if information == "payloads": self.show_payloads() return if plugins: if information == "plugins": self.show_plugins() return if options: if information == "options": self.show_options() return if information in informations: self.show_modules(information) else: self.print_usage(informations, plugins, options)
class HatSploitCommand(Command): payloads = Payloads() local_storage = LocalStorage() modules = Modules() jobs = Jobs() usage = "" usage += "run [option]\n\n" usage += " -h, --help Show this help message.\n" usage += " -j, --job Run current module as a background job.\n" details = { 'Category': "module", 'Name': "run", 'Authors': ['Ivan Nikolsky (enty8080)'], 'Description': "Run current module.", 'Usage': usage, 'MinArgs': 0 } def entry_to_module(self, argc, argv, current_module): if argc > 0: if argv[0] in ['-j', '--job']: self.output_process("Running module as a background job...") job_id = self.jobs.create_job(current_module.details['Name'], current_module.details['Module'], current_module.run) self.output_information("Module started as a background job " + str(job_id) + ".") return current_module.run() def run(self, argc, argv): if argc > 0: if argv[0] in ['-h', '--help']: self.output_usage(self.details['Usage']) return if self.modules.check_current_module(): current_module = self.modules.get_current_module_object() current_payload = self.payloads.get_current_payload() missed = 0 if hasattr(current_module, "options"): for option in current_module.options.keys(): current_option = current_module.options[option] if not current_option['Value'] and current_option[ 'Value'] != 0 and current_option['Required']: missed += 1 if current_payload: if hasattr(current_payload, "options"): for option in current_payload.options.keys(): current_option = current_payload.options[option] if not current_option['Value'] and current_option[ 'Value'] != 0 and current_option['Required']: missed += 1 if missed > 0: self.output_error("Missed some required options!") else: try: if current_payload: payload_name = current_module.payload['Value'] self.output_process( f"Configuring {payload_name} payload...") payload_data = current_payload.run() payload, args, session = None, None, None if isinstance(payload_data, tuple): if len(payload_data) == 2: payload, args = payload_data[0], payload_data[ 1] elif len(payload_data) == 3: payload, args, session = payload_data[ 0], payload_data[1], payload_data[2] else: payload = payload_data current_module.payload[ 'Category'] = current_payload.details['Category'] current_module.payload[ 'Platform'] = current_payload.details['Platform'] current_module.payload[ 'Type'] = current_payload.details['Type'] current_module.payload['Payload'] = payload current_module.payload['Args'] = args current_module.payload['Session'] = session self.entry_to_module(argc, argv, current_module) except Exception as e: self.output_error("An error occurred in module: " + str(e) + "!") else: self.output_warning("No module selected.")
class Modules: def __init__(self): self.types = Types() self.badges = Badges() self.sessions = Sessions() self.payloads = Payloads() self.local_storage = LocalStorage() self.importer = Importer() def check_exist(self, name): if self.check_style(name): all_modules = self.local_storage.get("modules") if all_modules: for database in all_modules.keys(): modules = all_modules[database] category = self.get_category(name) platform = self.get_platform(name) if category in modules.keys(): if platform in modules[category].keys(): module = self.get_name(name) if module in modules[category][platform].keys(): return True return False def check_imported(self, name): imported_modules = self.local_storage.get("imported_modules") if imported_modules: if name in imported_modules.keys(): return True return False @staticmethod def check_style(name): if len(name.split('/')) >= 3: return True return False def check_current_module(self): if self.local_storage.get("current_module"): if len(self.local_storage.get("current_module")) > 0: return True return False def get_module_object(self, category, platform, name): module_full_name = self.get_full_name(category, platform, name) if self.check_exist(module_full_name): database = self.get_database(module_full_name) return self.local_storage.get( "modules")[database][category][platform][name] return None def get_current_module_object(self): if self.check_current_module(): return self.local_storage.get_array( "current_module", self.local_storage.get("current_module_number")) return None def get_current_module_platform(self): if self.check_current_module(): return self.local_storage.get_array( "current_module", self.local_storage.get( "current_module_number")).details['Platform'] return None def get_current_module_name(self): if self.check_current_module(): return self.local_storage.get_array( "current_module", self.local_storage.get( "current_module_number")).details['Module'] return None def get_database(self, name): if self.check_style(name): all_modules = self.local_storage.get("modules") if all_modules: for database in all_modules.keys(): modules = all_modules[database] category = self.get_category(name) platform = self.get_platform(name) if category in modules.keys(): if platform in modules[category].keys(): module = self.get_name(name) if module in modules[category][platform].keys(): return database return None def get_category(self, name): if self.check_style(name): return name.split('/')[0] return None def get_platform(self, name): if self.check_style(name): return name.split('/')[1] return None def get_name(self, name): if self.check_style(name): return os.path.join(*(name.split(os.path.sep)[2:])) return None @staticmethod def get_full_name(category, platform, name): return category + '/' + platform + '/' + name def compare_types(self, value_type, value): if value_type and not value_type.lower == 'all': if value_type.lower() == 'mac': if not self.types.is_mac(value): self.badges.output_error( "Invalid value, expected valid MAC!") return False if value_type.lower() == 'ip': if not self.types.is_ip(value): self.badges.output_error( "Invalid value, expected valid IP!") return False if value_type.lower() == 'ipv4': if not self.types.is_ipv4(value): self.badges.output_error( "Invalid value, expected valid IPv4!") return False if value_type.lower() == 'ipv6': if not self.types.is_ipv6(value): self.badges.output_error( "Invalid value, expected valid IPv6!") return False if value_type.lower() == 'ipv4_range': if not self.types.is_ipv4_range(value): self.badges.output_error( "Invalid value, expected valid IPv4 range!") return False if value_type.lower() == 'ipv6_range': if not self.types.is_ipv6_range(value): self.badges.output_error( "Invalid value, expected valid IPv6 range!") return False if value_type.lower() == 'port': if not self.types.is_port(value): self.badges.output_error( "Invalid value, expected valid port!") return False if value_type.lower() == 'port_range': if not self.types.is_port_range(value): self.badges.output_error( "Invalid value, expected valid port range!") return False if value_type.lower() == 'number': if not self.types.is_number(value): self.badges.output_error( "Invalid value, expected valid number!") return False if value_type.lower() == 'integer': if not self.types.is_integer(value): self.badges.output_error( "Invalid value, expected valid integer!") return False if value_type.lower() == 'float': if not self.types.is_float(value): self.badges.output_error( "Invalid value, expected valid float!") return False if value_type.lower() == 'boolean': if not self.types.is_boolean(value): self.badges.output_error( "Invalid value, expected valid boolean!") return False if value_type.lower() == 'session': module_platform = self.get_current_module_platform() if not self.sessions.check_exist(module_platform, value): return False return True def set_current_module_option(self, option, value): if self.check_current_module(): current_module = self.get_current_module_object() if not hasattr(current_module, "options") and not hasattr( current_module, "payload"): self.badges.output_warning("Module has no options.") return if hasattr(current_module, "payload") and option.lower() == "payload": if self.payloads.check_exist(value): module_name = self.get_current_module_name() platform = self.payloads.get_platform(value) architecture = self.payloads.get_architecture(value) name = self.payloads.get_name(value) payload = self.payloads.get_payload_object( platform, architecture, name) module_payload = current_module.payload valid = 0 if module_payload['Types'] is None or payload[ 'Type'] in module_payload['Types']: valid += 1 if module_payload['Categories'] is None or payload[ 'Category'] in module_payload['Categories']: valid += 1 if module_payload['Platforms'] is None or payload[ 'Platform'] in module_payload['Platforms']: valid += 1 if module_payload['Architectures'] is None or payload[ 'Architecture'] in module_payload['Architectures']: valid += 1 if valid == 4: if not self.payloads.add_payload( module_name, platform, architecture, name): self.badges.output_error( "Invalid payload, expected valid payload!") return self.badges.output_information(option + " ==> " + value) self.local_storage.set_module_payload( "current_module", self.local_storage.get("current_module_number"), value) return self.badges.output_error( "Incompatible payload type, category or platform!") return self.badges.output_error( "Invalid payload, expected valid payload!") return if hasattr(current_module, "options"): if option in current_module.options.keys(): value_type = current_module.options[option]['Type'] if self.compare_types(value_type, value): self.badges.output_information(option + " ==> " + value) self.local_storage.set_module_option( "current_module", self.local_storage.get("current_module_number"), option, value) return if hasattr(current_module, "payload"): current_payload = self.payloads.get_current_payload() if current_payload and hasattr(current_payload, "options"): if option in current_payload.options.keys(): value_type = current_payload.options[option]['Type'] if self.compare_types(value_type, value): self.badges.output_information(option + " ==> " + value) self.local_storage.set_payload_option( current_module.details['Module'], current_payload.details['Payload'], option, value) else: self.badges.output_error("Unrecognized option!") else: self.badges.output_error("Unrecognized option!") else: self.badges.output_warning("No module selected.") def import_module(self, category, platform, name): modules = self.get_module_object(category, platform, name) try: module_object = self.importer.import_module(modules['Path']) if not self.local_storage.get("imported_modules"): self.local_storage.set("imported_modules", dict()) self.local_storage.update( "imported_modules", {self.get_full_name(category, platform, name): module_object}) except Exception: return None return module_object def add_module(self, category, platform, name): modules = self.get_module_object(category, platform, name) not_installed = list() for dependence in modules['Dependencies']: if not self.importer.import_check(dependence): not_installed.append(dependence) if not not_installed: imported_modules = self.local_storage.get("imported_modules") full_name = self.get_full_name(category, platform, name) if self.check_imported(full_name): module_object = imported_modules[full_name] self.add_to_global(module_object) else: module_object = self.import_module(category, platform, name) if module_object: if hasattr(module_object, "payload"): payload_name = module_object.payload['Value'] platform = self.payloads.get_platform(payload_name) architecture = self.payloads.get_architecture( payload_name) name = self.payloads.get_name(payload_name) self.badges.output_process("Using default payload " + payload_name + "...") if self.payloads.check_exist(payload_name): if self.payloads.add_payload( full_name, platform, architecture, name): self.add_to_global(module_object) return self.badges.output_error("Invalid default payload!") return self.add_to_global(module_object) else: self.badges.output_error( "Failed to select module from database!") else: self.badges.output_error( "Module depends this dependencies which is not installed:") for dependence in not_installed: self.badges.output_empty(" * " + dependence) def add_to_global(self, module_object): if self.check_current_module(): self.local_storage.add_array("current_module", '') self.local_storage.set( "current_module_number", self.local_storage.get("current_module_number") + 1) self.local_storage.set_array( "current_module", self.local_storage.get("current_module_number"), module_object) else: self.local_storage.set("current_module", []) self.local_storage.set("current_module_number", 0) self.local_storage.add_array("current_module", '') self.local_storage.set_array( "current_module", self.local_storage.get("current_module_number"), module_object)
def __init__(self): self.badges = Badges() self.importer = Importer() self.local_storage = LocalStorage() self.payloads = Payloads()