Repository navigation
Expand file tree
/
Copy pathrek_notify.py
More file actions
269 lines (236 loc) · 10.3 KB
/
Copy pathrek_notify.py
File metadata and controls
269 lines (236 loc) · 10.3 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
257
258
259
260
261
262
263
264
265
266
267
268
269
"""
REK Notification System
Sends scan alerts and finding notifications to Slack or Discord webhooks.
Supports: Slack Incoming Webhooks, Discord Webhooks, file-based config.
"""
import json
import os
import requests
import time
from typing import Dict, List, Optional, Any
from termcolor import colored
import logging
logger = logging.getLogger(__name__)
CONFIG_FILE = os.path.expanduser('~/.rek_notify_config.json')
SEVERITY_COLORS = {
'critical': '#FF0000',
'high': '#FF6600',
'medium': '#FFA500',
'low': '#FFFF00',
'info': '#00FF00',
}
DISCORD_SEVERITY_COLORS = {
'critical': 16711680, # Red
'high': 16744192, # Orange
'medium': 16776960, # Yellow
'low': 65535, # Cyan
'info': 65280, # Green
}
class NotificationManager:
def __init__(self, slack_webhook: str = None, discord_webhook: str = None, silent: bool = False):
self.silent = silent
config = self.load_config()
self.slack_webhook = slack_webhook or config.get('slack_webhook', '')
self.discord_webhook = discord_webhook or config.get('discord_webhook', '')
def load_config(self) -> Dict:
"""Load saved notification config."""
try:
if os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE) as f:
return json.load(f)
except Exception:
pass
return {}
def save_config(self, slack_webhook: str = None, discord_webhook: str = None):
"""Save notification config."""
config = self.load_config()
if slack_webhook:
config['slack_webhook'] = slack_webhook
if discord_webhook:
config['discord_webhook'] = discord_webhook
try:
with open(CONFIG_FILE, 'w') as f:
json.dump(config, f, indent=2)
os.chmod(CONFIG_FILE, 0o600)
if not self.silent:
print(colored(f"[✓] Notification config saved to {CONFIG_FILE}", "green"))
except Exception as e:
print(colored(f"[!] Error saving notify config: {e}", "red"))
def send_slack(self, message: str, title: str = None, severity: str = 'info', fields: List[Dict] = None) -> bool:
"""Send a notification to Slack."""
if not self.slack_webhook:
return False
color = SEVERITY_COLORS.get(severity.lower(), '#00FF00')
payload = {
'attachments': [{
'color': color,
'title': title or 'REK Notification',
'text': message,
'footer': 'REK Reconnaissance Toolkit',
'ts': int(time.time()),
}]
}
if fields:
payload['attachments'][0]['fields'] = [
{'title': f['name'], 'value': f['value'], 'short': f.get('short', True)}
for f in fields
]
try:
r = requests.post(self.slack_webhook, json=payload, timeout=10)
if r.status_code == 200:
if not self.silent:
print(colored(f"[✓] Slack notification sent", "green"))
return True
else:
if not self.silent:
print(colored(f"[!] Slack notification failed: HTTP {r.status_code}", "yellow"))
except Exception as e:
if not self.silent:
print(colored(f"[!] Slack notification error: {e}", "red"))
return False
def send_discord(self, message: str, title: str = None, severity: str = 'info', fields: List[Dict] = None) -> bool:
"""Send a notification to Discord."""
if not self.discord_webhook:
return False
color = DISCORD_SEVERITY_COLORS.get(severity.lower(), 65280)
embed = {
'title': title or 'REK Notification',
'description': message,
'color': color,
'footer': {'text': 'REK Reconnaissance Toolkit'},
'timestamp': time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()),
}
if fields:
embed['fields'] = [
{'name': f['name'], 'value': str(f['value'])[:1024], 'inline': f.get('short', True)}
for f in fields
]
payload = {'embeds': [embed]}
try:
r = requests.post(self.discord_webhook, json=payload, timeout=10)
if r.status_code in [200, 204]:
if not self.silent:
print(colored(f"[✓] Discord notification sent", "green"))
return True
else:
if not self.silent:
print(colored(f"[!] Discord notification failed: HTTP {r.status_code}", "yellow"))
except Exception as e:
if not self.silent:
print(colored(f"[!] Discord notification error: {e}", "red"))
return False
def notify(self, message: str, title: str = None, severity: str = 'info', fields: List[Dict] = None) -> bool:
"""Send notification to all configured channels."""
sent = False
if self.slack_webhook:
sent |= self.send_slack(message, title, severity, fields)
if self.discord_webhook:
sent |= self.send_discord(message, title, severity, fields)
if not sent and not self.silent:
print(colored("[!] No notification channels configured. Use --slack-webhook or --discord-webhook", "yellow"))
return sent
def notify_scan_start(self, domain: str, scan_type: str = 'Full Recon'):
"""Notify that a scan has started."""
self.notify(
message=f"Starting {scan_type} on `{domain}`",
title=f"REK Scan Started: {domain}",
severity='info',
fields=[
{'name': 'Target', 'value': domain},
{'name': 'Scan Type', 'value': scan_type},
{'name': 'Time', 'value': time.strftime('%Y-%m-%d %H:%M:%S UTC', time.gmtime())},
]
)
def notify_scan_complete(self, domain: str, stats: Dict):
"""Notify that a scan has completed with summary stats."""
fields = [
{'name': 'Target', 'value': domain},
{'name': 'Subdomains', 'value': str(stats.get('subdomains', 0))},
{'name': 'Live Hosts', 'value': str(stats.get('live_hosts', 0))},
{'name': 'Endpoints', 'value': str(stats.get('endpoints', 0))},
{'name': 'Vulnerabilities', 'value': str(stats.get('vulnerabilities', 0))},
{'name': 'Duration', 'value': stats.get('duration', 'N/A')},
]
severity = 'high' if stats.get('vulnerabilities', 0) > 0 else 'info'
self.notify(
message=f"Recon completed for `{domain}`. Found {stats.get('vulnerabilities', 0)} potential vulnerabilities.",
title=f"REK Scan Complete: {domain}",
severity=severity,
fields=fields
)
def notify_finding(self, finding_type: str, target: str, details: str, severity: str = 'high'):
"""Notify about a critical finding."""
self.notify(
message=f"**{finding_type}** found on `{target}`\n\n{details}",
title=f"[{severity.upper()}] REK Finding: {finding_type}",
severity=severity,
fields=[
{'name': 'Finding Type', 'value': finding_type},
{'name': 'Target', 'value': target},
{'name': 'Details', 'value': details[:500]},
{'name': 'Severity', 'value': severity.upper()},
]
)
def notify_new_subdomain(self, domain: str, new_subs: List[str]):
"""Notify about newly discovered subdomains (for monitoring mode)."""
if not new_subs:
return
sub_list = '\n'.join(new_subs[:20])
if len(new_subs) > 20:
sub_list += f'\n... and {len(new_subs) - 20} more'
self.notify(
message=f"**{len(new_subs)} new subdomains** discovered for `{domain}`:\n```\n{sub_list}\n```",
title=f"New Subdomains: {domain}",
severity='medium',
fields=[
{'name': 'Domain', 'value': domain},
{'name': 'New Count', 'value': str(len(new_subs))},
{'name': 'Time', 'value': time.strftime('%Y-%m-%d %H:%M:%S UTC', time.gmtime())},
]
)
def notify_takeover(self, subdomain: str, cname: str, service: str):
"""Notify about a subdomain takeover finding."""
self.notify(
message=f"**SUBDOMAIN TAKEOVER DETECTED!**\n`{subdomain}` → `{cname}` ({service})",
title=f"[CRITICAL] Subdomain Takeover: {subdomain}",
severity='critical',
fields=[
{'name': 'Subdomain', 'value': subdomain},
{'name': 'CNAME', 'value': cname},
{'name': 'Service', 'value': service},
{'name': 'Severity', 'value': 'CRITICAL - Immediate action required'},
]
)
def test_webhooks(self):
"""Test all configured webhooks."""
print(colored("[*] Testing notification webhooks...", "yellow"))
sent = self.notify(
message="This is a test notification from REK.",
title="REK Webhook Test",
severity='info',
fields=[{'name': 'Status', 'value': 'Test successful'}]
)
return sent
def configure_interactive(self):
"""Interactive webhook configuration."""
print(colored("\n[*] Notification Configuration", "cyan"))
print(colored("Current config:", "yellow"))
print(f" Slack: {self.slack_webhook or '(not set)'}")
print(f" Discord: {self.discord_webhook or '(not set)'}")
slack = input(colored("Slack webhook URL (press Enter to keep current): ", "yellow")).strip()
discord = input(colored("Discord webhook URL (press Enter to keep current): ", "yellow")).strip()
if slack:
self.slack_webhook = slack
if discord:
self.discord_webhook = discord
if slack or discord:
self.save_config(
slack_webhook=self.slack_webhook if slack else None,
discord_webhook=self.discord_webhook if discord else None,
)
test = input(colored("Test webhooks now? (y/n): ", "yellow")).strip().lower()
if test == 'y':
self.test_webhooks()
if __name__ == '__main__':
mgr = NotificationManager()
mgr.configure_interactive()