From ad2c8cb2472b68f3e094f1910f2a2b1ed9331399 Mon Sep 17 00:00:00 2001 From: Shane Peters Date: Fri, 11 Jan 2019 10:22:04 -0500 Subject: [PATCH] initial commit --- LICENSE | 19 ++++++++++ README.md | 20 +++++++++++ hivealert.py | 92 +++++++++++++++++++++++++++++++++++++++++++++++ hivealert.service | 12 +++++++ requirements.txt | 3 ++ 5 files changed, 146 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 hivealert.py create mode 100644 hivealert.service create mode 100644 requirements.txt diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9cf1062 --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +MIT License + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..40c1956 --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# What is it +Hivealert is a simple HTTP callback server to generate alerts in TheHive. This began as a need to generate TheHive alerts from Graylog using an HTTP callback and didn't have time to learn how to build graylog plugins. This has the added benefit of being called from anywhere to generate an alert, just add a route for it. + +# Install +```bash +export HH=/opt/hivealert +sudo useradd hivealert -k /dev/null -m -d $HH +sudo -H -u hivealert git clone https://gitlab.com/shanerade/hivealert.git $HH/hivealert +cd $HH/hivealert +sudo -H -u hivealert pip3 install -r requirements.txt +sudo cp hivealert.service /etc/systemd/system +sudo systemctl daemon-reload +sudo systemctl enable hivealert && sudo systemctl start hivealert +``` +# Note: Be sure to set the set the following values in `hivealert.py`: +``` +GRAYLOG = 'GRAYLOG_URL' +HIVE = 'HIVE_URL' +HIVE_API_KEY = 'HIVE_API_KEY' +``` diff --git a/hivealert.py b/hivealert.py new file mode 100644 index 0000000..310926f --- /dev/null +++ b/hivealert.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python3 + +import uuid +import json +import time +import logging +import requests +import pprint +from tempfile import NamedTemporaryFile +from base64 import b64decode +from flask import Flask, request, Response +from thehive4py.api import TheHiveApi +from thehive4py.models import Alert, AlertArtifact, CustomFieldHelper + +app = Flask(__name__) + +DEBUG_LEVEL = logging.DEBUG # DEBUG, INFO, ERROR, WARNING +GRAYLOG = 'GRAYLOG_URL' +HIVE = 'HIVE_URL' +HIVE_API_KEY = 'HIVE_API_KEY' +LOG_FORMAT = '%(asctime)-15s %(message)s' +api = TheHiveApi(HIVE, HIVE_API_KEY) +logging.basicConfig(format=LOG_FORMAT, level=DEBUG_LEVEL) + + +@app.route('/alertsuricata', methods=['POST']) +def create_alert(): + try: + raw = json.loads(request.data.decode()) + log = raw['check_result']['matching_messages'][0] + alert = log['fields'] + except Exception as e: + logging.debug(e) + if 'Dummy alert' in str(request.data): + logging.debug('Test alert detected.') + return Response(status=200) + else: + logging.debug('Unable to parse message: ' + request.data.decode()) + return Response(status=503) + + logging.info("New Suricata alert received!") + logging.debug(alert) + + artifacts = [] + + # Attach original alert as artifact + alert_file = NamedTemporaryFile('w+t', prefix='alert_', suffix='.txt') + alert_file.write(json.dumps(alert, indent=2)) + alert_file.flush() + artifacts.append(AlertArtifact(dataType='file', data=alert_file.name)) + + # Attach packet as binary file artifact + if 'packet' in alert: + packet_file = NamedTemporaryFile('w+b', prefix='packet_', + suffix='.pcap') + packet_file.write(b64decode(alert['packet'])) + packet_file.flush() + artifacts.append(AlertArtifact(dataType='file', data=packet_file.name)) + + # Build description + desc = '[{}] signature: {} -- link: \ + {}/messages/{}/{}\n'.format(alert['alert_severity'], + alert['alert_signature'], + GRAYLOG, log['index'], log['id']) + + hivealert = Alert(title=alert['alert_category'], + tlp=3, + tags=['ids', 'suricata'], + description=desc, + type='external', + source=alert['name'], + sourceRef=str(uuid.uuid4())[0:6], + artifacts=artifacts) + + response = api.create_alert(hivealert) + if response.status_code == 201: + logging.debug(json.dumps(response.json(), indent=4, sort_keys=True)) + id = response.json()['id'] + else: + logging.debug('ko: {}/{}'.format(response.status_code, response.text)) + + # Confirm alert in TheHive + response = api.get_alert(id) + if response.status_code == requests.codes.ok: + logging.debug(json.dumps(response.json(), indent=4, sort_keys=True)) + else: + logging.debug('ko: {}/{}'.format(response.status_code, response.text)) + alert_file.close() + packet_file.close() + return Response(status=201) + +# app.run('0.0.0.0') diff --git a/hivealert.service b/hivealert.service new file mode 100644 index 0000000..54c03fd --- /dev/null +++ b/hivealert.service @@ -0,0 +1,12 @@ +[Unit] +Description=Hive Alert +After=network.target + +[Service] +Type=simple +User=hivealert +WorkingDirectory=/opt/hivealert +ExecStart=/opt/hivealert/.local/bin/gunicorn -b 0.0.0.0:5000 --access-logfile /opt/hivealert/access.log --chdir /opt/hivealert/hivealert hivealert:app + +[Install] +WantedBy=multi-user.target diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e5d314d --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +thehive4py>=1.6.0 +Flask>=1.0.2 +gunicorn>=19.7.1