import sys from lib.module_manager import ModuleManager if __name__ == "__main__": manager = ModuleManager() manager.load() if len(sys.argv) > 1: module_name = sys.argv[1] if module_name not in manager.list_modules(): print("Invalid module") sys.exit(1) else: print("Trying to build {}".format(module_name)) manager[module_name].build() print("Building complete!") else: print("Building all modules") manager.build_all() print("Building complete!")
class FakerNetConsole(): def __init__(self, ip="127.0.0.1"): wait = animation.Wait(text="Looking for local FakerNet server") wait.start() self.mm = ModuleManager(ip=ip) error = self.mm.load() wait.stop() if error is not None: self.mm = ModuleManager(ip=ip, https=True, https_ignore=True) error = self.mm.load() wait.stop() if error is not None: if ip == "127.0.0.1": print_formatted_text( HTML('\n\n<ansired>{}</ansired>'.format(error))) wait = animation.Wait(text="FakerNet console is starting") wait.start() self.mm = ModuleManager() self.mm.load() wait.stop() self.mm['init'].check() self.host = "local" else: print_formatted_text( HTML( '<ansired>Failed to connect to the server at {}</ansired>' .format(ip))) sys.exit(1) else: self.host = self.mm.ip else: self.host = self.mm.ip file_history = FileHistory(".fnhistory") self.session = PromptSession(history=file_history) self.global_vars = {"AUTO_ADD": False} self.completer = CommandCompleter(self.mm, self.global_vars) if self.mm.ip == None: err, _ = self.mm['init'].run("verify_permissions") if err is not None: print_formatted_text(HTML('<ansired>{}</ansired>'.format(err))) sys.exit(1) print_formatted_text(HTML( '<ansigreen>{}</ansigreen>'.format(ASCIIART))) print_formatted_text(HTML('<skyblue>Internet-in-a-box\n</skyblue>')) if self.mm.ip == None: print_formatted_text( HTML('<ansigreen>NOTE: In non-server mode!</ansigreen>')) if self.mm['init'].init_needed: self.setup_prompts() else: print_formatted_text( HTML('<ansigreen>Connected to {}</ansigreen>'.format( self.mm.ip))) self.running = True self.current_command = None self.mm.logger.info("Started console") def setup_prompts(self): print_formatted_text( HTML( '<skyblue>Welcome to FakerNet. We need to setup the base of your fake internet.</skyblue>' )) base_net = None # Setup network premessage = "Please enter the initial network allocation. The main DNS server will be hosted here." ok = False while not ok: print_formatted_text( HTML('<slateblue>{}</slateblue>'.format(premessage))) base_net = prompt_builder.prompt_get_network( prompt_text="network>") err, _ = self.mm['netreserve'].run( "add_network", net_addr=base_net, description= "The central network for Fakernet. Hosts central DNS server and other critical services.", switch="fakernet0") if err is None: ok = True else: print_formatted_text(HTML('<ansired>{}</ansired>'.format(err))) # Setup DNS server dns_root = None ok = False while not ok: premessage = "Please enter the root name for the DNS server, this could be 'test', 'fake' or something else." print_formatted_text( HTML('<slateblue>{}</slateblue>'.format(premessage))) dns_root = prompt_builder.prompt_get_dns_name( prompt_text="dns name>") premessage = "Enter the IP of the main DNS server. This will be the main resolver for your FakerNet instance.\n\n(You will need to point all systems to this DNS server for things to work.)" print_formatted_text( HTML('<slateblue>{}</slateblue>'.format(premessage))) dns_ip = prompt_builder.prompt_get_ip_in_network( base_net, prompt_text="dns ip>") err, _ = self.mm['dns'].run( "add_server", ip_addr=dns_ip, description="FakerNet Main DNS Resolver", domain=dns_root) if err is None: ok = True err, _ = self.mm['dns'].run("add_zone", id=1, direction="fwd", zone=dns_root) if err is not None: print_formatted_text( HTML('<ansired>{}</ansired>'.format(err))) ok = False else: print_formatted_text(HTML('<ansired>{}</ansired>'.format(err))) # Setup CA ok = False while not ok: premessage = "Please enter the IP of the main CA server. Services will auto-generate their certificates from here." print_formatted_text( HTML('<slateblue>{}</slateblue>'.format(premessage))) ca_ip = prompt_builder.prompt_get_ip_in_network( base_net, prompt_text="ca ip>") err, _ = self.mm['minica'].run("add_server", fqdn="ca." + dns_root, ip_addr=ca_ip) if err is None: ok = True else: print_formatted_text(HTML('<ansired>{}</ansired>'.format(err))) print_formatted_text( HTML('<ansigreen>{}</ansigreen>'.format("Setup complete!"))) def print_result(self, error, result): if error is not None: print_formatted_text( HTML('<ansired>Error: {}</ansired>'.format(error))) else: if isinstance(result, dict) and 'rows' in result and 'columns' in result: print_table(result['rows'], result['columns']) else: print_formatted_text(HTML('<ansigreen>OK</ansigreen>')) def run_module_function(self, module_name, function_name, args): error, result = self.mm[module_name].run(function_name, **args) self.print_result(error, result) def start(self): while self.running: try: prompt = self.host + '> ' if self.current_command is not None: self.completer.in_func_level = True self.completer.run_options = self.current_command[ 'function'] prompt = self.host + "(" + self.current_command[ 'display_name'] + ')> ' command_string = self.session.prompt(prompt, completer=self.completer) command_split = [] try: command_split = shlex.split(command_string) except ValueError: print_formatted_text( HTML('<ansired>Invalid quotes or command</ansired>')) if len(command_split) > 0: if self.current_command is not None: self.func_level(command_split) else: self.main_level(command_split) except KeyboardInterrupt: self.running = False except EOFError: pass def main_level(self, command_input): command = command_input[0].lower() if command == "exit": self.running = False elif command == "run": self.command_run(command_input[1:]) elif command == "global": pass elif command == "uglobal": pass elif command == "userls": error, users = self.mm.list_users() for user in users: print(" * " + user) elif command == "useradd": username = input("username> ") ok = False while not ok: password1 = getpass(prompt="password> ") password2 = getpass(prompt="password (again)> ") if password1 != password2: print("Passwords do not match") else: ok = True self.mm.add_user(username, password1) elif command == "list_all": error, server_list = self.mm.list_all_servers() if error is None: print_table(server_list, ["Module", "ID", "IP", "Description", "status"]) else: print_formatted_text( HTML('<ansired>Error: "{}"</ansired>'.format(error))) elif command == "save": error = None if len(command_input) > 1: self.mm.save_state(save_name=command_input[1]) else: self.mm.save_state() if error is None: print_formatted_text(HTML('<ansigreen>Save OK</ansigreen>')) else: print_formatted_text( HTML('<ansired>Save Error: "{}"</ansired>'.format(error))) elif command == "restore": error = None if len(command_input) > 1: error, _ = self.mm.restore_state(save_name=command_input[1]) else: error, _ = self.mm.restore_state() if error is None: print_formatted_text(HTML('<ansigreen>Restore OK</ansigreen>')) else: print_formatted_text( HTML('<ansired>Restore Error: "{}"</ansired>'.format( error))) else: print_formatted_text( HTML('<ansired>Error: Invalid command "{}"</ansired>'.format( command))) def func_level(self, command_input): command = command_input[0].lower() if command == "exit": self.current_command = None self.completer.in_func_level = False elif command == "run": self.command_run(command_input[1:]) elif command == "set": if len(command_input) < 3: print_formatted_text( HTML('<ansiyellow>set VAR VALUE</ansiyellow>')) return var_name = command_input[1] value = command_input[2] if var_name not in self.current_command['function']: print_formatted_text( HTML('<ansired>Error: Invalid variable "{}"</ansired>'. format(var_name))) return self.current_command['vars'][var_name] = value elif command == "unset": if len(command_input) < 2: print_formatted_text( HTML('<ansiyellow>unset VAR</ansiyellow>')) return var_name = command_input[1] if var_name not in self.current_command['function']: print_formatted_text( HTML('<ansired>Error: Invalid variable "{}"</ansired>'. format(var_name))) return del self.current_command['vars'][var_name] elif command == "show": if "_desc" in self.current_command['function']: print_formatted_text( HTML('<ansigreen>{}</ansigreen>'.format( self.current_command['function']['_desc']))) rows = [] for variable in self.current_command['function']: if variable == "_desc": continue if variable in self.current_command['vars']: rows.append( [variable, self.current_command['vars'][variable]]) else: rows.append([variable, "NULL"]) print_table(rows, ["Variables", "Values"]) elif command == "execute": for variable in self.current_command['function']: if variable == "_desc": continue if variable not in self.current_command['vars']: print_formatted_text( HTML('<ansired>Error: Variable "{}" not set</ansired>'. format(variable))) return module_name = self.current_command['module_name'] if module_name not in self.mm.list_modules(): print_formatted_text( HTML('<ansired>Error: Got invalid module "{}"</ansired>'. format(module_name))) function_name = self.current_command['function_name'] self.run_module_function(module_name, function_name, self.current_command['vars']) else: print_formatted_text( HTML('<ansired>Error: Invalid command "{}"</ansired>'.format( command))) def command_run(self, args): if len(args) < 2: print_formatted_text( HTML('<ansiyellow>run MODULE FUNCTION</ansiyellow>')) return module_name = args[0] if module_name not in self.mm.list_modules(): print_formatted_text( HTML('<ansired>Error: Invalid module "{}"</ansired>'.format( module_name))) return module = self.mm[module_name] function = args[1] # Special function "help" lists all available functions if function == "help": rows = [] for function in module.__FUNCS__.keys(): description = "" if "_desc" in module.__FUNCS__[function]: description = module.__FUNCS__[function]['_desc'] rows.append([function, description]) print_table(rows, ["Functions", "Descriptions"]) elif function in module.__FUNCS__.keys(): # If there are not parameters, just run the function if len(module.__FUNCS__[function]) == 0 or len( module.__FUNCS__[function] ) == 1 and "_desc" in module.__FUNCS__[function]: error, result = module.run(function) if error is None: if 'rows' in result and 'columns' in result: print_table(result['rows'], result['columns']) else: print_formatted_text( HTML('<ansired>Error: {}</ansired>'.format(error))) else: show_name = module_name + '.' + function out = show_name if "_desc" in module.__FUNCS__[function]: out += ": " + module.__FUNCS__[function]['_desc'] print_formatted_text( HTML('<ansigreen>{}</ansigreen>'.format(out))) self.current_command = { "module_name": module_name, "display_name": show_name, "function_name": function, "function": module.__FUNCS__[function], "vars": {} } else: print_formatted_text( HTML('<ansired>Error: Invalid function "{}"</ansired>'.format( function)))