plainshark 0.1.0__tar.gz

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.
Files changed (29) hide show
  1. plainshark-0.1.0/PKG-INFO +164 -0
  2. plainshark-0.1.0/README.md +142 -0
  3. plainshark-0.1.0/pyproject.toml +38 -0
  4. plainshark-0.1.0/setup.cfg +4 -0
  5. plainshark-0.1.0/src/plainshark/__init__.py +4 -0
  6. plainshark-0.1.0/src/plainshark/adapters/__init__.py +26 -0
  7. plainshark-0.1.0/src/plainshark/adapters/base.py +25 -0
  8. plainshark-0.1.0/src/plainshark/adapters/wireshark.py +66 -0
  9. plainshark-0.1.0/src/plainshark/cli.py +391 -0
  10. plainshark-0.1.0/src/plainshark/display.py +110 -0
  11. plainshark-0.1.0/src/plainshark/providers/__init__.py +38 -0
  12. plainshark-0.1.0/src/plainshark/providers/base.py +22 -0
  13. plainshark-0.1.0/src/plainshark/providers/naive.py +31 -0
  14. plainshark-0.1.0/src/plainshark/providers/openai_compat.py +106 -0
  15. plainshark-0.1.0/src/plainshark/runner.py +177 -0
  16. plainshark-0.1.0/src/plainshark/scoring.py +94 -0
  17. plainshark-0.1.0/src/plainshark/task.py +92 -0
  18. plainshark-0.1.0/src/plainshark.egg-info/PKG-INFO +164 -0
  19. plainshark-0.1.0/src/plainshark.egg-info/SOURCES.txt +27 -0
  20. plainshark-0.1.0/src/plainshark.egg-info/dependency_links.txt +1 -0
  21. plainshark-0.1.0/src/plainshark.egg-info/entry_points.txt +2 -0
  22. plainshark-0.1.0/src/plainshark.egg-info/requires.txt +7 -0
  23. plainshark-0.1.0/src/plainshark.egg-info/top_level.txt +1 -0
  24. plainshark-0.1.0/tests/test_check.py +76 -0
  25. plainshark-0.1.0/tests/test_display.py +34 -0
  26. plainshark-0.1.0/tests/test_naive_provider.py +19 -0
  27. plainshark-0.1.0/tests/test_openai_provider.py +72 -0
  28. plainshark-0.1.0/tests/test_scoring.py +68 -0
  29. plainshark-0.1.0/tests/test_task.py +55 -0
@@ -0,0 +1,164 @@
1
+ Metadata-Version: 2.4
2
+ Name: plainshark
3
+ Version: 0.1.0
4
+ Summary: Turn plain English into Wireshark/tshark display filters, offline
5
+ Author: Aashish Thapa
6
+ License: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/aashish-thapa/plainshark
8
+ Project-URL: Repository, https://github.com/aashish-thapa/plainshark
9
+ Keywords: wireshark,tshark,pcap,network,display-filter,llm,offline
10
+ Classifier: Environment :: Console
11
+ Classifier: Intended Audience :: System Administrators
12
+ Classifier: License :: OSI Approved :: Apache Software License
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Topic :: System :: Networking :: Monitoring
15
+ Requires-Python: >=3.9
16
+ Description-Content-Type: text/markdown
17
+ Provides-Extra: dev
18
+ Requires-Dist: pytest; extra == "dev"
19
+ Provides-Extra: publish
20
+ Requires-Dist: build; extra == "publish"
21
+ Requires-Dist: twine; extra == "publish"
22
+
23
+ # plainshark
24
+
25
+ **Turn plain English into Wireshark / tshark filters, with a small model that runs
26
+ offline on your own machine — plus the execution-graded benchmark that proves it.**
27
+
28
+ ```
29
+ $ plainshark ask "TCP packets that were retransmitted"
30
+ TCP packets that were retransmitted -> tcp.analysis.retransmission
31
+
32
+ $ plainshark grep capture.pcap "the SMTP traffic"
33
+ # Time Source Destination Proto Info
34
+ 4 0.437 2607:f8b0:400c:c03::1a 2001:470:e5bf:dead:49… SMTP S: 220 mx.google.com ESMTP …
35
+ 3 packet(s)
36
+ ```
37
+
38
+ You describe what you want; it finds the packets. No memorizing Wireshark's
39
+ thousands of field names. The model is ~940 MB, runs on **CPU**, and nothing
40
+ leaves your machine.
41
+
42
+ ## Install & use
43
+
44
+ ```bash
45
+ pip install -e . # or: pipx install .
46
+ plainshark serve & # start the local model (needs llama.cpp on PATH)
47
+ plainshark ask "failed TLS handshakes"
48
+ plainshark grep capture.pcap "DNS responses"
49
+ ```
50
+
51
+ On NixOS, `./scripts/serve.sh` launches llama.cpp via nix. `ask`/`grep` talk to any
52
+ OpenAI-compatible endpoint (`--base-url`), so you can also point them at Ollama or
53
+ a cloud API.
54
+
55
+ ## How good is it? (measured, not claimed)
56
+
57
+ plainshark grades by **running** the generated filter against real captures and
58
+ checking it selected exactly the right packets — no LLM-as-judge. On a 42-task
59
+ benchmark (26 synthetic + 12 independent on real pcaps + 4 held-out):
60
+
61
+ | | common fields | real-pcap, unseen fields | held-out protocol |
62
+ |---|---|---|---|
63
+ | naive keyword | 31% | 0% | — |
64
+ | Qwen-1.5B untuned | 50% | 17% | — |
65
+ | **fine-tuned (offline Q4)** | **92%** | **~92–100%** | **0/4** |
66
+
67
+ **Honest:** strong on common filters; weaker on compound boolean logic and on
68
+ protocols it was trained on *nothing* for (the held-out SCTP frontier is 0/4). It
69
+ learns patterns, not magic numbers — which is why the project is benchmark + a
70
+ continuously-growing dataset. Full method and the generalization story:
71
+ `training/README.md`.
72
+
73
+ ## Why the benchmark is different
74
+
75
+ - **Score packet sets, not filter text.** Two filters that select the same frames
76
+ are equally correct. Ground truth is *executed*, never hand-labeled.
77
+ - **Thin engine, community content.** Adding a task is a JSON file; adding a tool
78
+ is one adapter. You never touch the engine to add value.
79
+ - **Local and private.** Everything — training, grading, serving — runs on your
80
+ machine. No cloud, no API keys required.
81
+
82
+ ## Installing
83
+
84
+ - **Nix / NixOS (recommended — bundles `tshark` + `llama.cpp`):**
85
+ ```bash
86
+ nix run github:aashish-thapa/plainshark -- ask "DNS traffic"
87
+ nix profile install github:aashish-thapa/plainshark # then: plainshark ...
88
+ ```
89
+ - **pipx (isolated, works where pip is externally managed):** `pipx install plainshark`
90
+ - **Model:** `plainshark setup --repo <hf-user>/plainshark-ft` downloads the GGUF
91
+ to `~/.cache/plainshark/`, which `plainshark serve` then uses.
92
+
93
+ ## Benchmark / development
94
+
95
+ ```bash
96
+ pip install -e ".[dev]" # in a venv if your system Python is managed
97
+ pytest # the scorer's logic, no tshark needed
98
+ plainshark validate # load and check the task set
99
+ plainshark score --provider naive # grade a baseline (needs tshark + fixtures)
100
+ ```
101
+
102
+ `naive` is a deliberately dumb keyword baseline. Its job is to **fail** — if it
103
+ scores well, the task set is too easy and needs harder cases.
104
+
105
+ ## How a task works
106
+
107
+ ```json
108
+ {
109
+ "id": "l4-tcp-retransmissions",
110
+ "prompt": "TCP packets that were retransmitted",
111
+ "tool": "wireshark-display-filter",
112
+ "fixture": "fixtures/tcp-retransmits.pcap",
113
+ "reference_filters": ["tcp.analysis.retransmission"],
114
+ "stratum": "L4"
115
+ }
116
+ ```
117
+
118
+ The reference filter is the label: run it against the fixture and the frames it
119
+ selects are the correct answer. A candidate passes if it selects the same set.
120
+ Tasks are stratified L1 (single field) through L5 (stateful) so results show
121
+ *where* a model breaks, not just an average.
122
+
123
+ ## Architecture
124
+
125
+ ```
126
+ engine (yours, stable) content (community, grows)
127
+ scoring.py set comparison tasks/ one JSON per task
128
+ task.py load + validate fixtures/ the captures tasks run against
129
+ adapters/ one per tool <------- referenced by task.tool
130
+ providers/ one per candidate source (naive | model | api | base)
131
+ runner.py orchestrates tasks x providers
132
+ cli.py validate | score
133
+ ```
134
+
135
+ - A new target tool (tcpdump/BPF, `dig`, nftables) = a new **adapter**.
136
+ - A new candidate (your fine-tune, GPT-4, the base model) = a new **provider**.
137
+ - Neither touches the scorer.
138
+
139
+ ## Contributing
140
+
141
+ The point of the project is that you can contribute a test case without reading
142
+ the engine. Add a `content/tasks/*.json` with a prompt, a correct reference
143
+ filter, and a fixture; CI grades that the reference actually compiles and selects
144
+ a non-trivial set. (Contribution flow and CI: coming with the first fixtures.)
145
+
146
+ ## Publishing (maintainers)
147
+
148
+ Everything is in place; shipping to the world is three uploads:
149
+
150
+ ```bash
151
+ # 1. the model -> HuggingFace (so `plainshark setup` can fetch it)
152
+ huggingface-cli upload <hf-user>/plainshark-ft training/out/plainshark-ft-q4.gguf
153
+
154
+ # 2. the package -> PyPI (so `pipx install plainshark` works)
155
+ pip install -e ".[publish]"
156
+ python -m build && twine upload dist/*
157
+
158
+ # 3. the repo -> GitHub (CI runs on push; enables `nix run github:...`)
159
+ gh repo create plainshark --public --source . --push
160
+ ```
161
+
162
+ ## License
163
+
164
+ Apache-2.0.
@@ -0,0 +1,142 @@
1
+ # plainshark
2
+
3
+ **Turn plain English into Wireshark / tshark filters, with a small model that runs
4
+ offline on your own machine — plus the execution-graded benchmark that proves it.**
5
+
6
+ ```
7
+ $ plainshark ask "TCP packets that were retransmitted"
8
+ TCP packets that were retransmitted -> tcp.analysis.retransmission
9
+
10
+ $ plainshark grep capture.pcap "the SMTP traffic"
11
+ # Time Source Destination Proto Info
12
+ 4 0.437 2607:f8b0:400c:c03::1a 2001:470:e5bf:dead:49… SMTP S: 220 mx.google.com ESMTP …
13
+ 3 packet(s)
14
+ ```
15
+
16
+ You describe what you want; it finds the packets. No memorizing Wireshark's
17
+ thousands of field names. The model is ~940 MB, runs on **CPU**, and nothing
18
+ leaves your machine.
19
+
20
+ ## Install & use
21
+
22
+ ```bash
23
+ pip install -e . # or: pipx install .
24
+ plainshark serve & # start the local model (needs llama.cpp on PATH)
25
+ plainshark ask "failed TLS handshakes"
26
+ plainshark grep capture.pcap "DNS responses"
27
+ ```
28
+
29
+ On NixOS, `./scripts/serve.sh` launches llama.cpp via nix. `ask`/`grep` talk to any
30
+ OpenAI-compatible endpoint (`--base-url`), so you can also point them at Ollama or
31
+ a cloud API.
32
+
33
+ ## How good is it? (measured, not claimed)
34
+
35
+ plainshark grades by **running** the generated filter against real captures and
36
+ checking it selected exactly the right packets — no LLM-as-judge. On a 42-task
37
+ benchmark (26 synthetic + 12 independent on real pcaps + 4 held-out):
38
+
39
+ | | common fields | real-pcap, unseen fields | held-out protocol |
40
+ |---|---|---|---|
41
+ | naive keyword | 31% | 0% | — |
42
+ | Qwen-1.5B untuned | 50% | 17% | — |
43
+ | **fine-tuned (offline Q4)** | **92%** | **~92–100%** | **0/4** |
44
+
45
+ **Honest:** strong on common filters; weaker on compound boolean logic and on
46
+ protocols it was trained on *nothing* for (the held-out SCTP frontier is 0/4). It
47
+ learns patterns, not magic numbers — which is why the project is benchmark + a
48
+ continuously-growing dataset. Full method and the generalization story:
49
+ `training/README.md`.
50
+
51
+ ## Why the benchmark is different
52
+
53
+ - **Score packet sets, not filter text.** Two filters that select the same frames
54
+ are equally correct. Ground truth is *executed*, never hand-labeled.
55
+ - **Thin engine, community content.** Adding a task is a JSON file; adding a tool
56
+ is one adapter. You never touch the engine to add value.
57
+ - **Local and private.** Everything — training, grading, serving — runs on your
58
+ machine. No cloud, no API keys required.
59
+
60
+ ## Installing
61
+
62
+ - **Nix / NixOS (recommended — bundles `tshark` + `llama.cpp`):**
63
+ ```bash
64
+ nix run github:aashish-thapa/plainshark -- ask "DNS traffic"
65
+ nix profile install github:aashish-thapa/plainshark # then: plainshark ...
66
+ ```
67
+ - **pipx (isolated, works where pip is externally managed):** `pipx install plainshark`
68
+ - **Model:** `plainshark setup --repo <hf-user>/plainshark-ft` downloads the GGUF
69
+ to `~/.cache/plainshark/`, which `plainshark serve` then uses.
70
+
71
+ ## Benchmark / development
72
+
73
+ ```bash
74
+ pip install -e ".[dev]" # in a venv if your system Python is managed
75
+ pytest # the scorer's logic, no tshark needed
76
+ plainshark validate # load and check the task set
77
+ plainshark score --provider naive # grade a baseline (needs tshark + fixtures)
78
+ ```
79
+
80
+ `naive` is a deliberately dumb keyword baseline. Its job is to **fail** — if it
81
+ scores well, the task set is too easy and needs harder cases.
82
+
83
+ ## How a task works
84
+
85
+ ```json
86
+ {
87
+ "id": "l4-tcp-retransmissions",
88
+ "prompt": "TCP packets that were retransmitted",
89
+ "tool": "wireshark-display-filter",
90
+ "fixture": "fixtures/tcp-retransmits.pcap",
91
+ "reference_filters": ["tcp.analysis.retransmission"],
92
+ "stratum": "L4"
93
+ }
94
+ ```
95
+
96
+ The reference filter is the label: run it against the fixture and the frames it
97
+ selects are the correct answer. A candidate passes if it selects the same set.
98
+ Tasks are stratified L1 (single field) through L5 (stateful) so results show
99
+ *where* a model breaks, not just an average.
100
+
101
+ ## Architecture
102
+
103
+ ```
104
+ engine (yours, stable) content (community, grows)
105
+ scoring.py set comparison tasks/ one JSON per task
106
+ task.py load + validate fixtures/ the captures tasks run against
107
+ adapters/ one per tool <------- referenced by task.tool
108
+ providers/ one per candidate source (naive | model | api | base)
109
+ runner.py orchestrates tasks x providers
110
+ cli.py validate | score
111
+ ```
112
+
113
+ - A new target tool (tcpdump/BPF, `dig`, nftables) = a new **adapter**.
114
+ - A new candidate (your fine-tune, GPT-4, the base model) = a new **provider**.
115
+ - Neither touches the scorer.
116
+
117
+ ## Contributing
118
+
119
+ The point of the project is that you can contribute a test case without reading
120
+ the engine. Add a `content/tasks/*.json` with a prompt, a correct reference
121
+ filter, and a fixture; CI grades that the reference actually compiles and selects
122
+ a non-trivial set. (Contribution flow and CI: coming with the first fixtures.)
123
+
124
+ ## Publishing (maintainers)
125
+
126
+ Everything is in place; shipping to the world is three uploads:
127
+
128
+ ```bash
129
+ # 1. the model -> HuggingFace (so `plainshark setup` can fetch it)
130
+ huggingface-cli upload <hf-user>/plainshark-ft training/out/plainshark-ft-q4.gguf
131
+
132
+ # 2. the package -> PyPI (so `pipx install plainshark` works)
133
+ pip install -e ".[publish]"
134
+ python -m build && twine upload dist/*
135
+
136
+ # 3. the repo -> GitHub (CI runs on push; enables `nix run github:...`)
137
+ gh repo create plainshark --public --source . --push
138
+ ```
139
+
140
+ ## License
141
+
142
+ Apache-2.0.
@@ -0,0 +1,38 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "plainshark"
7
+ version = "0.1.0"
8
+ description = "Turn plain English into Wireshark/tshark display filters, offline"
9
+ readme = "README.md"
10
+ requires-python = ">=3.9"
11
+ license = { text = "Apache-2.0" }
12
+ authors = [{ name = "Aashish Thapa" }]
13
+ keywords = ["wireshark", "tshark", "pcap", "network", "display-filter", "llm", "offline"]
14
+ classifiers = [
15
+ "Environment :: Console",
16
+ "Intended Audience :: System Administrators",
17
+ "License :: OSI Approved :: Apache Software License",
18
+ "Programming Language :: Python :: 3",
19
+ "Topic :: System :: Networking :: Monitoring",
20
+ ]
21
+ dependencies = [] # engine is stdlib-only; grading shells out to tshark, serving to llama.cpp
22
+
23
+ [project.urls]
24
+ Homepage = "https://github.com/aashish-thapa/plainshark"
25
+ Repository = "https://github.com/aashish-thapa/plainshark"
26
+
27
+ [project.optional-dependencies]
28
+ dev = ["pytest"]
29
+ publish = ["build", "twine"]
30
+
31
+ [project.scripts]
32
+ plainshark = "plainshark.cli:main"
33
+
34
+ [tool.setuptools.packages.find]
35
+ where = ["src"]
36
+
37
+ [tool.pytest.ini_options]
38
+ testpaths = ["tests"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,4 @@
1
+ """plainshark: an open, local, execution-graded benchmark for natural-language -> network filters."""
2
+ from __future__ import annotations
3
+
4
+ __version__ = "0.1.0"
@@ -0,0 +1,26 @@
1
+ """Tool adapters. Register a new tool here; nothing else changes."""
2
+ from __future__ import annotations
3
+
4
+ from typing import Callable, Dict
5
+
6
+ from .base import Adapter
7
+ from .wireshark import WiresharkDisplayFilterAdapter
8
+
9
+ # Lazy factories: building an adapter may require an external tool (tshark), so we
10
+ # only construct the one a run actually needs.
11
+ ADAPTER_FACTORIES: Dict[str, Callable[[], Adapter]] = {
12
+ WiresharkDisplayFilterAdapter.name: WiresharkDisplayFilterAdapter,
13
+ }
14
+
15
+
16
+ def build_adapter(tool: str) -> Adapter:
17
+ try:
18
+ factory = ADAPTER_FACTORIES[tool]
19
+ except KeyError:
20
+ raise KeyError(
21
+ f"no adapter for tool '{tool}'; known tools: {sorted(ADAPTER_FACTORIES)}"
22
+ )
23
+ return factory()
24
+
25
+
26
+ __all__ = ["Adapter", "WiresharkDisplayFilterAdapter", "build_adapter", "ADAPTER_FACTORIES"]
@@ -0,0 +1,25 @@
1
+ """The one seam for a target tool.
2
+
3
+ An adapter knows how to run a filter for ONE tool (Wireshark display filter,
4
+ tcpdump/BPF, ...) against a fixture and report the frames it selects. Adding a
5
+ new tool is a new adapter, never an edit to the scorer or runner.
6
+ """
7
+ from __future__ import annotations
8
+
9
+ from typing import Optional, Protocol, runtime_checkable
10
+
11
+ from ..scoring import FrameSet
12
+
13
+
14
+ @runtime_checkable
15
+ class Adapter(Protocol):
16
+ name: str
17
+
18
+ def select(self, filter_str: str, fixture_path: str) -> Optional[FrameSet]:
19
+ """Return the frame numbers `filter_str` selects on `fixture_path`.
20
+
21
+ Return `None` if the filter is invalid or cannot be executed; the scorer
22
+ treats that as a failed (invalid) candidate. An empty frozenset means the
23
+ filter ran and matched nothing.
24
+ """
25
+ ...
@@ -0,0 +1,66 @@
1
+ """Wireshark display-filter adapter: grade a filter by running tshark.
2
+
3
+ `tshark -r <pcap> -Y <filter> -T fields -e frame.number` prints the frame number
4
+ of every packet the display filter selects. That set is the ground truth.
5
+ """
6
+ from __future__ import annotations
7
+
8
+ import shutil
9
+ import subprocess
10
+ from typing import Optional
11
+
12
+ from ..scoring import FrameSet
13
+
14
+
15
+ class TsharkNotFound(RuntimeError):
16
+ pass
17
+
18
+
19
+ class WiresharkDisplayFilterAdapter:
20
+ name = "wireshark-display-filter"
21
+
22
+ def __init__(self, tshark_path: Optional[str] = None, timeout: float = 30.0):
23
+ resolved = tshark_path or shutil.which("tshark")
24
+ if resolved is None:
25
+ raise TsharkNotFound(
26
+ "tshark not found on PATH; install Wireshark CLI to run grading"
27
+ )
28
+ self._tshark = resolved
29
+ self._timeout = timeout
30
+
31
+ def select(self, filter_str: str, fixture_path: str) -> Optional[FrameSet]:
32
+ if not filter_str.strip():
33
+ return None
34
+ try:
35
+ result = subprocess.run(
36
+ [
37
+ self._tshark,
38
+ "-r",
39
+ fixture_path,
40
+ "-Y",
41
+ filter_str,
42
+ "-T",
43
+ "fields",
44
+ "-e",
45
+ "frame.number",
46
+ ],
47
+ capture_output=True,
48
+ text=True,
49
+ timeout=self._timeout,
50
+ )
51
+ except subprocess.TimeoutExpired:
52
+ return None
53
+
54
+ # tshark exits non-zero when the display filter is syntactically invalid.
55
+ if result.returncode != 0:
56
+ return None
57
+
58
+ frames = set()
59
+ for line in result.stdout.splitlines():
60
+ line = line.strip()
61
+ if line:
62
+ try:
63
+ frames.add(int(line))
64
+ except ValueError:
65
+ return None
66
+ return frozenset(frames)