def test_os_not_match(): data = os_data.os_info() if platform.system() == 'Linux': just_linux = os_data.os_match('linux') assert data.matches(just_linux) == True plat_data = platform.linux_distribution() if 'ubuntu' in plat_data[0].lower(): ubuntu_match = os_data.os_match('linux', 'centos') assert data.matches(ubuntu_match) == False ubuntu_match = os_data.os_match('linux', 'ubuntu', "<" + plat_data[1]) assert data.matches(ubuntu_match) == False elif 'centos' in plat_data[0].lower(): centos_match = os_data.os_match('linux', 'ubuntu') assert data.matches(centos_match) == False centos_match = os_data.os_match('linux', 'centos', "<" + plat_data[1]) assert data.matches(centos_match) == False
def test_get_package_manager(): test = simple_packages() my_os = os_data.os_info() ubuntu_match = os_data.os_match('linux', 'ubuntu') centos_match = os_data.os_match('linux', 'centos') if my_os.matches(ubuntu_match): assert test.get_package_manager() == "apt" elif my_os.matches(centos_match): assert test.get_package_manager() == "yum"
def test_os_match_obj_invalid(): try: invalid = os_data.os_match('nope') except ValueError: assert True try: invalid = os_data.os_match('linux', "nope") except ValueError: assert True try: invalid = os_data.os_match('linux', "nope", {}) except ValueError: assert True
def test_os_match_obj(): test_obj = os_data.os_match('linux') assert test_obj.os_type() == "linux" assert test_obj.flavor() == "*" assert test_obj.version_range() == "*" test_obj = os_data.os_match('linux', 'ubuntu') assert test_obj.os_type() == "linux" assert test_obj.flavor() == 'ubuntu' assert test_obj.version_range() == "*" test_obj = os_data.os_match('linux', 'ubuntu', '>12.04') assert test_obj.os_type() == "linux" assert test_obj.flavor() == 'ubuntu' assert test_obj.version_range() == '>12.04'
def test_get_network(): test_obj = networking() if os_data.os_info().matches(os_data.os_match('linux')): value = test_obj.get_networks("lo") assert value[0] == "127.0.0.0" assert value[1] == "0:0:0:0:0:0:0:1" value = test_obj.get_networks("eth0")
def test_version_restriction_os(): test_obj = module_util.import_module("test_module") vuln = test_obj.new_vulnerability("Restrict_Test_OS", "Test", "restrict", "1.0.0") test_obj.add_vulnerability(vuln) assert len(test_obj.get_vulnerabilities(force=True)) == 1 if os_data.os_info().matches(os_data.os_match("linux", "ubuntu")): vuln.add_supported_os("linux", "centos") else: vuln.add_supported_os("linux", "ubuntu") assert len(test_obj.get_vulnerabilities(force=True)) == 0
def test_version_restriction_os(): test_obj = module_util.import_module('test_module') vuln = test_obj.new_vulnerability("Restrict_Test_OS", "Test", "restrict", "1.0.0") test_obj.add_vulnerability(vuln) assert len(test_obj.get_vulnerabilities(force=True)) == 1 if os_data.os_info().matches(os_data.os_match('linux', 'ubuntu')): vuln.add_supported_os('linux', 'centos') else: vuln.add_supported_os('linux', 'ubuntu') assert len(test_obj.get_vulnerabilities(force=True)) == 0
def test_get_addresses(): test_obj = networking() if os_data.os_info().matches(os_data.os_match('linux')): value = test_obj.get_addresses("lo") assert 'mac' in value assert 'ipv4' in value assert 'ipv6' in value assert value['mac']['address'] == "00:00:00:00:00:00" assert value['mac']['broadcast'] == "00:00:00:00:00:00" assert value['ipv4']['address'] == "127.0.0.1" assert value['ipv4']['mask'] == "8" assert value['ipv6']['address'] == "::1" assert value['ipv6']['mask'] == "128"
def test_available_ports(): test_obj = networking() port_list = [] if os_data.os_info().matches(os_data.os_match('linux')): code, output, error = simple_command().run('netstat -tunap | grep LISTEN') for line in output: line_list = line.split(" ") new_line = [] for item in line_list: if item != "": new_line.append(item) listen_section = new_line[3] listen_split = listen_section.split(":") port = listen_split[len(listen_split) - 1] port_list.append(int(port)) for port in port_list: assert test_obj.is_port_available(port) == False i = 1 for j in range(5): while i in port_list and i <= 65535: i += 5 if i == 65535: assert False assert test_obj.is_port_available(i) == True i += 5
def os_matches(self, type_string, flavor_string="*", version_string="*"): matcher = os_data.os_match(type_string, flavor_string, version_string) return self.os_info().matches(matcher)
def __fill(self): if os_data.os_info().matches(os_data.os_match('linux')): cmd = simple_command.simple_command() code, output, errors = cmd.run("ip addr") if code == 0: output = "\n".join(output) raw_interfaces = re.split("\n[0-9]+: ", "\n" + output) for interface in raw_interfaces: if interface == "": continue data_split = interface.split(": ") interface_name = data_split[0] del data_split[0] interface_info = ": ".join(data_split) self.__interfaces[interface_name] = {} info_split = interface_info.split("\n") for line in info_split: if line.startswith(" "): line = line.strip() line_split = line.split(" ") if line.startswith("link"): if len(line_split) == 4: if not 'mac' in self.__interfaces[interface_name]: self.__interfaces[interface_name]['mac'] = {} self.__interfaces[interface_name]['mac']['address'] = line_split[1] self.__interfaces[interface_name]['mac']['broadcast'] = line_split[3] elif line.startswith("inet "): if len(line_split) >= 4: ipv4_data = line_split[1] ip_split = ipv4_data.split("/") addr = ip_split[0] mask = ip_split[1] if not 'ipv4' in self.__interfaces[interface_name]: self.__interfaces[interface_name]['ipv4'] = {} self.__interfaces[interface_name]['ipv4']['address'] = addr self.__interfaces[interface_name]['ipv4']['mask'] = mask if "brd" in line: self.__interfaces[interface_name]['ipv4']['broadcast'] = line_split[3] elif line.startswith("inet6 "): if len(line_split) >= 4: ipv6_data = line_split[1] ip_split = ipv6_data.split("/") addr = ip_split[0] mask = ip_split[1] if not 'ipv6' in self.__interfaces[interface_name]: self.__interfaces[interface_name]['ipv6'] = {} self.__interfaces[interface_name]['ipv6']['address'] = addr self.__interfaces[interface_name]['ipv6']['mask'] = mask if "brd" in line: self.__interfaces[interface_name]['ipv6']['broadcast'] = line_split[3] #~ print(self.__interfaces) else: print("Error running ip addr") elif os_data.os_info().matches(os_data.os_match('windows')): pass
def test_get_interfaces(): test_obj = networking() if os_data.os_info().matches(os_data.os_match('linux')): value = test_obj.get_interfaces() assert "lo" in value
def add_supported_os(self, os_type, flavor="*", version="*"): insert_os = os_data.os_match(os_type, flavor, version) self.__supported_os_list.append(insert_os)