capsight 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- capsight/__init__.py +3 -0
- capsight/cli.py +171 -0
- capsight/codes.py +257 -0
- capsight/codes_5g.py +258 -0
- capsight/correlate.py +268 -0
- capsight/decode.py +167 -0
- capsight/engine.py +145 -0
- capsight/fields.py +80 -0
- capsight/identity.py +66 -0
- capsight/model.py +50 -0
- capsight/redact.py +79 -0
- capsight/rules.py +522 -0
- capsight/rules_5g.py +322 -0
- capsight/summary.py +116 -0
- capsight/timeline.py +238 -0
- capsight/web.py +116 -0
- capsight-0.2.0.dist-info/METADATA +261 -0
- capsight-0.2.0.dist-info/RECORD +21 -0
- capsight-0.2.0.dist-info/WHEEL +4 -0
- capsight-0.2.0.dist-info/entry_points.txt +3 -0
- capsight-0.2.0.dist-info/licenses/LICENSE +21 -0
capsight/identity.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"""Attach a subscriber identity to NAS messages that do not carry one.
|
|
2
|
+
|
|
3
|
+
After the first message of an attach, NAS stops carrying the IMSI. S1AP keeps one
|
|
4
|
+
eNB-UE-S1AP-ID per UE connection, so the IMSI seen at the start of a connection is
|
|
5
|
+
carried forward to later messages with the same ID. IDs are reused over time, so a new
|
|
6
|
+
IMSI on the same ID replaces the old one.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from collections.abc import Iterable
|
|
12
|
+
|
|
13
|
+
from capsight.model import Packet
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def s1ap_imsi_map(packets: Iterable[Packet]) -> dict[int, str]:
|
|
17
|
+
"""Return {packet number: IMSI} for S1AP/NAS packets, inferred by eNB-UE-S1AP-ID."""
|
|
18
|
+
current: dict[str, str] = {}
|
|
19
|
+
result: dict[int, str] = {}
|
|
20
|
+
for packet in packets:
|
|
21
|
+
if not packet.has("s1ap"):
|
|
22
|
+
continue
|
|
23
|
+
enb_ids = packet.fields.get("s1ap.ENB_UE_S1AP_ID", [])
|
|
24
|
+
if not enb_ids:
|
|
25
|
+
continue
|
|
26
|
+
imsi = packet.first("e212.imsi")
|
|
27
|
+
if imsi and len(enb_ids) == 1:
|
|
28
|
+
current[enb_ids[0]] = imsi
|
|
29
|
+
known = imsi or (current.get(enb_ids[0]) if len(enb_ids) == 1 else None)
|
|
30
|
+
if known:
|
|
31
|
+
result[packet.number] = known
|
|
32
|
+
return result
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _supi_from_suci(packet: Packet) -> str | None:
|
|
36
|
+
"""SUPI (IMSI) from a SUCI that uses the null protection scheme; None if concealed."""
|
|
37
|
+
msin = packet.first("nas-5gs.mm.suci.msin")
|
|
38
|
+
if not msin:
|
|
39
|
+
return None
|
|
40
|
+
scheme = packet.first("nas-5gs.mm.suci.scheme_id")
|
|
41
|
+
mcc, mnc = packet.first("e212.mcc"), packet.first("e212.mnc")
|
|
42
|
+
if scheme not in (None, "0") or not mcc or not mnc:
|
|
43
|
+
return None
|
|
44
|
+
return f"{mcc}{mnc}{msin}"
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def ngap_imsi_map(packets: Iterable[Packet]) -> dict[int, str]:
|
|
48
|
+
"""Return {packet number: IMSI} for NGAP/NAS-5GS packets, inferred by RAN-UE-NGAP-ID.
|
|
49
|
+
|
|
50
|
+
Only a SUCI with the null scheme reveals the IMSI. A concealed SUCI stays unknown.
|
|
51
|
+
"""
|
|
52
|
+
current: dict[str, str] = {}
|
|
53
|
+
result: dict[int, str] = {}
|
|
54
|
+
for packet in packets:
|
|
55
|
+
if not packet.has("ngap"):
|
|
56
|
+
continue
|
|
57
|
+
ids = set(packet.fields.get("ngap.RAN_UE_NGAP_ID", []))
|
|
58
|
+
if len(ids) != 1:
|
|
59
|
+
continue
|
|
60
|
+
ue = ids.pop()
|
|
61
|
+
supi = _supi_from_suci(packet)
|
|
62
|
+
if supi:
|
|
63
|
+
current[ue] = supi
|
|
64
|
+
if ue in current:
|
|
65
|
+
result[packet.number] = current[ue]
|
|
66
|
+
return result
|
capsight/model.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"""Core data model shared by the decoder, correlator and rule engine."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
from enum import StrEnum
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass(frozen=True, slots=True)
|
|
10
|
+
class Packet:
|
|
11
|
+
"""One decoded frame, reduced to the fields capsight cares about."""
|
|
12
|
+
|
|
13
|
+
number: int
|
|
14
|
+
time: float
|
|
15
|
+
protocols: tuple[str, ...]
|
|
16
|
+
fields: dict[str, list[str]] = field(default_factory=dict)
|
|
17
|
+
|
|
18
|
+
def has(self, protocol: str) -> bool:
|
|
19
|
+
return protocol in self.protocols
|
|
20
|
+
|
|
21
|
+
def first(self, name: str) -> str | None:
|
|
22
|
+
values = self.fields.get(name)
|
|
23
|
+
return values[0] if values else None
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class Severity(StrEnum):
|
|
27
|
+
INFO = "info"
|
|
28
|
+
WARNING = "warning"
|
|
29
|
+
FAILURE = "failure"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@dataclass(frozen=True)
|
|
33
|
+
class Finding:
|
|
34
|
+
"""A diagnosis produced by a rule. Every finding cites its evidence packets."""
|
|
35
|
+
|
|
36
|
+
rule_id: str
|
|
37
|
+
severity: Severity
|
|
38
|
+
verdict: str
|
|
39
|
+
why: str
|
|
40
|
+
evidence: tuple[int, ...]
|
|
41
|
+
fix: str = ""
|
|
42
|
+
subscribers: tuple[str, ...] = ()
|
|
43
|
+
count: int = 1
|
|
44
|
+
# When it happened: first/last occurrence (epoch seconds) and occurrences per minute,
|
|
45
|
+
# as (minute start epoch, count). Used by summary mode.
|
|
46
|
+
first_time: float | None = None
|
|
47
|
+
last_time: float | None = None
|
|
48
|
+
per_minute: tuple[tuple[int, int], ...] = ()
|
|
49
|
+
# Occurrences per subscriber, most affected first (top 20).
|
|
50
|
+
by_subscriber: tuple[tuple[str, int], ...] = ()
|
capsight/redact.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"""--redact: make capsight output safe to paste into a ticket or chat.
|
|
2
|
+
|
|
3
|
+
Subscriber numbers (IMSI, MSISDN), IP addresses and host / realm names are replaced with
|
|
4
|
+
stable placeholders. The same value always maps to the same placeholder within one run, so
|
|
5
|
+
"which subscribers are the same" survives; the original values never appear.
|
|
6
|
+
|
|
7
|
+
IMSIs keep their first 5 digits (MCC + 2-digit MNC) because the home network is often the
|
|
8
|
+
point of the diagnosis (roaming, partner). Use --redact-all to hide those too.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import ipaddress
|
|
14
|
+
import re
|
|
15
|
+
from typing import Any
|
|
16
|
+
|
|
17
|
+
DIGITS = re.compile(r"(?<![\w.])\d{10,15}(?![\w.])")
|
|
18
|
+
IPV4 = re.compile(r"(?<![\d.])(?:\d{1,3}\.){3}\d{1,3}(?![\d.])")
|
|
19
|
+
IPV6 = re.compile(r"(?<![\w:])(?:[0-9a-fA-F]{0,4}:){2,7}[0-9a-fA-F]{0,4}(?![\w:])")
|
|
20
|
+
# host names / Diameter realms: three or more labels, last one alphabetic (not file names)
|
|
21
|
+
HOST = re.compile(
|
|
22
|
+
r"(?<![\w.-])(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.){2,}[A-Za-z]{2,24}(?![\w-])"
|
|
23
|
+
)
|
|
24
|
+
FILE_SUFFIXES = (".pcap", ".pcapng", ".cap", ".json", ".txt")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class Redactor:
|
|
28
|
+
def __init__(self, keep_plmn: bool = True) -> None:
|
|
29
|
+
self.keep_plmn = keep_plmn
|
|
30
|
+
self.maps: dict[str, dict[str, str]] = {"sub": {}, "ip": {}, "host": {}}
|
|
31
|
+
|
|
32
|
+
def _label(self, kind: str, value: str, make) -> str:
|
|
33
|
+
table = self.maps[kind]
|
|
34
|
+
if value not in table:
|
|
35
|
+
table[value] = make(len(table) + 1)
|
|
36
|
+
return table[value]
|
|
37
|
+
|
|
38
|
+
def _sub(self, match: re.Match[str]) -> str:
|
|
39
|
+
value = match.group(0)
|
|
40
|
+
if self.keep_plmn and len(value) >= 14:
|
|
41
|
+
return self._label("sub", value, lambda n: f"{value[:5]}-sub{n}")
|
|
42
|
+
return self._label("sub", value, lambda n: f"sub{n}")
|
|
43
|
+
|
|
44
|
+
def _ip(self, match: re.Match[str]) -> str:
|
|
45
|
+
value = match.group(0)
|
|
46
|
+
try:
|
|
47
|
+
ipaddress.ip_address(value)
|
|
48
|
+
except ValueError:
|
|
49
|
+
return value
|
|
50
|
+
return self._label("ip", value, lambda n: f"ip{n}")
|
|
51
|
+
|
|
52
|
+
def _host(self, match: re.Match[str]) -> str:
|
|
53
|
+
value = match.group(0)
|
|
54
|
+
if value.lower().endswith(FILE_SUFFIXES) or value.lower().startswith(("e.g", "i.e")):
|
|
55
|
+
return value
|
|
56
|
+
return self._label("host", value.lower(), lambda n: f"host{n}.invalid")
|
|
57
|
+
|
|
58
|
+
def text(self, value: str) -> str:
|
|
59
|
+
value = IPV4.sub(self._ip, value)
|
|
60
|
+
value = IPV6.sub(self._ip, value)
|
|
61
|
+
value = HOST.sub(self._host, value)
|
|
62
|
+
return DIGITS.sub(self._sub, value)
|
|
63
|
+
|
|
64
|
+
def data(self, value: Any) -> Any:
|
|
65
|
+
"""Redact every string in a JSON-like structure (keys included)."""
|
|
66
|
+
if isinstance(value, str):
|
|
67
|
+
return self.text(value)
|
|
68
|
+
if isinstance(value, dict):
|
|
69
|
+
return {self.data(k): self.data(v) for k, v in value.items()}
|
|
70
|
+
if isinstance(value, (list, tuple)):
|
|
71
|
+
return [self.data(v) for v in value]
|
|
72
|
+
return value
|
|
73
|
+
|
|
74
|
+
def counts(self) -> str:
|
|
75
|
+
m = self.maps
|
|
76
|
+
return (
|
|
77
|
+
f"redacted {len(m['sub'])} subscriber numbers, {len(m['ip'])} IP addresses, "
|
|
78
|
+
f"{len(m['host'])} host/realm names"
|
|
79
|
+
)
|