initial commit

This commit is contained in:
Shane Peters
2019-01-11 10:22:04 -05:00
commit ad2c8cb247
5 changed files with 146 additions and 0 deletions

19
LICENSE Normal file
View File

@@ -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.

20
README.md Normal file
View File

@@ -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'
```

92
hivealert.py Normal file
View File

@@ -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')

12
hivealert.service Normal file
View File

@@ -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

3
requirements.txt Normal file
View File

@@ -0,0 +1,3 @@
thehive4py>=1.6.0
Flask>=1.0.2
gunicorn>=19.7.1