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/web.py ADDED
@@ -0,0 +1,116 @@
1
+ """Upload-a-capture web demo (Gradio). Install with: pip install "capsight[web]".
2
+
3
+ capsight-web # http://127.0.0.1:7860
4
+
5
+ Uploads are analyzed in a temporary directory and deleted right after the request.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import os
11
+ import shutil
12
+ import tempfile
13
+ from pathlib import Path
14
+
15
+ from capsight.cli import render_text
16
+ from capsight.decode import TsharkError
17
+ from capsight.engine import analyze
18
+
19
+ MAX_BYTES = 20 * 1024 * 1024
20
+ ALLOWED = (".pcap", ".pcapng", ".cap")
21
+ # Demo captures: from a source checkout, or CAPSIGHT_SAMPLES when installed (e.g. Docker).
22
+ SAMPLES = Path(
23
+ os.environ.get(
24
+ "CAPSIGHT_SAMPLES",
25
+ Path(__file__).resolve().parent.parent.parent / "samples" / "synthetic",
26
+ )
27
+ )
28
+
29
+ INTRO = """# capsight
30
+ Drop in a 4G/5G packet capture (`.pcap` / `.pcapng`, up to 20 MB) and get a plain-English
31
+ diagnosis of why a device couldn't attach, register or get data.
32
+
33
+ The file is analyzed and deleted right away; nothing is stored. Do not upload captures with
34
+ real subscriber data to a public demo: run capsight locally for those.
35
+ """
36
+
37
+
38
+ def analyze_upload(path: str, display_name: str | None = None):
39
+ """Analyze a file and return (Report, None) or (None, error message).
40
+
41
+ The file is copied to a private temp dir, analyzed and deleted (unless it is a demo
42
+ capture). Used by the Streamlit page, which renders the Report itself.
43
+ """
44
+ src = Path(path)
45
+ if src.suffix.lower() not in ALLOWED:
46
+ return None, f"Unsupported file type {src.suffix!r}. Use .pcap or .pcapng."
47
+ if src.stat().st_size > MAX_BYTES:
48
+ return None, "File is larger than 20 MB. Run capsight locally for big captures."
49
+ workdir = Path(tempfile.mkdtemp(prefix="capsight-"))
50
+ try:
51
+ copy = workdir / f"upload{src.suffix.lower()}"
52
+ shutil.copyfile(src, copy)
53
+ report = analyze(copy)
54
+ report.capture = display_name or src.name
55
+ return report, None
56
+ except TsharkError as exc:
57
+ return None, f"Could not read this capture: {exc}"
58
+ finally:
59
+ shutil.rmtree(workdir, ignore_errors=True)
60
+ if not _is_sample(src):
61
+ src.unlink(missing_ok=True)
62
+
63
+
64
+ def diagnose(path: str | None) -> str:
65
+ """Analyze an uploaded file and return the text report (or a readable error)."""
66
+ if not path:
67
+ return "Upload a capture first."
68
+ src = Path(path)
69
+ if src.suffix.lower() not in ALLOWED:
70
+ return f"Unsupported file type {src.suffix!r}. Use .pcap or .pcapng."
71
+ if src.stat().st_size > MAX_BYTES:
72
+ return "File is larger than 20 MB. Run capsight locally for big captures."
73
+ workdir = Path(tempfile.mkdtemp(prefix="capsight-"))
74
+ try:
75
+ copy = workdir / f"upload{src.suffix.lower()}"
76
+ shutil.copyfile(src, copy)
77
+ report = analyze(copy)
78
+ report.capture = src.name
79
+ return render_text(report)
80
+ except TsharkError as exc:
81
+ return f"Could not read this capture: {exc}"
82
+ finally:
83
+ shutil.rmtree(workdir, ignore_errors=True)
84
+ if not _is_sample(src):
85
+ src.unlink(missing_ok=True)
86
+
87
+
88
+ def _is_sample(path: Path) -> bool:
89
+ """Example files (or the copy the web framework makes of them) are never deleted."""
90
+ if path.parent == SAMPLES:
91
+ return True
92
+ original = SAMPLES / path.name
93
+ return original.is_file() and original.stat().st_size == path.stat().st_size
94
+
95
+
96
+ def build_app():
97
+ import gradio as gr
98
+
99
+ examples = [[str(p)] for p in sorted(SAMPLES.glob("*.pcap"))]
100
+ with gr.Blocks(title="capsight") as app:
101
+ gr.Markdown(INTRO)
102
+ upload = gr.File(label="Capture", file_types=list(ALLOWED), type="filepath")
103
+ button = gr.Button("Diagnose", variant="primary")
104
+ output = gr.Code(label="Diagnosis", language=None, lines=18)
105
+ button.click(diagnose, inputs=upload, outputs=output)
106
+ if examples:
107
+ gr.Examples(examples, inputs=upload, label="Try a demo capture")
108
+ return app
109
+
110
+
111
+ def main() -> None:
112
+ build_app().launch()
113
+
114
+
115
+ if __name__ == "__main__":
116
+ main()
@@ -0,0 +1,261 @@
1
+ Metadata-Version: 2.5
2
+ Name: capsight
3
+ Version: 0.2.0
4
+ Summary: Drop in a 4G/5G packet capture, get a plain-English diagnosis of why a device couldn't attach, register or get data.
5
+ Project-URL: Homepage, https://github.com/Kx96xx/capsight
6
+ Project-URL: Issues, https://github.com/Kx96xx/capsight/issues
7
+ Author: Kx96xx
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Keywords: 5g,diameter,gtp,lte,ngap,pcap,pfcp,telecom,tshark,wireshark
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Environment :: Console
13
+ Classifier: Intended Audience :: Telecommunications Industry
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Topic :: System :: Networking :: Monitoring
17
+ Requires-Python: >=3.11
18
+ Provides-Extra: web
19
+ Requires-Dist: gradio>=4; extra == 'web'
20
+ Description-Content-Type: text/markdown
21
+
22
+ # capsight
23
+
24
+ **Drop in a 4G/5G packet capture, get a plain-English diagnosis of why a device couldn't attach, register or get data.**
25
+
26
+ > Status: early development, v0.2. 4G core (Diameter S6a, GTPv2-C, EPS NAS, RADIUS) and 5G SA (NGAP, 5G NAS, PFCP, GTP-U) work today. See [docs/PLAN.md](https://github.com/Kx96xx/capsight/blob/main/docs/PLAN.md).
27
+
28
+ **Try it in the browser:** [capsight-dewfuba7pvrbayxcmnamqb.streamlit.app](https://capsight-dewfuba7pvrbayxcmnamqb.streamlit.app/) (pick a demo capture or upload your own; the app sleeps when idle and takes ~30 s to wake).
29
+
30
+ ![capsight diagnosing two captures](https://raw.githubusercontent.com/Kx96xx/capsight/main/docs/img/demo.svg)
31
+
32
+ Debugging a mobile core usually means scrolling through thousands of Diameter, GTP and NAS packets in Wireshark and looking up result codes and cause values by hand. capsight does that part: it pairs requests with answers, matches known failure patterns and tells you what broke, for whom, where in the capture, and what to check next.
33
+
34
+
35
+ ## Who it is for
36
+
37
+ - **Core / NOC engineers**: a customer says "my device won't connect". Grab a capture, run capsight, get the reject and the subscriber in seconds instead of reading packets.
38
+ - **Private LTE/5G and lab builders** (Open5GS, free5GC, srsRAN): find out why your phone won't attach to the network you just set up.
39
+ - **Students and people learning telecom**: see what a failure means in plain words, with the packet numbers to open in Wireshark.
40
+ - **Automation**: run it in a script or CI job on captures from a test lab and fail the build if a reject appears.
41
+
42
+ ## Install
43
+
44
+ You need Python 3.11+ and `tshark` (comes with Wireshark).
45
+
46
+ ```bash
47
+ # tshark
48
+ brew install wireshark # macOS
49
+ sudo apt install tshark # Debian/Ubuntu
50
+
51
+ # capsight
52
+ pip install capsight # from PyPI (after the first release)
53
+
54
+ # or from source
55
+ git clone https://github.com/Kx96xx/capsight.git
56
+ cd capsight
57
+ uv sync
58
+ ```
59
+
60
+ ## Quick start
61
+
62
+ Generate a few demo captures (synthetic, safe to share) and analyze them:
63
+
64
+ ```bash
65
+ uv run python scripts/make_demo_captures.py
66
+ uv run capsight analyze samples/synthetic/attach-user-unknown.pcap
67
+ uv run capsight analyze samples/synthetic/radius-accept-then-reject.pcap
68
+ uv run capsight analyze samples/synthetic/ipv4v6-rejected.pcap
69
+ ```
70
+
71
+ ## How to use it
72
+
73
+ ### 1. Get a capture
74
+
75
+ capsight reads `.pcap` / `.pcapng` files. Capture on the interface that carries the signalling you care about:
76
+
77
+ | Problem | Capture where | Filter example |
78
+ |---|---|---|
79
+ | 4G: device won't attach | MME (S1-MME + S6a) | `sctp or port 3868` |
80
+ | 4G: attached but no data | MME/SGW/PGW GTP-C | `udp port 2123` |
81
+ | 4G: session rejected by AAA | PGW + RADIUS | `udp port 2123 or udp port 1812 or udp port 1813` |
82
+ | 5G: gNB or phone won't register | AMF (N2) | `sctp port 38412` |
83
+ | 5G: registered but no PDU session | AMF + SMF/UPF (N2, N4) | `sctp port 38412 or udp port 8805` |
84
+ | 5G: session up but no internet | UPF (N3 + N6) | `udp port 2152 or icmp` |
85
+
86
+ ```bash
87
+ tshark -i any -f "sctp or port 3868 or udp port 2123" -w attach.pcapng
88
+ ```
89
+
90
+ Reproduce the problem (power-cycle the device, toggle airplane mode), then stop the capture.
91
+
92
+ ### 2. Analyze
93
+
94
+ ```bash
95
+ capsight analyze attach.pcapng
96
+ ```
97
+
98
+ Each finding has:
99
+
100
+ - **Severity**: `FAILURE` (it broke), `WARNING` (degraded or partially working), `INFO` (looks odd but is normal, e.g. a first-attach SQN resync)
101
+ - **WHY**: the exact code or cause and what it means
102
+ - **WHO**: the subscriber(s) affected (IMSI)
103
+ - **WHERE**: packet numbers. Open the capture in Wireshark and jump there with `Ctrl+G`
104
+ - **FIX**: what to check next
105
+
106
+ Repeated failures are grouped into one finding with a count. Retransmissions of the same request are counted once, so a retransmission storm does not look like a thousand failures.
107
+
108
+ ### 3. Options
109
+
110
+ ```bash
111
+ capsight analyze FILE [--format text|json] [--timeout SECONDS] [--fail-on failure|warning|never]
112
+ ```
113
+
114
+ | Option | What it does |
115
+ |---|---|
116
+ | `--format json` | machine-readable output, for scripts and dashboards |
117
+ | `--timeout 5` | seconds without an answer before a request counts as unanswered (default 5) |
118
+ | `--fail-on failure` | exit code 1 if any failure is found (use in CI or scripts) |
119
+
120
+ Exit codes: `0` ok, `1` findings at the `--fail-on` level, `2` the capture could not be read.
121
+
122
+ ### Follow one subscriber
123
+
124
+ When a capture has many devices, `--imsi` shows one subscriber's steps in order and where
125
+ they stopped, plus only the findings about that subscriber:
126
+
127
+ ```
128
+ $ capsight analyze attach.pcap --imsi 001010000000001
129
+
130
+ subscriber 001010000000001
131
+ + 0.000s #7 nas Attach Request ok
132
+ + 0.001s #8,#9 diameter AIR -> AIA FAILED DIAMETER_ERROR_USER_UNKNOWN (5001)
133
+ + 0.004s #10 nas Attach Reject FAILED EMM cause #8 (...)
134
+ stopped at: AIR -> AIA (packet #9), DIAMETER_ERROR_USER_UNKNOWN (5001)
135
+ ```
136
+
137
+ If the IMSI is not in the capture, capsight lists the subscribers it did find. The IMSI can
138
+ be written as `001010000000001` or `imsi-001010000000001`. In 5G the subscriber is known only
139
+ when the SUCI is not concealed.
140
+
141
+ ### Busy captures: --summary
142
+
143
+ On a capture with thousands of failures, `--summary` gives the shape of the problem in one
144
+ screen: top problems by occurrences (with first/last time and how many subscribers),
145
+ the most affected subscribers, and problems over time:
146
+
147
+ ```
148
+ $ capsight analyze hss-5min.pcapng --summary
149
+
150
+ summary 11 problems, 32,185 occurrences, 7,553 subscribers affected
151
+
152
+ TOP PROBLEMS
153
+ 1. FAILURE 28,384x AIR rejected: DIAMETER_ERROR_USER_UNKNOWN.
154
+ 16:25:00 - 16:34:59 UTC, 6,496 subscribers
155
+ 2. FAILURE 3,327x AIR rejected: DIAMETER_ERROR_UNKNOWN_EPS_SUBSCRIPTION.
156
+ ...
157
+ MOST AFFECTED SUBSCRIBERS
158
+ 001010000000001 57x AIR rejected: DIAMETER_ERROR_USER_UNKNOWN.
159
+ ...
160
+ OVER TIME (UTC, per 1 min)
161
+ 16:25 ######################################## 5,976
162
+ 16:30 #### 559
163
+ ```
164
+
165
+ Time buckets grow with the capture (1 minute up to a day) so the chart stays under 25 rows.
166
+ `--format json --summary` adds the same data under `summary`. Without `--summary`, capsight
167
+ suggests it once a capture has 100+ problem occurrences.
168
+
169
+ ### Sharing output: --redact
170
+
171
+ `--redact` masks what should not leave your network before you paste output into a ticket
172
+ or chat. It works with every other option (`--summary`, `--imsi`, `--format json`):
173
+
174
+ - subscriber numbers (IMSI, MSISDN) become `00101-sub1`, `00101-sub2`, ... The first 5
175
+ digits (the home network) are kept because they are often the diagnosis; `--redact-all`
176
+ hides them too
177
+ - IP addresses become `ip1`, `ip2`, ...; host names and Diameter realms become `host1.invalid`
178
+ - the capture's file name becomes `capture.pcap`
179
+
180
+ The same value always gets the same placeholder within a run, so "these 40 failures are the
181
+ same subscriber" still shows. The last line says how many values were masked.
182
+
183
+ ### 4. Web demo
184
+
185
+ A small upload page, for people who would rather not use a terminal:
186
+
187
+ ```bash
188
+ pip install "capsight[web]"
189
+ capsight-web # http://127.0.0.1:7860
190
+ ```
191
+
192
+ Uploads are analyzed in a temporary folder and deleted right after. The [hosted demo](https://capsight-dewfuba7pvrbayxcmnamqb.streamlit.app/) runs
193
+ `web/streamlit_app.py` on Streamlit Community Cloud; `web/Dockerfile` builds the Gradio page
194
+ as a container for self-hosting.
195
+
196
+ ### 5. Use it in scripts / CI
197
+
198
+ ```bash
199
+ for f in captures/*.pcapng; do
200
+ capsight analyze "$f" --fail-on failure > "${f%.pcapng}.txt" || echo "FAILED: $f"
201
+ done
202
+
203
+ capsight analyze attach.pcapng --format json | jq '.findings[] | {severity, verdict, count}'
204
+ ```
205
+
206
+ ## What it detects today
207
+
208
+ **Diameter (S6a and base protocol)**
209
+ - Failure Result-Code / Experimental-Result-Code: user unknown, roaming not allowed, RAT not allowed, unknown EPS subscription, auth data unavailable, unable to deliver, too busy, ...
210
+ - Requests that never got an answer
211
+
212
+ **GTPv2-C**
213
+ - Rejections with the cause and the APN: missing or unknown APN, APN access denied, IP pool exhausted, context not found, system failure, ...
214
+ - Whether the cause came from the responder or a node further down the path
215
+ - **User authentication failed right after RADIUS Access-Accept**: credentials were fine, the gateway refused on policy
216
+ - **PDN type split**: IPv4v6 rejected while IPv4 works for the same subscriber
217
+ - Requests with no response, and retransmission folding
218
+
219
+ **NAS (EPS)**
220
+ - Attach / TAU / Service Reject with EMM cause, linked to the subscriber even when the reject carries no IMSI
221
+ - Authentication failure: MAC failure (SIM and HSS keys differ) vs sync failure (and whether it recovered)
222
+ - PDN Connectivity Reject, and whether it happened during attach (failure) or for an extra APN after a working attach (warning)
223
+
224
+ **5G SA**
225
+ - NGAP failures with the cause, e.g. NG Setup rejected for an unknown PLMN or tracking area
226
+ - Registration / Service / Security Mode Reject with 5GMM cause (no slices available, PLMN not allowed, ...)
227
+ - 5G-AKA authentication failure: MAC failure (keys mismatch) vs sync failure, and whether the network then rejected
228
+ - PDU session request refused by the AMF (e.g. 5GMM #91, DNN not subscribed), naming the DNN
229
+ - PDU Session Establishment Reject from the SMF with 5GSM cause and DNN
230
+ - PFCP rejects between SMF and UPF (rule creation failure, no association, no resources, ...) and unanswered PFCP requests
231
+ - One-way user plane: uplink enters the GTP-U tunnel and nothing comes back, with a NAT hint when a private UE address leaks onto N6
232
+ - SUPI shown when the SUCI is not concealed; never guessed when it is
233
+
234
+ Checked against real public captures, 4G and 5G: [docs/VALIDATION.md](https://github.com/Kx96xx/capsight/blob/main/docs/VALIDATION.md).
235
+
236
+ ## How it works
237
+
238
+ 1. **Decode**: `tshark` turns the capture into structured fields. capsight never writes its own dissectors.
239
+ 2. **Correlate**: requests are paired with answers (Diameter hop-by-hop + end-to-end ID, GTPv2 and PFCP sequence numbers), and NAS messages are linked to a subscriber through the S1AP / NGAP UE ID.
240
+ 3. **Diagnose**: deterministic rules match known failure patterns. Every verdict cites packet numbers. Nothing is guessed.
241
+
242
+ ## Privacy
243
+
244
+ Captures contain subscriber identities. capsight runs entirely on your machine, sends nothing anywhere, and stores nothing. Do not commit real captures: the repo's `.gitignore` blocks `*.pcap` / `*.pcapng` outside `samples/`. Use `--redact` before sharing output.
245
+
246
+ ## Roadmap
247
+
248
+ v0.2 real-capture usability (released), v0.3 operator coverage (roaming, Gx/Gy, MAP), v0.4 voice and SMS, v0.5 diagrams/HTML/MCP, v1.0 stable. Details and exit tests: [docs/ROADMAP.md](https://github.com/Kx96xx/capsight/blob/main/docs/ROADMAP.md).
249
+
250
+ ## Contributing
251
+
252
+ Public captures that show a failure are the most useful contribution. Open an issue with a link (never attach captures with real subscriber data).
253
+
254
+ ```bash
255
+ uv run pytest # tests generate their own captures; tshark required
256
+ uv run ruff check .
257
+ ```
258
+
259
+ ## License
260
+
261
+ MIT
@@ -0,0 +1,21 @@
1
+ capsight/__init__.py,sha256=PJmDU1lMv7zumCPrFy7q71Wl6flZSp4dTlXFjkyH8HI,95
2
+ capsight/cli.py,sha256=sRJONT4aXnXd-mXme5aorjg7DvQ2JtWMeh0ZVg91kj0,6440
3
+ capsight/codes.py,sha256=cgfBelA_uGzt2cx7gWjpQetMQ2hdJWxz75fRIvejsyo,9997
4
+ capsight/codes_5g.py,sha256=SswZBOi8LzbQicW20OU_3XZoYAonKzeohtXKCTuRv2Y,9940
5
+ capsight/correlate.py,sha256=-aYUAxXlwXjaqk1iviy0EdpSmUSSPBzwbUh-touLNCQ,9092
6
+ capsight/decode.py,sha256=3U5xLeXxzqDEzzwTDW0-EH5exOPHFPD1h2kxjK-KoU4,5688
7
+ capsight/engine.py,sha256=81MLXqbf_17-La8sPwRk2jY6JHfjB5KuiS2LEeJHSV0,4378
8
+ capsight/fields.py,sha256=71Xd6y7A6oYDd06DKo7_QJQMTbnjqwGF1yW4GPE8b8I,1727
9
+ capsight/identity.py,sha256=RKha2iP0KX_4L3Zh6SP5bwpmm6-M4zYz87oD0M24hl8,2338
10
+ capsight/model.py,sha256=VkArrOyOGV1VdF-nY0pgis4aP6r3wfg73Asnlk5X2BU,1424
11
+ capsight/redact.py,sha256=hVEOvFC7cWT04G16LIbQKfFF8eS5Cjl_kWNCGh9ylx8,3090
12
+ capsight/rules.py,sha256=3qQQpSO-KgrXSvJ5fIHOcX1ldwUXPnxbCFvaUZ80uAg,20443
13
+ capsight/rules_5g.py,sha256=SG0RHxq0urZkiiVMPONXBNO1rT1DtDxeeCTIRCfqzNI,12744
14
+ capsight/summary.py,sha256=-lLk07YcnopMH6MJmM4EXASveqk9DnP7wnFEcQ_9RjY,4406
15
+ capsight/timeline.py,sha256=3Eujxrfm7sDGlHUB7FOoFKvOWvktFeHeVkoBReFgNm8,8971
16
+ capsight/web.py,sha256=Ta19FvmKTatjXW4LhlMJjdcJi_aZkV3alHsiszh3H3w,4049
17
+ capsight-0.2.0.dist-info/METADATA,sha256=Upfn3JD0ZOh6n3_GsOkciSUpi43HlYHql6N1Ph5JaDI,11902
18
+ capsight-0.2.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
19
+ capsight-0.2.0.dist-info/entry_points.txt,sha256=Z3Lyh9jqdWFkOv4Jhr_lDCmz8epJH2u59YYL6GS5U4E,80
20
+ capsight-0.2.0.dist-info/licenses/LICENSE,sha256=6cBrNRXhVfnpx5-SjrSdkOEGmmJvT2W3XCKo5-7gugc,1063
21
+ capsight-0.2.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.33.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,3 @@
1
+ [console_scripts]
2
+ capsight = capsight.cli:main
3
+ capsight-web = capsight.web:main
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Kx96xx
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.