def parse_nmap_scan(out_file): xmldoc = minidom.parse(out_file) hostslist = xmldoc.getElementsByTagName('hosts') # We only scan one host at a time if int(hostslist[0].attributes['down'].value) > 0: print(utils.error_message(), config.current_target, "was unreachable") else: port_list = xmldoc.getElementsByTagName('port') print("") print(utils.normal_message(), len(port_list), "ports are open") cpe_list = [ x.firstChild.nodeValue for x in xmldoc.getElementsByTagName('cpe') ] detector.detect_os(cpe_list) detector.detect_apps(cpe_list) # New line for nicer formatting print("") searchsploit_nmap_scan(out_file) for open_port in port_list: detector.detect_service(open_port)
def __execute_init_module(target: Target) -> None: """ Run InitModules against the Target :param target: Target object to scan """ if len(LOADED_INIT_MODULES) > 0: module = LOADED_INIT_MODULES[0] if module.can_execute_module() is ModuleExecuteState.CanExecute: print(utils.normal_message(), "Executing {PROGRAM}".format(PROGRAM=module.name)) module.execute(target.get_address(), 0) else: print( utils.error_message(), "Unable to meet dependencies for {MODULE}. Quitting".format( MODULE=module.name)) sys.exit(ExitCode.CriticalDependencyNotInstalled) else: print(utils.error_message(), "No Init Modules loaded. Quitting") sys.exit(ExitCode.EntryPointModulesNotLoaded)
def detect_os(cpe_list): for cpe in cpe_list: cpe_os_type = "cpe:/o" if cpe.startswith(cpe_os_type): print(utils.normal_message(), "Target OS appears to be", cpe_utils.CPE(cpe).human()) if cpe_utils.CPE(cpe).matches(cpe_utils.CPE("cpe:/o:microsoft:windows")) \ and platform.system() == "linux": print(utils.warning_message(), "Target machine is running Microsoft Windows") print(utils.warning_message(), "Will commence enumeration using enum4linux") print(utils.error_message(), "enum4linux not yet implemented")
def format(self, record: logging.LogRecord) -> str: """ Custom formatter for the stdout logging :param record: The Log to format :return: The formatted string """ # TODO: Fix cyclic imports from core.utils import warning_message, error_message, normal_message from core.ArgHandler import get_verbose, get_very_verbose if record.levelno == logging.WARNING: if get_verbose() or get_very_verbose(): return warning_message() + ' [{NAME}] {MESSAGE}'.format(NAME=record.name, MESSAGE=record.getMessage()) else: return warning_message() + ' {MESSAGE}'.format(NAME=record.name, MESSAGE=record.getMessage()) if record.levelno >= logging.ERROR: return error_message() + ' [{NAME}] {MESSAGE}'.format(NAME=record.name, MESSAGE=record.getMessage()) return normal_message() + ' [{NAME}] {MESSAGE}'.format(NAME=record.name, MESSAGE=record.getMessage())
def parse_arguments(args) -> None: """ Parse the command line arguments :param args: The arguments to parse """ global __args parser = create_parser() if len(args) is 0: print(error_message(), "No arguments supplied, showing help...\n") time.sleep(0.5) parser.print_help() sys.exit(1) if len(args) is 1 and "--version" in args: print(normal_message(), "Lancer {VERSION}".format(VERSION=__version__)) sys.exit(0) if len(args) is 1 and "-h" or "--help" in args: parser.print_help() sys.exit(0) __args = parser.parse_args(args)
def init(): """ Initialise all of the needed prerequisites for Lancer. This should: - Register the signal handler for Ctrl+C - Load the config file - Parse command line arguments - Show the header - Check that we're on a supported Python version - Show an option to update the VirtualTerminal registry key if on Win 10 - Show a warning that localisation support is not yet implemented if there is a non-default -l parameter - Display a legal disclaimer about using Lancer for illegal use - Warn if the cache is over 500mb in size - Clear the cache if we want to """ # Register the signal handler for a more graceful Ctrl+C signal.signal(signal.SIGINT, utils.signal_handler) # Load the config file config.load_config() # Parse the arguments ArgHandler.parse_arguments(sys.argv[1:]) # Display the header utils.display_header() time.sleep(1.25) # Check we're on a supported Python version utils.python_version() # Update the Windows virtual terminal if necessary # If we're on Windows 10, import winutils if platform.system().lower() == "windows" and platform.release() == "10": from core.winutils import update_windows_virtual_terminal update_windows_virtual_terminal() # Language warning - not yet implemented if ArgHandler.get_language_code() != 'en': print(utils.error_message(), "Multi-language support is not yet implemented...") # Show a legal disclaimer disclaimer = utils.terminal_width_string( "Legal Disclaimer: Usage of Lancer for attacking targets without prior mutual" " authorisation is illegal. It is the end user's responsibility to adhere to all local" " and international laws. The developers of this tool assume no liability and are not" " responsible for any misuse or damage caused by the use of this program." ) print(utils.error_message(), disclaimer) print() # Cache warning # If it is more than 1GB, we display an error-style warning root_directory = Path(config.get_cache_path()) size = sum(f.stat().st_size for f in root_directory.glob('**/*') if f.is_file()) / 1048576 # Bytes -> MB if size >= 2048: print(utils.error_message(), "Cache is {SIZE}gb in size. It is recommended to clear it with --clear-cache." .format(SIZE="{:.1f}".format(size/1024))) # If it is more than 500, we display a warning elif size >= 512: print(utils.warning_message(), "Cache is {SIZE}mb in size. You can clear it with --clear-cache." .format(SIZE="{:.1f}".format(size))) # Clear the cache if ArgHandler.get_clear_cache(): files = os.listdir(config.get_cache_path()) for filename in files: file_path = os.path.join(config.get_cache_path(), filename) if os.path.isfile(file_path) or os.path.islink(file_path): os.unlink(file_path) elif os.path.isdir(file_path) and file_path != config.get_current_cache_path(): shutil.rmtree(file_path) print(utils.normal_message(), "Removed {NUM} items from the cache".format(NUM=len(files))) # Check if we are admin, display a relevant message if utils.is_user_admin(): print(utils.normal_message(), "Lancer running with elevated permissions") else: non_admin_warning = utils.terminal_width_string("Lancer doesn't appear to being run with elevated" " permissions. Some functionality may not work" " correctly") print(utils.warning_message(), non_admin_warning) # Display warning about your IP address ip_address = utils.terminal_width_string( "Your IP Address has been detected as {IP}. This can be changed with -a [IP]" ) print(utils.normal_message(), ip_address.format(IP=get_ip())) print() # Preload all of the modules ModuleProvider.load()
Generate the reports """ # TODO: Load report generators dynamicaly logger.debug("Generating reports") report = JSONReport() report.generate_report(Loot.loot) report = TerminalReport() report.generate_report(Loot.loot) logger.debug("Finished generating reports") if __name__ == "__main__": init() logger = config.get_logger("Main") try: main() except NotImplementedError as e: logger.error(e) sys.exit(ExitCode.NotImplemented) except Exception as e: exc_type, exc_value, exc_traceback = sys.exc_info() tb = traceback.format_exception(exc_type, exc_value, exc_traceback) print(utils.error_message(), "Unknown error encountered ({ERR}) - please report this via Github\n{EXCEPTION}" .format(ERR=e.args[0], EXCEPTION="".join(tb))) sys.exit(ExitCode.UnknownError) finally: print() print(utils.normal_message(), "Thank you for using Lancer") config.save_config()
def test_error_message(): out = utils.error_message() assert "[!]" in out