2018-02-21 15:28:11 +00:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import re
import os
import argparse
2018-03-22 13:20:11 +00:00
parser = argparse . ArgumentParser ( description = ' Create a couple galaxy/cluster with cti \' s relationship \n Must be in the mitre/cti/enterprise-attack/relationship folder ' )
2018-02-21 15:28:11 +00:00
parser . add_argument ( " -v " , " --version " , type = int , required = True , help = " Version of the galaxy. Please increment the previous one " )
args = parser . parse_args ( )
values = [ ]
path = " relationship/ "
for element in os . listdir ( path ) :
with open ( path + element ) as json_data :
d = json . load ( json_data )
json_data . close ( )
2018-04-03 13:53:17 +00:00
2018-02-21 15:28:11 +00:00
temp = d [ ' objects ' ] [ 0 ]
source = temp [ ' source_ref ' ]
target = temp [ ' target_ref ' ]
relationship = temp [ ' relationship_type ' ]
if source . startswith ( ' attack-pattern ' ) :
paths = " attack-pattern/ "
elif source . startswith ( ' course-of-action ' ) :
paths = " course-of-action/ "
elif source . startswith ( ' identity ' ) :
paths = " identity/ "
elif source . startswith ( ' intrusion-set ' ) :
paths = " intrusion-set/ "
elif source . startswith ( ' malware ' ) :
paths = " malware/ "
elif source . startswith ( ' marking-definition ' ) :
paths = " marking-definition/ "
elif source . startswith ( ' tool ' ) :
paths = " tool/ "
else :
print ( ' Invalid value ' )
continue
with open ( paths + source + ' .json ' ) as json_data :
s = json . load ( json_data )
json_data . close ( )
if target . startswith ( ' attack-pattern ' ) :
patht = " attack-pattern/ "
elif target . startswith ( ' course-of-action ' ) :
patht = " course-of-action/ "
elif target . startswith ( ' identity ' ) :
patht = " identity/ "
elif target . startswith ( ' intrusion-set ' ) :
patht = " intrusion-set/ "
elif target . startswith ( ' malware ' ) :
patht = " malware/ "
elif target . startswith ( ' marking-definition ' ) :
patht = " marking-definition/ "
elif target . startswith ( ' tool ' ) :
patht = " tool/ "
else :
print ( ' Invalid value ' )
continue
with open ( patht + target + ' .json ' ) as json_data :
t = json . load ( json_data )
json_data . close ( )
value = { }
value [ ' meta ' ] = { }
value [ ' uuid ' ] = re . search ( ' --(.*)$ ' , temp [ ' id ' ] ) . group ( 0 ) [ 2 : ]
value [ ' meta ' ] [ ' source-uuid ' ] = re . search ( ' --(.*)$ ' , s [ ' objects ' ] [ 0 ] [ ' id ' ] ) . group ( 0 ) [ 2 : ]
value [ ' meta ' ] [ ' target-uuid ' ] = re . search ( ' --(.*)$ ' , t [ ' objects ' ] [ 0 ] [ ' id ' ] ) . group ( 0 ) [ 2 : ]
2018-04-03 13:53:17 +00:00
value [ ' value ' ] = s [ ' objects ' ] [ 0 ] [ ' name ' ] + ' ( ' + s [ ' objects ' ] [ 0 ] [ ' external_references ' ] [ 0 ] [ ' external_id ' ] + ' ) ' + relationship + ' ' + t [ ' objects ' ] [ 0 ] [ ' name ' ] + ' ( ' + t [ ' objects ' ] [ 0 ] [ ' external_references ' ] [ 0 ] [ ' external_id ' ] + ' ) '
# value['value'] = s['objects'][0]['name'] + ' ' + relationship + ' ' + t['objects'][0]['name']
2018-02-21 15:28:11 +00:00
values . append ( value )
galaxy = { }
2018-03-22 13:20:11 +00:00
galaxy [ ' name ' ] = " Enterprise Attack - Relationship "
galaxy [ ' type ' ] = " mitre-enterprise-attack-relationship "
2018-02-21 15:28:11 +00:00
galaxy [ ' description ' ] = " Mitre Relationship "
galaxy [ ' uuid ' ] = " fc404638-1707-11e8-a5cf-b78b9b562766 "
galaxy [ ' version ' ] = args . version
galaxy [ ' icon ' ] = " link "
2018-05-20 07:36:35 +00:00
galaxy [ ' namespace ' ] = " mitre-attack "
2018-02-21 15:28:11 +00:00
2018-04-03 13:53:17 +00:00
cluster = { }
2018-03-22 13:20:11 +00:00
cluster [ ' name ' ] = " Enterprise Attack - Relationship "
cluster [ ' type ' ] = " mitre-enterprise-attack-relationship "
2018-02-21 15:28:11 +00:00
cluster [ ' description ' ] = " MITRE Relationship "
cluster [ ' version ' ] = args . version
cluster [ ' source ' ] = " https://github.com/mitre/cti "
cluster [ ' uuid ' ] = " fc605f90-1707-11e8-9d6a-9f165ac2ab5c "
cluster [ ' authors ' ] = [ " MITRE " ]
cluster [ ' values ' ] = values
2018-03-22 13:20:11 +00:00
with open ( ' generate/galaxies/mitre-enterprise-attack-relationship.json ' , ' w ' ) as galaxy_file :
2018-02-21 15:28:11 +00:00
json . dump ( galaxy , galaxy_file , indent = 4 )
2018-03-22 13:20:11 +00:00
with open ( ' generate/clusters/mitre-enterprise-attack-relationship.json ' , ' w ' ) as cluster_file :
2018-02-21 15:28:11 +00:00
json . dump ( cluster , cluster_file , indent = 4 )