Merge pull request #167 from chrisr3d/csvimport

Updated csvimport
pull/169/head
Alexandre Dulaunoy 2018-03-02 09:15:54 +01:00 committed by GitHub
commit 8ad11e4be1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 25 deletions

View File

@ -1,9 +1,9 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import json, os import json, os, base64
import pymisp import pymisp
misperrors = {'error': 'Error'} misperrors = {'error': 'Error'}
mispattributes = {'input': ['file'], 'output': ['MISP attributes']} mispattributes = {'inputSource': ['file'], 'output': ['MISP attributes']}
moduleinfo = {'version': '0.1', 'author': 'Christian Studer', moduleinfo = {'version': '0.1', 'author': 'Christian Studer',
'description': 'Import Attributes from a csv file.', 'description': 'Import Attributes from a csv file.',
'module-type': ['import']} 'module-type': ['import']}
@ -16,39 +16,31 @@ def handler(q=False):
if q is False: if q is False:
return False return False
request = json.loads(q) request = json.loads(q)
if request.get('file'): if request.get('data'):
filename = request['file'] data = base64.b64decode(request['data']).decode('utf-8')
else: else:
misperrors['error'] = "Unsupported attributes type" misperrors['error'] = "Unsupported attributes type"
return misperrors return misperrors
if not request.get('config') and not request['config'].get('header'): if not request.get('config') and not request['config'].get('header'):
misperrors['error'] = "Configuration error" misperrors['error'] = "Configuration error"
return misperrors return misperrors
config = request['config'].get('header') config = request['config'].get('header').split(',')
#header = [] config = [c.strip() for c in config]
try: data = parse_data(data.split('\n'))
data = readFile(filename, 'utf-8')
except:
data = readFile(filename, 'iso-8859-1')
# find which delimiter is used # find which delimiter is used
delimiter, length = findDelimiter(config, data) delimiter, length = findDelimiter(config, data)
# build the attributes # build the attributes
result = buildAttributes(config, data, delimiter, length) result = buildAttributes(config, data, delimiter, length)
r = {'results': [{'types': mispattributes['output'], 'values': result}]} r = {'results': result}
return r return r
def readFile(filename, encoding): def parse_data(data):
data = [] return_data = []
with open(filename, 'r', encoding=encoding) as f: for line in data:
for line in f: l = line.split('#')[0].strip() if '#' in line else line.strip()
# split comments from data if l:
if '#' in line: return_data.append(l)
l = line.split('#')[0].strip() return return_data
else:
l = line.strip()
if l:
data.append(l)
return data
def findDelimiter(header, data): def findDelimiter(header, data):
n = len(header) n = len(header)
@ -74,7 +66,7 @@ def buildAttributes(header, dataValues, delimiter, length):
for data in dataValues: for data in dataValues:
d = data.strip() d = data.strip()
if d: if d:
attributes.append({'type': mispType, 'value': d}) 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 = findMispTypes(header)
@ -90,7 +82,7 @@ def buildAttributes(header, dataValues, delimiter, length):
datamisp.append(datasplit.pop(l).strip()) datamisp.append(datasplit.pop(l).strip())
# for each misp type, we create an attribute # for each misp type, we create an attribute
for m, dm in zip(misp, datamisp): for m, dm in zip(misp, datamisp):
attribute = {'type': m, 'value': dm} attribute = {'types': m, 'values': dm}
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()