def explore_pytaxonomies(): """ the tool should go in an endless loop asking for user input with auto complete until the right taxonomy is choosen example: PAP<enter> PAP:AMBER PAP:WHITE PAP:GREEN PAP:RED PAP:RED<enter> --> finished https://github.com/MISP/PyTaxonomies """ from pytaxonomies import Taxonomies taxonomies = Taxonomies() again_user_input = input("do you want to search within the pyTaxonomies? (y/n) ") if again_user_input.lower() == 'y': again = True else: again = False while again: try: char = input("Term you want to search for e.g. PAP, TLP, ...") # read 1 character from stdin # try autocomplete print("Suggestions") if (char is "") or (len(char) == 0): search_results = taxonomies.keys() else: #search_results = taxonomies.get(char).machinetags_expanded() all = taxonomies.all_machinetags() resultarray = [] for iterm in all: for item2 in iterm: if char in item2.lower(): # lower to be able to find PAP if you look for p resultarray.append(item2) for resultitem in resultarray: print(resultitem) # print(search_results) again_user_input = input("again?") if again_user_input.lower() == 'n': again = False else: again = True except AttributeError as e: print("Seems we did not find the value "+str(e))
class TestPyTaxonomies(unittest.TestCase): def setUp(self): self.taxonomies_offline = Taxonomies() self.loaded_tax = {} for t in self.taxonomies_offline.manifest['taxonomies']: with open('{}/{}/{}'.format(self.taxonomies_offline.url, t['name'], 'machinetag.json'), 'r') as f: self.loaded_tax[t['name']] = json.load(f) def test_compareOnlineOffilne(self): taxonomies_online = Taxonomies(manifest_path=None) for t_online, t_offline in zip(taxonomies_online.values(), self.taxonomies_offline.values()): self.assertEqual(str(t_online), str(t_offline)) self.assertEqual(str(taxonomies_online), str(self.taxonomies_offline)) def test_expanded_machinetags(self): self.taxonomies_offline.all_machinetags(expanded=True) def test_machinetags(self): self.taxonomies_offline.all_machinetags() def test_dict(self): len(self.taxonomies_offline) for n, t in self.taxonomies_offline.items(): len(t) for p, value in t.items(): continue def test_search(self): self.taxonomies_offline.search('phish') def test_search_expanded(self): self.taxonomies_offline.search('phish', expanded=True) def test_print_classes(self): for taxonomy in self.taxonomies_offline.values(): print(taxonomy) for predicate in taxonomy.values(): print(predicate) for entry in predicate.values(): print(entry) def test_amountEntries(self): for tax in self.taxonomies_offline.values(): tax.amount_entries() def test_missingDependency(self): pytaxonomies.api.HAS_REQUESTS = False with self.assertRaises(Exception): Taxonomies(manifest_path=None) Taxonomies() pytaxonomies.api.HAS_REQUESTS = True def test_revert_machinetags(self): for tax in self.taxonomies_offline.values(): for p in tax.values(): if tax.has_entries(): for e in p.values(): mt = tax.make_machinetag(p, e) self.taxonomies_offline.revert_machinetag(mt) else: mt = tax.make_machinetag(p) self.taxonomies_offline.revert_machinetag(mt) def test_json(self): for key, t in self.taxonomies_offline.items(): json.dumps(t, cls=EncodeTaxonomies) def test_recreate_dump(self): self.maxDiff = None for key, t in self.taxonomies_offline.items(): out = t.to_dict() self.assertDictEqual(out, self.loaded_tax[t.name]) def test_validate_schema(self): self.taxonomies_offline.validate_with_schema()
class TestPyTaxonomies(unittest.TestCase): def setUp(self): self.taxonomies = Taxonomies() self.manifest_path = "./misp-taxonomies/MANIFEST.json" self.taxonomies_offline = Taxonomies(manifest_path=self.manifest_path) self.json_load_taxonomies() def __load_path(self, path): with open(path, 'r') as f: return json.load(f) def json_load_taxonomies(self): self.manifest = self.__load_path(self.manifest_path) self.loaded_tax = {} for t in self.manifest['taxonomies']: path = '{}/{}/{}'.format( os.path.dirname(os.path.realpath(self.manifest_path)), t['name'], self.manifest['path']) self.loaded_tax[t['name']] = self.__load_path(path) def test_compareOnlineOffilne(self): self.assertEqual(str(self.taxonomies), str(self.taxonomies_offline)) def test_expanded_machinetags(self): self.taxonomies.all_machinetags(expanded=True) def test_machinetags(self): self.taxonomies.all_machinetags() def test_dict(self): len(self.taxonomies) for n, t in self.taxonomies.items(): len(t) for p, value in t.items(): continue def test_search(self): self.taxonomies.search('phish') def test_search_expanded(self): self.taxonomies.search('phish', expanded=True) def test_print_classes(self): for taxonomy in self.taxonomies.values(): print(taxonomy) for predicate in taxonomy.values(): print(predicate) for entry in predicate.values(): print(entry) def test_amountEntries(self): for tax in self.taxonomies.values(): tax.amount_entries() def test_missingDependency(self): pytaxonomies.api.HAS_REQUESTS = False with self.assertRaises(Exception): Taxonomies() Taxonomies(manifest_path="./misp-taxonomies/MANIFEST.json") pytaxonomies.api.HAS_REQUESTS = True def test_revert_machinetags(self): for tax in self.taxonomies.values(): for p in tax.values(): if tax.has_entries(): for e in p.values(): mt = tax.make_machinetag(p, e) self.taxonomies.revert_machinetag(mt) else: mt = tax.make_machinetag(p) self.taxonomies.revert_machinetag(mt) def test_json(self): for key, t in self.taxonomies.items(): json.dumps(t, cls=EncodeTaxonomies) def test_recreate_dump(self): self.maxDiff = None for key, t in self.taxonomies.items(): out = t._json() print(t.name) self.assertCountEqual(out, self.loaded_tax[t.name])
class TestPyTaxonomies(unittest.TestCase): def setUp(self): self.maxDiff = None self.taxonomies_offline = Taxonomies() self.loaded_tax = {} for t in self.taxonomies_offline.manifest['taxonomies']: with open( '{}/{}/{}'.format(self.taxonomies_offline.url, t['name'], 'machinetag.json'), 'r') as f: self.loaded_tax[t['name']] = json.load(f) def test_compareOnlineOffilne(self): taxonomies_online = Taxonomies(manifest_path=None) for t_online, t_offline in zip(taxonomies_online.values(), self.taxonomies_offline.values()): self.assertEqual(str(t_online), str(t_offline)) self.assertEqual(str(taxonomies_online), str(self.taxonomies_offline)) def test_expanded_machinetags(self): self.taxonomies_offline.all_machinetags(expanded=True) def test_machinetags(self): self.taxonomies_offline.all_machinetags() def test_dict(self): len(self.taxonomies_offline) for n, t in self.taxonomies_offline.items(): len(t) for p, value in t.items(): continue def test_search(self): self.taxonomies_offline.search('phish') def test_search_expanded(self): self.taxonomies_offline.search('phish', expanded=True) def test_print_classes(self): for taxonomy in self.taxonomies_offline.values(): print(taxonomy) for predicate in taxonomy.values(): print(predicate) for entry in predicate.values(): print(entry) def test_amountEntries(self): for tax in self.taxonomies_offline.values(): tax.amount_entries() def test_missingDependency(self): pytaxonomies.api.HAS_REQUESTS = False with self.assertRaises(Exception): Taxonomies(manifest_path=None) Taxonomies() pytaxonomies.api.HAS_REQUESTS = True def test_revert_machinetags(self): for tax in self.taxonomies_offline.values(): for p in tax.values(): if tax.has_entries(): for e in p.values(): mt = tax.make_machinetag(p, e) self.taxonomies_offline.revert_machinetag(mt) else: mt = tax.make_machinetag(p) self.taxonomies_offline.revert_machinetag(mt) def test_json(self): for key, t in self.taxonomies_offline.items(): t.to_json() def test_recreate_dump(self): self.maxDiff = None for key, t in self.taxonomies_offline.items(): out = t.to_dict() self.assertDictEqual(out, self.loaded_tax[t.name]) def test_validate_schema(self): self.taxonomies_offline.validate_with_schema()