2024-05-18 21:23:35 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
# This program is free software; you can redistribute it
|
|
|
|
|
# and/or modify it under the terms of the GNU General Public
|
|
|
|
|
# License (version 2) as published by the FSF - Free Software
|
|
|
|
|
# Foundation.
|
|
|
|
|
#
|
2024-05-27 20:24:00 +02:00
|
|
|
# Rudi Klein, May 2024
|
2024-05-18 21:23:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
import requests
|
2024-05-30 17:11:49 +02:00
|
|
|
from requests import Response
|
2024-05-18 21:23:35 +02:00
|
|
|
|
2024-05-22 21:05:27 +02:00
|
|
|
from wazuh_notify_module import *
|
|
|
|
|
|
2024-05-18 21:23:35 +02:00
|
|
|
|
2024-05-22 21:05:27 +02:00
|
|
|
def main():
|
2024-05-27 20:24:00 +02:00
|
|
|
# The 'me' variable sets the called function (current function), the 'him' the calling function. Used for logging.
|
2024-05-30 17:11:49 +02:00
|
|
|
me: str = frame(0).f_code.co_name
|
|
|
|
|
him: str = frame(1).f_code.co_name
|
2024-05-18 21:23:35 +02:00
|
|
|
config: dict = get_config()
|
2024-05-29 20:37:53 +02:00
|
|
|
|
2024-05-30 17:11:49 +02:00
|
|
|
# Write header line in logfile
|
|
|
|
|
logger(level=99, config=config, me=me, him=him, message="")
|
|
|
|
|
|
|
|
|
|
# Load the TOML config.
|
|
|
|
|
logger(level=0, config=config, me=me, him=him, message="############# [Processing event] #########################")
|
2024-05-18 21:23:35 +02:00
|
|
|
|
2024-05-22 21:05:27 +02:00
|
|
|
# Get the arguments used with running the script.
|
2024-05-30 17:11:49 +02:00
|
|
|
arguments: dict = get_arguments()
|
2024-05-18 21:23:35 +02:00
|
|
|
|
2024-05-29 20:37:53 +02:00
|
|
|
# Check for test mode. Use test data if true.
|
2024-05-30 17:11:49 +02:00
|
|
|
event_data: dict = check_test_mode(config)
|
2024-05-18 21:23:35 +02:00
|
|
|
|
2024-05-30 17:11:49 +02:00
|
|
|
alert: dict = event_data["parameters"]["alert"]
|
|
|
|
|
logger(level=2, config=config, me=me, him=him, message="Extracting data from the event")
|
2024-05-18 21:23:35 +02:00
|
|
|
|
2024-05-27 20:24:00 +02:00
|
|
|
# Check the config for any exclusion rules and abort when excluded.
|
2024-05-30 17:11:49 +02:00
|
|
|
if not exclusions_check(config, alert):
|
|
|
|
|
logger(level=1, config=config, me=me, him=him, message="Event excluded, no notification sent. Exiting")
|
|
|
|
|
exit()
|
|
|
|
|
logger(level=2, config=config, me=me, him=him, message="Event NOT excluded, notification will be sent")
|
2024-05-18 21:23:35 +02:00
|
|
|
|
2024-05-22 21:05:27 +02:00
|
|
|
# Get the mapping from event threat level to priority, color and mention_flag.
|
|
|
|
|
priority, color, mention = threat_mapping(config, alert['rule']['level'], alert['rule']['firedtimes'])
|
2024-05-24 13:06:46 +02:00
|
|
|
|
2024-05-22 21:05:27 +02:00
|
|
|
config["targets"] = arguments['targets'] if arguments['targets'] != "" else config["targets"]
|
2024-05-18 21:23:35 +02:00
|
|
|
|
2024-05-29 20:37:53 +02:00
|
|
|
# Discord notification handler
|
|
|
|
|
if "discord" in config["targets"]:
|
|
|
|
|
payload_json, discord_url = handle_discord_notification(config=config, arguments=arguments, alert=alert,
|
|
|
|
|
color=color, priority=priority, mention=mention)
|
2024-05-30 17:11:49 +02:00
|
|
|
discord_result: Response = requests.post(url=discord_url, json=payload_json)
|
|
|
|
|
logger(level=1, config=config, me=me, him=him, message="Discord notification constructed and sent: " +
|
|
|
|
|
str(discord_result))
|
2024-05-29 20:37:53 +02:00
|
|
|
# ntfy.sh notification handler
|
2024-05-18 21:23:35 +02:00
|
|
|
if "ntfy" in config["targets"]:
|
2024-05-29 20:37:53 +02:00
|
|
|
payload_data, payload_headers, ntfy_url = handle_ntfy_notification(config=config, arguments=arguments,
|
|
|
|
|
alert=alert, priority=priority)
|
2024-05-30 17:11:49 +02:00
|
|
|
ntfy_result: Response = requests.post(url=ntfy_url, data=payload_data, headers=payload_headers)
|
|
|
|
|
logger(level=1, config=config, me=me, him=him, message="Ntfy notification constructed and sent: " +
|
|
|
|
|
str(ntfy_result))
|
2024-05-29 20:37:53 +02:00
|
|
|
# Slack notification handler
|
|
|
|
|
if "slack" in config["targets"]:
|
|
|
|
|
payload_json, slack_url = handle_slack_notification(config=config, arguments=arguments, alert=alert,
|
|
|
|
|
color=color, priority=priority, mention=mention)
|
2024-05-30 17:11:49 +02:00
|
|
|
slack_result: Response = requests.post(url=slack_url, headers={'Content-Type': 'application/json'},
|
|
|
|
|
json=payload_json)
|
2024-05-29 20:37:53 +02:00
|
|
|
logger(1, config, me, him, "Slack notification constructed and sent: " + str(slack_result))
|
2024-05-22 21:05:27 +02:00
|
|
|
|
2024-05-30 17:11:49 +02:00
|
|
|
logger(0, config, me, him, "############# [Event processed] #########################")
|
2024-05-22 21:05:27 +02:00
|
|
|
exit(0)
|
2024-05-18 21:23:35 +02:00
|
|
|
|
|
|
|
|
|
2024-05-30 17:11:49 +02:00
|
|
|
if __name__ == "__main__":
|
2024-05-22 21:05:27 +02:00
|
|
|
main()
|