Merge branch 'master' of github.com:MISP/misp-taxonomies

pull/61/head
Alexandre Dulaunoy 2017-04-03 19:08:22 +02:00
commit 7f2c310d37
3 changed files with 30 additions and 18 deletions

View File

@ -50,7 +50,7 @@ for element in elements:
file_out = '../../misp-galaxy/machinetag.json' file_out = '../../misp-galaxy/machinetag.json'
with open(file_out, 'w') as f: with open(file_out, 'w') as f:
f.write(json.dumps(taxonomy, sort_keys=True, indent=4, separators=(',', ': '))) f.write(json.dumps(taxonomy, sort_keys=True, indent=4, separators=(',', ': ')))
print("JSON saved to "+ file_out) print("JSON saved to " + file_out)
# t = Taxonomy(taxonomy) # t = Taxonomy(taxonomy)

View File

@ -15,6 +15,7 @@ output['values'] = []
with open(filename) as fp: with open(filename) as fp:
t = json.load(fp) t = json.load(fp)
def lookupPredicate(predicate=False): def lookupPredicate(predicate=False):
if not predicate: if not predicate:
return False return False
@ -22,6 +23,7 @@ def lookupPredicate(predicate=False):
if p['value'] == predicate: if p['value'] == predicate:
return True return True
def lookupValues(predicate=False): def lookupValues(predicate=False):
if not predicate: if not predicate:
return False return False
@ -29,6 +31,7 @@ def lookupValues(predicate=False):
if p['predicate'] == predicate: if p['predicate'] == predicate:
return True return True
def machineTag(namespace=False, predicate=False, value=None, expanded=None): def machineTag(namespace=False, predicate=False, value=None, expanded=None):
if namespace is False or predicate is False: if namespace is False or predicate is False:
@ -54,6 +57,7 @@ def machineTag(namespace=False, predicate=False, value=None, expanded=None):
prefix = [] prefix = []
top = [] top = []
def flatten(root, prefix_keys=True): def flatten(root, prefix_keys=True):
dicts = [([], root)] dicts = [([], root)]
ret = {} ret = {}
@ -70,11 +74,12 @@ def flatten(root, prefix_keys=True):
else: else:
p = ':'.join(prefix.rsplit(':')[:-1]) p = ':'.join(prefix.rsplit(':')[:-1])
if debug: if debug:
print (namespace+":"+p+"="+v) print(namespace + ":" + p + "=" + v)
machineTag(namespace=namespace, predicate=p, value=prefix.split(':')[-1], expanded=v) machineTag(namespace=namespace, predicate=p, value=prefix.split(':')[-1], expanded=v)
ret[prefix] = v ret[prefix] = v
return ret return ret
flatten ( root = t)
print (json.dumps(output)) flatten(root=t)
print(json.dumps(output))

View File

@ -54,13 +54,16 @@ if args.a:
doc = doc + "= MISP taxonomies and classification as machine tags\n\n" doc = doc + "= MISP taxonomies and classification as machine tags\n\n"
doc = doc + "Generated from https://github.com/MISP/misp-taxonomies.\n\n" doc = doc + "Generated from https://github.com/MISP/misp-taxonomies.\n\n"
doc = doc + "\nimage::{images-cdn}misp-logo.png[MISP logo]\n" doc = doc + "\nimage::{images-cdn}misp-logo.png[MISP logo]\n"
doc = doc + "Taxonomies that can be used in MISP (2.4) and other information sharing tool and expressed in Machine Tags (Triple Tags). A machine tag is composed of a namespace (MUST), a predicate (MUST) and an (OPTIONAL) value. Machine tags are often called triple tag due to their format.\n" doc = "{} {} {} {}".format(doc, "Taxonomies that can be used in MISP (2.4) and other information sharing tool and expressed in Machine Tags (Triple Tags).",
"A machine tag is composed of a namespace (MUST), a predicate (MUST) and an (OPTIONAL) value.",
"Machine tags are often called triple tag due to their format.\n")
doc = doc + "\n\n" doc = doc + "\n\n"
if args.n: if args.n:
del taxonomies[:] del taxonomies[:]
taxonomies.append(args.n) taxonomies.append(args.n)
def asciidoc(content=False, adoc=doc, t='title'): def asciidoc(content=False, adoc=doc, t='title'):
if not args.a: if not args.a:
return False return False
@ -71,12 +74,15 @@ def asciidoc(content=False, adoc=doc, t='title'):
content = '=== ' + content content = '=== ' + content
elif t == 'namespace': elif t == 'namespace':
content = '== ' + content + '\n' content = '== ' + content + '\n'
content = content + 'NOTE: ' + namespace + ' namespace available in JSON format at https://github.com/MISP/misp-taxonomies/blob/master/' + namespace + '/machinetag.json[*this location*]. The JSON format can be freely reused in your application or automatically enabled in https://www.github.com/MISP/MISP[MISP] taxonomy.' content = "{} {} {} {} {} {} {}".format(content, 'NOTE:', namespace, 'namespace available in JSON format at https://github.com/MISP/misp-taxonomies/blob/master/',
namespace, '/machinetag.json[*this location*]. The JSON format can be freely reused in your application',
'or automatically enabled in https://www.github.com/MISP/MISP[MISP] taxonomy.')
elif t == 'description': elif t == 'description':
content = '\n'+content+'\n' content = '\n' + content + '\n'
adoc = adoc + content adoc = adoc + content
return adoc return adoc
def machineTag(namespace=False, predicate=False, value=None): def machineTag(namespace=False, predicate=False, value=None):
if namespace is False or predicate is False: if namespace is False or predicate is False:
@ -86,6 +92,7 @@ def machineTag(namespace=False, predicate=False, value=None):
else: else:
return (u'{0}:{1}=\"{2}\"'.format(namespace, predicate, value)) return (u'{0}:{1}=\"{2}\"'.format(namespace, predicate, value))
for taxonomy in taxonomies: for taxonomy in taxonomies:
filename = os.path.join(thisDir, "../", taxonomy, "machinetag.json") filename = os.path.join(thisDir, "../", taxonomy, "machinetag.json")
with open(filename) as fp: with open(filename) as fp:
@ -99,7 +106,7 @@ for taxonomy in taxonomies:
doc = asciidoc(content=t['namespace'], adoc=doc, t='namespace') doc = asciidoc(content=t['namespace'], adoc=doc, t='namespace')
doc = asciidoc(content=t['description'], adoc=doc, t='description') doc = asciidoc(content=t['description'], adoc=doc, t='description')
if args.v: if args.v:
print ('{0}'.format(t['description'])) print('{0}'.format(t['description']))
for predicate in t['predicates']: for predicate in t['predicates']:
if args.a: if args.a:
doc = asciidoc(content=predicate['value'], adoc=doc, t='predicate') doc = asciidoc(content=predicate['value'], adoc=doc, t='predicate')
@ -110,11 +117,11 @@ for taxonomy in taxonomies:
if predicate.get('description'): if predicate.get('description'):
doc = asciidoc(content=machineTag(namespace=namespace, predicate=predicate['description']), adoc=doc, t='description') doc = asciidoc(content=machineTag(namespace=namespace, predicate=predicate['description']), adoc=doc, t='description')
else: else:
print (machineTag(namespace=namespace, predicate=predicate['value'])) print(machineTag(namespace=namespace, predicate=predicate['value']))
if args.e: if args.e:
print ("--> " + machineTag(namespace=expanded_namespace, predicate=predicate['expanded'])) print("--> " + machineTag(namespace=expanded_namespace, predicate=predicate['expanded']))
if predicate.get('description'): if predicate.get('description'):
print ("--> " + predicate['description']) print("--> " + predicate['description'])
else: else:
for e in t['values']: for e in t['values']:
if e['predicate'] == predicate['value']: if e['predicate'] == predicate['value']:
@ -125,10 +132,10 @@ for taxonomy in taxonomies:
doc = asciidoc(content=machineTag(namespace=namespace, predicate=e['predicate'], value=v['value']), adoc=doc) doc = asciidoc(content=machineTag(namespace=namespace, predicate=e['predicate'], value=v['value']), adoc=doc)
doc = asciidoc(content=machineTag(namespace=namespace, predicate=v['expanded']), adoc=doc, t='description') doc = asciidoc(content=machineTag(namespace=namespace, predicate=v['expanded']), adoc=doc, t='description')
else: else:
print (machineTag(namespace=namespace, predicate=e['predicate'], value=v['value'])) print(machineTag(namespace=namespace, predicate=e['predicate'], value=v['value']))
if args.e: if args.e:
if'expanded' in v: if'expanded' in v:
print ("--> " + machineTag(namespace=namespace, predicate=expanded, value=v['expanded'])) print("--> " + machineTag(namespace=namespace, predicate=expanded, value=v['expanded']))
if args.a: if args.a:
print (doc) print(doc)