2018-07-02 11:38:33 +02:00
|
|
|
import json
|
2018-07-02 12:14:21 +02:00
|
|
|
try:
|
|
|
|
from stix2patterns.validator import run_validator
|
2018-09-08 02:53:15 +02:00
|
|
|
except ImportError:
|
2018-07-02 12:14:21 +02:00
|
|
|
print("stix2 patterns python library is missing, use 'pip3 install stix2-patterns' to install it.")
|
2018-07-02 11:38:33 +02:00
|
|
|
|
|
|
|
misperrors = {'error': 'Error'}
|
|
|
|
mispattributes = {'input': ['stix2-pattern'], 'output': ['text']}
|
|
|
|
moduleinfo = {'version': '0.1', 'author': 'Christian Studer', 'module-type': ['expansion', 'hover'],
|
|
|
|
'description': 'An expansion hover module to perform a syntax check on stix2 patterns.'}
|
|
|
|
moduleconfig = []
|
|
|
|
|
2018-12-11 15:29:09 +01:00
|
|
|
|
2018-07-02 11:38:33 +02:00
|
|
|
def handler(q=False):
|
|
|
|
if q is False:
|
|
|
|
return False
|
|
|
|
request = json.loads(q)
|
|
|
|
if not request.get('stix2-pattern'):
|
|
|
|
misperrors['error'] = 'STIX2 pattern missing'
|
|
|
|
return misperrors
|
|
|
|
pattern = request.get('stix2-pattern')
|
|
|
|
syntax_errors = []
|
|
|
|
for p in pattern[2:-2].split(' AND '):
|
|
|
|
syntax_validator = run_validator("[{}]".format(p))
|
|
|
|
if syntax_validator:
|
|
|
|
for error in syntax_validator:
|
|
|
|
syntax_errors.append(error)
|
|
|
|
if syntax_errors:
|
|
|
|
s = 's' if len(syntax_errors) > 1 else ''
|
|
|
|
s_errors = ""
|
2018-12-11 15:29:09 +01:00
|
|
|
for error in syntax_errors:
|
2018-07-02 11:38:33 +02:00
|
|
|
s_errors += "{}\n".format(error[6:])
|
|
|
|
result = "Syntax error{}: \n{}".format(s, s_errors[:-1])
|
|
|
|
else:
|
|
|
|
result = "Syntax valid"
|
|
|
|
return {'results': [{'types': mispattributes['output'], 'values': result}]}
|
|
|
|
|
2018-12-11 15:29:09 +01:00
|
|
|
|
2018-07-02 11:38:33 +02:00
|
|
|
def introspection():
|
|
|
|
return mispattributes
|
|
|
|
|
2018-12-11 15:29:09 +01:00
|
|
|
|
2018-07-02 11:38:33 +02:00
|
|
|
def version():
|
|
|
|
moduleinfo['config'] = moduleconfig
|
|
|
|
return moduleinfo
|