182 lines
6.6 KiB
Python
Raw Normal View History

#!/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-22 21:05:27 +02:00
# Rudi Klein, april 2024
import requests
2024-05-22 21:05:27 +02:00
from wazuh_notify_module import *
2024-05-22 21:05:27 +02:00
def main():
me = frame(0).f_code.co_name
him = frame(1).f_code.co_name
2024-05-22 21:05:27 +02:00
# Load the YAML config.
2024-05-24 13:06:46 +02:00
config: dict = get_config()
2024-05-22 21:05:27 +02:00
logger(0, config, me, him, "############ Processing event ###############################")
logger(2, config, me, him, "Loading yaml configuration")
2024-05-22 21:05:27 +02:00
# Get the arguments used with running the script.
2024-05-24 13:06:46 +02:00
2024-05-22 21:05:27 +02:00
arguments = get_arguments()
# Check if we are in test mode (test_mode setting in config yaml). If so, load test event instead of live event.
2024-05-27 12:40:39 +02:00
if config.get('python', 'test_mode'):
2024-05-24 13:06:46 +02:00
2024-05-22 21:05:27 +02:00
logger(1, config, me, him, "Running in test mode: using test message wazuh-notify-test-event.json")
2024-05-24 13:06:46 +02:00
# Load the test event data.
2024-05-22 21:05:27 +02:00
home_path, _, _ = set_environment()
with (open(home_path + '/etc/wazuh-notify-test-event.json') as event_file):
data: dict = json.loads(event_file.read())
else:
2024-05-24 13:06:46 +02:00
2024-05-22 21:05:27 +02:00
# We are running live. Load the data from the Wazuh process.
2024-05-24 13:06:46 +02:00
2024-05-22 21:05:27 +02:00
logger(2, config, me, him, "Running in live mode: using live message")
data = load_message()
# Extract the 'alert' section of the (JSON) event
2024-05-24 13:06:46 +02:00
alert = data["parameters"]["alert"]
2024-05-22 21:05:27 +02:00
logger(2, config, me, him, "Extracting data from the event")
# Check the config for any exclusion rules
2024-05-24 13:06:46 +02:00
fire_notification = exclusions_check(config, alert)
2024-05-22 21:05:27 +02:00
logger(1, config, me, him, "Checking if we are outside of the exclusion rules: " + str(fire_notification))
if not fire_notification:
2024-05-24 13:06:46 +02:00
2024-05-22 21:05:27 +02:00
# The event was excluded by the exclusion rules in the configuration.
2024-05-24 13:06:46 +02:00
2024-05-22 21:05:27 +02:00
logger(1, config, me, him, "Event excluded, no notification sent. Exiting")
exit()
else:
2024-05-24 13:06:46 +02:00
2024-05-22 21:05:27 +02:00
# The event was not excluded by the exclusion rules in the configuration. Keep processing.
2024-05-24 13:06:46 +02:00
2024-05-22 21:05:27 +02:00
logger(2, config, me, him, "Event NOT excluded, notification will be sent")
2024-05-22 21:05:27 +02:00
# Get the mapping from event threat level to priority, color and mention_flag.
2024-05-24 13:06:46 +02:00
2024-05-22 21:05:27 +02:00
priority, color, mention = threat_mapping(config, alert['rule']['level'], alert['rule']['firedtimes'])
2024-05-24 13:06:46 +02:00
logger(2, config, me, him, "Threat mapping done: " +
"prio:" + str(priority) + " color:" + str(color) + " mention:" + mention)
2024-05-22 21:05:27 +02:00
# If the target argument was used with the script, we'll use that instead of the configuration parameter.
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"]
# Prepare the messaging platform specific request and execute
2024-05-24 13:06:46 +02:00
if "discord" in config["targets"]:
caller = "discord"
2024-05-22 21:05:27 +02:00
# Load the url/webhook from the configuration.
2024-05-24 13:06:46 +02:00
discord_url, _, _ = get_env()
2024-05-22 21:05:27 +02:00
discord_url = arguments['url'] if arguments['url'] else discord_url
# Build the basic notification message content.
2024-05-24 13:06:46 +02:00
2024-05-22 21:05:27 +02:00
notification: str = construct_basic_message(config, arguments, caller, alert)
logger(2, config, me, him, caller + " basic message constructed")
# Build the payload(s) for the POST request.
2024-05-24 13:06:46 +02:00
2024-05-22 21:05:27 +02:00
_, _, payload_json = build_notification(caller,
config,
arguments,
notification,
alert,
priority,
color,
mention
)
# POST the notification through requests.
2024-05-24 13:06:46 +02:00
2024-05-22 21:05:27 +02:00
result = requests.post(discord_url, json=payload_json)
2024-05-24 13:06:46 +02:00
2024-05-22 21:05:27 +02:00
logger(1, config, me, him, caller + " notification constructed and HTTPS request done: " + str(result))
if "ntfy" in config["targets"]:
caller = "ntfy"
2024-05-22 21:05:27 +02:00
# Load the url/webhook from the configuration.
2024-05-24 13:06:46 +02:00
2024-05-22 21:05:27 +02:00
_, ntfy_url, _ = get_env()
# Build the basic notification message content.
2024-05-24 13:06:46 +02:00
2024-05-22 21:05:27 +02:00
notification: str = construct_basic_message(config, arguments, caller, alert)
2024-05-24 13:06:46 +02:00
2024-05-22 21:05:27 +02:00
logger(2, config, me, him, caller + " basic message constructed")
# Build the payload(s) for the POST request.
payload_headers, payload_data, _ = build_notification(caller,
config,
arguments,
notification,
alert,
priority,
color,
mention
)
# POST the notification through requests.
2024-05-24 13:06:46 +02:00
2024-05-22 21:05:27 +02:00
result = requests.post(ntfy_url, data=payload_data, headers=payload_headers)
logger(1, config, me, him, caller + " notification constructed and request done: " + str(result))
if "slack" in config["targets"]:
caller = "slack"
2024-05-22 21:05:27 +02:00
# Load the url/webhook from the configuration.
2024-05-24 13:06:46 +02:00
2024-05-22 21:05:27 +02:00
_, _, slack_url = get_env()
# Build the basic notification message content.
2024-05-24 13:06:46 +02:00
2024-05-22 21:05:27 +02:00
notification: str = construct_basic_message(config, arguments, caller, alert)
2024-05-24 13:06:46 +02:00
2024-05-22 21:05:27 +02:00
logger(2, config, me, him, caller + " basic message constructed")
# Build the payload(s) for the POST request.
2024-05-24 13:06:46 +02:00
2024-05-22 21:05:27 +02:00
_, _, payload_json = build_notification(caller,
config,
arguments,
notification,
alert,
priority,
color,
mention
)
# POST the notification through requests.
2024-05-24 13:06:46 +02:00
2024-05-22 21:05:27 +02:00
result = requests.post(slack_url, headers={'Content-Type': 'application/json'}, json=payload_json)
2024-05-24 13:06:46 +02:00
2024-05-22 21:05:27 +02:00
logger(1, config, me, him, caller + " notification constructed and request done: " + str(result))
logger(0, config, me, him, "############ Event processed ################################")
exit(0)
if __name__ == "__main__":
2024-05-22 21:05:27 +02:00
main()