-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathhandler.py
423 lines (371 loc) · 13.5 KB
/
handler.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017 Adel "0x4D31" Karimi
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import json
import time
import urllib2
import urllib
import logging
import boto3
import os
import base64
import smtplib
__author__ = 'Adel "0x4d31" Ka'
__version__ = '0.1'
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def honeylambda(event, context):
""" Main function """
# Load config file
config = load_config()
# Preparing alert message
alertMessage = alert_msg(event, config)
# Slack alert
if config['alert']['slack']['enabled'] == "true":
WEBHOOK_URL = config['alert']['slack']['webhook-url']
slack_alerter(alertMessage, WEBHOOK_URL)
# Email alert
if config['alert']['email']['enabled'] == "true":
email_alerter(alertMessage, config)
# SMS alert
if config['alert']['sms']['enabled'] == "true":
sms_alerter(alertMessage, config)
# Prepare and send HTTP response
response = generate_http_response(event, config)
logger.info("HTTP response sent")
return response
def load_config():
""" Load the configuration from local file or Amazon S3 """
# Check the environment variable for config type (local/s3)
CONFIGFILE = os.environ['configFile']
# Load config from S3
if CONFIGFILE == "s3":
BUCKET = os.environ['s3Bucket']
KEY = os.environ['s3Key']
s3 = boto3.client('s3')
try:
response = s3.get_object(Bucket=BUCKET, Key=KEY)
data = response['Body'].read()
conf = json.loads(data)
logger.info("Config file loaded from S3")
except Exception as err:
logger.error(err)
raise
else:
# Load config from local file
with open('config.json') as config_file:
conf = json.load(config_file)
logger.info("Local config file loaded")
return conf
def threat_intel_lookup(ip, cred):
""" Threat Intel lookup (source IP address) using Cymon v2 API """
CYMON_LOGIN_API = "https://api.cymon.io/v2/auth/login"
CYMON_SEARCHIP_API = "https://api.cymon.io/v2/ioc/search/ip/"
CYMON_URL = "https://app.cymon.io/search/ip/"
resp_dict = {}
# Anonymous IP lookup request (rate-limited)
lookup_req = urllib2.Request(
CYMON_SEARCHIP_API + ip,
headers={'Content-Type': 'application/json'}
)
# Authenticate if Cymon credential is provided
if cred:
auth_req = urllib2.Request(
CYMON_LOGIN_API,
data=json.dumps(cred),
headers={'Content-Type': 'application/json'}
)
try:
auth_resp = (urllib2.urlopen(auth_req)).read()
auth_token = (json.loads(auth_resp))['jwt']
lookup_req.add_header("Authorization", "Bearer {}".format(auth_token))
logger.info("Cymon JWT token received")
except urllib2.HTTPError as err:
logger.error("Cymon Auth request failed: {} {}".format(
err.code,
err.reason)
)
except urllib2.URLError as err:
logger.error("Cymon Auth connection failed: {}".format(err.reason))
# Send IP lookup request
try:
lookup_resp = (urllib2.urlopen(lookup_req)).read()
resp_dict = json.loads(lookup_resp)
logger.info("Cymon results received")
except urllib2.HTTPError as err:
logger.error("Cymon lookup request failed: {} {}".format(
err.code,
err.reason)
)
except urllib2.URLError as err:
logger.error("Cymon lookup connection failed: {}".format(err.reason))
# Prepare the result
if resp_dict:
if resp_dict['total'] != 0:
resp = ["- {} (tags: {})".format(h['title'], ', '.join(h['tags']))
for h in resp_dict['hits']]
resp.append("+ More info: {}{}".format(CYMON_URL, ip))
return resp
return None
def generate_http_response(e, conf):
""" Generate HTTP response """
req_path = e['resource']
if e['queryStringParameters']:
q, p = e['queryStringParameters'].items()[0]
req_token = "{}={}".format(q, p)
else:
req_token = ""
# Load the default HTTP response
con_type = conf['default-http-response']['content-type']
body_path = conf['default-http-response']['body']
# Check if the token exists and has a custom http-response
if req_token in conf['traps'][req_path]:
if 'http-response' in conf['traps'][req_path][req_token]:
con_type = (conf['traps'][req_path][req_token]
['http-response']['content-type'])
body_path = (conf['traps'][req_path][req_token]
['http-response']['body'])
with open(body_path) as body_file:
data = body_file.read()
if "image/" in con_type:
res = {
"statusCode": 200,
"headers": {
"Content-Type": con_type
},
"body": base64.b64encode(data),
"isBase64Encoded": True
}
elif "text/" in con_type:
res = {
"statusCode": 200,
"headers": {
"Content-Type": con_type
},
"body": data,
}
else:
logger.error("{} Content-Type is not supported".format(con_type))
res = {
"statusCode": 200,
"body": ":-(",
}
return res
def alert_msg(e, conf):
""" Prepare alert message dictionary """
# Message fields
path = e['resource']
full_path = e['requestContext']['path']
host = e['headers']['Host']
body = e['body']
http_method = e['httpMethod']
source_ip = e['requestContext']['identity']['sourceIp']
user_agent = e['headers']['User-Agent']
if "CloudFront-Viewer-Country" in e['headers']:
viewer_country = e['headers']['CloudFront-Viewer-Country']
else:
viewer_country = "None"
device_dict = {
"Tablet": e['headers']['CloudFront-Is-Tablet-Viewer'],
"Mobile": e['headers']['CloudFront-Is-Mobile-Viewer'],
"Desktop": e['headers']['CloudFront-Is-Desktop-Viewer'],
"SmartTV": e['headers']['CloudFront-Is-SmartTV-Viewer']
}
viewer_device = [dev for dev in device_dict if device_dict[dev] == "true"]
viewer_details = "Country: {}, Device: {}".format(
viewer_country,
viewer_device[0])
if e['queryStringParameters']:
q, p = e['queryStringParameters'].items()[0]
req_token = "{}={}".format(q, p)
else:
req_token = "None"
# Search the config for the token note
note = "None"
if req_token in conf['traps'][path]:
if 'note' in conf['traps'][path][req_token]:
note = conf['traps'][path][req_token]['note']
# Threat Intel Lookup (Cymon v2)
threat_intel = "None"
if conf['threat-intel-lookup']['enabled'] == "true":
username = conf['threat-intel-lookup']['cymon2-user']
password = conf['threat-intel-lookup']['cymon2-pass']
if username and password:
credential = {
"username": username,
"password": password
}
else:
credential = None
lookup_result = threat_intel_lookup(source_ip, credential)
if lookup_result:
threat_intel = "\n".join(lookup_result)
# Message dictionary
msg = {
"token-note": note,
"path": full_path,
"host": host,
"http-method": http_method,
"token": req_token,
"body": body,
"source-ip": source_ip,
"user-agent": user_agent,
"viewer-details": viewer_details,
"threat-intel": threat_intel
}
return msg
def email_alerter(msg, conf):
""" Send Email alert """
smtp_server = conf['alert']['email']['smtp_server']
smtp_port = conf['alert']['email']['smtp_port']
smtp_user = conf['alert']['email']['smtp_user']
smtp_password = conf['alert']['email']['smtp_password']
to_email = conf['alert']['email']['to_email']
subject = 'honeyLambda Alert'
now = time.strftime('%a, %d %b %Y %H:%M:%S %Z', time.localtime())
body = ("Honeytoken triggered!\n\n"
"Time: {}\n"
"Source IP: {}\n"
"Threat Intel Report: {}\n"
"User-Agent: {}\n"
"Viewer Details: {}\n"
"Token Note: {}\n"
"Token: {}\n"
"Path: {}\n"
"Host: {}").format(
now,
msg['source-ip'],
msg['threat-intel'] if msg['threat-intel'] else "None",
msg['user-agent'],
msg['viewer-details'],
msg['token-note'],
msg['token'],
msg['path'],
msg['host'])
email_text = "From: {}\nTo: {}\nSubject: {}\n\n{}".format(
smtp_user,
", ".join(to_email),
subject,
body)
try:
server = smtplib.SMTP_SSL(smtp_server, smtp_port)
server.ehlo()
server.login(smtp_user, smtp_password)
server.sendmail(smtp_user, to_email, email_text)
server.close()
logger.info("Email Sent")
except smtplib.SMTPException as err:
logger.error("Error sending email: {}".format(err))
def sms_alerter(msg, conf):
""" Send SMS alert """
TWILIO_SMS_URL = "https://api.twilio.com/2010-04-01/Accounts/{}/Messages.json"
to_number = conf['alert']['sms']['to_number']
from_number = conf['alert']['sms']['from_number']
twilio_account_sid = conf['alert']['sms']['twilio_account_sid']
twilio_auth_token = conf['alert']['sms']['twilio_auth_token']
body = (u"Honeytoken triggered by {}! \U0001F631\n"
"'{}'\n"
"Token Note: '{}'").format(
msg['source-ip'],
msg['viewer-details'],
msg['token-note']
)
populated_url = TWILIO_SMS_URL.format(twilio_account_sid)
post_params = {"To": to_number, "From": from_number, "Body": body}
data = urllib.urlencode(post_params)
req = urllib2.Request(populated_url)
authentication = "{}:{}".format(twilio_account_sid, twilio_auth_token)
base64string = base64.b64encode(authentication.encode('utf-8'))
req.add_header("Authorization", "Basic %s" % base64string.decode('ascii'))
try:
urllib2.urlopen(req, data)
logger.info("SMS Sent")
except Exception as err:
logger.error("Error sending SMS: {}".format(err))
return
def slack_alerter(msg, webhook_url):
""" Send Slack alert """
now = time.strftime('%a, %d %b %Y %H:%M:%S %Z', time.localtime())
# Preparing Slack message
slack_message = {
"text": "*Honeytoken triggered!*\nA honeytoken has been triggered by {}".format(msg['source-ip']),
"username": "honeyλ",
"icon_emoji": ":ghost:",
"attachments": [
{
"color": "danger",
# "title": "Alert details",
"text": "Alert details:",
"footer": "honeyλ",
"footer_icon": "https://raw.githubusercontent.com/0x4D31/honeyLambda/master/docs/slack-footer.png",
"fields": [
{
"title": "Time",
"value": now,
"short": "true"
},
{
"title": "Source IP Address",
"value": msg['source-ip'],
"short": "true"
},
{
"title": "Threat Intel Report",
"value": msg['threat-intel'] if msg['threat-intel'] else "None",
},
{
"title": "User-Agent",
"value": msg['user-agent']
},
{
"title": "Token Note",
"value": msg['token-note'],
"short": "true"
},
{
"title": "Token",
"value": msg['token'],
"short": "true"
},
{
"title": "Viewer Details",
"value": msg['viewer-details'],
"short": "true"
},
{
"title": "Path",
"value": msg['path'],
"short": "true"
},
{
"title": "Host",
"value": msg['host']
}
]
}
]
}
# Sending Slack message
req = urllib2.Request(webhook_url, json.dumps(slack_message))
try:
resp = urllib2.urlopen(req)
logger.info("Message posted to Slack")
except urllib2.HTTPError as err:
logger.error("Request failed: {} {}".format(err.code, err.reason))
except urllib2.URLError as err:
logger.error("Connection failed: {}".format(err.reason))
return