TTOPM commited on
Commit
481b86c
·
verified ·
1 Parent(s): 3407708

Upload 5 files

Browse files
src/utils/dns_lookup.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import socket
2
+ import logging
3
+
4
+ logging.basicConfig(level=logging.INFO)
5
+
6
+ def dns_lookup(domain):
7
+ try:
8
+ ip = socket.gethostbyname(domain)
9
+ logging.info(f"[DNS] {domain} resolved to {ip}")
10
+ return {"domain": domain, "ip_address": ip}
11
+ except socket.gaierror:
12
+ logging.warning(f"[DNS] Failed to resolve {domain}")
13
+ return {"domain": domain, "ip_address": None}
src/utils/email_alert.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import smtplib
2
+ from email.mime.text import MIMEText
3
+ import logging
4
+
5
+ def send_email_alert(subject, body, recipient_email, sender_email, smtp_server, smtp_port, smtp_username, smtp_password):
6
+ try:
7
+ msg = MIMEText(body)
8
+ msg["Subject"] = subject
9
+ msg["From"] = sender_email
10
+ msg["To"] = recipient_email
11
+
12
+ with smtplib.SMTP_SSL(smtp_server, smtp_port) as server:
13
+ server.login(smtp_username, smtp_password)
14
+ server.sendmail(sender_email, recipient_email, msg.as_string())
15
+
16
+ logging.info(f"Email alert sent to {recipient_email}")
17
+ except Exception as e:
18
+ logging.error(f"Failed to send email alert: {e}")
src/utils/violation_logger.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import uuid
3
+ import logging
4
+ from datetime import datetime
5
+ from pathlib import Path
6
+
7
+ # Optional integrations
8
+ try:
9
+ from src.protocol.security.alert_webhook import WebhookAlerter
10
+ from src.protocol.permanent_memory import PermanentMemory
11
+ memory = PermanentMemory()
12
+ except ImportError:
13
+ WebhookAlerter = None
14
+ memory = None
15
+
16
+ # === CONFIG ===
17
+ VIOLATIONS_JSON = Path("violations.json")
18
+ VIOLATIONS_STREAM = Path("violations.jsonl") # JSON Lines format
19
+ ENABLE_DISCORD_ALERTS = True
20
+ WEBHOOK_URL = "https://discord.com/api/webhooks/xxx/yyy" # Replace with your real one
21
+
22
+ # === LOGGER SETUP ===
23
+ logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
24
+
25
+ def log_violation(
26
+ violation_type,
27
+ description,
28
+ source_url=None,
29
+ severity="medium",
30
+ detected_by="AutoScanner",
31
+ context=None
32
+ ):
33
+ """
34
+ Logs a violation to both JSON and streaming JSONL files.
35
+ Optionally triggers alerts and writes to permanent memory.
36
+ """
37
+ entry = {
38
+ "id": str(uuid.uuid4()),
39
+ "timestamp": datetime.utcnow().isoformat() + "Z",
40
+ "violation_type": violation_type,
41
+ "description": description,
42
+ "source_url": source_url,
43
+ "severity": severity,
44
+ "detected_by": detected_by,
45
+ "context": context or {}
46
+ }
47
+
48
+ # === Write to violations.json ===
49
+ try:
50
+ if VIOLATIONS_JSON.exists():
51
+ with open(VIOLATIONS_JSON, "r") as f:
52
+ data = json.load(f)
53
+ else:
54
+ data = {"log_created": datetime.utcnow().isoformat() + "Z", "entries": []}
55
+ except Exception:
56
+ data = {"log_created": datetime.utcnow().isoformat() + "Z", "entries": []}
57
+
58
+ data["entries"].append(entry)
59
+ with open(VIOLATIONS_JSON, "w") as f:
60
+ json.dump(data, f, indent=2)
61
+
62
+ # === Write to violations.jsonl ===
63
+ with open(VIOLATIONS_STREAM, "a") as f:
64
+ f.write(json.dumps(entry) + "\n")
65
+
66
+ logging.warning(f"[{severity.upper()}] Violation logged: {violation_type} – {description}")
67
+
68
+ # === Alert Webhook ===
69
+ if ENABLE_DISCORD_ALERTS and WebhookAlerter:
70
+ try:
71
+ alerter = WebhookAlerter(WEBHOOK_URL)
72
+ alerter.send_alert(f"🚨 Violation Detected: `{violation_type}`\n> {description}")
73
+ except Exception as e:
74
+ logging.error(f"Failed to send webhook alert: {e}")
75
+
76
+ # === Permanent Memory Sync ===
77
+ if memory:
78
+ memory.write("violation_log", entry)
79
+
80
+ return entry
src/utils/webhook_alert.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import logging
3
+
4
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
5
+
6
+ def send_webhook_alert(webhook_url: str, payload: dict):
7
+ try:
8
+ response = requests.post(webhook_url, json=payload)
9
+ response.raise_for_status()
10
+ logging.info(f"Webhook alert sent. Status: {response.status_code}")
11
+ except Exception as e:
12
+ logging.error(f"Webhook alert failed: {e}")
src/utils/whois_lookup.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import whois
2
+ import logging
3
+
4
+ logging.basicConfig(level=logging.INFO)
5
+
6
+ def whois_lookup(domain):
7
+ try:
8
+ data = whois.whois(domain)
9
+ logging.info(f"[WHOIS] Data fetched for {domain}")
10
+ return {
11
+ "domain": domain,
12
+ "registrar": data.registrar,
13
+ "creation_date": str(data.creation_date),
14
+ "expiration_date": str(data.expiration_date),
15
+ "name_servers": data.name_servers
16
+ }
17
+ except Exception as e:
18
+ logging.error(f"[WHOIS] Failed for {domain}: {e}")
19
+ return {
20
+ "domain": domain,
21
+ "registrar": None,
22
+ "creation_date": None,
23
+ "expiration_date": None,
24
+ "name_servers": None
25
+ }