summaryrefslogtreecommitdiff
path: root/misc/server_example.py
blob: 4b53315d9b7d132cfbbd4fe7aed2f765258917a0 (plain)
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
#!/usr/bin/env python3

#
# basic example showing how to use noawareness data
#
# - set up webhook as you see fit
# - configure your logging search engine to ingest noawareness.log
# - point noawareness agents at this host to collect logs centrally
#

import socket
import json
import logging
import requests

# Configuration
UDP_PORT = 55555
LOG_FILE = "noawareness.log"
DISCORD_WEBHOOK_URL = "https://discord.com/api/webhooks/your/webhook/url"
TRIGGER_MD5 = "foo"

# Setup logging to just log raw messages
logging.basicConfig(filename=LOG_FILE, level=logging.INFO, format="%(message)s")

# Create UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(("0.0.0.0", UDP_PORT))
print(f"Listening on UDP port {UDP_PORT}...")

def send_discord_alert(message):
    payload = {"content": message}
    try:
        requests.post(DISCORD_WEBHOOK_URL, json=payload)
    except requests.RequestException:
        pass

while True:
    try:
        data, _ = sock.recvfrom(65535)
        message = data.decode("utf-8", errors="ignore").strip()
        logging.info(message)

        try:
            parsed = json.loads(message)
            if parsed.get("md5") == TRIGGER_MD5:
                send_discord_alert(f"Trigger match: md5 == {TRIGGER_MD5}")
        except json.JSONDecodeError:
            pass

    except KeyboardInterrupt:
        break