misp-taxonomies/tools/generator/veris.py

86 lines
2.1 KiB
Python
Raw Normal View History

import json
debug = False
filename = 'verisc-labels.json'
namespace = 'veris'
description = 'Vocabulary for Event Recording and Incident Sharing (VERIS)'
output = {}
output['namespace'] = namespace
output['description'] = description
2015-11-24 10:57:19 +01:00
output['version'] = 2
output['predicates'] = []
output['values'] = []
with open(filename) as fp:
t = json.load(fp)
2017-04-02 22:06:32 +02:00
def lookupPredicate(predicate=False):
if not predicate:
return False
for p in output['predicates']:
if p['value'] == predicate:
return True
2017-04-02 22:06:32 +02:00
def lookupValues(predicate=False):
if not predicate:
return False
for p in output['values']:
if p['predicate'] == predicate:
2017-04-02 22:06:32 +02:00
return True
def machineTag(namespace=False, predicate=False, value=None, expanded=None):
if namespace is False or predicate is False:
return None
if value is None:
return ('{0}:{1}'.format(namespace, predicate))
else:
if not lookupPredicate(predicate=predicate):
x = {}
x['value'] = predicate
output['predicates'].append(x)
y = {}
y['predicate'] = predicate
y['entry'] = []
z = {}
z['value'] = value
z['expanded'] = expanded
y['entry'].append(z)
output['values'].append(y)
return ('{0}:{1}=\"{2}\"'.format(namespace, predicate, value))
prefix = []
top = []
2017-04-02 22:06:32 +02:00
def flatten(root, prefix_keys=True):
dicts = [([], root)]
ret = {}
seen = set()
for path, d in dicts:
if id(d) in seen:
continue
seen.add(id(d))
for k, v in d.items():
new_path = path + [k]
prefix = ':'.join(new_path) if prefix_keys else k
if hasattr(v, 'items'):
dicts.append((new_path, v))
else:
p = ':'.join(prefix.rsplit(':')[:-1])
if debug:
2017-04-02 22:06:32 +02:00
print(namespace + ":" + p + "=" + v)
machineTag(namespace=namespace, predicate=p, value=prefix.split(':')[-1], expanded=v)
ret[prefix] = v
return ret
2017-04-02 22:06:32 +02:00
flatten(root=t)
print(json.dumps(output))