2019-05-06 22:16:14 +02:00
from pymisp import MISPEvent , MISPObject
2019-05-01 22:26:53 +02:00
import csv
import io
2018-12-11 15:29:09 +01:00
import base64
2018-01-25 15:44:08 +01:00
misperrors = { ' error ' : ' Error ' }
2024-08-12 11:23:10 +02:00
moduleinfo = {
' version ' : ' 0.2 ' ,
' author ' : ' Christian Studer ' ,
' module-type ' : [ ' import ' ] ,
' name ' : ' CSV Import ' ,
' description ' : ' Module to import MISP attributes from a csv file. ' ,
' requirements ' : [ ' PyMISP ' ] ,
' features ' : " In order to parse data from a csv file, a header is required to let the module know which column is matching with known attribute fields / MISP types. \n \n This header either comes from the csv file itself or is part of the configuration of the module and should be filled out in MISP plugin settings, each field separated by COMMAS. Fields that do not match with any type known in MISP or are not MISP attribute fields should be ignored in import, using a space or simply nothing between two separators (example: ' ip-src, , comment, ' ). \n \n If the csv file already contains a header that does not start by a ' # ' , you should tick the checkbox ' has_header ' to avoid importing it and have potential issues. You can also redefine the header even if it is already contained in the file, by following the rules for headers explained earlier. One reason why you would redefine a header is for instance when you want to skip some fields, or some fields are not valid types. " ,
' references ' : [ ' https://tools.ietf.org/html/rfc4180 ' , ' https://tools.ietf.org/html/rfc7111 ' ] ,
' input ' : ' CSV format file. ' ,
' output ' : ' MISP Event attributes ' ,
' logo ' : ' ' ,
}
2018-05-17 13:47:49 +02:00
moduleconfig = [ ]
2019-09-19 23:19:57 +02:00
userConfig = {
' header ' : {
' type ' : ' String ' ,
' message ' : ' Define the header of the csv file, with types (included in MISP attribute types or attribute fields) separated by commas. \n For fields that do not match these types or that you want to skip, please use space or simply nothing between commas. \n For instance: ip-src,domain, ,timestamp ' } ,
2018-12-11 15:29:09 +01:00
' has_header ' : {
' type ' : ' Boolean ' ,
2019-09-19 23:19:57 +02:00
' message ' : ' Tick this box ONLY if there is a header line, NOT COMMENTED, and all the fields of this header are respecting the recommendations above. ' } ,
' special_delimiter ' : {
' type ' : ' String ' ,
' message ' : ' IF THE DELIMITERS ARE NOT COMMAS, please specify which ones are used (for instance: " ; " , " | " , " / " , " \t " for tabs, etc). '
}
}
2019-05-01 22:28:19 +02:00
mispattributes = { ' userConfig ' : userConfig , ' inputSource ' : [ ' file ' ] , ' format ' : ' misp_standard ' }
2018-01-25 15:44:08 +01:00
2019-05-01 22:32:06 +02:00
misp_standard_csv_header = [ ' uuid ' , ' event_id ' , ' category ' , ' type ' , ' value ' , ' comment ' , ' to_ids ' , ' date ' ,
' object_relation ' , ' attribute_tag ' , ' object_uuid ' , ' object_name ' , ' object_meta_category ' ]
2019-05-02 14:07:36 +02:00
misp_context_additional_fields = [ ' event_info ' , ' event_member_org ' , ' event_source_org ' , ' event_distribution ' ,
' event_threat_level_id ' , ' event_analysis ' , ' event_date ' , ' event_tag ' ]
2019-05-01 22:32:06 +02:00
misp_extended_csv_header = misp_standard_csv_header + misp_context_additional_fields
2018-03-05 19:59:30 +01:00
2018-12-11 15:29:09 +01:00
2024-01-06 22:27:36 +01:00
class CsvParser :
2020-01-07 17:03:10 +01:00
def __init__ ( self , header , has_header , delimiter , data , from_misp , MISPtypes , categories ) :
2019-05-06 22:16:14 +02:00
self . misp_event = MISPEvent ( )
2019-09-19 23:19:57 +02:00
self . header = header
self . has_header = has_header
self . delimiter = delimiter
self . data = data
self . from_misp = from_misp
self . MISPtypes = MISPtypes
2020-01-07 17:03:10 +01:00
self . categories = categories
2019-09-19 23:19:57 +02:00
self . fields_number = len ( self . header )
2020-01-07 17:03:10 +01:00
self . __score_mapping = { 0 : self . __create_standard_attribute ,
2019-09-19 23:19:57 +02:00
1 : self . __create_attribute_with_ids ,
2 : self . __create_attribute_with_tags ,
2020-01-07 17:03:10 +01:00
3 : self . __create_attribute_with_ids_and_tags ,
4 : self . __create_attribute_check_category ,
5 : self . __create_attribute_check_category_and_ids ,
6 : self . __create_attribute_check_category_and_tags ,
7 : self . __create_attribute_check_category_with_ids_and_tags }
2019-09-19 23:19:57 +02:00
def parse_csv ( self ) :
if self . from_misp :
if self . header == misp_standard_csv_header :
self . __parse_misp_csv ( )
2018-07-27 23:13:47 +02:00
else :
2019-09-19 23:19:57 +02:00
attribute_fields = misp_standard_csv_header [ : 1 ] + misp_standard_csv_header [ 2 : 10 ]
object_fields = [ ' object_id ' ] + misp_standard_csv_header [ 10 : ]
attribute_indexes = [ ]
object_indexes = [ ]
for i in range ( len ( self . header ) ) :
if self . header [ i ] in attribute_fields :
attribute_indexes . append ( i )
elif self . header [ i ] in object_fields :
object_indexes . append ( i )
if object_indexes :
if not any ( field in self . header for field in ( ' object_uuid ' , ' object_id ' ) ) or ' object_name ' not in self . header :
for line in self . data :
for index in object_indexes :
if line [ index ] . strip ( ) :
return { ' error ' : ' It is not possible to import MISP objects from your csv file if you do not specify any object identifier and object name to separate each object from each other. ' }
if ' object_relation ' not in self . header :
return { ' error ' : ' In order to import MISP objects, an object relation for each attribute contained in an object is required. ' }
self . __build_misp_event ( attribute_indexes , object_indexes )
2018-07-27 23:13:47 +02:00
else :
2024-01-06 22:27:36 +01:00
attribute_fields = misp_standard_csv_header [ : 1 ] + misp_standard_csv_header [ 2 : 9 ]
2019-09-19 23:19:57 +02:00
attribute_indexes = [ ]
types_indexes = [ ]
for i in range ( len ( self . header ) ) :
if self . header [ i ] in attribute_fields :
attribute_indexes . append ( i )
elif self . header [ i ] in self . MISPtypes :
types_indexes . append ( i )
self . __parse_external_csv ( attribute_indexes , types_indexes )
self . __finalize_results ( )
return { ' success ' : 1 }
################################################################################
2019-10-03 17:10:47 +02:00
# Parsing csv data with MISP fields, #
# but a custom header #
2019-09-19 23:19:57 +02:00
################################################################################
def __build_misp_event ( self , attribute_indexes , object_indexes ) :
score = self . __get_score ( )
if object_indexes :
objects = { }
id_name = ' object_id ' if ' object_id ' in self . header else ' object_uuid '
object_id_index = self . header . index ( id_name )
name_index = self . header . index ( ' object_name ' )
for line in self . data :
attribute = self . __score_mapping [ score ] ( line , attribute_indexes )
object_id = line [ object_id_index ]
if object_id :
if object_id not in objects :
misp_object = MISPObject ( line [ name_index ] )
if id_name == ' object_uuid ' :
misp_object . uuid = object_id
objects [ object_id ] = misp_object
objects [ object_id ] . add_attribute ( * * attribute )
else :
self . event . add_attribute ( * * attribute )
for misp_object in objects . values ( ) :
self . misp_event . add_object ( * * misp_object )
2018-05-18 11:38:13 +02:00
else :
2019-09-19 23:19:57 +02:00
for line in self . data :
attribute = self . __score_mapping [ score ] ( line , attribute_indexes )
self . misp_event . add_attribute ( * * attribute )
2018-03-05 19:59:30 +01:00
2019-09-19 23:19:57 +02:00
################################################################################
2019-10-03 17:10:47 +02:00
# Parsing csv data containing fields that are not #
# MISP attributes or objects standard fields #
# (but should be MISP attribute types!!) #
2019-09-19 23:19:57 +02:00
################################################################################
def __parse_external_csv ( self , attribute_indexes , types_indexes ) :
score = self . __get_score ( )
if attribute_indexes :
for line in self . data :
2019-10-03 16:03:30 +02:00
try :
base_attribute = self . __score_mapping [ score ] ( line , attribute_indexes )
except IndexError :
continue
2019-09-19 23:19:57 +02:00
for index in types_indexes :
attribute = { ' type ' : self . header [ index ] , ' value ' : line [ index ] }
attribute . update ( base_attribute )
self . misp_event . add_attribute ( * * attribute )
2018-07-27 23:13:47 +02:00
else :
2019-09-19 23:19:57 +02:00
for line in self . data :
for index in types_indexes :
self . misp_event . add_attribute ( * * { ' type ' : self . header [ index ] , ' value ' : line [ index ] } )
2018-07-27 23:13:47 +02:00
2019-09-19 23:19:57 +02:00
################################################################################
2019-10-03 17:10:47 +02:00
# Parsing standard MISP csv format #
2019-09-19 23:19:57 +02:00
################################################################################
def __parse_misp_csv ( self ) :
2019-05-06 22:16:14 +02:00
objects = { }
2019-09-19 23:19:57 +02:00
attribute_fields = self . header [ : 1 ] + self . header [ 2 : 8 ]
2018-07-27 23:13:47 +02:00
for line in self . data :
2019-09-19 23:19:57 +02:00
a_uuid , _ , category , _type , value , comment , ids , timestamp , relation , tag , o_uuid , name , _ = line [ : self . fields_number ]
attribute = { t : v . strip ( ' " ' ) for t , v in zip ( attribute_fields , ( a_uuid , category , _type , value , comment , ids , timestamp ) ) }
2019-10-03 16:02:25 +02:00
attribute [ ' to_ids ' ] = True if attribute [ ' to_ids ' ] == ' 1 ' else False
2019-05-06 22:16:14 +02:00
if tag :
attribute [ ' Tag ' ] = [ { ' name ' : t . strip ( ) } for t in tag . split ( ' , ' ) ]
2018-07-27 23:13:47 +02:00
if relation :
2019-05-06 22:16:14 +02:00
if o_uuid not in objects :
2019-09-19 23:19:57 +02:00
objects [ o_uuid ] = MISPObject ( name )
2019-05-06 22:16:14 +02:00
objects [ o_uuid ] . add_attribute ( relation , * * attribute )
2018-07-27 23:13:47 +02:00
else :
2019-05-06 22:16:14 +02:00
self . misp_event . add_attribute ( * * attribute )
for uuid , misp_object in objects . items ( ) :
misp_object . uuid = uuid
self . misp_event . add_object ( * * misp_object )
2018-01-25 15:44:08 +01:00
2019-09-19 23:19:57 +02:00
################################################################################
2019-10-03 17:10:47 +02:00
# Utility functions #
2019-09-19 23:19:57 +02:00
################################################################################
2020-01-07 17:03:10 +01:00
def __create_attribute_check_category ( self , line , indexes ) :
attribute = self . __create_standard_attribute ( line , indexes )
self . __check_category ( attribute )
return attribute
def __create_attribute_check_category_and_ids ( self , line , indexes ) :
attribute = self . __create_attribute_with_ids ( line , indexes )
self . __check_category ( attribute )
return attribute
def __create_attribute_check_category_and_tags ( self , line , indexes ) :
attribute = self . __create_attribute_with_tags ( line , indexes )
self . __check_category ( attribute )
return attribute
def __create_attribute_check_category_with_ids_and_tags ( self , line , indexes ) :
attribute = self . __create_attribute_with_ids_and_tags ( line , indexes )
self . __check_category ( attribute )
return attribute
2019-09-19 23:19:57 +02:00
def __create_attribute_with_ids ( self , line , indexes ) :
2020-01-07 17:03:10 +01:00
attribute = self . __create_standard_attribute ( line , indexes )
self . __deal_with_ids ( attribute )
return attribute
2019-09-19 23:19:57 +02:00
def __create_attribute_with_ids_and_tags ( self , line , indexes ) :
2020-01-07 17:03:10 +01:00
attribute = self . __create_standard_attribute ( line , indexes )
self . __deal_with_ids ( attribute )
self . __deal_with_tags ( attribute )
return attribute
2019-09-19 23:19:57 +02:00
def __create_attribute_with_tags ( self , line , indexes ) :
2020-01-07 17:03:10 +01:00
attribute = self . __create_standard_attribute ( line , indexes )
self . __deal_with_tags ( attribute )
return attribute
2019-09-19 23:19:57 +02:00
2020-01-07 17:03:10 +01:00
def __create_standard_attribute ( self , line , indexes ) :
2019-09-19 23:19:57 +02:00
return { self . header [ index ] : line [ index ] for index in indexes if line [ index ] }
2020-01-07 17:03:10 +01:00
def __check_category ( self , attribute ) :
category = attribute [ ' category ' ]
if category in self . categories :
return
if category . capitalize ( ) in self . categories :
attribute [ ' category ' ] = category . capitalize ( )
return
del attribute [ ' category ' ]
2019-09-19 23:19:57 +02:00
@staticmethod
def __deal_with_ids ( attribute ) :
attribute [ ' to_ids ' ] = True if attribute [ ' to_ids ' ] == ' 1 ' else False
@staticmethod
def __deal_with_tags ( attribute ) :
2021-12-30 15:25:44 +01:00
if ' Tag ' in attribute . keys ( ) :
attribute [ ' Tag ' ] = [ { ' name ' : tag . strip ( ) } for tag in attribute [ ' Tag ' ] . split ( ' , ' ) ]
2019-09-19 23:19:57 +02:00
def __get_score ( self ) :
score = 1 if ' to_ids ' in self . header else 0
if ' attribute_tag ' in self . header :
score + = 2
2020-01-07 17:03:10 +01:00
if ' category ' in self . header :
score + = 4
2019-09-19 23:19:57 +02:00
return score
def __finalize_results ( self ) :
2024-01-06 22:27:36 +01:00
event = self . misp_event . to_dict ( )
2019-05-06 22:16:14 +02:00
self . results = { key : event [ key ] for key in ( ' Attribute ' , ' Object ' ) if ( key in event and event [ key ] ) }
2018-12-11 15:29:09 +01:00
2019-09-19 23:19:57 +02:00
def __any_mandatory_misp_field ( header ) :
return any ( field in header for field in ( ' type ' , ' value ' ) )
def __special_parsing ( data , delimiter ) :
2020-07-01 11:27:36 +02:00
return list ( tuple ( part . strip ( ) for part in line [ 0 ] . split ( delimiter ) ) for line in csv . reader ( io . TextIOWrapper ( io . BytesIO ( data . encode ( ) ) , encoding = ' utf-8 ' ) ) if line and not line [ 0 ] . startswith ( ' # ' ) )
2019-09-19 23:19:57 +02:00
def __standard_parsing ( data ) :
2020-06-30 23:10:35 +02:00
return list ( tuple ( part . strip ( ) for part in line ) for line in csv . reader ( io . TextIOWrapper ( io . BytesIO ( data . encode ( ) ) , encoding = ' utf-8 ' ) ) if line and not line [ 0 ] . startswith ( ' # ' ) )
2019-09-19 23:19:57 +02:00
2024-01-06 22:27:36 +01:00
def dict_handler ( request : dict ) :
2018-03-02 09:03:51 +01:00
if request . get ( ' data ' ) :
2020-03-12 11:02:43 +01:00
try :
2020-03-18 10:24:06 +01:00
data = base64 . b64decode ( request [ ' data ' ] ) . decode ( ' utf-8 ' )
2020-03-12 11:02:43 +01:00
except UnicodeDecodeError :
2020-03-18 10:24:06 +01:00
misperrors [ ' error ' ] = " Input is not valid UTF-8 "
return misperrors
2018-01-25 15:44:08 +01:00
else :
misperrors [ ' error ' ] = " Unsupported attributes type "
return misperrors
2018-05-18 11:33:53 +02:00
has_header = request [ ' config ' ] . get ( ' has_header ' )
has_header = True if has_header == ' 1 ' else False
2019-09-19 23:19:57 +02:00
header = request [ ' config ' ] [ ' header ' ] . split ( ' , ' ) if request [ ' config ' ] . get ( ' header ' ) . strip ( ) else [ ]
delimiter = request [ ' config ' ] [ ' special_delimiter ' ] if request [ ' config ' ] . get ( ' special_delimiter ' ) . strip ( ) else ' , '
data = __standard_parsing ( data ) if delimiter == ' , ' else __special_parsing ( data , delimiter )
if not header :
2018-07-27 23:13:47 +02:00
if has_header :
2019-09-19 23:19:57 +02:00
header = data . pop ( 0 )
2018-07-27 23:13:47 +02:00
else :
2019-09-19 23:19:57 +02:00
misperrors [ ' error ' ] = " Configuration error. Provide a header or use the one within the csv file and tick the checkbox ' Has_header ' . "
return misperrors
else :
header = [ h . strip ( ) for h in header ]
if has_header :
del data [ 0 ]
if header == misp_standard_csv_header or header == misp_extended_csv_header :
header = misp_standard_csv_header
2024-01-06 22:27:36 +01:00
description = MISPEvent ( ) . describe_types
misp_types = description [ ' types ' ]
2019-09-19 23:19:57 +02:00
for h in header :
2024-01-06 22:27:36 +01:00
if not any ( ( h in misp_types , h in misp_extended_csv_header , h in ( ' ' , ' ' , ' _ ' , ' object_id ' ) ) ) :
2019-09-19 23:19:57 +02:00
misperrors [ ' error ' ] = ' Wrong header field: {} . Please use a header value that can be recognized by MISP (or alternatively skip it using a whitespace). ' . format ( h )
return misperrors
from_misp = all ( ( h in misp_extended_csv_header or h in ( ' ' , ' ' , ' _ ' , ' object_id ' ) for h in header ) )
if from_misp :
if not __any_mandatory_misp_field ( header ) :
misperrors [ ' error ' ] = ' Please make sure the data you try to import can be identified with a type/value combinaison. '
2018-07-27 23:13:47 +02:00
return misperrors
else :
2019-09-19 23:19:57 +02:00
if __any_mandatory_misp_field ( header ) :
wrong_types = tuple ( wrong_type for wrong_type in ( ' type ' , ' value ' ) if wrong_type in header )
misperrors [ ' error ' ] = ' Error with the following header: {} . It contains the following field(s): {} , which is(are) already provided by the usage of at least on MISP attribute type in the header. ' . format ( header , ' and ' . join ( wrong_types ) )
return misperrors
2024-01-06 22:27:36 +01:00
csv_parser = CsvParser ( header , has_header , delimiter , data , from_misp , misp_types , description [ ' categories ' ] )
2018-01-30 11:20:28 +01:00
# build the attributes
2019-09-19 23:19:57 +02:00
result = csv_parser . parse_csv ( )
if ' error ' in result :
return result
2019-05-06 22:16:14 +02:00
return { ' results ' : csv_parser . results }
2018-01-30 11:20:28 +01:00
2018-12-11 15:29:09 +01:00
2018-01-25 15:44:08 +01:00
def introspection ( ) :
2019-05-01 22:28:19 +02:00
return mispattributes
2018-01-25 15:44:08 +01:00
2018-12-11 15:29:09 +01:00
2018-01-25 15:44:08 +01:00
def version ( ) :
moduleinfo [ ' config ' ] = moduleconfig
return moduleinfo