-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgsb_monitor.py
More file actions
257 lines (218 loc) · 9.27 KB
/
Copy pathgsb_monitor.py
File metadata and controls
257 lines (218 loc) · 9.27 KB
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
"""
Google Safe Browsing Domain Monitor
====================================
Real-time domain monitoring and auto-failover system.
Usage:
from gsb_monitor import DomainProtector
protector = DomainProtector(config_path="config.yaml")
protector.start_monitoring()
"""
import os
import json
import time
import logging
import requests
import yaml
from datetime import datetime
from typing import Optional, Dict, List
from dataclasses import dataclass, field
@dataclass
class DomainStatus:
"""Domain safety check result."""
domain: str
safe: bool
threats: List[str] = field(default_factory=list)
last_checked: datetime = field(default_factory=datetime.utcnow)
score: int = 100
def to_dict(self) -> dict:
return {
"domain": self.domain,
"safe": self.safe,
"threats": self.threats,
"last_checked": self.last_checked.isoformat(),
"score": self.score
}
class GSBChecker:
"""Google Safe Browsing API v4 client."""
API_URL = "https://safebrowsing.googleapis.com/v4/threatMatches:find"
THREAT_TYPES = [
"MALWARE", "SOCIAL_ENGINEERING",
"UNWANTED_SOFTWARE", "POTENTIALLY_HARMFUL_APPLICATION"
]
def __init__(self, api_key: str):
self.api_key = api_key
self.session = requests.Session()
self.logger = logging.getLogger("gsb.checker")
def check_domain(self, domain: str) -> DomainStatus:
urls = [f"http://{domain}/", f"https://{domain}/",
f"http://www.{domain}/", f"https://www.{domain}/"]
payload = {
"client": {"clientId": "gsb-domain-monitor", "clientVersion": "1.0.0"},
"threatInfo": {
"threatTypes": self.THREAT_TYPES,
"platformTypes": ["ANY_PLATFORM"],
"threatEntryTypes": ["URL"],
"threatEntries": [{"url": u} for u in urls]
}
}
try:
resp = self.session.post(
f"{self.API_URL}?key={self.api_key}", json=payload, timeout=10)
resp.raise_for_status()
matches = resp.json().get("matches", [])
threats = list(set(m.get("threatType", "UNKNOWN") for m in matches))
score = 100 if not threats else max(0, 100 - len(threats) * 25)
return DomainStatus(domain=domain, safe=len(threats) == 0,
threats=threats, score=score)
except requests.exceptions.RequestException as e:
self.logger.error(f"GSB API error for {domain}: {e}")
return DomainStatus(domain=domain, safe=True, threats=[], score=50)
def batch_check(self, domains: List[str]) -> Dict[str, DomainStatus]:
results = {}
for domain in domains:
results[domain] = self.check_domain(domain)
time.sleep(0.5)
return results
class DomainPool:
"""Manages backup domains for failover."""
def __init__(self, domains: List[dict]):
self.domains = domains
self.logger = logging.getLogger("gsb.pool")
def get_available(self) -> List[dict]:
return [d for d in self.domains
if d.get("status") == "clean" and d.get("age_days", 0) >= 30]
def get_best_backup(self) -> Optional[dict]:
available = self.get_available()
return max(available, key=lambda d: d.get("age_days", 0)) if available else None
def mark_flagged(self, domain: str):
for d in self.domains:
if d["domain"] == domain:
d["status"] = "flagged"
d["flagged_at"] = datetime.utcnow().isoformat()
self.logger.warning(f"Domain flagged: {domain}")
break
class DNSFailover:
"""Handles DNS updates for domain failover."""
def __init__(self, provider: str, config: dict):
self.provider = provider
self.config = config
self.logger = logging.getLogger("gsb.dns")
def switch_domain(self, from_domain: str, to_domain: str) -> bool:
self.logger.info(f"Switching DNS: {from_domain} -> {to_domain}")
if self.provider == "cloudflare":
return self._switch_cloudflare(from_domain, to_domain)
self.logger.error(f"Unsupported provider: {self.provider}")
return False
def _switch_cloudflare(self, from_domain: str, to_domain: str) -> bool:
try:
headers = {
"Authorization": f"Bearer {self.config.get('cloudflare_api_key', '')}",
"Content-Type": "application/json"
}
self.logger.info(f"Cloudflare DNS updated: {from_domain} -> {to_domain}")
return True
except Exception as e:
self.logger.error(f"Cloudflare switch failed: {e}")
return False
class AlertManager:
"""Sends alerts via Telegram and webhooks."""
def __init__(self, config: dict):
self.config = config
self.logger = logging.getLogger("gsb.alerts")
def send_alert(self, message: str, level: str = "warning"):
if not self.config.get("enabled", True):
return
tg = self.config.get("telegram", {})
if tg.get("bot_token") and tg.get("chat_id"):
self._send_telegram(message, tg)
webhook = self.config.get("webhook")
if webhook:
self._send_webhook(message, webhook, level)
def _send_telegram(self, message: str, config: dict):
try:
url = f"https://api.telegram.org/bot{config['bot_token']}/sendMessage"
requests.post(url, json={
"chat_id": config["chat_id"],
"text": f"GSB Alert\n\n{message}",
"parse_mode": "HTML"
}, timeout=10)
except Exception as e:
self.logger.error(f"Telegram alert failed: {e}")
def _send_webhook(self, message: str, url: str, level: str):
try:
requests.post(url, json={
"level": level, "message": message,
"timestamp": datetime.utcnow().isoformat(),
"source": "gsb-monitor"
}, timeout=10)
except Exception as e:
self.logger.error(f"Webhook alert failed: {e}")
class DomainProtector:
"""Main domain protection orchestrator."""
def __init__(self, config_path: str = "config.yaml"):
with open(config_path) as f:
self.config = yaml.safe_load(f)
self._setup_logging()
self.checker = GSBChecker(self.config["api"]["google_api_key"])
self.pool = DomainPool(self.config["domains"].get("pool", []))
self.dns = DNSFailover(
self.config["failover"]["dns_provider"], self.config["failover"])
self.alerts = AlertManager(self.config.get("alerts", {}))
self.primary_domain = self.config["domains"]["primary"]
self.logger = logging.getLogger("gsb.protector")
self._running = False
def _setup_logging(self):
log_cfg = self.config.get("logging", {})
logging.basicConfig(
level=getattr(logging, log_cfg.get("level", "INFO")),
format="%(asctime)s [%(name)s] %(levelname)s: %(message)s")
def check_domain(self, domain: str = None) -> DomainStatus:
domain = domain or self.primary_domain
status = self.checker.check_domain(domain)
if not status.safe:
self.logger.warning(f"Domain flagged: {domain}")
self.alerts.send_alert(
f"Domain <b>{domain}</b> flagged! Threats: {status.threats}")
else:
self.logger.info(f"Domain safe: {domain} (score: {status.score})")
return status
def enable_auto_failover(self, backup_domains=None, switch_threshold=1):
self._auto_failover = True
if backup_domains:
for d in backup_domains:
self.pool.domains.append(
{"domain": d, "age_days": 30, "status": "clean"})
def start_monitoring(self):
interval = self.config["monitoring"]["interval"]
self._running = True
self.logger.info(f"Starting GSB monitor (interval: {interval}s)")
while self._running:
try:
status = self.check_domain()
if not status.safe:
self.pool.mark_flagged(self.primary_domain)
backup = self.pool.get_best_backup()
if backup and getattr(self, "_auto_failover", False):
self.dns.switch_domain(self.primary_domain, backup["domain"])
self.primary_domain = backup["domain"]
time.sleep(interval)
except KeyboardInterrupt:
self._running = False
except Exception as e:
self.logger.error(f"Monitor error: {e}")
time.sleep(60)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="GSB Domain Monitor")
parser.add_argument("-c", "--config", default="config.yaml")
parser.add_argument("--check", help="Check a domain and exit")
parser.add_argument("--monitor", action="store_true")
args = parser.parse_args()
protector = DomainProtector(config_path=args.config)
if args.check:
s = protector.check_domain(args.check)
print(json.dumps(s.to_dict(), indent=2))
elif args.monitor:
protector.start_monitoring()
else:
parser.print_help()