mirror of https://github.com/MISP/misp-modules
Clarified functions arguments using a class
parent
4d7642ac91
commit
d885286792
|
@ -10,43 +10,28 @@ moduleinfo = {'version': '0.1', 'author': 'Christian Studer',
|
||||||
moduleconfig = ['header']
|
moduleconfig = ['header']
|
||||||
|
|
||||||
duplicatedFields = {'mispType': {'mispComment': 'comment'},
|
duplicatedFields = {'mispType': {'mispComment': 'comment'},
|
||||||
'attrField': {'eventComment': 'comment'}}
|
'attrField': {'attrComment': 'comment'}}
|
||||||
|
|
||||||
def handler(q=False):
|
class CsvParser():
|
||||||
if q is False:
|
def __init__(self, header):
|
||||||
return False
|
self.header = header
|
||||||
request = json.loads(q)
|
self.attributes = []
|
||||||
if request.get('data'):
|
|
||||||
data = base64.b64decode(request['data']).decode('utf-8')
|
|
||||||
else:
|
|
||||||
misperrors['error'] = "Unsupported attributes type"
|
|
||||||
return misperrors
|
|
||||||
if not request.get('config') and not request['config'].get('header'):
|
|
||||||
misperrors['error'] = "Configuration error"
|
|
||||||
return misperrors
|
|
||||||
config = request['config'].get('header').split(',')
|
|
||||||
config = [c.strip() for c in config]
|
|
||||||
data = parse_data(data.split('\n'))
|
|
||||||
# find which delimiter is used
|
|
||||||
delimiter, length = findDelimiter(config, data)
|
|
||||||
# build the attributes
|
|
||||||
result = buildAttributes(config, data, delimiter, length)
|
|
||||||
r = {'results': result}
|
|
||||||
return r
|
|
||||||
|
|
||||||
def parse_data(data):
|
def parse_data(self, data):
|
||||||
return_data = []
|
return_data = []
|
||||||
for line in data:
|
for line in data:
|
||||||
l = line.split('#')[0].strip() if '#' in line else line.strip()
|
l = line.split('#')[0].strip() if '#' in line else line.strip()
|
||||||
if l:
|
if l:
|
||||||
return_data.append(l)
|
return_data.append(l)
|
||||||
return return_data
|
self.data = return_data
|
||||||
|
# find which delimiter is used
|
||||||
|
self.delimiter, self.length = self.findDelimiter()
|
||||||
|
|
||||||
def findDelimiter(header, data):
|
def findDelimiter(self):
|
||||||
n = len(header)
|
n = len(self.header)
|
||||||
if n > 1:
|
if n > 1:
|
||||||
tmpData = []
|
tmpData = []
|
||||||
for da in data:
|
for da in self.data:
|
||||||
tmp = []
|
tmp = []
|
||||||
for d in (';', '|', '/', ',', '\t', ' ',):
|
for d in (';', '|', '/', ',', '\t', ' ',):
|
||||||
if da.count(d) == (n-1):
|
if da.count(d) == (n-1):
|
||||||
|
@ -58,24 +43,23 @@ def findDelimiter(header, data):
|
||||||
else:
|
else:
|
||||||
return None, 1
|
return None, 1
|
||||||
|
|
||||||
def buildAttributes(header, dataValues, delimiter, length):
|
def buildAttributes(self):
|
||||||
attributes = []
|
|
||||||
# if there is only 1 field of data
|
# if there is only 1 field of data
|
||||||
if delimiter is None:
|
if self.delimiter is None:
|
||||||
mispType = header[0]
|
mispType = self.header[0]
|
||||||
for data in dataValues:
|
for data in self.data:
|
||||||
d = data.strip()
|
d = data.strip()
|
||||||
if d:
|
if d:
|
||||||
attributes.append({'types': mispType, 'values': d})
|
self.attributes.append({'types': mispType, 'values': d})
|
||||||
else:
|
else:
|
||||||
# split fields that should be recognized as misp attribute types from the others
|
# split fields that should be recognized as misp attribute types from the others
|
||||||
list2pop, misp, head = findMispTypes(header)
|
list2pop, misp, head = self.findMispTypes()
|
||||||
# for each line of data
|
# for each line of data
|
||||||
for data in dataValues:
|
for data in self.data:
|
||||||
datamisp = []
|
datamisp = []
|
||||||
datasplit = data.split(delimiter)
|
datasplit = data.split(self.delimiter)
|
||||||
# in case there is an empty line or an error
|
# in case there is an empty line or an error
|
||||||
if len(datasplit) != length:
|
if len(datasplit) != self.length:
|
||||||
continue
|
continue
|
||||||
# pop from the line data that matches with a misp type, using the list of indexes
|
# pop from the line data that matches with a misp type, using the list of indexes
|
||||||
for l in list2pop:
|
for l in list2pop:
|
||||||
|
@ -86,18 +70,17 @@ def buildAttributes(header, dataValues, delimiter, length):
|
||||||
for h, ds in zip(head, datasplit):
|
for h, ds in zip(head, datasplit):
|
||||||
if h:
|
if h:
|
||||||
attribute[h] = ds.strip()
|
attribute[h] = ds.strip()
|
||||||
attributes.append(attribute)
|
self.attributes.append(attribute)
|
||||||
return attributes
|
|
||||||
|
|
||||||
def findMispTypes(header):
|
def findMispTypes(self):
|
||||||
descFilename = os.path.join(pymisp.__path__[0], 'data/describeTypes.json')
|
descFilename = os.path.join(pymisp.__path__[0], 'data/describeTypes.json')
|
||||||
with open(descFilename, 'r') as f:
|
with open(descFilename, 'r') as f:
|
||||||
MispTypes = json.loads(f.read())['result'].get('types')
|
MispTypes = json.loads(f.read())['result'].get('types')
|
||||||
list2pop = []
|
list2pop = []
|
||||||
misp = []
|
misp = []
|
||||||
head = []
|
head = []
|
||||||
for h in reversed(header):
|
for h in reversed(self.header):
|
||||||
n = header.index(h)
|
n = self.header.index(h)
|
||||||
# fields that are misp attribute types
|
# fields that are misp attribute types
|
||||||
if h in MispTypes:
|
if h in MispTypes:
|
||||||
list2pop.append(n)
|
list2pop.append(n)
|
||||||
|
@ -116,6 +99,27 @@ def findMispTypes(header):
|
||||||
# return list of indexes of the misp types, list of the misp types, remaining fields that will be attribute fields
|
# return list of indexes of the misp types, list of the misp types, remaining fields that will be attribute fields
|
||||||
return list2pop, misp, list(reversed(head))
|
return list2pop, misp, list(reversed(head))
|
||||||
|
|
||||||
|
def handler(q=False):
|
||||||
|
if q is False:
|
||||||
|
return False
|
||||||
|
request = json.loads(q)
|
||||||
|
if request.get('data'):
|
||||||
|
data = base64.b64decode(request['data']).decode('utf-8')
|
||||||
|
else:
|
||||||
|
misperrors['error'] = "Unsupported attributes type"
|
||||||
|
return misperrors
|
||||||
|
if not request.get('config') and not request['config'].get('header'):
|
||||||
|
misperrors['error'] = "Configuration error"
|
||||||
|
return misperrors
|
||||||
|
config = request['config'].get('header').split(',')
|
||||||
|
config = [c.strip() for c in config]
|
||||||
|
csv_parser = CsvParser(config)
|
||||||
|
csv_parser.parse_data(data.split('\n'))
|
||||||
|
# build the attributes
|
||||||
|
csv_parser.buildAttributes()
|
||||||
|
r = {'results': csv_parser.attributes}
|
||||||
|
return r
|
||||||
|
|
||||||
def introspection():
|
def introspection():
|
||||||
return mispattributes
|
return mispattributes
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue