def test_check_valid_manifest(self): Loader.check_valid_level_manifest({ "id": "testManifest", "startpointLevelId": "testId", "startpointCodename": "codeTest", "entrypoints": [], }) with self.assertRaises(InvalidManifestStructure): Loader.check_valid_level_manifest({})
def validate_stager(stager): stagers = Loader() stagers.type = "stager" stagers.paths = ["stagers/"] stagers.get_loadables() for s in stagers.loaded: if s.name == stager.lower(): return s return None
def __init__(self, prompt_session): Loader.__init__(self) self.type = "module" self.paths = ["modules/"] self.name = 'modules' self.prompt = HTML('ST (<ansired>modules</ansired>) ≫ ') self.completer = STCompleter(self) self.prompt_session = prompt_session self.selected = None self.get_loadables()
def __init__(self, prompt_session): Loader.__init__(self) self.type = "module" self.paths = ["modules/ipy", "modules/boo"] self.name = 'modules' self.description = 'Module menu' self.prompt = HTML('ST (<ansired>modules</ansired>) ≫ ') self.completer = STCompleter(self) self.prompt_session = prompt_session self.selected = None self.get_loadables()
def validate_listener(listener): listeners = Loader() listeners.type = "listener" listeners.paths = ["listeners/"] listeners.get_loadables() for l in listeners.loaded: if l.name == listener.name: l['BindIP'] = listener['BindIP'] l['Port'] = listener['Port'] return l return None
def clustering(dataset_folder, dataset, window_size=10, hash_module=1024, threshold=26, input_limit=None): loader = Loader() pages = loader.load_pages(dataset_folder, dataset) pages = pages[:input_limit] logger = Logger.get_instance() logger.print('############### INIZIO PASSO 1 ####################', 1) hash_table = {} algoritmo = Algoritmo() hash_table = algoritmo.passo1(pages, window_size, hash_module) logger.print('############### FINE PASSO 1 ####################', 1) logger.print(hash_table, 3) logger.print('############### INIZIO PASSO 2 ####################', 1) hash_table = algoritmo.passo2(hash_table, threshold) ## TODO: testing passo1 ## TODO: testing passo2 ## TODO: testing passo3 ## TODO: da rivedere bene come fare gli hash che per ora sono fortemente dipendenti dal modulo che scegliamo, anche alla luce dei risultati che raggiungiamo logger.print('############### FINE PASSO 2 ####################', 1) logger.print(hash_table, 3) logger.print('############### INIZIO PASSO 3 ####################', 1) cluster = {} cluster = algoritmo.passo3(hash_table, pages, hash_module, window_size) logger.print('################ FINE PASSO 3 ####################', 1) logger.print('Numero cluster ' + str(len(cluster)), 2) logger.print('\nClusters: \n', 2) logger.print(cluster, 2) file = open("prediction.csv", "w") index_cluster = 0 for key in cluster: logger.print("\ncluster\n", 3) for page in cluster[key]: file.write(page.name + ", " + str(index_cluster) + "\n") index_cluster += 1 file.close()
def __init__(self, prompt_session): Loader.__init__(self) self.type = "listener" self.paths = ["listeners/"] self.listeners = [] self.name = 'listeners' self.prompt = HTML('ST (<ansired>listeners</ansired>) ≫ ') self.completer = STCompleter(self) self.prompt_session = prompt_session self.selected = None ipc_server.attach(GET_LISTENERS, self.__get_running_listeners) self.get_loadables()
def __init__(self, prompt_session): Loader.__init__(self) self.type = "listener" self.paths = ["listeners/"] self.listeners = [] self.name = 'listeners' self.prompt = HTML('ST (<ansired>listeners</ansired>) ≫ ') self.completer = WordCompleter(['start', 'set', 'sessions', 'modules', 'stagers', 'options', 'use', 'exit', 'list'], ignore_case=True) self.prompt_session = prompt_session self.selected = None ipc_server.attach(GET_LISTENERS, self.__get_running_listeners) self.get_loadables()
def __init__(self, prompt_session): Loader.__init__(self) self.type = "stager" self.paths = ["stagers/"] self.name = 'stagers' self.description = 'Stager menu' self.prompt = HTML('ST (<ansired>stagers</ansired>) ≫ ') self.completer = STCompleter(self) self.prompt_session = prompt_session self.selected = None ipc_server.attach(events.GET_STAGERS, self.get_stagers) self.get_loadables()
def __init__(self, prompt_session): Loader.__init__(self) self.type = "module" self.paths = ["modules/"] self.name = 'modules' self.prompt = HTML('ST (<ansired>modules</ansired>) ≫ ') self.completer = WordCompleter([ 'set', 'use', 'run', 'sessions', 'stagers', 'list', 'listeners', 'options', 'exit' ], ignore_case=True) self.prompt_session = prompt_session self.selected = None self.get_loadables()
def __init__(self, prompt_session): Loader.__init__(self) self.type = "stager" self.paths = ["stagers/"] self.name = 'stagers' self.description = 'Stager menu' self.prompt = HTML('ST (<ansired>stagers</ansired>) ≫ ') self.completer = STCompleter(self) self.prompt_session = prompt_session self.selected = None ipc_server.attach(events.GET_STAGERS, self.get_stagers) self.get_loadables()
def __init__(self, prompt_session): Loader.__init__(self) self.type = "listener" self.paths = ["listeners/"] self.listeners = [] self.name = 'listeners' self.description = 'Listener menu' self.prompt = HTML('ST (<ansired>listeners</ansired>) ≫ ') self.completer = STCompleter(self) self.prompt_session = prompt_session self.selected = None ipc_server.attach(events.GET_LISTENERS, self.get_listeners) self.get_loadables()
def add_routes(app, path, service): """ Add the exposed functions of the *obj* object to the *base_url* route. """ route = f"/{path}/" for mth in Loader.get_public_methods(service): app.add_url_rule(route + mth, route + mth, getattr(service, mth), methods=['GET'])
def __init__(self, url=None, name=None, author='', description='', dl_form_message='', add_downloader=False): assert url is not None assert name is not None loader = Loader(paths=[settings.dict['paths']['wskeyloggerd']['usr_templates']], mtype='MPortalTemplate') templates = loader.get_loadables() for t in templates: if t.name == name: sys.exit(f'[*] Module {name} already exists. Choose a different name or delete the preexisting module.') print(t.name) print(f'[*] Setting module name to {name}') self.name = name self.url = url self.author = author self.description = description self.add_downloader = add_downloader self.target_host = url.replace('https://', '').replace('http://', '').split('/')[0] self.full_project_path = os.path.join(g_tmp_dir, name, self.target_host) self.proj_parent_path = os.path.join(g_tmp_dir, name) self.target_dir = os.path.join(g_template_dir, name) self.dl_form_message = dl_form_message
def __loadJSON(self): # Chargement depuis un fichier try: with open(self.__path, "rb") as outFile: self.__map = list(Loader.map(outFile, ["Loading map ...", "Map loaded ! ", "Map loading failed !"])) self.__dims = tuple((len(self.__map), len(self.__map[0]))) self.__cells = int(self.__dims[1]*self.__dims[0]) return(True) except Exception: return(False)
def __loadJSON(self): # Chargement des entités depuis un fichier try: with open(self.__path, 'r') as outFile: self.__entities = dict( Loader.json(outFile, [ "Loading entities ...", "Entities loaded ! ", "Entities loading Failed !" ])) return (True) except Exception: return (False)
def validate_stager(stager): stagers = Loader() stagers.type = "stager" stagers.paths = ["stagers/"] stagers.name = 'stagers' stagers.selected = None stagers.get_loadables() for s in stagers.loaded: if s.name == stager.lower(): return s return None
def setup(): """Initialize a basic game structure with the local levels installed""" levels = Loader.local_levels() story = Story() for level in levels: story.add_level(level) add_url( "actions/<level_identifier>/<action_identifier>", generate_resolve_action_view(story), )
def validate_listener(listener): listeners = Loader() listeners.type = "listener" listeners.paths = ["listeners/"] listeners.name = 'listeners' listeners.get_loadables() for l in listeners.loaded: if l.name == listener.name: l['BindIP'] = listener['BindIP'] l['Port'] = listener['Port'] return l return None
def test_local_levels_is_checking_current_class_inheritance(self): with self.assertRaises(InvalidCurrentClass): list(Loader.local_levels())
def test_local_levels_is_checking_missing_current_class(self): with self.assertRaises(MissingCurrentClass): list(Loader.local_levels())
def test_local_levels_is_checking_duplicated_ids(self): with self.assertRaises(DuplicatedLevelIdentifier): list(Loader.local_levels())
def test_local_levels_is_checking_manifest(self): with self.assertRaises(InvalidManifestStructure): list(Loader.local_levels())
def run(self): """Run the test All setup and teardown through controllers is included. """ self._logger.debug(self.name) # copy sources of l2 forwarding tools into VM shared dir if needed self._copy_fwd_tools_for_guest() self._logger.debug("Controllers:") loader = Loader() traffic_ctl = component_factory.create_traffic( self._traffic['traffic_type'], loader.get_trafficgen_class()) vnf_ctl = component_factory.create_vnf(self.deployment, loader.get_vnf_class()) vswitch_ctl = component_factory.create_vswitch( self.deployment, loader.get_vswitch_class(), self._traffic) collector = component_factory.create_collector( loader.get_collector_class(), self._results_dir, self.name) loadgen = component_factory.create_loadgen(self._loadgen, self._load_cfg) self._logger.debug("Setup:") with vswitch_ctl, loadgen: with vnf_ctl, collector: vswitch = vswitch_ctl.get_vswitch() # TODO BOM 15-08-07 the frame mod code assumes that the # physical ports are ports 1 & 2. The actual numbers # need to be retrived from the vSwitch and the metadata value # updated accordingly. bridge = S.getValue('VSWITCH_BRIDGE_NAME') if self._frame_mod == "vlan": # 0x8100 => VLAN ethertype self._logger.debug(" **** VLAN ***** ") flow = { 'table': '2', 'priority': '1000', 'metadata': '2', 'actions': ['push_vlan:0x8100', 'goto_table:3'] } vswitch.add_flow(bridge, flow) flow = { 'table': '2', 'priority': '1000', 'metadata': '1', 'actions': ['push_vlan:0x8100', 'goto_table:3'] } vswitch.add_flow(bridge, flow) elif self._frame_mod == "mpls": # 0x8847 => MPLS unicast ethertype self._logger.debug(" **** MPLS ***** ") flow = { 'table': '2', 'priority': '1000', 'metadata': '2', 'actions': ['push_mpls:0x8847', 'goto_table:3'] } vswitch.add_flow(bridge, flow) flow = { 'table': '2', 'priority': '1000', 'metadata': '1', 'actions': ['push_mpls:0x8847', 'goto_table:3'] } vswitch.add_flow(bridge, flow) elif self._frame_mod == "mac": flow = { 'table': '2', 'priority': '1000', 'metadata': '2', 'actions': ['mod_dl_src:22:22:22:22:22:22', 'goto_table:3'] } vswitch.add_flow(bridge, flow) flow = { 'table': '2', 'priority': '1000', 'metadata': '1', 'actions': ['mod_dl_src:11:11:11:11:11:11', 'goto_table:3'] } vswitch.add_flow(bridge, flow) elif self._frame_mod == "dscp": # DSCP 184d == 0x4E<<2 => 'Expedited Forwarding' flow = { 'table': '2', 'priority': '1000', 'metadata': '2', 'dl_type': '0x0800', 'actions': ['mod_nw_tos:184', 'goto_table:3'] } vswitch.add_flow(bridge, flow) flow = { 'table': '2', 'priority': '1000', 'metadata': '1', 'dl_type': '0x0800', 'actions': ['mod_nw_tos:184', 'goto_table:3'] } vswitch.add_flow(bridge, flow) elif self._frame_mod == "ttl": # 251 and 241 are the highest prime numbers < 255 flow = { 'table': '2', 'priority': '1000', 'metadata': '2', 'dl_type': '0x0800', 'actions': ['mod_nw_ttl:251', 'goto_table:3'] } vswitch.add_flow(bridge, flow) flow = { 'table': '2', 'priority': '1000', 'metadata': '1', 'dl_type': '0x0800', 'actions': ['mod_nw_ttl:241', 'goto_table:3'] } vswitch.add_flow(bridge, flow) elif self._frame_mod == "ip_addr": flow = { 'table': '2', 'priority': '1000', 'metadata': '2', 'dl_type': '0x0800', 'actions': [ 'mod_nw_src:10.10.10.10', 'mod_nw_dst:20.20.20.20', 'goto_table:3' ] } vswitch.add_flow(bridge, flow) flow = { 'table': '2', 'priority': '1000', 'metadata': '1', 'dl_type': '0x0800', 'actions': [ 'mod_nw_src:20.20.20.20', 'mod_nw_dst:10.10.10.10', 'goto_table:3' ] } vswitch.add_flow(bridge, flow) elif self._frame_mod == "ip_port": # TODO BOM 15-08-27 The traffic generated is assumed # to be UDP (nw_proto 17d) which is the default case but # we will need to pick up the actual traffic params in use. flow = { 'table': '2', 'priority': '1000', 'metadata': '2', 'dl_type': '0x0800', 'nw_proto': '17', 'actions': [ 'mod_tp_src:44444', 'mod_tp_dst:44444', 'goto_table:3' ] } vswitch.add_flow(bridge, flow) flow = { 'table': '2', 'priority': '1000', 'metadata': '1', 'dl_type': '0x0800', 'nw_proto': '17', 'actions': [ 'mod_tp_src:44444', 'mod_tp_dst:44444', 'goto_table:3' ] } vswitch.add_flow(bridge, flow) else: pass with traffic_ctl: traffic_ctl.send_traffic(self._traffic) # dump vswitch flows before they are affected by VNF termination vswitch_ctl.dump_vswitch_flows() self._logger.debug("Traffic Results:") traffic_ctl.print_results() self._logger.debug("Collector Results:") collector.print_results() output_file = os.path.join( self._results_dir, "result_" + self.name + "_" + self.deployment + ".csv") tc_results = self._append_results(traffic_ctl.get_results()) TestCase._write_result_to_file(tc_results, output_file) report.generate(output_file, tc_results, collector.get_results())
def get_service(service_config): """ Build and return the service object given it's configuration """ return Loader.get_class(service_config["module"], service_config["class"])(service_config)
class Dispatcher: def __init__(self): # dirty hack to obtain help message without initializing the -h flag parser = ArgumentParser() self.add_arguments(parser) self._print_help = parser.print_help parser = ArgumentParser(add_help=False) self.add_hidden_help_method(parser) self.add_arguments(parser) self.all_modules = [] self.master_parser = parser self.init_loaders() self.init_loadables() self.dispatch = { 'crypter': None, 'ekeys': [], 'executor': None, 'runner': None, 'master': None, 'mutator': None, 'dkeys': [], 'decrypter': None, 'postmodules': [], 'premodules': [], 'interface': None, } self.options = { 'crypter': {}, 'ekeys': [], 'executor': {}, 'runner': {}, 'master': {}, 'mutator': {}, 'dkeys': [], 'decrypter': {}, 'postmodules': [], 'premodules': [], 'interface': {}, } #dispatch = { # # 'crypter' : crypter_parser, # 'ekey' : ekey_parser, # 'executor' : executor_parser, # 'mutator' : mutator_parser, # 'postmodules' : { # 'postmodule_a' : postmodule_a_parser, # 'postmodule_b' : postmodule_b_parser, # }, # 'premodules' : { # 'premodule_a' : premodule_a_parser, # 'premodule_b' : premodule_b_parser, # }, #} @staticmethod def at_least_one_module_type_is_selected(args): return any([ #args.ekey is not None, args.ekeys != [], args.dkeys != [], args.crypter is not None, args.decrypter is not None, args.executor is not None, args.mutator is not None, args.runner is not None, args.premodules != [], args.postmodules != [], ]) def print_help(self): #todo: dkey / decryptor appear to be missing if self.at_least_one_module_type_is_selected(self.master_args): if self.master_args.executor is not None: self.executors[self.master_args.executor].print_help() if self.master_args.runner is not None: self.runners[self.master_args.runner].print_help() #if self.master_args.ekey is not None: # self.ekeys[self.master_args.ekey].print_help() if self.master_args.crypter is not None: self.crypters[self.master_args.crypter].print_help() if self.master_args.decrypter is not None: self.decrypters[self.master_args.decrypter].print_help() if self.master_args.mutator is not None: self.mutators[self.master_args.mutator].print_help() if self.master_args.postmodules != []: for post in self.master_args.postmodules: self.premodules[post].print_help() if self.master_args.premodules != []: for pre in self.master_args.premodules: self.premodules[pre].print_help() if self.master_args.ekeys != []: for e in self.master_args.ekeys: self.ekeys[e].print_help() else: self._print_help() def init_loaders(self): ## interface - public self.interface_loader = Loader(paths=['./modules/interfaces'], mtype='MRunnerInterface') # input - public self.ekey_loader = Loader(paths=['./modules/input/ekeys'], mtype='MEKey') self.crypter_loader = Loader(paths=['./modules/input/crypters'], mtype='MCrypter') # output - public self.executor_loader = Loader(paths=['./modules/output/executors'], mtype='MExecutor') self.premodule_loader = Loader(paths=['./modules/output/premodules'], mtype='MPreModule') self.postmodule_loader = Loader(paths=['./modules/output/postmodules'], mtype='MPostModule') self.decrypter_loader = Loader(paths=['./modules/output/decrypters'], mtype='MDecrypter') self.dkey_loader = Loader(paths=['./modules/output/dkeys'], mtype='MDKey') self.mutator_loader = Loader(paths=['./modules/mutators'], mtype='MMutator') self.runner_loader = Loader(paths=['./modules/output/runners'], mtype='MRunner') def init_loadables(self): ## interface - public self.interfaces = self.interface_loader.get_loadables() self.all_modules += self.interfaces ## input - public self.ekeys = self.ekey_loader.get_loadables() self.all_modules += self.ekeys self.crypters = self.crypter_loader.get_loadables() self.all_modules += self.crypters # output - public self.executors = self.executor_loader.get_loadables() self.all_modules += self.executors self.mutators = self.mutator_loader.get_loadables() self.all_modules += self.mutators self.premodules = self.premodule_loader.get_loadables() self.all_modules += self.premodules self.postmodules = self.postmodule_loader.get_loadables() self.all_modules += self.postmodules self.decrypters = self.decrypter_loader.get_loadables() self.all_modules += self.decrypters self.dkeys = self.dkey_loader.get_loadables() self.all_modules += self.dkeys self.runners = self.runner_loader.get_loadables() self.all_modules += self.runners def list_compatible(self, mod): print() print('For module:', mod) if hasattr(mod, 'compatible_omodules'): print() print(' Listing compatible output modules:') print() for omodule in mod.compatible_omodules: print(' ', omodule) print() if hasattr(mod, 'compatible_imodules'): print() print(' Listing compatible input modules:') print() for imodule in mod.compatible_imodules: print(' ', imodule) print() if hasattr(mod, 'compatible_interfaces'): print() print(' Listing compatible interfaces:') print() for iface in mod.compatible_interfaces: print(' ', iface) print() print() def list_compatible_interfaces(self, mods): print() for mod in mods: if self.master_args.interface in mod.compatible_interfaces: print(' ', mod.name) print() def parse_args(self): self.master_args, unknown = self.master_parser.parse_known_args() self.options['master'] = self.master_args.__dict__ if self.master_args.debug: self.print_args() if self.master_args.print_help: self.print_help() sys.exit() if self.master_args.validate_modules: for m in self.all_modules: m.validate() sys.exit() if self.master_args.list is not None: if self.master_args.compatible: if self.master_args.interface is not None: print() print('For interface:', self.master_args.interface) print() if self.master_args.list == [] or 'all' in self.master_args.list or 'executors' in self.master_args.list: print(' Listing list executors:') self.list_compatible_interfaces(self.executors) if self.master_args.list == [] or 'all' in self.master_args.list or 'crypters' in self.master_args.list: print(' Listing list crypters:') self.list_compatible_interfaces(self.crypters) if self.master_args.list == [] or 'all' in self.master_args.list or 'decrypters' in self.master_args.list: print(' Listing list decrypters:') self.list_compatible_interfaces(self.decrypters) if self.master_args.list == [] or 'all' in self.master_args.list or 'runners' in self.master_args.list: print(' Listing list runners:') self.list_compatible_interfaces(self.runners) if self.master_args.list == [] or 'all' in self.master_args.list or 'postmodules' in self.master_args.list: print(' Listing list premodules:') self.list_compatible_interfaces(self.premodules) if self.master_args.list == [] or 'all' in self.master_args.list or 'premodules' in self.master_args.list: print(' Listing list postmodules:') self.list_compatible_interfaces(self.postmodules) if self.master_args.list == [] or 'all' in self.master_args.list or 'ekeys' in self.master_args.list: print(' Listing list ekeys:') self.list_compatible_interfaces(self.ekeys) if self.master_args.list == [] or 'all' in self.master_args.list or 'dkeys' in self.master_args.list: print(' Listing list dkeys:') self.list_compatible_interfaces(self.dkeys) #self.dispatch['interface'] = self.interfaces[self.master_args.interface] if self.master_args.executor is not None: mod = self.executors[self.master_args.executor] self.list_compatible(mod) if self.master_args.crypter is not None: mod = self.crypters[self.master_args.crypter] self.list_compatible(mod) if self.master_args.decrypter is not None: mod = self.decrypters[self.master_args.decrypter] self.list_compatible(mod) if self.master_args.runner is not None: mod = self.runners[self.master_args.runner] self.list_compatible(mod) if self.master_args.premodules != []: for pre in self.master_args.premodules: mod = self.premodules[pre] self.list_compatible(mod) if self.master_args.postmodules != []: for post in self.master_args.postmodules: mod = self.postmodules[post] self.list_compatible(mod) if self.master_args.ekeys != []: for ekey in self.master_args.ekeys: mod = self.ekeys[ekey] self.list_compatible(mod) if self.master_args.dkeys != []: for dkey in self.master_args.dkeys: mod = self.dkeys[dkey] self.list_compatible(mod) sys.exit() else: print() list_all = self.master_args.list == [] or 'all' in self.master_args.list if list_all or 'interfaces' in self.master_args.list: print('Listing interfaces:') print() for _ in self.interfaces: print(f' {_.name}') print() if list_all or 'ekeys' in self.master_args.list: print('Listing ekeys:') print() for _ in self.ekeys: print(f' {_.name}') print() if list_all or 'dkeys' in self.master_args.list: print('Listing dkeys:') print() for _ in self.dkeys: print(f' {_.name}') print() if list_all or 'executors' in self.master_args.list: print('Listing executors:') print() for _ in self.executors: print(f' {_.name}') print() if list_all or 'crypters' in self.master_args.list: print('Listing crypters:') print() for _ in self.crypters: print(f' {_.name}') print() if list_all or 'decrypters' in self.master_args.list: print('Listing decrypters:') print() for _ in self.decrypters: print(f' {_.name}') print() if list_all or 'mutators' in self.master_args.list: print('Listing mutators:') print() for _ in self.mutators: print(f' {_.name}') print() if list_all or 'runners' in self.master_args.list: print('Listing runners:') print() for _ in self.runners: print(f' {_.name}') print() if list_all or 'premodules' in self.master_args.list: print('Listing premodles:') print() for _ in self.premodules: print(f' {_.name}') print() if list_all or 'postmodules' in self.master_args.list: print('Listing postmodules:') print() for _ in self.postmodules: print(f' {_.name}') print() sys.exit() #elif self.at_least_one_module_type_is_selected(self.master_args): elif self.master_args.build: #if self.master_args.ekey is not None: # self.dispatch['ekey'] = self.ekeys[self.master_args.ekey] # unknown = self.ekeys[self.master_args.ekey].parse_args(unknown) # self.options['ekey'] = self.ekeys[self.master_args.ekey].get_options() if self.master_args.interface is not None: self.dispatch['interface'] = self.interfaces[ self.master_args.interface] #if self.master_args.dkey is not None: # self.dispatch['dkey'] = self.dkeys[self.master_args.dkey] # unknown = self.dkeys[self.master_args.dkey].parse_args(unknown) # self.options['dkey'] = self.dkeys[self.master_args.dkey].get_options() if self.master_args.executor is not None: self.dispatch['executor'] = self.executors[ self.master_args.executor] unknown = self.executors[self.master_args.executor].parse_args( unknown) self.options['executor'] = self.executors[ self.master_args.executor].get_options() if self.master_args.crypter is not None: self.dispatch['crypter'] = self.crypters[ self.master_args.crypter] unknown = self.crypters[self.master_args.crypter].parse_args( unknown) self.options['crypter'] = self.crypters[ self.master_args.crypter].get_options() if self.master_args.decrypter is not None: self.dispatch['decrypter'] = self.decrypters[ self.master_args.decrypter] unknown = self.decrypters[ self.master_args.decrypter].parse_args(unknown) self.options['decrypter'] = self.decrypters[ self.master_args.decrypter].get_options() if self.master_args.mutator is not None: self.dispatch['mutator'] = self.mutators[ self.master_args.mutator] unknown = self.mutators[self.master_args.mutator].parse_args( unknown) self.options['mutator'] = self.mutators[ self.master_args.mutator].get_options() if self.master_args.runner is not None: self.dispatch['runner'] = self.runners[self.master_args.runner] unknown = self.runners[self.master_args.runner].parse_args( unknown) self.options['runner'] = self.runners[ self.master_args.runner].get_options() if self.master_args.premodules != []: for pre in self.master_args.premodules: self.dispatch['premodules'].append(self.premodules[pre]) unknown = self.premodules[pre].parse_args(unknown) self.options['premodules'].append( self.premodules[pre].get_options()) if self.master_args.postmodules != []: for post in self.master_args.postmodules: self.dispatch['postmodules'].append(self.postmodules[post]) unknown = self.postmodules[post].parse_args(unknown) self.options['postmodules'].append( self.postmodules[post].get_options()) if self.master_args.ekeys != []: for ekey in self.master_args.ekeys: self.dispatch['ekeys'].append(self.ekeys[ekey]) unknown = self.ekeys[ekey].parse_args(unknown) self.options['ekeys'].append( self.ekeys[ekey].get_options()) if self.master_args.dkeys != []: for dkey in self.master_args.dkeys: self.dispatch['dkeys'].append(self.dkeys[dkey]) unknown = self.dkeys[dkey].parse_args(unknown) self.options['dkeys'].append( self.dkeys[dkey].get_options()) self.validate_build_args() self.validate_module_compatibility() else: print_banner() sys.exit() def validate_build_args(self): interface = self.dispatch['interface'] runner = self.dispatch['runner'] crypter = self.dispatch['crypter'] decrypter = self.dispatch['decrypter'] ekeys = self.dispatch['ekeys'] dkeys = self.dispatch['dkeys'] executor = self.dispatch['executor'] postmodules = self.dispatch['postmodules'] premodules = self.dispatch['premodules'] mutator = self.dispatch['mutator'] shellcode = self.master_args.shellcode if interface is None: pnoselect('interface') if runner is None: pnoselect('runner') if crypter is None: pnoselect('crypter') if decrypter is None: pnoselect('decrypter') if ekeys == []: pnoselect('ekeys') if dkeys == []: pnoselect('dkeys') if executor is None: pnoselect('executor') if mutator is None: pnoselect('mutator') if shellcode is None: perror('No shellcode was provided to DropEngine.') perror( 'You must specify the path to your shellcode using the --input-file flag.' ) pexit('Aborting.') if not os.path.exists(shellcode): perror('The shellcode path you provided was invalid.') perror( 'You must specify a valid path to your shellcode using the --input-file flag.' ) pexit('Aborting.') def print_args(self): print(json.dumps(self.options, indent=4, sort_keys=True)) def print_dispatch(self): print(self.dispatch) def vmc_iface(self, mod, iface, mtype): if iface.name not in mod.compatible_interfaces: picompat(mod.name, iface.name, mtype) def vmc_imodule(self, imodule, omodule, iface, mtype): if imodule.name not in omodule.compatible_imodules: pcompat(imodule.name, omodule.name, mtype) self.vmc_iface(imodule, iface, mtype) def vmc_omodule(self, omodule, imodule, iface, mtype): if omodule.name not in imodule.compatible_omodules: pcompat(omodule.name, imodule.name, mtype) self.vmc_iface(omodule, iface, mtype) def validate_module_compatibility(self): #print(json.dumps(self.options, indent=4, sort_keys=True)) interface = self.dispatch['interface'] runner = self.dispatch['runner'] crypter = self.dispatch['crypter'] decrypter = self.dispatch['decrypter'] ekeys = self.dispatch['ekeys'] dkeys = self.dispatch['dkeys'] executor = self.dispatch['executor'] postmodules = self.dispatch['postmodules'] premodules = self.dispatch['premodules'] vmc_iface = self.vmc_iface vmc_imodule = self.vmc_imodule vmc_omodule = self.vmc_omodule # validate crypter vmc_imodule(crypter, decrypter, interface, 'crypter') # validate decrypter vmc_omodule(decrypter, crypter, interface, 'decrypter') # validate ekeys and dkeys for ekey, dkey in zip(ekeys, dkeys): vmc_imodule(ekey, dkey, interface, 'ekeys') vmc_omodule(dkey, ekey, interface, 'dkeys') # validate executor vmc_iface(executor, interface, 'executor') # validate postmodules for post in postmodules: vmc_iface(post, interface, 'postmodules') # validate premodules for pre in premodules: vmc_iface(pre, interface, 'premodules') # validate runner vmc_iface(runner, interface, 'runner') @staticmethod def add_hidden_help_method(parser): parser.add_argument('--help', '-h', dest='print_help', action='store_true', help='Print help message') @staticmethod def get_choices(path, mtype): return [ c for c in Loader(paths=[path], mtype=mtype).get_loadables().modules ] @staticmethod def add_arguments(parser): modules_group = parser.add_argument_group('Modules') modules_group.add_argument('--crypter', dest='crypter', type=str, required=False, default=None, choices=Dispatcher.get_choices( './modules/input/crypters', 'MCrypter'), help='Select crypter') modules_group.add_argument('--interface', dest='interface', type=str, required=False, default=None, choices=Dispatcher.get_choices( './modules/interfaces', 'MRunnerInterface'), help='Select interface') #modules_group.add_argument('--ekey', # dest='ekey', # type=str, # required=False, # default=None, # choices=Dispatcher.get_choices('./modules/input/ekeys', 'MEKey'), # help='Select ekey') #modules_group.add_argument('--dkey', # dest='dkey', # type=str, # required=False, # default=None, # choices=Dispatcher.get_choices('./modules/output/dkeys', 'MDKey'), # help='Select dkey') parser.add_argument('--debug', dest='debug', action='store_true', help='Display debug output') parser.add_argument('--output-file', '-o', dest='output_file', type=str, default=None, help='Store finished payload in output file.') modules_group.add_argument('--decrypter', dest='decrypter', type=str, required=False, default=None, choices=Dispatcher.get_choices( './modules/output/decrypters', 'MDecrypter'), help='Select decrypter') modules_group.add_argument('--executor', dest='executor', type=str, required=False, default=None, choices=Dispatcher.get_choices( './modules/output/executors', 'MExecutor'), help='Select executor') modules_group.add_argument('--mutator', dest='mutator', type=str, required=False, default=None, choices=Dispatcher.get_choices( './modules/mutators', 'MMutator'), help='Select mutator') modules_group.add_argument('--runner', dest='runner', type=str, required=False, default=None, choices=Dispatcher.get_choices( './modules/output/runners', 'MRunner'), help='Select runner') modules_group.add_argument('--postmodules', dest='postmodules', type=str, required=False, nargs='*', default=[], choices=Dispatcher.get_choices( './modules/output/postmodules', 'MPostModule'), help='Select postmodules') modules_group.add_argument('--premodules', dest='premodules', type=str, required=False, nargs='*', choices=Dispatcher.get_choices( './modules/output/premodules', 'MPreModule'), default=[], help='Select premodules') modules_group.add_argument('--ekeys', dest='ekeys', type=str, required=False, nargs='*', default=[], choices=Dispatcher.get_choices( './modules/input/ekeys', 'MEKey'), help='Select ekey') modules_group.add_argument('--dkeys', dest='dkeys', type=str, required=False, nargs='*', default=[], choices=Dispatcher.get_choices( './modules/output/dkeys', 'MDKey'), help='Select dkey') modes_group = parser.add_argument_group('Modes') modes_group.add_argument('--list', dest='list', type=str, required=False, default=None, nargs='*', choices=[ 'all', 'crypters', 'ekeys', 'decrypters', 'dkeys', 'mutators', 'executors', 'interfaces', 'postmodules', 'premodules', 'runners', ], help='List modules') modes_group.add_argument('--compatible', dest='compatible', action='store_true', help='List compatible modules') modes_group.add_argument('--build', dest='build', action='store_true', help='Build a payload') modes_group.add_argument('--validate-modules', dest='validate_modules', action='store_true', help='Validate the sanity of a custom module') build_group = parser.add_argument_group('Build') build_group.add_argument( '--input-file', '-i', '--shellcode', dest='shellcode', type=str, required=False, default=None, help='Specify path to input file containing shellcode')
def step_run(self): """ Execute actions specified by TestSteps list :return: False if any error was detected True otherwise """ # anything to do? if not self.test: return True # required for VNFs initialization loader = Loader() # initialize list with results self._step_result = [None] * len(self.test) # We have to suppress pylint report, because test_object has to be set according # to the test step definition # pylint: disable=redefined-variable-type # run test step by step... for i, step in enumerate(self.test): step_ok = not self._step_check if step[0] == 'vswitch': test_object = self._vswitch_ctl.get_vswitch() elif step[0] == 'namespace': test_object = namespace elif step[0] == 'veth': test_object = veth elif step[0] == 'settings': test_object = S elif step[0] == 'tools': test_object = TestStepsTools() step[1] = step[1].title() elif step[0] == 'trafficgen': test_object = self._traffic_ctl # in case of send_traffic or send_traffic_async methods, ensure # that specified traffic values are merged with existing self._traffic if step[1].startswith('send_traffic'): tmp_traffic = copy.deepcopy(self._traffic) tmp_traffic.update(step[2]) step[2] = tmp_traffic elif step[0].startswith('vnf'): if not self._step_vnf_list[step[0]]: # initialize new VM self._step_vnf_list[step[0]] = loader.get_vnf_class()() test_object = self._step_vnf_list[step[0]] elif step[0] == 'wait': input(os.linesep + "Step {}: Press Enter to continue with " "the next step...".format(i) + os.linesep + os.linesep) continue elif step[0] == 'sleep': self._logger.debug("Sleep %s seconds", step[1]) time.sleep(int(step[1])) continue else: self._logger.error("Unsupported test object %s", step[0]) self._step_status = { 'status': False, 'details': ' '.join(step) } self.step_report_status("Step '{}'".format(' '.join(step)), self._step_status['status']) return False test_method = getattr(test_object, step[1]) if self._step_check: test_method_check = getattr(test_object, CHECK_PREFIX + step[1]) else: test_method_check = None step_params = [] try: # eval parameters, but use only valid step_results # to support negative indexes step_params = TestCase.step_eval_params( step[2:], self._step_result[:i]) step_log = '{} {}'.format(' '.join(step[:2]), step_params) self._logger.debug("Step %s '%s' start", i, step_log) self._step_result[i] = test_method(*step_params) self._logger.debug("Step %s '%s' results '%s'", i, step_log, self._step_result[i]) time.sleep(S.getValue('TEST_STEP_DELAY')) if self._step_check: step_ok = test_method_check(self._step_result[i], *step_params) except (AssertionError, AttributeError, IndexError) as ex: step_ok = False self._logger.error("Step %s raised %s", i, type(ex).__name__) if self._step_check: self.step_report_status("Step {} - '{}'".format(i, step_log), step_ok) if not step_ok: self._step_status = {'status': False, 'details': step_log} # Stop all VNFs started by TestSteps self.step_stop_vnfs() return False # all steps processed without any issue return True
class Dispatcher: def __init__(self): # dirty hack to obtain help message without initializing the -h flag parser = ArgumentParser() self.add_arguments(parser) self._print_help = parser.print_help parser = ArgumentParser(add_help=False) self.add_hidden_help_method(parser) self.add_arguments(parser) self.all_modules = [] self.master_parser = parser self.init_loaders() self.init_loadables() self.dispatch = { 'crypter': None, 'ekeys': [], 'executor': None, 'runner': None, 'master': None, 'mutator': None, 'dkeys': [], 'decrypter': None, 'postmodules': [], 'premodules': [], 'interface': None, } self.options = { 'crypter': {}, 'ekeys': [], 'executor': {}, 'runner': {}, 'master': {}, 'mutator': {}, 'dkeys': [], 'decrypter': {}, 'postmodules': [], 'premodules': [], 'interface': {}, } #dispatch = { # # 'crypter' : crypter_parser, # 'ekey' : ekey_parser, # 'executor' : executor_parser, # 'mutator' : mutator_parser, # 'postmodules' : { # 'postmodule_a' : postmodule_a_parser, # 'postmodule_b' : postmodule_b_parser, # }, # 'premodules' : { # 'premodule_a' : premodule_a_parser, # 'premodule_b' : premodule_b_parser, # }, #} @staticmethod def at_least_one_module_type_is_selected(args): return any([ #args.ekey is not None, args.ekeys != [], args.dkeys != [], args.crypter is not None, args.decrypter is not None, args.executor is not None, args.mutator is not None, args.runner is not None, args.premodules != [], args.postmodules != [], ]) def print_help(self): #todo: dkey / decryptor appear to be missing if self.at_least_one_module_type_is_selected(self.master_args): if self.master_args.executor is not None: self.executors[self.master_args.executor].print_help() if self.master_args.runner is not None: self.runners[self.master_args.runner].print_help() #if self.master_args.ekey is not None: # self.ekeys[self.master_args.ekey].print_help() if self.master_args.crypter is not None: self.crypters[self.master_args.crypter].print_help() if self.master_args.decrypter is not None: self.decrypters[self.master_args.decrypter].print_help() if self.master_args.mutator is not None: self.mutators[self.master_args.mutator].print_help() if self.master_args.postmodules != []: for post in self.master_args.postmodules: self.premodules[post].print_help() if self.master_args.premodules != []: for pre in self.master_args.premodules: self.premodules[pre].print_help() if self.master_args.ekeys != []: for e in self.master_args.ekeys: self.ekeys[e].print_help() else: self._print_help() def init_loaders(self): ## interface - public self.interface_loader = Loader(paths=['./modules/interfaces'], mtype='MRunnerInterface') # input - public self.ekey_loader = Loader(paths=['./modules/input/ekeys'], mtype='MEKey') self.crypter_loader = Loader(paths=['./modules/input/crypters'], mtype='MCrypter') # output - public self.executor_loader = Loader(paths=['./modules/output/executors'], mtype='MExecutor') self.premodule_loader = Loader(paths=['./modules/output/premodules'], mtype='MPreModule') self.postmodule_loader = Loader(paths=['./modules/output/postmodules'], mtype='MPostModule') self.decrypter_loader = Loader(paths=['./modules/output/decrypters'], mtype='MDecrypter') self.dkey_loader = Loader(paths=['./modules/output/dkeys'], mtype='MDKey') self.mutator_loader = Loader(paths=['./modules/mutators'], mtype='MMutator') self.runner_loader = Loader(paths=['./modules/output/runners'], mtype='MRunner') def init_loadables(self): ## interface - public self.interfaces = self.interface_loader.get_loadables() self.all_modules += self.interfaces ## input - public self.ekeys = self.ekey_loader.get_loadables() self.all_modules += self.ekeys self.crypters = self.crypter_loader.get_loadables() self.all_modules += self.crypters # output - public self.executors = self.executor_loader.get_loadables() self.all_modules += self.executors self.mutators = self.mutator_loader.get_loadables() self.all_modules += self.mutators self.premodules = self.premodule_loader.get_loadables() self.all_modules += self.premodules self.postmodules = self.postmodule_loader.get_loadables() self.all_modules += self.postmodules self.decrypters = self.decrypter_loader.get_loadables() self.all_modules += self.decrypters self.dkeys = self.dkey_loader.get_loadables() self.all_modules += self.dkeys self.runners = self.runner_loader.get_loadables() self.all_modules += self.runners def parse_args(self): self.master_args, unknown = self.master_parser.parse_known_args() self.options['master'] = self.master_args.__dict__ if self.master_args.debug: self.print_args() if self.master_args.print_help: self.print_help() sys.exit() if self.master_args.validate_modules: for m in self.all_modules: m.validate() sys.exit() if self.master_args.list is not None: print() list_all = self.master_args.list == [] or 'all' in self.master_args.list if list_all or 'ekeys' in self.master_args.list: print('Listing ekeys:') print() for _ in self.ekeys: print(f' {_}') print() if list_all or 'dkeys' in self.master_args.list: print('Listing dkeys:') print() for _ in self.dkeys: print(f' {_}') print() if list_all or 'executors' in self.master_args.list: print('Listing executors:') print() for _ in self.executors: print(f' {_}') print() if list_all or 'crypters' in self.master_args.list: print('Listing crypters:') print() for _ in self.crypters: print(f' {_}') print() if list_all or 'decrypters' in self.master_args.list: print('Listing decrypters:') print() for _ in self.decrypters: print(f' {_}') print() if list_all or 'mutators' in self.master_args.list: print('Listing mutators:') print() for _ in self.mutators: print(f' {_}') print() if list_all or 'runners' in self.master_args.list: print('Listing runners:') print() for _ in self.runners: print(f' {_}') print() if list_all or 'interfaces' in self.master_args.list: print('Listing interfaces:') print() for _ in self.interfaces: print(f' {_}') print() if list_all or 'premodules' in self.master_args.list: print('Listing premodles:') print() for _ in self.premodules: print(f' {_}') print() if list_all or 'postmodules' in self.master_args.list: print('Listing postmodules:') print() for _ in self.postmodules: print(f' {_}') print() sys.exit() elif self.at_least_one_module_type_is_selected(self.master_args): #if self.master_args.ekey is not None: # self.dispatch['ekey'] = self.ekeys[self.master_args.ekey] # unknown = self.ekeys[self.master_args.ekey].parse_args(unknown) # self.options['ekey'] = self.ekeys[self.master_args.ekey].get_options() if self.master_args.interface is not None: self.dispatch['interface'] = self.interfaces[ self.master_args.interface] #if self.master_args.dkey is not None: # self.dispatch['dkey'] = self.dkeys[self.master_args.dkey] # unknown = self.dkeys[self.master_args.dkey].parse_args(unknown) # self.options['dkey'] = self.dkeys[self.master_args.dkey].get_options() if self.master_args.executor is not None: self.dispatch['executor'] = self.executors[ self.master_args.executor] unknown = self.executors[self.master_args.executor].parse_args( unknown) self.options['executor'] = self.executors[ self.master_args.executor].get_options() if self.master_args.crypter is not None: self.dispatch['crypter'] = self.crypters[ self.master_args.crypter] unknown = self.crypters[self.master_args.crypter].parse_args( unknown) self.options['crypter'] = self.crypters[ self.master_args.crypter].get_options() if self.master_args.decrypter is not None: self.dispatch['decrypter'] = self.decrypters[ self.master_args.decrypter] unknown = self.decrypters[ self.master_args.decrypter].parse_args(unknown) self.options['decrypter'] = self.decrypters[ self.master_args.decrypter].get_options() if self.master_args.mutator is not None: self.dispatch['mutator'] = self.mutators[ self.master_args.mutator] unknown = self.mutators[self.master_args.mutator].parse_args( unknown) self.options['mutator'] = self.mutators[ self.master_args.mutator].get_options() if self.master_args.runner is not None: self.dispatch['runner'] = self.runners[self.master_args.runner] unknown = self.runners[self.master_args.runner].parse_args( unknown) self.options['runner'] = self.runners[ self.master_args.runner].get_options() if self.master_args.premodules != []: for pre in self.master_args.premodules: self.dispatch['premodules'].append(self.premodules[pre]) unknown = self.premodules[pre].parse_args(unknown) self.options['premodules'].append( self.premodules[pre].get_options()) if self.master_args.postmodules != []: for post in self.master_args.postmodules: self.dispatch['postmodules'].append(self.postmodules[post]) unknown = self.postmodules[post].parse_args(unknown) self.options['postmodules'].append( self.postmodules[post].get_options()) if self.master_args.ekeys != []: for ekey in self.master_args.ekeys: self.dispatch['ekeys'].append(self.ekeys[ekey]) unknown = self.ekeys[ekey].parse_args(unknown) self.options['ekeys'].append( self.ekeys[ekey].get_options()) if self.master_args.dkeys != []: for dkey in self.master_args.dkeys: self.dispatch['dkeys'].append(self.dkeys[dkey]) unknown = self.dkeys[dkey].parse_args(unknown) self.options['dkeys'].append( self.dkeys[dkey].get_options()) def print_args(self): print(json.dumps(self.options, indent=4, sort_keys=True)) def print_dispatch(self): print(self.dispatch) @staticmethod def add_hidden_help_method(parser): parser.add_argument('--help', '-h', dest='print_help', action='store_true', help='Print help message') @staticmethod def get_choices(path, mtype): return [ c for c in Loader(paths=[path], mtype=mtype).get_loadables().modules ] @staticmethod def add_arguments(parser): modules_group = parser.add_argument_group('Modules') modules_group.add_argument('--crypter', dest='crypter', type=str, required=False, default=None, choices=Dispatcher.get_choices( './modules/input/crypters', 'MCrypter'), help='Select crypter') modules_group.add_argument('--interface', dest='interface', type=str, required=False, default=None, help='Select interface') #modules_group.add_argument('--ekey', # dest='ekey', # type=str, # required=False, # default=None, # choices=Dispatcher.get_choices('./modules/input/ekeys', 'MEKey'), # help='Select ekey') #modules_group.add_argument('--dkey', # dest='dkey', # type=str, # required=False, # default=None, # choices=Dispatcher.get_choices('./modules/output/dkeys', 'MDKey'), # help='Select dkey') parser.add_argument('--debug', dest='debug', action='store_true', help='Display debug output') parser.add_argument('--output-file', '-o', dest='output_file', type=str, default=None, help='Store finished payload in output file.') modules_group.add_argument('--decrypter', dest='decrypter', type=str, required=False, default=None, choices=Dispatcher.get_choices( './modules/output/decrypters', 'MDecrypter'), help='Select decrypter') modules_group.add_argument('--executor', dest='executor', type=str, required=False, default=None, choices=Dispatcher.get_choices( './modules/output/executors', 'MExecutor'), help='Select executor') modules_group.add_argument('--mutator', dest='mutator', type=str, required=False, default=None, choices=Dispatcher.get_choices( './modules/mutators', 'MMutator'), help='Select mutator') modules_group.add_argument('--runner', dest='runner', type=str, required=False, default=None, choices=Dispatcher.get_choices( './modules/output/runners', 'MRunner'), help='Select runner') modules_group.add_argument('--postmodules', dest='postmodules', type=str, required=False, nargs='*', default=[], choices=Dispatcher.get_choices( './modules/output/postmodules', 'MPostModule'), help='Select postmodules') modules_group.add_argument('--premodules', dest='premodules', type=str, required=False, nargs='*', choices=Dispatcher.get_choices( './modules/output/premodules', 'MPreModule'), default=[], help='Select premodules') modules_group.add_argument('--ekeys', dest='ekeys', type=str, required=False, nargs='*', default=[], choices=Dispatcher.get_choices( './modules/input/ekeys', 'MEKey'), help='Select ekey') modules_group.add_argument('--dkeys', dest='dkeys', type=str, required=False, nargs='*', default=[], choices=Dispatcher.get_choices( './modules/output/dkeys', 'MDKey'), help='Select dkey') modes_group = parser.add_argument_group('Modes') modes_group.add_argument('--list', dest='list', type=str, required=False, default=None, nargs='*', choices=[ 'all', 'crypters', 'ekeys', 'decrypters', 'dkeys', 'mutators', 'executors', 'interfaces', 'postmodules', 'premodules', 'runners', ], help='List modules') modes_group.add_argument('--build', dest='build', action='store_true', help='Build a payload') modes_group.add_argument('--validate-modules', dest='validate_modules', action='store_true', help='Build a payload') build_group = parser.add_argument_group('Build') build_group.add_argument('--shellcode', dest='shellcode', type=str, required=False, default=None, help='Select shellcode')
def get_choices(path, mtype): return [ c for c in Loader(paths=[path], mtype=mtype).get_loadables().modules ]
def init_loaders(self): ## interface - public self.interface_loader = Loader(paths=['./modules/interfaces'], mtype='MRunnerInterface') # input - public self.ekey_loader = Loader(paths=['./modules/input/ekeys'], mtype='MEKey') self.crypter_loader = Loader(paths=['./modules/input/crypters'], mtype='MCrypter') # output - public self.executor_loader = Loader(paths=['./modules/output/executors'], mtype='MExecutor') self.premodule_loader = Loader(paths=['./modules/output/premodules'], mtype='MPreModule') self.postmodule_loader = Loader(paths=['./modules/output/postmodules'], mtype='MPostModule') self.decrypter_loader = Loader(paths=['./modules/output/decrypters'], mtype='MDecrypter') self.dkey_loader = Loader(paths=['./modules/output/dkeys'], mtype='MDKey') self.mutator_loader = Loader(paths=['./modules/mutators'], mtype='MMutator') self.runner_loader = Loader(paths=['./modules/output/runners'], mtype='MRunner')
def test__check_level_exists(self, *args): self.assertTrue(Loader._check_level_exists("foo")) self.assertFalse(Loader._check_level_exists("bar"))
def test_local_levels_ok(self): local_levels = list(Loader.local_levels()) self.assertEqual(len(local_levels), 1)
def test_local_levels_missing_required_files(self): with self.assertRaises(InvalidLevelStructure): list(Loader.local_levels())
def step_run(self): """ Execute actions specified by TestSteps list :return: False if any error was detected True otherwise """ # anything to do? if not self.test: return True # required for VNFs initialization loader = Loader() # initialize list with results self._step_result = [None] * len(self.test) # run test step by step... for i, step in enumerate(self.test): step_ok = not self._step_check step_check = self._step_check regex = None # configure step result mapping if step alias/label is detected if step[0].startswith('#'): key = step[0][1:] if key.isdigit(): raise RuntimeError( 'Step alias can\'t be an integer value {}'.format(key)) if key in self._step_result_mapping: raise RuntimeError( 'Step alias {} has been used already for step ' '{}'.format(key, self._step_result_mapping[key])) self._step_result_mapping[step[0][1:]] = i step = step[1:] # store regex filter if it is specified if isinstance(step[-1], str) and step[-1].startswith('|'): # evalute macros and variables used in regex regex = self.step_eval_params([step[-1][1:]], self._step_result[:i])[0] step = step[:-1] # check if step verification should be suppressed if step[0].startswith('!'): step_check = False step_ok = True step[0] = step[0][1:] if step[0] == 'vswitch': test_object = self._vswitch_ctl.get_vswitch() elif step[0] == 'namespace': test_object = namespace elif step[0] == 'veth': test_object = veth elif step[0] == 'settings': test_object = S elif step[0] == 'tools': test_object = TestStepsTools() step[1] = step[1].title() elif step[0] == 'trafficgen': test_object = self._traffic_ctl # in case of send_traffic or send_traffic_async methods, ensure # that specified traffic values are merged with existing self._traffic if step[1].startswith('send_traffic'): tmp_traffic = copy.deepcopy(self._traffic) tmp_traffic.update(step[2]) step[2] = tmp_traffic # store indication that traffic has been sent # so it is not sent again after the execution of teststeps self._step_send_traffic = True elif step[0].startswith('vnf'): # use vnf started within TestSteps if not self._step_vnf_list[step[0]]: # initialize new VM self._step_vnf_list[step[0]] = loader.get_vnf_class()() test_object = self._step_vnf_list[step[0]] elif step[0].startswith('VNF'): if step[1] in ('start', 'stop'): raise RuntimeError( "Cannot execute start() or stop() method of " "VNF deployed automatically by scenario.") # use vnf started by scenario deployment (e.g. pvp) vnf_index = int(step[0][3:]) try: test_object = self._vnf_list[vnf_index] except IndexError: raise RuntimeError( "VNF with index {} is not running.".format(vnf_index)) elif step[0] == 'wait': input(os.linesep + "Step {}: Press Enter to continue with " "the next step...".format(i) + os.linesep + os.linesep) continue elif step[0] == 'sleep': self._logger.debug("Sleep %s seconds", step[1]) time.sleep(int(step[1])) continue elif step[0] == 'log': test_object = self._logger # there isn't a need for validation of log entry step_check = False step_ok = True elif step[0] == 'pdb': import pdb pdb.set_trace() continue else: self._logger.error("Unsupported test object %s", step[0]) self._step_status = { 'status': False, 'details': ' '.join(step) } self.step_report_status("Step '{}'".format(' '.join(step)), self._step_status['status']) return False test_method = getattr(test_object, step[1]) if step_check: test_method_check = getattr(test_object, CHECK_PREFIX + step[1]) else: test_method_check = None step_params = [] try: # eval parameters, but use only valid step_results # to support negative indexes step_params = self.step_eval_params(step[2:], self._step_result[:i]) step_log = '{} {}'.format(' '.join(step[:2]), step_params) step_log += ' filter "{}"'.format(regex) if regex else '' self._logger.debug("Step %s '%s' start", i, step_log) self._step_result[i] = test_method(*step_params) if regex: # apply regex to step output self._step_result[i] = functions.filter_output( self._step_result[i], regex) self._logger.debug("Step %s '%s' results '%s'", i, step_log, self._step_result[i]) time.sleep(S.getValue('TEST_STEP_DELAY')) if step_check: step_ok = test_method_check(self._step_result[i], *step_params) except (AssertionError, AttributeError, IndexError) as ex: step_ok = False self._logger.error("Step %s raised %s", i, type(ex).__name__) if step_check: self.step_report_status("Step {} - '{}'".format(i, step_log), step_ok) if not step_ok: self._step_status = {'status': False, 'details': step_log} # Stop all VNFs started by TestSteps self.step_stop_vnfs() return False # all steps processed without any issue return True
def run_initialize(self): """ Prepare test execution environment """ # mount hugepages if needed self._mount_hugepages() self._logger.debug("Controllers:") loader = Loader() self._traffic_ctl = component_factory.create_traffic( self._traffic['traffic_type'], loader.get_trafficgen_class()) self._vnf_ctl = component_factory.create_vnf(self.deployment, loader.get_vnf_class(), len(self._step_vnf_list)) self._vnf_list = self._vnf_ctl.get_vnfs() self._pod_ctl = component_factory.create_pod(self.deployment, loader.get_pod_class()) self._pod_list = self._pod_ctl.get_pods() # verify enough hugepages are free to run the testcase if not self._check_for_enough_hugepages(): raise RuntimeError('Not enough hugepages free to run test.') # perform guest related handling tmp_vm_count = self._vnf_ctl.get_vnfs_number() + len( self._step_vnf_list) if tmp_vm_count: # copy sources of l2 forwarding tools into VM shared dir if needed self._copy_fwd_tools_for_all_guests(tmp_vm_count) # in case of multi VM in parallel, set the number of streams to the number of VMs if self.deployment.startswith('pvpv'): # for each VM NIC pair we need an unique stream streams = 0 for vm_nic in S.getValue('GUEST_NICS_NR')[:tmp_vm_count]: streams += int(vm_nic / 2) if vm_nic > 1 else 1 self._logger.debug( "VMs with parallel connection were detected. " "Thus Number of streams was set to %s", streams) # update streams if needed; In case of additional VNFs deployed by TestSteps # user can define a proper stream count manually if 'multistream' not in self._traffic or self._traffic[ 'multistream'] < streams: self._traffic.update({'multistream': streams}) # OVS Vanilla requires guest VM MAC address and IPs to work if 'linux_bridge' in S.getValue('GUEST_LOOPBACK'): self._traffic['l2'].update({ 'srcmac': S.getValue('VANILLA_TGEN_PORT1_MAC'), 'dstmac': S.getValue('VANILLA_TGEN_PORT2_MAC') }) self._traffic['l3'].update({ 'srcip': S.getValue('VANILLA_TGEN_PORT1_IP'), 'dstip': S.getValue('VANILLA_TGEN_PORT2_IP') }) if self._vswitch_none: self._vswitch_ctl = component_factory.create_pktfwd( self.deployment, loader.get_pktfwd_class()) else: self._vswitch_ctl = component_factory.create_vswitch( self.deployment, loader.get_vswitch_class(), self._traffic, self._tunnel_operation) self._collector = component_factory.create_collector( loader.get_collector_class(), self._results_dir, self.name) self._loadgen = component_factory.create_loadgen( loader.get_loadgen_class(), self._load_cfg) self._output_file = os.path.join( self._results_dir, "result_{}_{}_{}.csv".format(str(S.getValue('_TEST_INDEX')), self.name, self.deployment)) self._step_status = {'status': True, 'details': ''} # Perform LLC-allocations if S.getValue('LLC_ALLOCATION'): self._rmd.setup_llc_allocation() self._logger.debug("Setup:")