First we need to find a named regular expression that will match the filename.
Here is the one we came up with:
OUTPUT_REGEX = r'\[INF\] JSON report will be saved to (?P<output_path>)'
An integration of mytool with secator would look like:
secator/tasks/mytool.py
from secator.decorators import task
from secator.runners import Command
from secator.output_types import Url, Tag, Vulnerability
from secator.serializers import RegexSerializer
from secator.tasks._categories import Vuln
OUTPUT_REGEX = r'\[INF\] JSON report will be saved to (?P<output_path>)'
@task()
class mytool(Command):
cmd = '/home/osboxes/.local/bin/mytool'
input_flag = '-u'
json_flag = '-o json'
output_types = [Url, Vulnerability]
# Override the default item loader (JSONSerializer) with the RegexSerializer
item_loaders = [
RegexSerializer(
OUTPUT_REGEX,
fields=['output_path']
)
]
# React to items loaded by the RegexSerializer, and set the output path
@staticmethod
def on_regex_loaded(self, item):
self.output_path = item['output_path']
return
# When the command completes, load the JSON file and yield secator output types
@staticmethod
def on_cmd_done(self):
with open(self.output_path, 'r') as f:
results = f.read()
for r in results:
if r['type'] == 'url':
yield Url(
url=r['url'],
status_code=r['status'],
)
elif r['type'] == 'vulnerability':
yield Vulnerability(
id=r['cve_id'],
name=r['name'],
matched_at=r['target'],
severity=r['severity'].lower()
)