forked from ReconInfoSec/thehive-slack-webhook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_function.py
151 lines (127 loc) · 4.71 KB
/
lambda_function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# https://github.com/ReconInfoSec/thehive-slack-webhook
from __future__ import print_function
import json
import logging
import time
import config as cfg
import requests
from flask import Flask, request
logger = logging.getLogger()
logger.setLevel(logging.INFO)
app = Flask(__name__)
def add_object(title,value,short):
object = {"title": title,"value": value,"short": short}
return object
def process_event(event):
logger.info("Processing event: %s", event)
fields = []
titleLink = False
objectType = ""
caseId = None
caseLink = ""
titleLink = ""
if "Delete" not in event['operation']:
if event['objectType'] == "case":
objectType = "Case"
caseId = event['object']['caseId']
fields.append(add_object("Case #", caseId, True))
caseLink = cfg.caseURL + event['objectId'] + "/details"
titleLink = caseLink
elif event['objectType'] == "case_task":
objectType = "Task"
titleLink = cfg.caseURL + event['rootId'] + "/tasks/" + event['objectId']
elif event['objectType'] == "case_task_log":
objectType = "Task Log"
titleLink = cfg.caseURL + event['rootId'] + "/tasks/" + event['objectId']
elif event['objectType'] == "alert":
objectType = "Alert"
titleLink = cfg.alertsURL
else:
caseId = "none"
if event['operation'] == "Creation":
operation = "created"
#elif event['operation'] == "Delete":
# operation = "deleted"
#elif event['operation'] == "Update":
# operation = "updated"
#else:
# operation = event['operation']
else:
return
if "description" in event['object']:
description = event['object']['description'] # create/update
fields.append(add_object("Description",description,False))
elif "message" in event['object']:
description = event['object']['message']
fields.append(add_object("Description",description,False))
else:
description = "none"
if "status" in event['object']:
status = event['object']['status']
fields.append(add_object("Status",status,True))
else:
status = "none"
if "owner" in event['object']:
owner = event['object']['owner']
fields.append(add_object("Owner",owner,True))
else:
owner = "none"
if "tlp" in event['object']:
tlp = event['object']['tlp']
fields.append(add_object("TLP",tlp,True))
else:
tlp = "none"
if "createdBy" in event['object']:
createdBy = event['object']['createdBy']
fields.append(add_object("Created By",createdBy,True))
else:
createdBy = "none"
if "Update" in event['operation']:
updatedBy = event['object']['updatedBy']
fields.append(add_object("Updated By",updatedBy,True))
else:
updatedBy = owner
if "updatedAt" in event['object']:
timestamp = event['object']['updatedAt']
else:
timestamp = int(time.time())
if not titleLink: # if we haven't set it based on object type
titleLink = cfg.hiveURL
activity = "TheHive " + str(objectType) + " " + operation + "."
if "title" in event['object']:
title = event['object']['title']
else:
title = activity
attachments = {
":exclamation:": activity,
"Owner": (str(updatedBy)),
"Title": (str(title)),
"Link": titleLink
#"color": "danger",
#"fields": fields,
#"footer": cfg.orgName,
#"footer_icon": cfg.orgIcon,
#"ts": timestamp
}
send_to_slack(event,attachments)
def send_to_slack(event,attachments):
time.sleep(1) # Timeout to avoid HTTP 429, Slack API allows 1 request per second
slack_message = {
'username': 'TheHive',
'icon_emoji': ':honeybee:',
'channel': cfg.slackChannel,
'attachments': attachments
}
message=slack_message['icon_emoji']+"\n\n"
for key, value in attachments.items():
message+=f'{key} : {value}\n\n'
try:
req = requests.post(cfg.hookURL, json={"text":message}, proxies=cfg.PROXY_CONFIG, headers={'Content-type': 'application/json'})
logger.info("Message posted to %s", slack_message['channel'])
except Exception as e:
logger.error("Something Went wrong: %s", e)
@app.route('/webhookLambda', methods=['POST'])
def lambda_handler():
hive_event = request.get_json()
process_event(hive_event)
return ('Message Sent', 200, None)