-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreporting.py
More file actions
196 lines (182 loc) · 6.87 KB
/
Copy pathreporting.py
File metadata and controls
196 lines (182 loc) · 6.87 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
"""Shared terminal and human-log reporting for the anomaly detectors."""
from __future__ import annotations
import json
import sys
from pathlib import Path
from typing import Any, Optional
class Reporter:
COLORS = {
"reset": "\033[0m",
"bold": "\033[1m",
"dim": "\033[2m",
"cyan": "\033[36m",
"blue": "\033[34m",
"yellow": "\033[33m",
"red": "\033[31m",
"magenta": "\033[35m",
"green": "\033[32m",
"white": "\033[37m",
}
def __init__(
self,
log_path: Path,
quiet: bool = False,
color: str = "auto",
show_data: bool = True,
):
self.quiet = quiet
self.show_data = show_data
self.color_enabled = color == "always" or (
color == "auto" and sys.stdout.isatty()
)
self.log_path = log_path
self.log = log_path.open("w", encoding="utf-8")
def paint(self, text: str, *styles: str) -> str:
if not self.color_enabled:
return text
prefix = "".join(self.COLORS[style] for style in styles)
return f"{prefix}{text}{self.COLORS['reset']}"
def _terminal(self, text: str) -> None:
if not self.quiet:
print(text)
def _log(self, text: str = "") -> None:
self.log.write(text + "\n")
self.log.flush()
def section(self, title: str, subtitle: str = "") -> None:
plain = f"{'=' * 18} {title} {'=' * 18}"
self._log("\n" + plain)
if subtitle:
self._log(subtitle)
if not self.quiet:
print()
print(self.paint(plain, "bold", "cyan"))
if subtitle:
print(self.paint(subtitle, "dim"))
def data(
self,
*,
host: str,
hour: int,
phase: str,
features: dict[str, Any],
protocol: Optional[str] = None,
) -> None:
scope = f"{protocol:<14} " if protocol else ""
plain = (
f"DATA {scope}host={host:<39} hour={hour:<8} "
f"phase={phase:<9} {json.dumps(features, sort_keys=True)}"
)
self._log(plain)
if self.show_data:
self._terminal(self.paint(plain, "blue"))
def anomaly(
self,
*,
kind: str,
host: str,
timestamp: int | float,
score: float,
reasons: list[dict[str, Any]],
protocol: Optional[str] = None,
confidence: Optional[str] = None,
responsible_flows: Optional[list[dict[str, Any]]] = None,
responsible_flow_count: Optional[int] = None,
) -> None:
label = "ALERT" if kind == "ssl-flow" else "ANOMALY"
protocol_text = f" protocol={protocol}" if protocol else ""
confidence_text = f" confidence={confidence}" if confidence else ""
headline = (
f"{label:<8} type={kind}{protocol_text} host={host} ts={timestamp} "
f"score={score:.4f}{confidence_text}"
)
self._log(headline)
self._terminal(self.paint(headline, "bold", "red"))
for reason in reasons:
details = (
f" - {reason.get('feature', 'unknown')}: "
f"value={reason.get('value', 'n/a')}"
)
if "zscore" in reason:
details += (
f" z={reason['zscore']} threshold={reason.get('threshold')}"
)
if reason.get("direction"):
details += f" direction={reason['direction']}"
self._log(details)
self._terminal(self.paint(details, "yellow"))
if reason.get("explanation"):
explanation = f" {reason['explanation']}"
self._log(explanation)
self._terminal(self.paint(explanation, "dim"))
flows = responsible_flows or []
total = responsible_flow_count if responsible_flow_count is not None else len(flows)
attribution = (
f" Responsible flows: showing {len(flows)} of {total}"
)
self._log(attribution)
self._terminal(self.paint(attribution, "bold", "cyan"))
for flow in flows:
flow_line = (
f" - log={flow.get('log', 'unknown')} "
f"ts={flow.get('ts', 'n/a')} "
f"uid={flow.get('uid') or flow.get('fuid') or 'n/a'} "
f"{flow.get('src', '?')}:{flow.get('src_port', '?')} -> "
f"{flow.get('dst', '?')}:{flow.get('dst_port', '?')}"
)
detail_values = flow.get("details", {})
if detail_values:
flow_line += f" details={json.dumps(detail_values, sort_keys=True)}"
if flow.get("matched_features"):
flow_line += (
" matched="
+ ",".join(sorted(flow["matched_features"]))
)
self._log(flow_line)
self._terminal(self.paint(flow_line, "white"))
def global_anomaly(self, event: dict[str, Any]) -> None:
headline = (
f"GLOBAL host={event['host']} hour={event['hour_start']} "
f"score={event['global_score']:.4f} "
f"confidence={event['confidence']} "
f"protocols={','.join(event['protocols'])}"
)
self._log(headline)
self._terminal(self.paint(headline, "bold", "magenta"))
for protocol, contribution in sorted(
event["protocol_contributions"].items()
):
detail = f" - {protocol}: contribution={contribution:.4f}"
self._log(detail)
self._terminal(self.paint(detail, "yellow"))
flows = event.get("responsible_flows", [])
total = event.get("responsible_flow_count", len(flows))
attribution = (
f" Responsible flows: showing {len(flows)} of {total}"
)
self._log(attribution)
self._terminal(self.paint(attribution, "bold", "cyan"))
for flow in flows:
detail = (
f" - log={flow.get('log')} ts={flow.get('ts')} "
f"uid={flow.get('uid') or flow.get('fuid') or 'n/a'} "
f"{flow.get('src', '?')} -> {flow.get('dst', '?')}"
)
self._log(detail)
self._terminal(self.paint(detail, "white"))
def summary(self, title: str, rows: list[tuple[str, Any]]) -> None:
plain = f"{'=' * 18} {title} {'=' * 18}"
self._log("\n" + plain)
print()
print(self.paint(plain, "bold", "cyan"))
width = max((len(label) for label, _ in rows), default=0)
for label, value in rows:
line = f"{label:<{width}} : {value}"
self._log(line)
print(
self.paint(
line,
"green" if "anomal" not in label.lower() else "yellow",
)
)
def close(self) -> None:
self.log.close()