def add_doc(self, vuln, description): if not cross_version.isstring(vuln): raise ValueError("Vulnerabilty value is not a string") if not cross_version.isstring(description): raise ValueError("Description value is not a string") self.__doc_list.append({"module": self.__module, "vulnerability": vuln, "description": description})
def __init__(self, provides_string, version_range): if not cross_version.isstring(provides_string): raise ValueError("Provides string is not a string") else: self.__provides_string = provides_string if cross_version.isstring(version_range): self.__version_range = version_util.version_range(version_range) elif isinstance(version_range, version_util.version_range): self.__version_range = version_range
def __help(self, options): if len(options) == 0: print("Available commands:") for command in VALID_COMMANDS: print("\t" + command) print("For help with commands, type 'help <COMMAND>'\n\n") print("Other help topics:") print("\tvulns\n\tvars\n") else: command = options[0] if not command in VALID_COMMANDS: print("Invalid command") else: if command in HELP_TOPICS: if cross_version.isstring(HELP_TOPICS[command]): print(command + ": " + HELP_TOPICS[command]) elif "DEFAULT" in HELP_TOPICS[command] and len(options) == 1: print("\n" + command + ": " + HELP_TOPICS[command]["DEFAULT"] + "\n") if len(HELP_TOPICS[command]) > 1: print("\nSubcommands:\n") for sub in HELP_TOPICS[command]: if sub != 'DEFAULT': print("\t" + sub) print("\n") elif len(options) == 2: subcommand = options[1] if subcommand in HELP_TOPICS[command]: print("\n" + command + " " + subcommand + ": " + HELP_TOPICS[command][subcommand] + "\n") else: print("No help topics available") else: print("No help topics available")
def is_installed(self, package): if not cross_version.isstring(package): raise ValueError("Package name is not a string") exists_command = "" if self.__package_manager == "yum": exists_command = "yum list installed " + package elif self.__package_manager == "apt": exists_command = "dpkg -s " + package returncode, return_list, error_list = simple_command.simple_command( ).run(exists_command) if self.__package_manager == "yum": if returncode == 0: return True else: return False elif self.__package_manager == "apt": if returncode == 0: return True else: return False
def get_vulnerability_object(self, vuln_name): if not cross_version.isstring(vuln_name): raise ValueError("Name is not string") if vuln_name in self.__vulnerability_list: return self.__vulnerability_list[vuln_name] else: return False
def _add_temp_version_restriction(self, provides_string, version_range): if not cross_version.isstring(provides_string): raise ValueError("Provides string must be a string") for restriction_item in self.__version_restrictions: if restriction_item.version_range() == version_range and restriction_item.provides_string() == provides_string: return self.__tmp_version_restrictions.append( version_restriction(provides_string, version_range) )
def __init__(self, name, description, provides='', provides_version=''): if (not cross_version.isstring(name) or not cross_version.isstring(description) or not cross_version.isstring(provides) or not cross_version.isstring(provides_version)): raise ValueError("All values must be strings") self.__name = name self.__description = description if provides.strip() == "": self.__provides = None else: self.__provides = provides if provides_version.strip() == '': self.__version = None else: self.__version = version_util.version(provides_version) self.__dependencies = [] self.__cmd_uses = [] self.__cmd_modifies = [] self.__supported_os_list = [] self.__difficulty = None
def set_difficulty(self, difficulty): if cross_version.isinteger(difficulty): if difficulty >= 1 and difficulty <= 3: self.__difficulty = difficulty else: raise ValueError("Invalid difficulty number ( Must be 1 - 3 )") return elif cross_version.isstring(difficulty): self.__difficulty = convert_difficulty(difficulty) else: raise ValueError("Invalid type for difficulty")
def _add_temp_version_restriction(self, provides_string, version_range): if not cross_version.isstring(provides_string): raise ValueError("Provides string must be a string") for restriction_item in self.__version_restrictions: if ( restriction_item.version_range() == version_range and restriction_item.provides_string() == provides_string ): return self.__tmp_version_restrictions.append(version_restriction(provides_string, version_range))
def has_difficulty(self, search): if cross_version.isstring(search): search = convert_difficulty(search) if not cross_version.isinteger(search): raise ValueError("Invalid difficulty") for vuln in self.__vulnerability_list: if self.__vulnerability_list[vuln].get_difficulty() <= search or self.__vulnerability_list[vuln].get_difficulty() == None: return True return False
def run(self, command_string, send_list=[]): PYTHON_VERSION = cross_version.get_python_version() if not cross_version.isstring(command_string): raise ValueError("Invalid command") proc = subprocess.Popen([command_string], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) if len(send_list) > 0: stdin_string = "" for value in send_list: stdin_string += value + "\n" if PYTHON_VERSION == 3: stdin_string = bytes(stdin_string, 'UTF-8') else: stdin_string = None try: if PYTHON_VERSION == 3: output, error = proc.communicate(stdin_string, timeout=60) output = output.decode('UTF-8') error = error.decode('UTF-8') elif PYTHON_VERSION == 2: output, error = proc.communicate(stdin_string) if not output.strip() == "": output_list = output.strip().split("\n") else: output_list = [] if not error.strip() == "": error_list = error.strip().split("\n") else: error_list = [] if '' in output_list: output_list.remove('') if '' in error_list: error_list.remove('') return (proc.returncode, output_list, error_list) except OSError as e: return ([], ["OSError", e.message])
def __init__(self, choice_list): # ~ print("Choice List") # ~ print(choice_list) # ~ print(inspect.stack()) self.__dep_links = [] if len(choice_list) == 0: raise ValueError("No dependencies set") for choice in choice_list: if ( isinstance(choice, tuple) and len(choice) == 2 and cross_version.isstring(choice[0]) and cross_version.isstring(choice[1]) ): self.__dep_links.append(Dependency_Choice(choice[0], version_util.version_range(choice[1]))) else: raise ValueError("Invalid tuple") self.__backup = copy.deepcopy(self.__dep_links)
def __init__(self, name, description, provides="", provides_version=""): if ( not cross_version.isstring(name) or not cross_version.isstring(description) or not cross_version.isstring(provides) or not cross_version.isstring(provides_version) ): raise ValueError("All values must be strings") self.__name = name self.__description = description if provides.strip() == "": self.__provides = None else: self.__provides = provides if provides_version.strip() == "": self.__version = None else: self.__version = version_util.version(provides_version) self.__dependencies = [] self.__cmd_uses = [] self.__cmd_modifies = [] self.__supported_os_list = [] self.__difficulty = None
def run(self, command_string, send_list=[]): PYTHON_VERSION = cross_version.get_python_version() if not cross_version.isstring(command_string): raise ValueError("Invalid command") proc = subprocess.Popen([command_string], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) if len(send_list) > 0: stdin_string = "" for value in send_list: stdin_string += value + "\n" if PYTHON_VERSION == 3: stdin_string = bytes(stdin_string, 'UTF-8') else: stdin_string = None try: if PYTHON_VERSION == 3: output, error = proc.communicate(stdin_string, timeout=60) output = output.decode('UTF-8') error = error.decode('UTF-8') elif PYTHON_VERSION == 2: output, error = proc.communicate(stdin_string) if not output.strip() == "": output_list = output.strip().split("\n") else: output_list = [] if not error.strip() == "": error_list = error.strip().split("\n") else: error_list = [] if '' in output_list: output_list.remove('') if '' in error_list: error_list.remove('') return (proc.returncode ,output_list, error_list) except OSError as e: return ([], ["OSError", e.message])
def has_difficulty(self, search): if cross_version.isstring(search): search = convert_difficulty(search) if not cross_version.isinteger(search): raise ValueError("Invalid difficulty") for vuln in self.__vulnerability_list: if ( self.__vulnerability_list[vuln].get_difficulty() <= search or self.__vulnerability_list[vuln].get_difficulty() == None ): return True return False
def add_dependency_restriction(self, provides_string, version_range): if not cross_version.isstring(provides_string): raise ValueError("Provides string must be a string") for restriction_item in self.__dependency_restrictions: if restriction_item.version_range() == version_range and restriction_item.provides_string() == provides_string: return #~ self.__version_restrictions = {} #~ self.__dependency_restrictions = {} #~ self.__commands_modified_restrictions = [] self.__dependency_restrictions.append( dependency_restriction(provides_string, version_range) )
def add_dependency_restriction(self, provides_string, version_range): if not cross_version.isstring(provides_string): raise ValueError("Provides string must be a string") for restriction_item in self.__dependency_restrictions: if ( restriction_item.version_range() == version_range and restriction_item.provides_string() == provides_string ): return # ~ self.__version_restrictions = {} # ~ self.__dependency_restrictions = {} # ~ self.__commands_modified_restrictions = [] self.__dependency_restrictions.append(dependency_restriction(provides_string, version_range))
def uninstall(self, package): if not cross_version.isstring(package): raise ValueError("Package name is not a string") uninstall_command = "" if self.__package_manager == "yum": install_command = "yum remove -y " + package elif self.__package_manager == "apt": install_command = "apt-get remove -y --purge " + package returncode, return_list, error_list = simple_command.simple_command().run(install_command) if returncode == 0: return True else: return False
def range_pass(self, provides_string, version_range): if not cross_version.isstring(provides_string): raise ValueError("Provides string is not a string") if not isinstance(version_range, version_util.version_range): range_obj = version_util.version_range(version_range) else: range_obj = version_range if not self.provides_string() == provides_string: return False if range_obj.intersects(self.version_range()): return True return False
def __init__(self, choice_list): #~ print("Choice List") #~ print(choice_list) #~ print(inspect.stack()) self.__dep_links = [] if len(choice_list) == 0: raise ValueError("No dependencies set") for choice in choice_list: if isinstance(choice, tuple) and len(choice) == 2 and cross_version.isstring(choice[0]) and cross_version.isstring(choice[1]): self.__dep_links.append(Dependency_Choice(choice[0], version_util.version_range(choice[1]))) else: raise ValueError("Invalid tuple") self.__backup = copy.deepcopy(self.__dep_links)
def uninstall(self, package): if not cross_version.isstring(package): raise ValueError("Package name is not a string") uninstall_command = "" if self.__package_manager == "yum": install_command = "yum remove -y " + package elif self.__package_manager == "apt": install_command = "apt-get remove -y --purge " + package returncode, return_list, error_list = simple_command.simple_command( ).run(install_command) if returncode == 0: return True else: return False
def version_pass(self, provides_string, version_string): if not self.provides_string() == provides_string: return False if version_string == None: return True if isinstance(version_string, version_util.version): version_obj = version_string elif cross_version.isstring(provides_string): version_obj = version_util.version(version_string) else: raise ValueError("Provides string is not a string or version object") if self.version_range().in_range(version_obj): return True return False
def install(self, package, version=None): if not cross_version.isstring(package): raise ValueError("Package name is not a string") install_command = "" if self.__package_manager == "yum": install_command = "yum install -y " + package elif self.__package_manager == "apt": returncode, return_list, error_list = simple_command.simple_command().run("apt-get update") install_command = "apt-get install -y " + package returncode, return_list, error_list = simple_command.simple_command().run(install_command) if returncode == 0: return True else: return False
def __init__(self, version_range_string): if not cross_version.isstring(version_range_string): raise ValueError("Invalid value for range") self.__range_direction = "" self.__version = None self.__string = version_range_string if version_range_string == "*" or version_range_string == "-": self.__range_direction = version_range_string self.__version = None elif version_range_string.startswith("<=") or version_range_string.startswith(">="): self.__range_direction = version_range_string[:2] self.__version = version(version_range_string[2:]) elif version_range_string.startswith("<") or version_range_string.startswith(">") or version_range_string.startswith("="): self.__range_direction = version_range_string[:1] self.__version = version(version_range_string[1:]) else: raise ValueError("Invalid range: " + version_range_string)
def install(self, package, version=None): if not cross_version.isstring(package): raise ValueError("Package name is not a string") install_command = "" if self.__package_manager == "yum": install_command = "yum install -y " + package elif self.__package_manager == "apt": returncode, return_list, error_list = simple_command.simple_command( ).run("apt-get update") install_command = "apt-get install -y " + package returncode, return_list, error_list = simple_command.simple_command( ).run(install_command) if returncode == 0: return True else: return False
def __init__(self, input_string): if not cross_version.isstring(input_string): raise ValueError("Version must be a string") version_list = input_string.split(".") if len(version_list) == 1 and str(version_list[0]).isdigit(): version_list.append('0') elif len(version_list) < 2: raise ValueError("Invalid version string") elif len(version_list) > 4: raise ValueError("Invalid version string") final_version_list = [] for i in range(len(version_list)): current = version_list[i] if i == 0 and not current.isdigit(): raise ValueError( "Invalid version string. First section must be an integer") else: if current.isdigit(): final_version_list.append(int(current)) if i > 0: final_version_list.append('') else: # Check for numbers followed by check = re.match(r"^([0-9]+)([a-zA-Z]+).*$", current) if not check == None: check_groups = check.groups() final_version_list.append(int(check_groups[0])) final_version_list.append(check_groups[1]) else: pass if len(final_version_list) < 3 or len(final_version_list) > 7: raise ValueError("Invalid version string") self.__v_tuple = tuple(final_version_list)
def add_module(self, module, forced=[]): if issubclass(module.__class__, base.module_base): module_name = module.get_class_name() elif cross_version.isstring(module): module_name = module else: raise ValueError("Invalid module to add. Must be module object or name") if module_util.module_exists(module_name): if self.__debug: print("Adding module " + module_name) self.__insert_module(module_name, module_util.import_module(module_name)) self.__set_modules.append(module_name) if len(forced) > 0: self.__name_map[module_name].set_forced(forced) else: return
def __init__(self, input_string): if not cross_version.isstring(input_string): raise ValueError("Version must be a string") version_list = input_string.split(".") if len(version_list) == 1 and str(version_list[0]).isdigit(): version_list.append('0') elif len(version_list) < 2: raise ValueError("Invalid version string") elif len(version_list) > 4: raise ValueError("Invalid version string") final_version_list = [] for i in range(len(version_list)): current = version_list[i] if i == 0 and not current.isdigit(): raise ValueError("Invalid version string. First section must be an integer") else: if current.isdigit(): final_version_list.append(int(current)) if i > 0: final_version_list.append('') else: # Check for numbers followed by check = re.match(r"^([0-9]+)([a-zA-Z]+).*$", current) if not check == None: check_groups = check.groups() final_version_list.append(int(check_groups[0])) final_version_list.append(check_groups[1]) else: pass if len(final_version_list) < 3 or len(final_version_list) > 7: raise ValueError("Invalid version string") self.__v_tuple = tuple(final_version_list)
def __init__(self, version_range_string): if not cross_version.isstring(version_range_string): raise ValueError("Invalid value for range") self.__range_direction = "" self.__version = None self.__string = version_range_string if version_range_string == "*" or version_range_string == "-": self.__range_direction = version_range_string self.__version = None elif version_range_string.startswith( "<=") or version_range_string.startswith(">="): self.__range_direction = version_range_string[:2] self.__version = version(version_range_string[2:]) elif version_range_string.startswith( "<") or version_range_string.startswith( ">") or version_range_string.startswith("="): self.__range_direction = version_range_string[:1] self.__version = version(version_range_string[1:]) else: raise ValueError("Invalid range: " + version_range_string)
def is_installed(self, package): if not cross_version.isstring(package): raise ValueError("Package name is not a string") exists_command = "" if self.__package_manager == "yum": exists_command = "yum list installed " + package elif self.__package_manager == "apt": exists_command = "dpkg -s " + package returncode, return_list, error_list = simple_command.simple_command().run(exists_command) if self.__package_manager == "yum": if returncode == 0: return True else: return False elif self.__package_manager == "apt": if returncode == 0: return True else: return False
def add_module(self, module, forced=[]): if issubclass(module.__class__, base.module_base): module_name = module.get_class_name() elif cross_version.isstring(module): module_name = module else: raise ValueError( "Invalid module to add. Must be module object or name") if module_util.module_exists(module_name): if self.__debug: print("Adding module " + module_name) self.__insert_module(module_name, module_util.import_module(module_name)) self.__set_modules.append(module_name) if len(forced) > 0: self.__name_map[module_name].set_forced(forced) else: return
def __help(self, options): if len(options) == 0: print("Available commands:") for command in VALID_COMMANDS: print("\t" + command) print("For help with commands, type 'help <COMMAND>'\n\n") print("Other help topics:") print("\tvulns\n\tvars\n") else: command = options[0] if not command in VALID_COMMANDS: print("Invalid command") else: if command in HELP_TOPICS: if cross_version.isstring(HELP_TOPICS[command]): print(command + ": " + HELP_TOPICS[command]) elif "DEFAULT" in HELP_TOPICS[command] and len( options) == 1: print("\n" + command + ": " + HELP_TOPICS[command]["DEFAULT"] + "\n") if len(HELP_TOPICS[command]) > 1: print("\nSubcommands:\n") for sub in HELP_TOPICS[command]: if sub != 'DEFAULT': print("\t" + sub) print("\n") elif len(options) == 2: subcommand = options[1] if subcommand in HELP_TOPICS[command]: print("\n" + command + " " + subcommand + ": " + HELP_TOPICS[command][subcommand] + "\n") else: print("No help topics available") else: print("No help topics available")
def isstring_tests(): assert cross_version.isstring("hi there") == True assert cross_version.isstring('wassup') == True assert cross_version.isstring({"nope": "nope"}) == False assert cross_version.isstring(['i']) == False
def set_link(self, link): if cross_version.isstring(link) and link.startswith("http://") or link.startswith("https://"): self.__link = link else: raise ValueError("Invalid link")
def add_cmd_modifies(self, cmd): if cross_version.isstring(cmd): if not cmd in self.__cmd_modifies: self.__cmd_modifies.append(cmd) else: raise ValueError("Invalid command")