Skip to content

Ghidrust provides a hyper fast, AI native reverse engineering platform built in Rust, inspired by Ghidra. Includes experimental GPU kernels to rapidly decompile large files, a decryption suite, and network monitor for packet to binary reverse engineering.

Add ET Open install, ghidnetcap live capture, and Suricata-subset IDS.

oofz5aa381fcf7cd
diff --git a/Cargo.lock b/Cargo.lock
index f09aef3..998f98a 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -134,6 +134,15 @@ dependencies = [
"zerocopy",
]
+[[package]]
+name = "aho-corasick"
+version = "1.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c982642fa9e8606056828ee9a8505737230110bb1099153c79efe865c59d12ba"
+dependencies = [
+ "memchr",
+]
+
[[package]]
name = "android-activity"
version = "0.6.1"
@@ -453,6 +462,12 @@ version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
+[[package]]
+name = "base64"
+version = "0.22.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
+
[[package]]
name = "bincode"
version = "1.3.3"
@@ -1170,6 +1185,16 @@ dependencies = [
"simd-adler32",
]
+[[package]]
+name = "filetime"
+version = "0.2.29"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5c287a33c7f0a620c38e641e7f60827713987b3c0f26e8ddc9462cc69cf75759"
+dependencies = [
+ "cfg-if",
+ "libc",
+]
+
[[package]]
name = "find-msvc-tools"
version = "0.1.9"
@@ -1516,8 +1541,10 @@ name = "ghidrust-net-detect"
version = "0.1.1"
dependencies = [
"ghidrust-net-flow",
+ "ghidrust-net-parse",
"ghidrust-net-rules",
"ghidrust-net-schema",
+ "regex",
"serde",
"serde_json",
]
@@ -1544,9 +1571,14 @@ dependencies = [
name = "ghidrust-net-rules"
version = "0.1.1"
dependencies = [
+ "flate2",
"ghidrust-net-schema",
+ "md-5",
+ "regex",
"serde",
"serde_json",
+ "tar",
+ "ureq",
]
[[package]]
@@ -2105,6 +2137,16 @@ dependencies = [
"libc",
]
+[[package]]
+name = "md-5"
+version = "0.10.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf"
+dependencies = [
+ "cfg-if",
+ "digest",
+]
+
[[package]]
name = "memchr"
version = "2.8.3"
@@ -2953,6 +2995,35 @@ dependencies = [
"bitflags 2.13.0",
]
+[[package]]
+name = "regex"
+version = "1.13.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d"
+dependencies = [
+ "aho-corasick",
+ "memchr",
+ "regex-automata",
+ "regex-syntax",
+]
+
+[[package]]
+name = "regex-automata"
+version = "0.4.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2"
+dependencies = [
+ "aho-corasick",
+ "memchr",
+ "regex-syntax",
+]
+
+[[package]]
+name = "regex-syntax"
+version = "0.8.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4"
+
[[package]]
name = "renderdoc-sys"
version = "1.1.0"
@@ -2983,6 +3054,20 @@ dependencies = [
"windows-sys 0.59.0",
]
+[[package]]
+name = "ring"
+version = "0.17.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7"
+dependencies = [
+ "cc",
+ "cfg-if",
+ "getrandom 0.2.17",
+ "libc",
+ "untrusted",
+ "windows-sys 0.52.0",
+]
+
[[package]]
name = "rustc-hash"
version = "1.1.0"
@@ -3030,6 +3115,41 @@ dependencies = [
"windows-sys 0.61.2",
]
+[[package]]
+name = "rustls"
+version = "0.23.43"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0283386ce02abc0151e1761d08802dfe86c173b0b494af5cbc086574e453da06"
+dependencies = [
+ "log",
+ "once_cell",
+ "ring",
+ "rustls-pki-types",
+ "rustls-webpki",
+ "subtle",
+ "zeroize",
+]
+
+[[package]]
+name = "rustls-pki-types"
+version = "1.15.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2f4925028c7eb5d1fcdaf196971378ed9d2c1c4efc7dc5d011256f76c99c0a96"
+dependencies = [
+ "zeroize",
+]
+
+[[package]]
+name = "rustls-webpki"
+version = "0.103.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f3c3cf1d8b1e7d4927e2d154c3fcb02979afb9939629c62cd9048d4f07b60ac2"
+dependencies = [
+ "ring",
+ "rustls-pki-types",
+ "untrusted",
+]
+
[[package]]
name = "rustversion"
version = "1.0.23"
@@ -3332,6 +3452,12 @@ dependencies = [
"syn",
]
+[[package]]
+name = "subtle"
+version = "2.6.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
+
[[package]]
name = "syn"
version = "2.0.119"
@@ -3354,6 +3480,17 @@ dependencies = [
"syn",
]
+[[package]]
+name = "tar"
+version = "0.4.46"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3f6221d9a6003c78398e3b239969f352578258df48c8eb051caadae0015bc840"
+dependencies = [
+ "filetime",
+ "libc",
+ "xattr",
+]
+
[[package]]
name = "tempfile"
version = "3.27.0"
@@ -3592,6 +3729,28 @@ version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853"
+[[package]]
+name = "untrusted"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1"
+
+[[package]]
+name = "ureq"
+version = "2.12.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "02d1a66277ed75f640d608235660df48c8e3c19f3b4edb6a263315626cc3c01d"
+dependencies = [
+ "base64",
+ "flate2",
+ "log",
+ "once_cell",
+ "rustls",
+ "rustls-pki-types",
+ "url",
+ "webpki-roots 0.26.11",
+]
+
[[package]]
name = "url"
version = "2.5.8"
@@ -3885,6 +4044,24 @@ dependencies = [
"web-sys",
]
+[[package]]
+name = "webpki-roots"
+version = "0.26.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "521bc38abb08001b01866da9f51eb7c5d647a19260e00054a8c7fd5f9e57f7a9"
+dependencies = [
+ "webpki-roots 1.0.9",
+]
+
+[[package]]
+name = "webpki-roots"
+version = "1.0.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7dcd9d09a39985f5344844e66b0c530a33843579125f23e21e9f0f220850f22a"
+dependencies = [
+ "rustls-pki-types",
+]
+
[[package]]
name = "weezl"
version = "0.1.12"
@@ -4380,6 +4557,16 @@ version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea6fc2961e4ef194dcbfe56bb845534d0dc8098940c7e5c012a258bfec6701bd"
+[[package]]
+name = "xattr"
+version = "1.6.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "32e45ad4206f6d2479085147f02bc2ef834ac85886624a23575ae137c8aa8156"
+dependencies = [
+ "libc",
+ "rustix 1.1.4",
+]
+
[[package]]
name = "xcursor"
version = "0.3.10"
@@ -4645,6 +4832,12 @@ dependencies = [
"synstructure",
]
+[[package]]
+name = "zeroize"
+version = "1.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e13c156562582aa81c60cb29407084cdb54c4164760106ab78e6c5b0858cf64e"
+
[[package]]
name = "zerotrie"
version = "0.2.4"
diff --git a/README.md b/README.md
index 3bbef81..9d1a84f 100644
--- a/README.md
+++ b/README.md
@@ -4,7 +4,7 @@
Ghidrust loads PE/ELF (and raw blobs), produces Capstone-class multi-arch listings (23 ISAs; x86-64 primary analyze/decompile pipeline), runs Auto Analysis, decompiles to pseudo-C, parses IL2CPP metadata / Unity install inventory, and saves durable projects — from a CLI, an MCP server for agents, or an egui CodeBrowser-style GUI.
-**Ghidnet** (native network plane) attributes host sockets and capture flows to processes/images, runs in-tree GNR detection rules, and digs into the offending binary with the same RE stack.
+**Ghidnet** (native network plane) attributes host sockets and capture flows to processes/images, runs in-tree GNR / Proofpoint ET Open detection (IDS-class `alert`/`pass`/`drop` records — not kernel IPS), and digs into the offending binary with the same RE stack.
It is **not** a Ghidra fork or wrapper. Analysis logic (loaders, decode, analyzers, decompile, Ghidnet) is written in-tree so the core stays small, auditable, and freestanding.
@@ -22,7 +22,7 @@ Includes custom GPU kernels to conduct decompiling at lightning speed, this feat
| **CPU-correct first** | CPU paths are the oracle; optional GPU paths must match or enrich them, not replace honesty with speed claims |
| **Agent-ready** | Headless CLI + stdio MCP with artifact spill/drain, program identity (`path` or `project`+`file_id`), PE install inventory, RTTI catalog query, UTF-16 xref support, function create / bounded disasm / call graphs, IL2CPP touch-map + body proof, live process bridge (Windows), **Ghidnet** (`net_*`, `tool_surface` ≥ 8), and egui panes for every surface. **2026-07-19:** Windows agent disasm pipeline + bounds honesty (`--brief`/`--pretty`, `bounds_suspect`, `listing_text`) — see [CHANGELOG.md](CHANGELOG.md) |
| **Practical projects** | Create a workspace, import binaries, run analyzers, persist results (`analysis.bin`), reopen later |
-| **Ghidnet (native)** | Process-attributed sockets/flows → GNR alerts → dig into image (`load`/`analyze`/`decompile` / live process). In-tree only — see [Ghidnet](#ghidnet-native-network-plane) |
+| **Ghidnet (native)** | Process-attributed sockets/flows → GNR/ET Open IDS alerts → dig into image (`load`/`analyze`/`decompile` / live process). In-tree only — see [Ghidnet](#ghidnet-native-network-plane) |
---
@@ -204,7 +204,7 @@ First-class commands for the queries agents used to need ad-hoc scripts for. Sam
| `ghidrust tree <path>` | Bounded file tree index (existence/size; no unpack) | `--max-depth`, `--ext`, `--name`, `--json` |
| `ghidrust artifact get\|query\|list` | Drain spilled analysis artifacts (`next_offset`) | `--offset`, `--limit`, `--json` |
| `ghidrust process list\|attach\|launch\|…\|break\|wait\|scan\|watch` | Live Process Bridge (Windows; observe default, `mode=debug` for BP/step; `tool_surface` ≥ 7) | session_id / `-mode` / `--args` / `--cwd` / `--addr` / `--module` / `--rva` |
-| `ghidrust net dig\|playbook\|connections\|owners\|ifaces\|capture\|flows\|detect\|alerts\|rules\|pivots` | **Ghidnet** — attribute sockets/flows, GNR detect, dig into image (`tool_surface` ≥ 8) | See [Ghidnet](#ghidnet-native-network-plane) |
+| `ghidrust net dig\|playbook\|connections\|owners\|ifaces\|capture\|flows\|detect\|alerts\|rules\|pivots` | **Ghidnet** — attribute sockets/flows, ET Open / GNR detect, dig into image (`tool_surface` ≥ 8) | See [Ghidnet](#ghidnet-native-network-plane); `ghidrust net help` |
| `ghidrust il2cpp meta\|map\|touch-map\|stubs\|icalls` | IL2CPP metadata + touch-map + method map (body proof / baseline) + stubs + icalls | See [docs/IL2CPP.md](docs/IL2CPP.md) |
| `ghidrust unity-inventory <dir>` | Unity player layout (reuses PE VERSIONINFO helpers) | `--json`, `--out FILE` |
| `ghidrust disasm <path>` | Capstone-class listing; bounded by function end by default; `decode_gaps` when `--skip-bad`; JSON `stop_reason` | `--addr HEX`, `--count N` (default 16), `--skip-bad`, `--linear`/`--flow`, `--arch`, `--mode`, `--syntax`, `--detail`/`--no-detail`, `--detail-real`, `--skipdata`, `--skipdata-mnemonic`, `--skipdata-size`, `--unsigned-imm`, `--only-offset-branch`, `--litbase`, `--mnem-override ID:MNEMONIC`, `--out`, `--json` |
@@ -558,7 +558,8 @@ On Linux/macOS:
| `net_dig` / `net_playbook` | hint fields (`path`/`pid`/`host`/`ioc`) or alert/flow refs | Compile or execute dig playbook; closed-loop from alert (`tool_surface` ≥ 8) |
| `net_connections` / `net_owners` / `net_ifaces` | optional pid / filter | Host sockets → owning image; interface list |
| `net_capture_start` / `net_capture_stop` / `net_capture_status` / `net_flows` | session / iface / replay path | In-process capture session + attributed flows (MCP/GUI; not CLI one-shot reuse) |
-| `net_detect` / `net_alerts` / `net_rules_list` / `net_rules_load` / `net_rules_check` | pcap/rules path / max | In-tree GNR compile + detect + alert index |
+| `net_detect` / `net_alerts` | pcap / optional `rules` / max | GNR/ET detect on a capture; omit `rules` → active pack + `ghidrust-minimal.rules` |
+| `net_rules_list` / `net_rules_load` / `net_rules_check` / `net_rules_update` | path / `from_archive` / `url` / `et_version` / `dest` | List/check packs; one-click ET Open install (same `install_et_open` as CLI/GUI) |
| `net_pivots` / `net_job_get` | pcap / job id | DNS/TLS/HTTP/SMB pivot fields; dig job status |
#### Live process (Windows)
@@ -647,23 +648,93 @@ You should not need to type JSON by hand day-to-day — the IDE/agent does that
## Ghidnet (native network plane)
-Ghidnet is Ghidrust’s in-tree network investigation plane: **attribute → detect → dig**. It does not vendor foreign capture/IDS products. Outcome target is host investigation that pivots into the same `load` / `analyze` / `decompile` / live-process stack — not packet-UI feature parity with desktop sniffers.
+Ghidnet is Ghidrust’s in-tree network investigation plane: **attribute → detect → dig**. It does not vendor Suricata, Wireshark, Npcap crates, or `suricata-update`. Outcome target is host investigation that pivots into the same `load` / `analyze` / `decompile` / live-process stack — not packet-UI feature parity with desktop sniffers, and not a kernel IPS.
+
+Requires `tool_surface >= 8`. `server_info.network` advertises the plane (`caps` include `et-open` / `ids`). Airgap policy blocks live observe tools (`net_connections`, capture, detect) while still allowing path-only dig. Full agent SOP: [skill/ghidnet.md](skill/ghidnet.md).
### Pipeline
| Stage | What you get | CLI | MCP |
|-------|--------------|-----|-----|
| **Attribute** | TCP/UDP sockets → owning PID/image; interface list | `net connections`, `net owners`, `net ifaces` | `net_connections`, `net_owners`, `net_ifaces` |
-| **Capture / flows** | In-process session, `GRNCAP01` files, replay backend, flow table | `net capture …`, `net flows`, `ghidrust-netcap` | `net_capture_*`, `net_flows` |
-| **Detect** | GNR rules → alerts (in-tree MPM) | `net detect`, `net alerts`, `net rules …` | `net_detect`, `net_alerts`, `net_rules_*` |
+| **Capture / flows** | In-process session, `GRNCAP01` files, **ghidnetcap** live NIC, replay backend, flow table | `net capture …`, `net flows`, `ghidrust-netcap` | `net_capture_*`, `net_flows` |
+| **Rules / ET Open** | Fetch/install Proofpoint ET Open; activate pack; merge with bundled GNR | `net rules update et-open`, `net rules list\|check` | `net_rules_update`, `net_rules_list`, `net_rules_check` |
+| **Detect (IDS)** | Compiled GNR/ET rules → `alert`/`pass`/`drop` records (in-tree matcher) | `net detect`, `net alerts` | `net_detect`, `net_alerts` |
| **Pivots** | DNS / TLS SNI / HTTP / SMB fields from frames | `net pivots --pcap FILE` | `net_pivots` |
| **Dig** | Playbook compile/execute; closed-loop from alert → image RE | `net dig`, `net playbook` | `net_dig`, `net_playbook`, `net_job_get` |
-Requires `tool_surface >= 8`. `server_info.network` advertises the plane to agents. Airgap policy blocks live observe tools (`net_connections`, capture, detect) while still allowing path-only dig.
+`ghidrust net help` prints the full subcommand list.
+
+### ET Open install (one-click)
+
+Proofpoint **Emerging Threats Open** rules are used **as written**. The in-tree matcher implements a Suricata-shaped subset; SIDs that need unimplemented keywords are **skipped** (counted in the install/list report), not rewritten.
+
+CLI, GUI **Download / update ET Open**, and MCP `net_rules_update` all call the same `install_et_open`:
+
+1. Fetch `emerging.rules.tar.gz` + `.md5` from `https://rules.emergingthreats.net/open` (or `--from-archive` / `from_archive` for airgap/CI).
+2. Verify MD5, extract under the user rules root, write `ghidrust.vars` (`$HTTP_SERVERS=$HOME_NET` and other Suricata aliases).
+3. Point `active.json` at that pack. Detect then compiles **all `*.rules` in the pack plus** [`rules/ghidrust-minimal.rules`](rules/ghidrust-minimal.rules).
+
+```bash
+# Online (default ET Open 7.0.3)
+./target/release/ghidrust net rules update et-open --json
+
+# Airgap / CI: local tarball + sidecar .md5
+./target/release/ghidrust net rules update et-open --from-archive fixtures/et-open-mini.tar.gz --et-version mini --dest DIR --json
+
+./target/release/ghidrust net rules list --json
+# → packs.active, loaded, skipped, skip_reasons[]
+```
+
+Rules root (override with `GHIDRUST_RULES_DIR`):
+
+| OS | Default |
+|----|---------|
+| Windows | `%APPDATA%\ghidrust\rules` |
+| Linux | `$XDG_CONFIG_HOME/ghidrust/rules` or `~/.ghidrust/rules` |
+| Pack layout | `et-open/<version>/` + `active.json` |
+
+Optional `--url` / `--et-version` / `--et-code` (ET Pro oink; never logged). Do not vendor the ~40 MB ET tarball in git; CI uses `fixtures/et-open-mini.tar.gz`.
+
+### Detect (IDS, not kernel IPS)
+
+Without `--rules` / MCP `rules`, detect uses `load_active_ruleset()` (active ET pack + minimal). Explicit `--rules PATH` still compiles that file only.
+
+```bash
+# After ET install — uses active pack
+./target/release/ghidrust net detect --pcap path/to/capture.grncap --json
+./target/release/ghidrust net detect --payload-file fixtures/et-http-gate.bin --json
+
+# Explicit pack
+./target/release/ghidrust net detect --pcap capture.grncap --rules rules/ghidrust-minimal.rules --json
+./target/release/ghidrust net rules check --path rules/ghidrust-minimal.rules --json
+```
+
+| Matcher covers | Skipped (SID not loaded; listed in `skipped` / `skip_reasons`) |
+|----------------|---------------------------------------------------------------|
+| `content` / `pcre`, `nocase`, `distance`/`within` (cursor = previous hit **end**) | `flowbits` / `xbits` / `flowint` |
+| Sticky buffers: `http.uri`/`host`/`method`/`header`, `dns.query`, `tls.sni` | `ja3` / `ja3s` |
+| `flow:established,to_server,to_client` | `lua` / `luajit` |
+| `$HOME_NET` / `$HTTP_SERVERS` / CIDR / IP lists | `threshold` / `detection_filter`; `file-data`; `byte_test`/`byte_jump`; unknown `http.*`/`tls.*` buffers |
+| Actions `alert`, `pass`, `drop` **recorded as alerts** | Kernel/NFQUEUE/WFP block — **not implemented** |
+
+`drop` is an IDS action tag on the alert, not an inline packet drop. Kernel IPS / `GHIDRUST_NET_INLINE=1` inline block is a separate, disabled-by-default gate.
+
+### Capture (ghidnetcap)
+
+`net ifaces` lists OS adapters (Windows `GetAdaptersInfo`; Linux sysfs) plus a `replay` backend. Live open (`net capture start --iface NAME`):
+
+- Windows: optional NPF `CreateFile` if a driver is present; otherwise IPv4 raw sockets (`SIO_RCVALL`). Cargo does **not** link Npcap/WinPcap.
+- Unix: packet sockets.
+- Fail-closed: `privilege_required` or `backend_unavailable`. Never fabricates live frames.
+- `GHIDRUST_NET_LIVE_STUB=1` is test-only and **errors** on live open (does not silently emit empty captures).
+- Prefer `--replay FILE` / MCP `replay_path` in CI.
### Quick examples
```bash
+./target/release/ghidrust net help
+
# Dig playbook from a hint (compile only)
./target/release/ghidrust net dig --path fixtures/tiny_x64.pe --json
@@ -673,10 +744,8 @@ Requires `tool_surface >= 8`. `server_info.network` advertises the plane to agen
# Host sockets (Windows IP Helper / Linux /proc)
./target/release/ghidrust net connections --json
./target/release/ghidrust net owners --pid <PID> --json
+./target/release/ghidrust net ifaces --json
-# Detect on a capture file with shipped minimal rules
-./target/release/ghidrust net detect --pcap path/to/capture.grncap --rules rules/ghidrust-minimal.rules --json
-./target/release/ghidrust net rules check --path rules/ghidrust-minimal.rules --json
./target/release/ghidrust net pivots --pcap path/to/capture.grncap --json
```
@@ -690,12 +759,10 @@ Same APIs as CLI/MCP, in-process:
2. **Connections:** Refresh → select row → **Dig** (fills Dig tab hint)
3. **Capture:** set iface / replay path → **Start** → **Flows** → **Detect** / **Pivots**
4. **Alerts:** severity queue → **Dig** (closed-loop when owner path known)
-5. **Rules:** pack path → **Check** (default `rules/ghidrust-minimal.rules`)
+5. **Rules:** **Download / update ET Open** (one-click fetch + activate) → pack path → **Check** (default: active ET pack + `rules/ghidrust-minimal.rules`)
6. **Dig:** **Compile** / **Execute** → **Open in Listing** (loads image into CodeBrowser)
-**Honesty bounds:** live NIC acquisition may be stub/replay-first; capture sessions are process-local. Host header / Help→About show `NetworkInfo` (wave/caps). Capture **Export path** reveals `out_path` (copy + folder). **Inline block** is disabled unless `GHIDRUST_NET_INLINE=1` (hover shows reason).
-
-Shipped rules: [`rules/ghidrust-minimal.rules`](rules/ghidrust-minimal.rules).
+**Honesty bounds:** live NIC needs elevation (`privilege_required` otherwise). Capture sessions are process-local. Host header / Help→About show `NetworkInfo` (wave/caps). Capture **Export path** reveals `out_path` (copy + folder). **Inline block** is disabled unless `GHIDRUST_NET_INLINE=1` (hover shows reason).
---
@@ -759,10 +826,10 @@ MCP equivalents are `crypt_constants`, `recover_strings`, `list_crypto_capabilit
| `ghidrust-unity-inventory` | Unity player layout inventory (assemblies, plugins, metadata) |
| `ghidrust-net-schema` | Ghidnet VOs (`NetHint`, `Alert`, `DigPlan`, flows/pivots) |
| `ghidrust-net-attr` | Host socket → owner (Windows IP Helper / Linux `/proc`) |
-| `ghidrust-net-capture` | Capture sessions, `GRNCAP01`, replay; bin `ghidrust-netcap` |
+| `ghidrust-net-capture` | Capture sessions, `GRNCAP01`, ghidnetcap live NIC, replay; bin `ghidrust-netcap` |
| `ghidrust-net-flow` | Flow table, reassembly, filters |
-| `ghidrust-net-rules` | GNR dialect parse/compile |
-| `ghidrust-net-detect` | In-tree MPM + alerts; optional inline via `GHIDRUST_NET_INLINE` |
+| `ghidrust-net-rules` | GNR/Suricata-shaped parse/compile, ET Open install, active pack + `$HOME_NET` aliases |
+| `ghidrust-net-detect` | In-tree matcher (sticky buffers, pcre, distance/within); IDS `alert`/`pass`/`drop` records; optional inline via `GHIDRUST_NET_INLINE` |
| `ghidrust-net-parse` | DNS / TLS SNI / HTTP / SMB pivot extractors |
| `ghidrust-net-correlate` | Dig playbooks + closed-loop alert → RE |
| `ghidrust-cli` | CLI + MCP + benches / `gpu-decompile` / `decompile-bench` / `net …` |
diff --git a/crates/ghidrust-agent/src/policy.rs b/crates/ghidrust-agent/src/policy.rs
index d96ad4f..17ee6e4 100644
--- a/crates/ghidrust-agent/src/policy.rs
+++ b/crates/ghidrust-agent/src/policy.rs
@@ -162,8 +162,13 @@ mod tests {
assert!(is_network_observe_tool("net_connections"));
assert!(!is_network_observe_tool("net_dig"));
assert!(!is_network_observe_tool("net_playbook"));
+ assert!(!is_network_observe_tool("net_rules_update"));
+ assert!(!is_network_observe_tool("net_rules_list"));
+ assert!(!is_network_observe_tool("net_rules_check"));
assert!(!network_tool_allowed(AgentMode::Airgap, "net_capture_start"));
assert!(network_tool_allowed(AgentMode::ReadOnly, "net_capture_start"));
assert!(network_tool_allowed(AgentMode::Airgap, "net_dig"));
+ assert!(network_tool_allowed(AgentMode::Airgap, "net_rules_update"));
+ assert!(network_tool_allowed(AgentMode::Airgap, "net_rules_list"));
}
}
diff --git a/crates/ghidrust-cli/Cargo.toml b/crates/ghidrust-cli/Cargo.toml
index cc04b1d..cf697ac 100644
--- a/crates/ghidrust-cli/Cargo.toml
+++ b/crates/ghidrust-cli/Cargo.toml
@@ -30,6 +30,7 @@ serde_json = "1"
[dev-dependencies]
ghidrust-net-capture = { workspace = true }
ghidrust-net-flow = { workspace = true }
+ghidrust-net-rules = { workspace = true }
ghidrust-net-schema = { workspace = true }
serde_json = "1"
diff --git a/crates/ghidrust-cli/src/main.rs b/crates/ghidrust-cli/src/main.rs
index ad96e79..92ab7a2 100644
--- a/crates/ghidrust-cli/src/main.rs
+++ b/crates/ghidrust-cli/src/main.rs
@@ -53,7 +53,8 @@ const PACKAGE_VERSION: &str = env!("CARGO_PKG_VERSION");
/// `5` = decode engine surface (decode_support, decode_query, extended disassemble).
/// `6` = crypt_constants, recover_strings, decode_bake/magic, crypto_capabilities.
/// `7` = live process debug bridge (break/step/regs/stack/scan/watch).
-/// `8` = native network dig / connections / capture / detect / pivots.
+/// `8` = native network dig / connections / capture / detect / pivots /
+/// ET Open install (`net_rules_update`) / IDS detect (`alert`/`pass`/`drop` records).
const TOOL_SURFACE_VERSION: u32 = 8;
fn package_version() -> &'static str {
@@ -90,9 +91,20 @@ fn server_info_json() -> Value {
"net_dig",
"net_playbook",
"net_connections",
+ "net_owners",
+ "net_ifaces",
"net_capture_start",
+ "net_capture_stop",
+ "net_capture_status",
+ "net_flows",
"net_detect",
- "net_pivots"
+ "net_alerts",
+ "net_rules_list",
+ "net_rules_load",
+ "net_rules_check",
+ "net_rules_update",
+ "net_pivots",
+ "net_job_get"
],
"decode_diet": decode_block["features"]["decode_diet"],
"x86_reduce": decode_block["features"]["x86_reduce"],
@@ -314,7 +326,10 @@ fn print_help() {
break|continue|pause|wait|step|threads|regs|stack|scan|watch|vtable|snapshot … [--json]\n\
# attach/launch: -mode observe|debug; launch debug: -break-at-entry\n\
ghidrust net dig|playbook|connections|owners|ifaces|capture|flows|detect|alerts|rules|pivots … [--json]\n\
- # Ghidnet (tool_surface>=8): attribute → detect → dig; see README Ghidnet\n\
+ # Ghidnet (tool_surface>=8): attribute → detect → dig; `ghidrust net help`\n\
+ # net rules update et-open [--from-archive FILE] [--et-version V] [--url URL] [--dest DIR]\n\
+ # net detect --pcap FILE [--rules PATH] # default: active ET pack + ghidrust-minimal\n\
+ # net capture start [--iface NAME|--replay FILE] # live = ghidnetcap; privilege_required if unprivileged\n\
ghidrust rtti <path> [--filter|--name|--exact] [--match MODE] [--json]\n\
ghidrust analyzers [--json]\n\
ghidrust analyze <path> [--analyzers a,b | --analyzer NAME ...] [--gpu] [--json]\n\
@@ -4083,12 +4098,12 @@ description: "Auto-detect a short peel chain (depth 1..4) maximizing printable r
},
ToolDef {
name: "net_ifaces",
- description: "List capture interfaces / backends",
+ description: "List OS adapters (ghidnetcap) plus the replay backend",
input_schema: json!({ "type": "object", "properties": {} }),
},
ToolDef {
name: "net_capture_start",
- description: "Start native capture session (replay_path for fixtures; airgap refuses)",
+ description: "Start ghidnetcap live or replay capture (replay_path for fixtures; live fail-closed privilege_required; airgap refuses)",
input_schema: json!({
"type": "object",
"properties": {
@@ -4133,7 +4148,7 @@ description: "Auto-detect a short peel chain (depth 1..4) maximizing printable r
},
ToolDef {
name: "net_detect",
- description: "Run native GNR rules on a capture file / payload blob",
+ description: "Run GNR/ET Open rules on a capture file. Omit rules to use the active pack (ET Open + ghidrust-minimal). IDS alerts only — drop is not kernel IPS.",
input_schema: json!({
"type": "object",
"properties": {
@@ -4154,7 +4169,7 @@ description: "Auto-detect a short peel chain (depth 1..4) maximizing printable r
},
ToolDef {
name: "net_rules_list",
- description: "List bundled/native rule packs",
+ description: "List active ET Open pack + bundled ghidrust-minimal (loaded/skipped counts)",
input_schema: json!({ "type": "object", "properties": {} }),
},
ToolDef {
@@ -4175,6 +4190,19 @@ description: "Auto-detect a short peel chain (depth 1..4) maximizing printable r
"required": ["path"]
}),
},
+ ToolDef {
+ name: "net_rules_update",
+ description: "Download/install Proofpoint ET Open (or from_archive) and activate as the detect pack. Same function as CLI net rules update et-open and GUI Download / update ET Open.",
+ input_schema: json!({
+ "type": "object",
+ "properties": {
+ "from_archive": { "type": "string" },
+ "url": { "type": "string" },
+ "et_version": { "type": "string" },
+ "dest": { "type": "string" }
+ }
+ }),
+ },
ToolDef {
name: "net_pivots",
description: "Extract DNS/TLS/HTTP/SMB pivot fields from a capture or payload file",
@@ -4262,7 +4290,8 @@ fn run_mcp_stdio() -> ExitCode {
"serverInfo": {
"name": "ghidrust",
"version": package_version(),
- "toolSurface": TOOL_SURFACE_VERSION
+ "toolSurface": TOOL_SURFACE_VERSION,
+ "network": net_cmds::network_info_json()
}
}
})
diff --git a/crates/ghidrust-cli/src/net_cmds.rs b/crates/ghidrust-cli/src/net_cmds.rs
index 68729aa..5c2f994 100644
--- a/crates/ghidrust-cli/src/net_cmds.rs
+++ b/crates/ghidrust-cli/src/net_cmds.rs
@@ -11,7 +11,10 @@ use ghidrust_net_correlate::{
use ghidrust_net_detect::{compile_rules_text, detect_capture_file, detect_frames, last_alerts, load_rules};
use ghidrust_net_flow::Frame;
use ghidrust_net_parse::extract_pivots;
-use ghidrust_net_rules::compile_rules;
+use ghidrust_net_rules::{
+ compile_rules, install_et_open, list_rules_json, load_active_ruleset, CompiledRuleset,
+ InstallOpts,
+};
use ghidrust_net_schema::{
Alert, ClosedLoopConfig, Confidence, FlowKey, NetHint, NetworkInfo, Owner,
};
@@ -36,6 +39,8 @@ pub fn network_info_json() -> Value {
"alerts".into(),
"rules".into(),
"pivots".into(),
+ "et-open".into(),
+ "ids".into(),
],
native: true,
capture: true,
@@ -59,14 +64,44 @@ fn opt(args: &[String], name: &str) -> Option<String> {
None
}
+fn print_net_help() {
+ eprintln!(
+ "ghidrust net — Ghidnet (tool_surface>=8): attribute → detect → dig\n\
+ \n\
+ Usage:\n\
+ ghidrust net connections [--pid N] [--path SUB] [--proto tcp|udp] [--listening] [--max N] [--json]\n\
+ ghidrust net owners --pid N [--json]\n\
+ ghidrust net ifaces [--json]\n\
+ ghidrust net capture start [--iface NAME] [--replay FILE] [--filter BPF] [--pid N] [--path SUB] [--out DIR] [--json]\n\
+ ghidrust net capture stop|status <session_id> [--json]\n\
+ ghidrust net flows <session_id> [--max N] [--json]\n\
+ ghidrust net rules update et-open [--from-archive FILE] [--url URL] [--et-version V] [--dest DIR] [--json]\n\
+ ghidrust net rules list [--json]\n\
+ ghidrust net rules check|load --path FILE [--json]\n\
+ ghidrust net detect --pcap FILE [--rules PATH] [--json]\n\
+ ghidrust net detect --payload-file FILE [--rules PATH] [--json]\n\
+ ghidrust net alerts [--max N] [--json]\n\
+ ghidrust net pivots --pcap FILE [--json]\n\
+ ghidrust net dig [--path FILE] [--host H] [--ioc S] [--from-alert ID] [--execute] [--live] [--json]\n\
+ ghidrust net playbook [--path FILE] [--host H] [--ioc S] [--json]\n\
+ \n\
+ Detect (no --rules) uses the active ET Open pack + rules/ghidrust-minimal.rules.\n\
+ Pack dir: GHIDRUST_RULES_DIR, else %APPDATA%/ghidrust/rules (Windows) or ~/.ghidrust/rules.\n\
+ Live capture: ghidnetcap (Windows NPF or SIO_RCVALL; Unix packet sockets). Fail-closed: privilege_required.\n\
+ IDS: alert/pass/drop are recorded as alerts; drop is not kernel IPS. See README Ghidnet.\n"
+ );
+}
+
pub fn cmd_net(args: &[String], json: bool) -> ExitCode {
if args.is_empty() {
- eprintln!(
- "usage: ghidrust net dig|playbook|connections|owners|ifaces|capture|flows|detect|alerts|rules|pivots … [-json]"
- );
+ print_net_help();
return ExitCode::from(2);
}
match args[0].as_str() {
+ "help" | "-h" | "--help" => {
+ print_net_help();
+ ExitCode::SUCCESS
+ }
"dig" => cmd_dig(&args[1..], json),
"playbook" => cmd_playbook(&args[1..], json),
"connections" => cmd_connections(&args[1..], json),
@@ -251,7 +286,11 @@ fn cmd_owners(args: &[String], json: bool) -> ExitCode {
fn cmd_capture(args: &[String], json: bool) -> ExitCode {
if args.is_empty() {
- eprintln!("usage: ghidrust net capture start|stop|status …");
+ eprintln!(
+ "usage: ghidrust net capture start [--iface NAME] [--replay FILE] [--filter BPF] [--pid N] [--out DIR]\n\
+ ghidrust net capture stop|status <session_id>\n\
+ Live iface needs elevation (privilege_required). Prefer --replay FILE in CI."
+ );
return ExitCode::from(2);
}
match args[0].as_str() {
@@ -333,15 +372,26 @@ fn cmd_flows(args: &[String], json: bool) -> ExitCode {
}
}
+/// `--rules PATH` or active ET pack + bundled `ghidrust-minimal.rules`.
+fn resolve_detect_rules(rules_path: Option<&str>) -> Result<(CompiledRuleset, String), String> {
+ if let Some(p) = rules_path {
+ let r = load_rules(std::path::Path::new(p))?;
+ return Ok((r, p.to_string()));
+ }
+ match load_active_ruleset() {
+ Ok(r) => Ok((r, "active-pack".into())),
+ Err(_) => {
+ let fallback = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
+ .join("../../rules/ghidrust-minimal.rules");
+ let r = load_rules(&fallback)?;
+ Ok((r, fallback.display().to_string()))
+ }
+ }
+}
+
fn cmd_detect(args: &[String], json: bool) -> ExitCode {
- let rules_path = opt(args, "rules").unwrap_or_else(|| {
- PathBuf::from(env!("CARGO_MANIFEST_DIR"))
- .join("../../rules/ghidrust-minimal.rules")
- .display()
- .to_string()
- });
- let rules = match load_rules(std::path::Path::new(&rules_path)) {
- Ok(r) => r,
+ let (rules, rules_path) = match resolve_detect_rules(opt(args, "rules").as_deref()) {
+ Ok(v) => v,
Err(e) => {
eprintln!("error: {e}");
return ExitCode::FAILURE;
@@ -392,50 +442,76 @@ fn cmd_detect(args: &[String], json: bool) -> ExitCode {
}
}
} else {
- eprintln!("usage: ghidrust net detect --pcap FILE [--rules PATH] [-json]");
+ eprintln!(
+ "usage: ghidrust net detect --pcap FILE [--rules PATH] [--json]\n\
+ ghidrust net detect --payload-file FILE [--rules PATH] [--json]\n\
+ Default rules: active ET Open pack + rules/ghidrust-minimal.rules (GHIDRUST_RULES_DIR)."
+ );
ExitCode::from(2)
}
}
fn cmd_rules(args: &[String], json: bool) -> ExitCode {
if args.is_empty() {
- eprintln!("usage: ghidrust net rules list|load|check …");
+ eprintln!(
+ "usage: ghidrust net rules list|load|check|update et-open [--from-archive FILE] [--et-version V] [--url URL] [--dest DIR] [--json]"
+ );
return ExitCode::from(2);
}
match args[0].as_str() {
"list" => {
- let default = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
- .join("../../rules/ghidrust-minimal.rules");
- print_val(
- &json!({"packs":[default.display().to_string()], "native": true}),
- json,
- );
+ print_val(&list_rules_json(), json);
ExitCode::SUCCESS
}
+ "update" => {
+ let rest = &args[1..];
+ let _ = rest.first().map(|s| s.as_str()); // et-open
+ let opts = InstallOpts {
+ from_archive: opt(args, "from-archive"),
+ url: opt(args, "url"),
+ et_version: opt(args, "et-version"),
+ et_code: opt(args, "et-code"),
+ dest_root: opt(args, "dest"),
+ };
+ match install_et_open(opts) {
+ Ok(rep) => {
+ print_val(&serde_json::to_value(&rep).unwrap(), json);
+ if json {
+ ExitCode::SUCCESS
+ } else {
+ println!(
+ "et-open version={} loaded={} skipped={} path={}",
+ rep.version, rep.loaded, rep.skipped, rep.path
+ );
+ ExitCode::SUCCESS
+ }
+ }
+ Err(e) => {
+ eprintln!("error: {e}");
+ ExitCode::FAILURE
+ }
+ }
+ }
"load" | "check" => {
let Some(path) = opt(args, "path").or_else(|| args.get(1).cloned()) else {
eprintln!("usage: ghidrust net rules check --path FILE");
return ExitCode::from(2);
};
match std::fs::read_to_string(&path) {
- Ok(text) => match compile_rules(&text) {
- Ok(set) => {
- print_val(
- &json!({
- "ok": true,
- "path": path,
- "rule_count": set.rules.len(),
- "warnings": set.warnings,
- }),
- json,
- );
- ExitCode::SUCCESS
- }
- Err(e) => {
- eprintln!("error: {e}");
- ExitCode::FAILURE
- }
- },
+ Ok(text) => {
+ let set = compile_rules(&text);
+ print_val(
+ &json!({
+ "ok": true,
+ "path": path,
+ "rule_count": set.rules.len(),
+ "skipped": set.skipped.len(),
+ "warnings": set.warnings,
+ }),
+ json,
+ );
+ ExitCode::SUCCESS
+ }
Err(e) => {
eprintln!("error: {e}");
ExitCode::FAILURE
@@ -605,17 +681,8 @@ pub fn mcp_net(tool: &str, args: &Value, mode: AgentMode) -> Result<String, Stri
.get("pcap")
.and_then(|v| v.as_str())
.ok_or_else(|| "pcap required".to_string())?;
- let rules_path = args
- .get("rules")
- .and_then(|v| v.as_str())
- .map(|s| s.to_string())
- .unwrap_or_else(|| {
- PathBuf::from(env!("CARGO_MANIFEST_DIR"))
- .join("../../rules/ghidrust-minimal.rules")
- .display()
- .to_string()
- });
- let rules = load_rules(std::path::Path::new(&rules_path))?;
+ let (rules, rules_path) =
+ resolve_detect_rules(args.get("rules").and_then(|v| v.as_str()))?;
let mut attr: Vec<(FlowKey, Owner)> = Vec::new();
if let Some(ap) = args.get("attr").and_then(|v| v.as_str()) {
if let Ok(text) = std::fs::read_to_string(ap) {
@@ -638,7 +705,7 @@ pub fn mcp_net(tool: &str, args: &Value, mode: AgentMode) -> Result<String, Stri
}
let alerts = detect_capture_file(std::path::Path::new(pcap), &rules, &attr)?;
ALERT_INDEX.lock().unwrap().clone_from(&alerts);
- Ok(serde_json::to_string_pretty(&json!({"alerts": alerts})).unwrap())
+ Ok(serde_json::to_string_pretty(&json!({"alerts": alerts, "rules": rules_path})).unwrap())
}
"net_alerts" => {
let max = args.get("max").and_then(|v| v.as_u64()).map(|u| u as usize);
@@ -648,11 +715,18 @@ pub fn mcp_net(tool: &str, args: &Value, mode: AgentMode) -> Result<String, Stri
}
Ok(serde_json::to_string_pretty(&json!({"alerts": alerts})).unwrap())
}
- "net_rules_list" => Ok(serde_json::to_string_pretty(&json!({
- "packs": ["rules/ghidrust-minimal.rules"],
- "native": true
- }))
- .unwrap()),
+ "net_rules_list" => Ok(serde_json::to_string_pretty(&list_rules_json()).unwrap()),
+ "net_rules_update" => {
+ let opts = InstallOpts {
+ from_archive: args.get("from_archive").and_then(|v| v.as_str()).map(|s| s.to_string()),
+ url: args.get("url").and_then(|v| v.as_str()).map(|s| s.to_string()),
+ et_version: args.get("et_version").and_then(|v| v.as_str()).map(|s| s.to_string()),
+ et_code: args.get("et_code").and_then(|v| v.as_str()).map(|s| s.to_string()),
+ dest_root: args.get("dest").and_then(|v| v.as_str()).map(|s| s.to_string()),
+ };
+ let rep = install_et_open(opts)?;
+ Ok(serde_json::to_string_pretty(&rep).unwrap())
+ }
"net_rules_load" | "net_rules_check" => {
let path = args
.get("path")
@@ -664,6 +738,7 @@ pub fn mcp_net(tool: &str, args: &Value, mode: AgentMode) -> Result<String, Stri
"ok": true,
"path": path,
"rule_count": set.rules.len(),
+ "skipped": set.skipped.len(),
"warnings": set.warnings
}))
.unwrap())
diff --git a/crates/ghidrust-cli/tests/cli_mcp.rs b/crates/ghidrust-cli/tests/cli_mcp.rs
index 82f7cb2..ecaae5a 100644
--- a/crates/ghidrust-cli/tests/cli_mcp.rs
+++ b/crates/ghidrust-cli/tests/cli_mcp.rs
@@ -346,9 +346,16 @@ fn mcp_server_info_and_live_tools() {
"inventory",
"il2cpp_touch_map",
"function_create",
+ "net_rules_update",
+ "net_rules_list",
+ "net_detect",
] {
assert!(s.contains(tool), "missing {tool} in {s}");
}
+ assert!(
+ s.contains("et-open") || s.contains("\"et-open\""),
+ "initialize/server_info missing et-open network cap: {s}"
+ );
// tool_surface must be at least 3 (touch-map / body_class / function_create surface)
let surface_ok = s.contains("\"toolSurface\":3")
|| s.contains("\"tool_surface\": 3")
diff --git a/crates/ghidrust-cli/tests/et_open.rs b/crates/ghidrust-cli/tests/et_open.rs
new file mode 100644
index 0000000..b3ae05f
--- /dev/null
+++ b/crates/ghidrust-cli/tests/et_open.rs
@@ -0,0 +1,79 @@
+//! ET Open --from-archive install + detect using the shipped CLI.
+
+use std::path::PathBuf;
+use std::process::Command;
+
+fn bin() -> PathBuf {
+ PathBuf::from(env!("CARGO_BIN_EXE_ghidrust"))
+}
+
+fn archive() -> PathBuf {
+ let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../fixtures");
+ let tree = root.join("et-open-mini-tree");
+ ghidrust_net_rules::write_mini_et_tree(&tree).expect("tree");
+ let bytes = ghidrust_net_rules::tar_gz_dir(&tree).expect("tar");
+ let arch = root.join("et-open-mini.tar.gz");
+ std::fs::write(&arch, &bytes).expect("write archive");
+ let md5 = ghidrust_net_rules::md5_hex_pub(&bytes);
+ std::fs::write(root.join("et-open-mini.tar.gz.md5"), md5).expect("md5");
+ let http = root.join("et-http-gate.bin");
+ std::fs::write(&http, b"GET /gate.php HTTP/1.1\r\nHost: a.test\r\n\r\n").expect("http");
+ arch
+}
+
+#[test]
+fn cli_from_archive_then_detect_gate_php() {
+ let arch = archive();
+ let dest = std::env::temp_dir().join(format!("et-cli-{}", std::process::id()));
+ let _ = std::fs::create_dir_all(&dest);
+ let out = Command::new(bin())
+ .args([
+ "net",
+ "rules",
+ "update",
+ "et-open",
+ "--from-archive",
+ arch.to_str().unwrap(),
+ "--dest",
+ dest.to_str().unwrap(),
+ "--et-version",
+ "mini",
+ "-json",
+ ])
+ .env("GHIDRUST_RULES_DIR", dest.display().to_string())
+ .output()
+ .expect("update");
+ assert!(
+ out.status.success(),
+ "stderr={} stdout={}",
+ String::from_utf8_lossy(&out.stderr),
+ String::from_utf8_lossy(&out.stdout)
+ );
+ let v: serde_json::Value = serde_json::from_slice(&out.stdout).expect("json");
+ assert_eq!(v["loaded"].as_u64().unwrap(), 5, "{v}");
+ assert_eq!(v["skipped"].as_u64().unwrap(), 1, "{v}");
+ let http = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../fixtures/et-http-gate.bin");
+ let det = Command::new(bin())
+ .args([
+ "net",
+ "detect",
+ "--pcap",
+ http.to_str().unwrap(),
+ "-json",
+ ])
+ .env("GHIDRUST_RULES_DIR", dest.display().to_string())
+ .output()
+ .expect("detect");
+ assert!(
+ det.status.success(),
+ "stderr={}",
+ String::from_utf8_lossy(&det.stderr)
+ );
+ let dv: serde_json::Value = serde_json::from_slice(&det.stdout).expect("detect json");
+ let alerts = dv["alerts"].as_array().expect("alerts");
+ assert!(
+ alerts.iter().any(|a| a["sid"].as_u64() == Some(2000002)),
+ "{dv}"
+ );
+}
+
diff --git a/crates/ghidrust-cli/tests/eval_cli_mcp_surface.rs b/crates/ghidrust-cli/tests/eval_cli_mcp_surface.rs
index 635d588..55d8345 100644
--- a/crates/ghidrust-cli/tests/eval_cli_mcp_surface.rs
+++ b/crates/ghidrust-cli/tests/eval_cli_mcp_surface.rs
@@ -90,6 +90,23 @@ const EXPECTED_MCP_TOOLS: &[&str] = &[
"function_at",
"get_function_by_address",
"function_create",
+ "net_dig",
+ "net_playbook",
+ "net_connections",
+ "net_owners",
+ "net_ifaces",
+ "net_capture_start",
+ "net_capture_stop",
+ "net_capture_status",
+ "net_flows",
+ "net_detect",
+ "net_alerts",
+ "net_rules_list",
+ "net_rules_load",
+ "net_rules_check",
+ "net_rules_update",
+ "net_pivots",
+ "net_job_get",
];
const EXPECTED_CLI_COMMANDS: &[&str] = &[
@@ -118,6 +135,7 @@ const EXPECTED_CLI_COMMANDS: &[&str] = &[
"tree",
"artifact",
"process",
+ "net",
"bulk-bench",
"decompile",
"decompile-bench",
@@ -622,6 +640,22 @@ fn eval_cli_mcp_surface_catalog() {
truncate(&blob, 400),
));
}
+ if ok && blob.contains("ghidrust net") {
+ rows.push(row_pass(
+ "catalog",
+ "cli_help_lists_net",
+ ms,
+ "help includes ghidrust net",
+ ));
+ } else {
+ rows.push(row_fail(
+ "catalog",
+ "cli_help_lists_net",
+ ms,
+ "help missing net",
+ truncate(&blob, 400),
+ ));
+ }
}
// --- CLI smoke ---
@@ -810,6 +844,19 @@ fn eval_cli_mcp_surface_catalog() {
&["process", "list", "-json"],
&["["],
));
+ rows.push(expect_cli_json(
+ "cli:net_ifaces",
+ &["net", "ifaces", "-json"],
+ ));
+ rows.push(expect_cli_json(
+ "cli:net_dig",
+ &["net", "dig", "-path", &pe, "-json"],
+ ));
+ rows.push(expect_cli_honest(
+ "cli:net_owners_usage",
+ &["net", "owners"],
+ &["usage", "pid"],
+ ));
for (name, sub) in [
("cli:process_attach_usage", "attach"),
("cli:process_launch_usage", "launch"),
@@ -1062,6 +1109,93 @@ fn eval_cli_mcp_surface_catalog() {
"process_step_out",
json!({ "session_id": "eval-missing-session" }),
),
+ (
+ "process_step_into",
+ json!({ "session_id": "eval-missing-session" }),
+ ),
+ (
+ "process_step_over",
+ json!({ "session_id": "eval-missing-session" }),
+ ),
+ (
+ "process_continue",
+ json!({ "session_id": "eval-missing-session" }),
+ ),
+ (
+ "process_pause",
+ json!({ "session_id": "eval-missing-session" }),
+ ),
+ (
+ "process_wait",
+ json!({ "session_id": "eval-missing-session" }),
+ ),
+ (
+ "process_threads",
+ json!({ "session_id": "eval-missing-session" }),
+ ),
+ (
+ "process_stack",
+ json!({ "session_id": "eval-missing-session" }),
+ ),
+ (
+ "process_break_list",
+ json!({ "session_id": "eval-missing-session" }),
+ ),
+ (
+ "process_break_set",
+ json!({ "session_id": "eval-missing-session", "addr": "0x140001000" }),
+ ),
+ (
+ "process_break_clear",
+ json!({ "session_id": "eval-missing-session" }),
+ ),
+ (
+ "process_thread_context_get",
+ json!({ "session_id": "eval-missing-session" }),
+ ),
+ (
+ "process_thread_context_set",
+ json!({ "session_id": "eval-missing-session" }),
+ ),
+ (
+ "process_scan",
+ json!({ "session_id": "eval-missing-session", "aob": "90" }),
+ ),
+ (
+ "process_watch_expr",
+ json!({ "session_id": "eval-missing-session", "expr": "*+0" }),
+ ),
+ (
+ "process_export_snapshot",
+ json!({ "session_id": "eval-missing-session" }),
+ ),
+ (
+ "process_vtable_probe",
+ json!({ "session_id": "eval-missing-session", "addr": "0x140001000" }),
+ ),
+ ("net_ifaces", json!({})),
+ ("net_alerts", json!({})),
+ ("net_rules_list", json!({})),
+ ("net_dig", json!({ "path": pe })),
+ ("net_playbook", json!({ "path": pe })),
+ ("net_connections", json!({})),
+ ("net_owners", json!({})),
+ ("net_capture_start", json!({})),
+ ("net_capture_stop", json!({})),
+ (
+ "net_capture_status",
+ json!({ "session_id": "eval-missing-session" }),
+ ),
+ (
+ "net_flows",
+ json!({ "session_id": "eval-missing-session" }),
+ ),
+ ("net_detect", json!({ "pcap": "missing.pcap" })),
+ ("net_rules_load", json!({ "path": "missing.rules" })),
+ ("net_rules_check", json!({ "path": "missing.rules" })),
+ ("net_rules_update", json!({})),
+ ("net_pivots", json!({ "pcap": pe })),
+ ("net_job_get", json!({ "job_id": "none" })),
];
const CHUNK: usize = 10;
diff --git a/crates/ghidrust-core/src/analyzers/mod.rs b/crates/ghidrust-core/src/analyzers/mod.rs
index 81b8b66..c2367ef 100644
--- a/crates/ghidrust-core/src/analyzers/mod.rs
+++ b/crates/ghidrust-core/src/analyzers/mod.rs
@@ -329,6 +329,39 @@ mod tests {
let rep = run_analyzers_opts(&mut prog, &["Not A Real Analyzer"], true).expect("opts");
assert_eq!(rep.results.len(), 1);
assert_eq!(rep.results[0].status, "error");
+ assert!(
+ rep.results[0].message.contains("unknown analyzer"),
+ "{}",
+ rep.results[0].message
+ );
+ assert!(
+ !rep.results[0].message.contains("unwired analyzer"),
+ "unknown names must not use the unwired-catalog label: {}",
+ rep.results[0].message
+ );
assert!(!rep.results[0].message.contains("gpu_enrich"));
}
+
+ #[test]
+ fn registered_analyzer_names_never_emit_unwired() {
+ for name in ANALYZER_NAMES {
+ let mut prog = load_path(fixture_path("tiny_x64.pe")).expect("pe");
+ let rep = run_analyzers(&mut prog, &[*name]).expect("run");
+ assert_eq!(rep.results.len(), 1, "{name}");
+ assert!(
+ !rep.results[0].message.contains("unwired analyzer"),
+ "{name}: {}",
+ rep.results[0].message
+ );
+ assert_eq!(
+ rep.results[0].status, "ok",
+ "{name}: {}",
+ rep.results[0].message
+ );
+ assert!(
+ !rep.results[0].message.is_empty(),
+ "{name} empty primary message"
+ );
+ }
+ }
}
diff --git a/crates/ghidrust-core/src/cparse.rs b/crates/ghidrust-core/src/cparse.rs
new file mode 100644
index 0000000..46a50b7
--- /dev/null
+++ b/crates/ghidrust-core/src/cparse.rs
@@ -0,0 +1,510 @@
+//! Hand-rolled C header parser (Ghidra Parse C Source analog).
+//!
+//! Reference grammar (not linked): Clang `ParseDecl` / Ghidra `CParser` /
+//! pycparser c_parser — structs, typedefs, enums, function prototypes.
+//! No clang/libclang/tree-sitter runtime.
+
+use serde::{Deserialize, Serialize};
+
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct ParsedType {
+ pub name: String,
+ pub kind: String,
+ pub members: Vec<ParsedMember>,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct ParsedMember {
+ pub name: String,
+ pub ty: String,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct ParseError {
+ pub message: String,
+}
+
+impl std::fmt::Display for ParseError {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "{}", self.message)
+ }
+}
+impl std::error::Error for ParseError {}
+
+/// Parse a C header/source snippet into named types with members/params.
+pub fn parse_c_source(src: &str) -> Result<Vec<ParsedType>, ParseError> {
+ let stripped = strip_comments_and_pp(src);
+ let toks = tokenize(&stripped);
+ let mut p = Parser { toks, i: 0 };
+ p.parse_unit()
+}
+
+fn strip_comments_and_pp(src: &str) -> String {
+ let mut out = String::new();
+ let b = src.as_bytes();
+ let mut i = 0;
+ while i < b.len() {
+ if i + 1 < b.len() && b[i] == b'/' && b[i + 1] == b'/' {
+ while i < b.len() && b[i] != b'\n' {
+ i += 1;
+ }
+ continue;
+ }
+ if i + 1 < b.len() && b[i] == b'/' && b[i + 1] == b'*' {
+ i += 2;
+ while i + 1 < b.len() && !(b[i] == b'*' && b[i + 1] == b'/') {
+ i += 1;
+ }
+ i = (i + 2).min(b.len());
+ out.push(' ');
+ continue;
+ }
+ if b[i] == b'#' {
+ while i < b.len() && b[i] != b'\n' {
+ i += 1;
+ }
+ continue;
+ }
+ out.push(b[i] as char);
+ i += 1;
+ }
+ out
+}
+
+fn tokenize(s: &str) -> Vec<String> {
+ let mut toks = Vec::new();
+ let mut cur = String::new();
+ for c in s.chars() {
+ if c.is_ascii_whitespace() {
+ if !cur.is_empty() {
+ toks.push(std::mem::take(&mut cur));
+ }
+ } else if matches!(c, '{' | '}' | '(' | ')' | ';' | ',' | '*') {
+ if !cur.is_empty() {
+ toks.push(std::mem::take(&mut cur));
+ }
+ toks.push(c.to_string());
+ } else {
+ cur.push(c);
+ }
+ }
+ if !cur.is_empty() {
+ toks.push(cur);
+ }
+ toks
+}
+
+struct Parser {
+ toks: Vec<String>,
+ i: usize,
+}
+
+impl Parser {
+ fn peek(&self) -> Option<&str> {
+ self.toks.get(self.i).map(String::as_str)
+ }
+ fn bump(&mut self) -> Option<String> {
+ if self.i < self.toks.len() {
+ let t = self.toks[self.i].clone();
+ self.i += 1;
+ Some(t)
+ } else {
+ None
+ }
+ }
+ fn eat(&mut self, s: &str) -> bool {
+ if self.peek() == Some(s) {
+ self.i += 1;
+ true
+ } else {
+ false
+ }
+ }
+ fn expect(&mut self, s: &str) -> Result<(), ParseError> {
+ if self.eat(s) {
+ Ok(())
+ } else {
+ Err(ParseError {
+ message: format!("expected '{s}', got {:?}", self.peek()),
+ })
+ }
+ }
+
+ fn parse_unit(&mut self) -> Result<Vec<ParsedType>, ParseError> {
+ let mut out = Vec::new();
+ while self.peek().is_some() {
+ if self.peek() == Some(";") {
+ self.bump();
+ continue;
+ }
+ if let Some(t) = self.parse_item()? {
+ out.push(t);
+ }
+ }
+ if out.is_empty() {
+ return Err(ParseError {
+ message: "no types parsed".into(),
+ });
+ }
+ Ok(out)
+ }
+
+ fn parse_item(&mut self) -> Result<Option<ParsedType>, ParseError> {
+ match self.peek() {
+ Some("struct") => self.parse_struct(),
+ Some("enum") => self.parse_enum(),
+ Some("typedef") => self.parse_typedef(),
+ Some("union") => self.parse_union(),
+ Some(_) => self.parse_prototype_or_skip(),
+ None => Ok(None),
+ }
+ }
+
+ fn parse_struct(&mut self) -> Result<Option<ParsedType>, ParseError> {
+ self.expect("struct")?;
+ let name = self
+ .bump()
+ .filter(|t| is_ident(t))
+ .ok_or_else(|| ParseError {
+ message: "struct name".into(),
+ })?;
+ if !self.eat("{") {
+ // opaque `struct Foo;`
+ let _ = self.eat(";");
+ return Ok(Some(ParsedType {
+ name,
+ kind: "struct".into(),
+ members: Vec::new(),
+ }));
+ }
+ let members = self.parse_fields()?;
+ self.expect("}")?;
+ let _ = self.eat(";");
+ Ok(Some(ParsedType {
+ name,
+ kind: "struct".into(),
+ members,
+ }))
+ }
+
+ fn parse_union(&mut self) -> Result<Option<ParsedType>, ParseError> {
+ self.expect("union")?;
+ let name = self
+ .bump()
+ .filter(|t| is_ident(t))
+ .unwrap_or_else(|| "anon_union".into());
+ if !self.eat("{") {
+ let _ = self.eat(";");
+ return Ok(None);
+ }
+ let members = self.parse_fields()?;
+ self.expect("}")?;
+ let _ = self.eat(";");
+ Ok(Some(ParsedType {
+ name,
+ kind: "union".into(),
+ members,
+ }))
+ }
+
+ fn parse_enum(&mut self) -> Result<Option<ParsedType>, ParseError> {
+ self.expect("enum")?;
+ let name = self
+ .bump()
+ .filter(|t| is_ident(t))
+ .ok_or_else(|| ParseError {
+ message: "enum name".into(),
+ })?;
+ self.expect("{")?;
+ let mut members = Vec::new();
+ while let Some(t) = self.peek() {
+ if t == "}" {
+ break;
+ }
+ if t == "," {
+ self.bump();
+ continue;
+ }
+ let n = self.bump().unwrap();
+ if is_ident(&n) {
+ members.push(ParsedMember {
+ name: n,
+ ty: "int".into(),
+ });
+ }
+ if self.eat("=") {
+ let _ = self.bump();
+ }
+ }
+ self.expect("}")?;
+ let _ = self.eat(";");
+ Ok(Some(ParsedType {
+ name,
+ kind: "enum".into(),
+ members,
+ }))
+ }
+
+ fn parse_typedef(&mut self) -> Result<Option<ParsedType>, ParseError> {
+ self.expect("typedef")?;
+ if self.peek() == Some("struct") {
+ let mut st = self.parse_struct()?.ok_or_else(|| ParseError {
+ message: "typedef struct".into(),
+ })?;
+ // `typedef struct Foo { } Foo_t;` — last ident before `;` already eaten
+ // After struct parse we already consumed `;`. Name stays struct name.
+ if let Some(alias) = self.peek().filter(|t| is_ident(t)).map(|s| s.to_string()) {
+ self.bump();
+ let _ = self.eat(";");
+ st.kind = "typedef".into();
+ st.members.insert(
+ 0,
+ ParsedMember {
+ name: st.name.clone(),
+ ty: "struct".into(),
+ },
+ );
+ st.name = alias;
+ }
+ return Ok(Some(st));
+ }
+ let ty = self.parse_type_spec();
+ let name = self
+ .bump()
+ .filter(|t| is_ident(t))
+ .ok_or_else(|| ParseError {
+ message: "typedef name".into(),
+ })?;
+ if self.eat("(") {
+ let params = self.parse_params()?;
+ self.expect(")")?;
+ let _ = self.eat(";");
+ return Ok(Some(ParsedType {
+ name,
+ kind: "prototype".into(),
+ members: params,
+ }));
+ }
+ let _ = self.eat(";");
+ Ok(Some(ParsedType {
+ name,
+ kind: "typedef".into(),
+ members: vec![ParsedMember {
+ name: "_alias".into(),
+ ty,
+ }],
+ }))
+ }
+
+ fn parse_prototype_or_skip(&mut self) -> Result<Option<ParsedType>, ParseError> {
+ let ty = self.parse_type_spec();
+ if ty.is_empty() {
+ self.bump();
+ return Ok(None);
+ }
+ let name = match self.peek() {
+ Some(t) if is_ident(t) => self.bump().unwrap(),
+ _ => {
+ // skip until ; or {
+ while let Some(t) = self.bump() {
+ if t == ";" || t == "}" {
+ break;
+ }
+ if t == "{" {
+ let mut depth = 1;
+ while depth > 0 {
+ match self.bump().as_deref() {
+ Some("{") => depth += 1,
+ Some("}") => depth -= 1,
+ None => break,
+ _ => {}
+ }
+ }
+ break;
+ }
+ }
+ return Ok(None);
+ }
+ };
+ if self.eat("(") {
+ let params = self.parse_params()?;
+ self.expect(")")?;
+ if self.eat("{") {
+ let mut depth = 1;
+ while depth > 0 {
+ match self.bump().as_deref() {
+ Some("{") => depth += 1,
+ Some("}") => depth -= 1,
+ None => break,
+ _ => {}
+ }
+ }
+ }
+ let _ = self.eat(";");
+ return Ok(Some(ParsedType {
+ name,
+ kind: "prototype".into(),
+ members: params,
+ }));
+ }
+ let _ = self.eat(";");
+ Ok(None)
+ }
+
+ fn parse_fields(&mut self) -> Result<Vec<ParsedMember>, ParseError> {
+ let mut members = Vec::new();
+ while let Some(t) = self.peek() {
+ if t == "}" {
+ break;
+ }
+ let ty = self.parse_type_spec();
+ let name = self
+ .bump()
+ .filter(|t| is_ident(t))
+ .unwrap_or_else(|| "_".into());
+ let _ = self.eat(";");
+ if !ty.is_empty() {
+ members.push(ParsedMember { name, ty });
+ }
+ }
+ Ok(members)
+ }
+
+ fn parse_params(&mut self) -> Result<Vec<ParsedMember>, ParseError> {
+ let mut params = Vec::new();
+ if self.peek() == Some(")") {
+ return Ok(params);
+ }
+ if self.peek() == Some("void") {
+ let _ = self.bump();
+ return Ok(params);
+ }
+ loop {
+ let ty = self.parse_type_spec();
+ let name = if self.peek().is_some_and(is_ident) && self.peek() != Some(")") {
+ self.bump().unwrap()
+ } else {
+ format!("arg{}", params.len())
+ };
+ if !ty.is_empty() {
+ params.push(ParsedMember { name, ty });
+ }
+ if self.eat(",") {
+ continue;
+ }
+ break;
+ }
+ Ok(params)
+ }
+
+ fn parse_type_spec(&mut self) -> String {
+ let mut parts = Vec::new();
+ const KW: &[&str] = &[
+ "const",
+ "volatile",
+ "unsigned",
+ "signed",
+ "struct",
+ "enum",
+ "union",
+ "int",
+ "char",
+ "short",
+ "long",
+ "void",
+ "float",
+ "double",
+ "size_t",
+ "uint8_t",
+ "uint16_t",
+ "uint32_t",
+ "uint64_t",
+ "int8_t",
+ "int16_t",
+ "int32_t",
+ "int64_t",
+ ];
+ while let Some(t) = self.peek() {
+ let continue_ident = is_ident(t)
+ && (parts.is_empty()
+ || matches!(
+ parts.last().map(String::as_str),
+ Some("struct" | "enum" | "union" | "unsigned" | "signed" | "long" | "short" | "const")
+ ));
+ if KW.contains(&t) || continue_ident {
+ parts.push(self.bump().unwrap());
+ continue;
+ }
+ if t == "*" {
+ parts.push(self.bump().unwrap());
+ continue;
+ }
+ break;
+ }
+ parts.join(" ")
+ }
+}
+
+fn is_ident(s: &str) -> bool {
+ let mut cs = s.chars();
+ match cs.next() {
+ Some(c) if c.is_ascii_alphabetic() || c == '_' => {
+ cs.all(|c| c.is_ascii_alphanumeric() || c == '_')
+ }
+ _ => false,
+ }
+}
+
+/// Pretty body stored in `ProgramEdits.user_types`.
+pub fn format_type_body(t: &ParsedType) -> String {
+ let mut s = format!("{} {}\n", t.kind, t.name);
+ for m in &t.members {
+ s.push_str(&format!(" {} {}\n", m.ty, m.name));
+ }
+ s
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ const SNIPPET: &str = r#"
+ struct Widget {
+ int id;
+ char *name;
+ };
+ typedef unsigned int u32;
+ enum Color { RED, GREEN, BLUE };
+ int draw(struct Widget *w, int flags);
+ "#;
+
+ #[test]
+ fn parses_struct_typedef_enum_prototype() {
+ let types = parse_c_source(SNIPPET).expect("parse");
+ assert!(!types.is_empty());
+ let names: Vec<_> = types.iter().map(|t| t.name.as_str()).collect();
+ assert!(names.contains(&"Widget"), "{names:?}");
+ assert!(names.contains(&"u32"), "{names:?}");
+ assert!(names.contains(&"Color"), "{names:?}");
+ assert!(names.contains(&"draw"), "{names:?}");
+ let w = types.iter().find(|t| t.name == "Widget").unwrap();
+ assert_eq!(w.kind, "struct");
+ assert!(w.members.iter().any(|m| m.name == "id"));
+ assert!(w.members.iter().any(|m| m.name == "name"));
+ let d = types.iter().find(|t| t.name == "draw").unwrap();
+ assert_eq!(d.kind, "prototype");
+ assert!(d.members.iter().any(|m| m.ty.contains("Widget") || m.name.contains("w") || m.ty.contains("int")));
+ let c = types.iter().find(|t| t.name == "Color").unwrap();
+ assert!(c.members.iter().any(|m| m.name == "RED"));
+ }
+
+ #[test]
+ fn apply_parsed_c_stores_fields_on_program() {
+ let mut prog = crate::Program::new("t".into(), "PE32+");
+ let types = parse_c_source(SNIPPET).unwrap();
+ prog.edits.apply_parsed_c(types);
+ let w = prog.edits.parsed_types.get("Widget").expect("Widget");
+ assert!(w.members.iter().any(|m| m.name == "id" || m.name == "name"));
+ assert!(prog.edits.user_types["Widget"].contains("id"));
+ }
+}
diff --git a/crates/ghidrust-core/src/diff.rs b/crates/ghidrust-core/src/diff.rs
new file mode 100644
index 0000000..827f394
--- /dev/null
+++ b/crates/ghidrust-core/src/diff.rs
@@ -0,0 +1,157 @@
+//! Program-vs-program diff (changed bytes + unmatched sections/functions).
+//!
+//! Reference: Ghidra Version Tracking / radiff2 block comparison — not a count-only dialog.
+
+use crate::program::Program;
+use serde::{Deserialize, Serialize};
+
+#[derive(Debug, Clone, Serialize, Deserialize, Default)]
+pub struct ProgramDiff {
+ pub left_name: String,
+ pub right_name: String,
+ pub unmatched_sections_left: Vec<String>,
+ pub unmatched_sections_right: Vec<String>,
+ pub unmatched_functions_left: Vec<String>,
+ pub unmatched_functions_right: Vec<String>,
+ pub byte_diffs: Vec<ByteDiff>,
+}
+
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct ByteDiff {
+ pub va: u64,
+ pub left: u8,
+ pub right: u8,
+}
+
+impl ProgramDiff {
+ pub fn is_count_only(&self) -> bool {
+ self.byte_diffs.is_empty()
+ && self.unmatched_sections_left.is_empty()
+ && self.unmatched_sections_right.is_empty()
+ && self.unmatched_functions_left.is_empty()
+ && self.unmatched_functions_right.is_empty()
+ }
+
+ pub fn render(&self) -> String {
+ let mut s = format!(
+ "diff {} vs {}\n",
+ self.left_name, self.right_name
+ );
+ if self.is_count_only() {
+ s.push_str("no section/function/byte differences\n");
+ return s;
+ }
+ for n in &self.unmatched_sections_left {
+ s.push_str(&format!("- section {n}\n"));
+ }
+ for n in &self.unmatched_sections_right {
+ s.push_str(&format!("+ section {n}\n"));
+ }
+ for n in &self.unmatched_functions_left {
+ s.push_str(&format!("- function {n}\n"));
+ }
+ for n in &self.unmatched_functions_right {
+ s.push_str(&format!("+ function {n}\n"));
+ }
+ for d in self.byte_diffs.iter().take(64) {
+ s.push_str(&format!(" {:#x}: {:02x} -> {:02x}\n", d.va, d.left, d.right));
+ }
+ if self.byte_diffs.len() > 64 {
+ s.push_str(&format!(" … {} more byte diffs\n", self.byte_diffs.len() - 64));
+ }
+ s
+ }
+}
+
+pub fn diff_programs(left: &Program, right: &Program) -> ProgramDiff {
+ let mut d = ProgramDiff {
+ left_name: left.name.clone(),
+ right_name: right.name.clone(),
+ ..Default::default()
+ };
+ let lsec: Vec<_> = left.sections.iter().map(|s| s.name.clone()).collect();
+ let rsec: Vec<_> = right.sections.iter().map(|s| s.name.clone()).collect();
+ for n in &lsec {
+ if !rsec.iter().any(|x| x == n) {
+ d.unmatched_sections_left.push(n.clone());
+ }
+ }
+ for n in &rsec {
+ if !lsec.iter().any(|x| x == n) {
+ d.unmatched_sections_right.push(n.clone());
+ }
+ }
+ let lfn: Vec<_> = left
+ .analysis
+ .functions
+ .iter()
+ .map(|f| format!("{}:{:#x}", f.name, f.entry))
+ .collect();
+ let rfn: Vec<_> = right
+ .analysis
+ .functions
+ .iter()
+ .map(|f| format!("{}:{:#x}", f.name, f.entry))
+ .collect();
+ for n in &lfn {
+ if !rfn.iter().any(|x| x == n) {
+ d.unmatched_functions_left.push(n.clone());
+ }
+ }
+ for n in &rfn {
+ if !lfn.iter().any(|x| x == n) {
+ d.unmatched_functions_right.push(n.clone());
+ }
+ }
+ for lb in &left.blocks {
+ if let Some(rb) = right.blocks.iter().find(|b| b.va == lb.va) {
+ let n = lb.bytes.len().min(rb.bytes.len());
+ for i in 0..n {
+ if lb.bytes[i] != rb.bytes[i] {
+ d.byte_diffs.push(ByteDiff {
+ va: lb.va + i as u64,
+ left: lb.bytes[i],
+ right: rb.bytes[i],
+ });
+ if d.byte_diffs.len() >= 256 {
+ return d;
+ }
+ }
+ }
+ }
+ }
+ d
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::{create_function, load_bytes};
+
+ #[test]
+ fn mutated_copy_reports_byte_or_function_diff() {
+ let pe = crate::fixture_path("tiny_x64.pe");
+ let data = std::fs::read(&pe).expect("pe");
+ let mut left = load_bytes(&data, "a").expect("left");
+ let mut right = load_bytes(&data, "b").expect("right");
+ let entry = left.entry.unwrap();
+ let _ = create_function(&mut left, entry, None);
+ if let Some(b) = right.blocks.first_mut() {
+ if !b.bytes.is_empty() {
+ b.bytes[0] ^= 0xff;
+ }
+ }
+ let d = diff_programs(&left, &right);
+ assert!(
+ !d.is_count_only(),
+ "expected real diff, got {}",
+ d.render()
+ );
+ let text = d.render();
+ assert!(
+ text.contains("function") || text.contains("0x") || text.contains("section"),
+ "{text}"
+ );
+ assert!(!text.starts_with("Sections:"), "must not be count-only");
+ }
+}
diff --git a/crates/ghidrust-core/src/edits.rs b/crates/ghidrust-core/src/edits.rs
index 0a382d7..4329d48 100644
--- a/crates/ghidrust-core/src/edits.rs
+++ b/crates/ghidrust-core/src/edits.rs
@@ -101,10 +101,15 @@ pub struct ProgramEdits {
/// Function-signature edits keyed by function entry VA.
#[serde(default)]
pub function_signatures: BTreeMap<u64, FunctionSignatureEdit>,
- /// User-defined data types.
- /// Stage-0 stores as strings; later upgrades to a parsed structure.
+ /// User-defined data types (pretty bodies for DTM).
#[serde(default)]
pub user_types: BTreeMap<String, String>,
+ /// Structured C-parser output (name → fields/members).
+ #[serde(default)]
+ pub parsed_types: BTreeMap<String, crate::cparse::ParsedType>,
+ /// Register values persisted on the program (Register Manager).
+ #[serde(default)]
+ pub register_values: BTreeMap<String, String>,
/// User-applied types at specific VAs (Listing "Data Type" apply).
#[serde(default)]
pub applied_types: BTreeMap<u64, String>,
@@ -281,6 +286,23 @@ impl ProgramEdits {
self.user_types.insert(name.into(), desc.into());
}
+ /// Apply C parser output: structured members + DTM pretty bodies.
+ pub fn apply_parsed_c(&mut self, types: Vec<crate::cparse::ParsedType>) {
+ for t in types {
+ let body = crate::cparse::format_type_body(&t);
+ self.user_types.insert(t.name.clone(), body);
+ self.parsed_types.insert(t.name.clone(), t);
+ }
+ }
+
+ pub fn set_register(&mut self, name: impl Into<String>, value: impl Into<String>) {
+ self.register_values.insert(name.into(), value.into());
+ }
+
+ pub fn register(&self, name: &str) -> Option<&str> {
+ self.register_values.get(name).map(String::as_str)
+ }
+
/// Look up the stored body for a user type by name.
pub fn user_type(&self, name: &str) -> Option<&str> {
self.user_types.get(name).map(String::as_str)
diff --git a/crates/ghidrust-core/src/lib.rs b/crates/ghidrust-core/src/lib.rs
index 2794fb4..241b911 100644
--- a/crates/ghidrust-core/src/lib.rs
+++ b/crates/ghidrust-core/src/lib.rs
@@ -3,6 +3,8 @@
pub mod analyzers;
pub mod artifacts;
pub mod bounds_honesty;
+pub mod cparse;
+pub mod diff;
pub mod bulk_scan;
pub mod callsite_hints;
pub mod crypto_pipeline;
@@ -17,6 +19,7 @@ pub mod inflate;
pub mod inventory;
pub mod io_util;
pub mod machine;
+pub mod macho;
pub mod pe;
pub mod pe_functions;
pub mod process;
@@ -53,7 +56,9 @@ pub use bulk_scan::{
set_preferred_bulk_mode, time_bulk_printable, BulkBackend, BulkHit, BulkScanMode,
BulkTimingReport, GpuScanReport, MAX_COMPUTE_WORKGROUPS_PER_DIMENSION_DEFAULT,
};
+pub use cparse::{format_type_body, parse_c_source, ParsedMember, ParsedType};
pub use callsite_hints::{collect_callsite_hints, CallsiteArg, CallsiteHint};
+pub use diff::{diff_programs, ByteDiff, ProgramDiff};
pub use crypto_pipeline::{recover_function_seeds, run_crypto_pipeline, suggest_recipe_for_hint};
pub use decode_bake::{bake, extract_iocs, magic, magic_with_crib, BakeOp, BakeResult};
pub use disasm::{
@@ -83,6 +88,7 @@ pub use inventory::{
PeInventoryEntry, VersionInfo, PE_INVENTORY_SCHEMA,
};
pub use io_util::{sanitize_path_component, sanitized_out_name, write_json_no_bom};
+pub use macho::{is_macho, load_macho, tiny_x64_bytes as macho_tiny_x64_bytes};
pub use machine::{
arch_mode_for_program, arch_mode_from_elf_emachine, arch_mode_from_pe_machine,
default_arch_mode, elf_class_from_bytes, elf_emachine_from_bytes, pe_machine_from_bytes,
@@ -141,9 +147,11 @@ pub fn load_bytes(data: &[u8], name: impl Into<String>) -> Result<Program> {
pe::load_pe(data, name)
} else if elf::is_elf(data) {
elf::load_elf(data, name)
+ } else if macho::is_macho(data) {
+ macho::load_macho(data, name)
} else {
Err(Error::UnsupportedFormat(
- "not a PE or ELF image (bad magic)".into(),
+ "not a PE, ELF, or Mach-O image (bad magic)".into(),
))
}
}
diff --git a/crates/ghidrust-core/src/macho.rs b/crates/ghidrust-core/src/macho.rs
new file mode 100644
index 0000000..eeb2896
--- /dev/null
+++ b/crates/ghidrust-core/src/macho.rs
@@ -0,0 +1,277 @@
+//! Minimal Mach-O 64-bit little-endian loader (hand-rolled).
+//!
+//! Reference: OS X ABI Mach-O File Format / `loader.h` (`mach_header_64`,
+//! `LC_SEGMENT_64`, `LC_UNIXTHREAD` / `LC_MAIN`) — no LLVM/libobject crate.
+
+use crate::error::{Error, Result};
+use crate::program::{MemoryBlock, Program, SectionInfo};
+
+const MH_MAGIC_64: u32 = 0xfeed_facf;
+const MH_CIGAM_64: u32 = 0xcffa_edfe;
+const LC_SEGMENT_64: u32 = 0x19;
+const LC_UNIXTHREAD: u32 = 0x5;
+const LC_MAIN: u32 = 0x8000_0028;
+const CPU_TYPE_X86_64: u32 = 0x0100_0007;
+const X86_THREAD_STATE64: u32 = 4;
+
+pub fn is_macho(data: &[u8]) -> bool {
+ if data.len() < 4 {
+ return false;
+ }
+ let m = u32::from_le_bytes(data[0..4].try_into().unwrap());
+ m == MH_MAGIC_64 || m == MH_CIGAM_64
+}
+
+fn rdu32(data: &[u8], off: usize) -> Result<u32> {
+ if off + 4 > data.len() {
+ return Err(Error::Parse("macho trunc u32".into()));
+ }
+ Ok(u32::from_le_bytes(data[off..off + 4].try_into().unwrap()))
+}
+
+fn rdu64(data: &[u8], off: usize) -> Result<u64> {
+ if off + 8 > data.len() {
+ return Err(Error::Parse("macho trunc u64".into()));
+ }
+ Ok(u64::from_le_bytes(data[off..off + 8].try_into().unwrap()))
+}
+
+fn cstr16(data: &[u8]) -> String {
+ let n = data.iter().position(|&b| b == 0).unwrap_or(data.len()).min(16);
+ String::from_utf8_lossy(&data[..n]).into_owned()
+}
+
+pub fn load_macho(data: &[u8], name: impl Into<String>) -> Result<Program> {
+ if !is_macho(data) {
+ return Err(Error::UnsupportedFormat("not Mach-O".into()));
+ }
+ let magic = rdu32(data, 0)?;
+ if magic != MH_MAGIC_64 {
+ return Err(Error::Parse("only little-endian MH_MAGIC_64".into()));
+ }
+ let cputype = rdu32(data, 4)?;
+ let ncmds = rdu32(data, 16)? as usize;
+ let sizeofcmds = rdu32(data, 20)? as usize;
+ let mut prog = Program::new(name.into(), "Mach-O 64");
+ if cputype == CPU_TYPE_X86_64 {
+ prog.image_base = 0x100000000;
+ }
+ prog.file_bytes = data.to_vec();
+
+ let mut off = 32usize;
+ let cmds_end = 32 + sizeofcmds;
+ for _ in 0..ncmds {
+ if off + 8 > data.len() || off + 8 > cmds_end {
+ break;
+ }
+ let cmd = rdu32(data, off)?;
+ let cmdsize = rdu32(data, off + 4)? as usize;
+ if cmdsize < 8 || off + cmdsize > data.len() {
+ break;
+ }
+ match cmd {
+ LC_SEGMENT_64 => parse_segment_64(&mut prog, data, off, cmdsize)?,
+ LC_UNIXTHREAD => {
+ if let Some(rip) = parse_unixthread_rip(data, off, cmdsize)? {
+ prog.entry = Some(rip);
+ }
+ }
+ LC_MAIN => {
+ if off + 24 <= data.len() {
+ let entryoff = rdu64(data, off + 8)?;
+ if prog.entry.is_none() {
+ prog.entry = Some(prog.image_base.saturating_add(entryoff));
+ }
+ }
+ }
+ _ => {}
+ }
+ off += cmdsize;
+ }
+ if prog.image_base == 0 {
+ if let Some(s) = prog.sections.iter().find(|s| s.va != 0) {
+ prog.image_base = s.va & !0xfff;
+ }
+ }
+ Ok(prog)
+}
+
+fn parse_segment_64(prog: &mut Program, data: &[u8], off: usize, cmdsize: usize) -> Result<()> {
+ // mach_header already skipped; segment_command_64 at off
+ // cmd(4) cmdsize(4) segname(16) vmaddr(8) vmsize(8) fileoff(8) filesize(8)
+ // maxprot(4) initprot(4) nsects(4) flags(4)
+ if cmdsize < 72 {
+ return Ok(());
+ }
+ let segname = cstr16(&data[off + 8..off + 24]);
+ let vmaddr = rdu64(data, off + 24)?;
+ let vmsize = rdu64(data, off + 32)?;
+ let fileoff = rdu64(data, off + 40)? as usize;
+ let filesize = rdu64(data, off + 48)? as usize;
+ let initprot = rdu32(data, off + 56 + 4)?; // skip maxprot
+ let nsects = rdu32(data, off + 64)? as usize;
+ if prog.image_base == 0 && vmaddr != 0 {
+ prog.image_base = vmaddr;
+ }
+ let mut sect_off = off + 72;
+ if nsects == 0 {
+ let mut bytes = vec![0u8; vmsize as usize];
+ let n = filesize.min(data.len().saturating_sub(fileoff)).min(bytes.len());
+ if n > 0 {
+ bytes[..n].copy_from_slice(&data[fileoff..fileoff + n]);
+ }
+ prog.sections.push(SectionInfo {
+ name: segname.clone(),
+ va: vmaddr,
+ virtual_size: vmsize,
+ raw_size: filesize as u64,
+ file_offset: fileoff as u64,
+ characteristics: initprot,
+ });
+ prog.blocks.push(MemoryBlock {
+ name: segname,
+ va: vmaddr,
+ size: vmsize,
+ bytes,
+ readable: initprot & 1 != 0,
+ writable: initprot & 2 != 0,
+ executable: initprot & 4 != 0,
+ });
+ return Ok(());
+ }
+ for _ in 0..nsects {
+ if sect_off + 80 > off + cmdsize || sect_off + 80 > data.len() {
+ break;
+ }
+ let sectname = cstr16(&data[sect_off..sect_off + 16]);
+ let _seg = cstr16(&data[sect_off + 16..sect_off + 32]);
+ let addr = rdu64(data, sect_off + 32)?;
+ let size = rdu64(data, sect_off + 40)?;
+ let soff = rdu32(data, sect_off + 48)? as usize;
+ let flags = rdu32(data, sect_off + 64)?;
+ let mut bytes = vec![0u8; size as usize];
+ let n = (size as usize).min(data.len().saturating_sub(soff));
+ if soff != 0 && n > 0 {
+ bytes[..n].copy_from_slice(&data[soff..soff + n]);
+ }
+ let name = if sectname.is_empty() {
+ segname.clone()
+ } else {
+ format!("{segname},{sectname}")
+ };
+ prog.sections.push(SectionInfo {
+ name: name.clone(),
+ va: addr,
+ virtual_size: size,
+ raw_size: size,
+ file_offset: soff as u64,
+ characteristics: flags,
+ });
+ prog.blocks.push(MemoryBlock {
+ name,
+ va: addr,
+ size,
+ bytes,
+ readable: true,
+ writable: flags & 0x1 == 0, // S_REGULAR writable-ish; keep honest defaults
+ executable: flags & 0x80000000 != 0 || segname == "__TEXT",
+ });
+ sect_off += 80;
+ }
+ Ok(())
+}
+
+fn parse_unixthread_rip(data: &[u8], off: usize, cmdsize: usize) -> Result<Option<u64>> {
+ // cmd, cmdsize, flavor, count, then 21×u64 for x86_THREAD_STATE64
+ // rip is the 16th qword (0-based index 16)
+ if cmdsize < 16 {
+ return Ok(None);
+ }
+ let flavor = rdu32(data, off + 8)?;
+ if flavor != X86_THREAD_STATE64 {
+ return Ok(None);
+ }
+ let state = off + 16;
+ let rip_off = state + 16 * 8;
+ if rip_off + 8 > off + cmdsize || rip_off + 8 > data.len() {
+ return Ok(None);
+ }
+ Ok(Some(rdu64(data, rip_off)?))
+}
+
+/// Tiny x86-64 MH_EXECUTE with `__TEXT,__text` containing `ret` at entry.
+pub fn tiny_x64_bytes() -> Vec<u8> {
+ // header 32 + LC_SEGMENT_64 (72+80=152) + LC_UNIXTHREAD (16+168=184) = 368
+ // text at file offset 0x180 (384), vmaddr 0x100000180
+ const HDR: usize = 32;
+ const SEG: usize = 152;
+ const THR: usize = 184;
+ const TEXT_OFF: usize = 0x180;
+ const VM: u64 = 0x100000000;
+ const TEXT_VA: u64 = VM + TEXT_OFF as u64;
+ let mut b = vec![0u8; TEXT_OFF + 16];
+ // mach_header_64
+ b[0..4].copy_from_slice(&MH_MAGIC_64.to_le_bytes());
+ b[4..8].copy_from_slice(&CPU_TYPE_X86_64.to_le_bytes());
+ b[8..12].copy_from_slice(&3u32.to_le_bytes()); // ALL
+ b[12..16].copy_from_slice(&2u32.to_le_bytes()); // MH_EXECUTE
+ b[16..20].copy_from_slice(&2u32.to_le_bytes()); // ncmds
+ b[20..24].copy_from_slice(&((SEG + THR) as u32).to_le_bytes());
+ b[24..28].copy_from_slice(&1u32.to_le_bytes()); // MH_NOUNDEFS
+ // LC_SEGMENT_64
+ let mut o = HDR;
+ b[o..o + 4].copy_from_slice(&LC_SEGMENT_64.to_le_bytes());
+ b[o + 4..o + 8].copy_from_slice(&(SEG as u32).to_le_bytes());
+ b[o + 8..o + 16].copy_from_slice(b"__TEXT\0\0");
+ b[o + 24..o + 32].copy_from_slice(&VM.to_le_bytes());
+ b[o + 32..o + 40].copy_from_slice(&0x1000u64.to_le_bytes());
+ b[o + 40..o + 48].copy_from_slice(&0u64.to_le_bytes());
+ b[o + 48..o + 56].copy_from_slice(&(TEXT_OFF as u64 + 16).to_le_bytes());
+ b[o + 56..o + 60].copy_from_slice(&7u32.to_le_bytes()); // maxprot rwx
+ b[o + 60..o + 64].copy_from_slice(&5u32.to_le_bytes()); // initprot rx
+ b[o + 64..o + 68].copy_from_slice(&1u32.to_le_bytes()); // nsects
+ o += 72;
+ // section_64
+ b[o..o + 6].copy_from_slice(b"__text");
+ b[o + 16..o + 22].copy_from_slice(b"__TEXT");
+ b[o + 32..o + 40].copy_from_slice(&TEXT_VA.to_le_bytes());
+ b[o + 40..o + 48].copy_from_slice(&16u64.to_le_bytes());
+ b[o + 48..o + 52].copy_from_slice(&(TEXT_OFF as u32).to_le_bytes());
+ b[o + 64..o + 68].copy_from_slice(&0x8000_0400u32.to_le_bytes()); // S_ATTR_PURE_INSTRUCTIONS|SOME_INSTRUCTIONS
+ // LC_UNIXTHREAD
+ o = HDR + SEG;
+ b[o..o + 4].copy_from_slice(&LC_UNIXTHREAD.to_le_bytes());
+ b[o + 4..o + 8].copy_from_slice(&(THR as u32).to_le_bytes());
+ b[o + 8..o + 12].copy_from_slice(&X86_THREAD_STATE64.to_le_bytes());
+ b[o + 12..o + 16].copy_from_slice(&42u32.to_le_bytes()); // count in u32
+ let rip_off = o + 16 + 16 * 8;
+ b[rip_off..rip_off + 8].copy_from_slice(&TEXT_VA.to_le_bytes());
+ b[TEXT_OFF] = 0xC3; // ret
+ b
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn tiny_fixture_loads_sections_and_entry() {
+ let bytes = tiny_x64_bytes();
+ assert!(is_macho(&bytes));
+ let dest = crate::fixture_path("tiny_x64.macho");
+ if !dest.is_file() {
+ let _ = std::fs::write(&dest, &bytes);
+ }
+ let p = load_macho(&bytes, "tiny_x64.macho").expect("load");
+ assert_eq!(p.format, "Mach-O 64");
+ assert!(!p.sections.is_empty(), "sections");
+ let entry = p.entry.expect("entry");
+ assert!(entry > 0);
+ let b = p.read_va(entry, 1).expect("text byte");
+ assert_eq!(b[0], 0xC3);
+ let via_path = crate::load_path(&dest).expect("load_path macho");
+ assert_eq!(via_path.format, "Mach-O 64");
+ assert!(via_path.entry.is_some());
+ assert!(!via_path.sections.is_empty());
+ }
+}
diff --git a/crates/ghidrust-gui/src/app/menus.rs b/crates/ghidrust-gui/src/app/menus.rs
index 246b6c8..03c1f57 100644
--- a/crates/ghidrust-gui/src/app/menus.rs
+++ b/crates/ghidrust-gui/src/app/menus.rs
@@ -75,7 +75,11 @@ impl GhidrustApp {
if ui.button("Parse C Source…").clicked() {
if let Some(path) = rfd::FileDialog::new().add_filter("C source", &["c", "h"]).pick_file() {
match std::fs::read_to_string(&path) {
- Ok(text) => { let name = path.file_stem().unwrap_or_default().to_string_lossy().to_string(); let _ = self.define_user_type(name.clone(), format!("C source stub from {}\n{text}", path.display())); self.status = format!("Imported C source stub {name} into Program types"); self.log(self.status.clone()); }
+ Ok(text) => {
+ if let Err(e) = self.tools_parse_c_source_text(&text) {
+ self.log_error(e);
+ }
+ }
Err(e) => self.log_error(e.to_string()),
}
}
@@ -410,14 +414,21 @@ impl GhidrustApp {
if ui.button("Compare Program…").clicked() {
if let Some(path) = rfd::FileDialog::new().pick_file() {
match load_path(&path) {
- Ok(other) => { self.show_tools_dialog = true; self.tools_dialog_title = "Program comparison".into(); self.tools_dialog_body = format!("{} sections / {} symbols\n{} sections / {} symbols", self.program.as_ref().map(|p| p.sections.len()).unwrap_or(0), self.program.as_ref().map(|p| p.analysis.symbols.len()).unwrap_or(0), other.sections.len(), other.analysis.symbols.len()); }
+ Ok(other) => { let _ = self.tools_diff_with(&other); }
Err(e) => self.log_error(e.to_string()),
}
}
ui.close_menu();
}
if ui.button("Program Differences…").clicked() {
- self.show_tools_dialog = true; self.tools_dialog_title = "Program differences".into(); self.tools_dialog_body = self.program.as_ref().map(|p| format!("Sections: {}\nSymbols: {}\nFunctions: {}", p.sections.len(), p.analysis.symbols.len(), p.analysis.functions.len())).unwrap_or_else(|| "No program loaded".into());
+ if let Some(path) = rfd::FileDialog::new().pick_file() {
+ match load_path(&path) {
+ Ok(other) => { let _ = self.tools_diff_with(&other); }
+ Err(e) => self.log_error(e.to_string()),
+ }
+ } else {
+ self.status = "Program Differences: pick a second program to diff".into();
+ }
ui.close_menu();
}
if ui
@@ -448,19 +459,11 @@ impl GhidrustApp {
ui.close_menu();
}
if ui.button("Create Function").clicked() {
- if let Some(va) = self.listing_focus_va {
- self.show_tools_dialog = true;
- self.tools_dialog_title = "Create Function".into();
- self.tools_dialog_body = format!("Function creation requested at {va:#x}. Use Analysis to discover and persist functions.");
- } else {
- self.status = "Select a listing address first".into();
- }
+ let _ = self.tools_create_function();
ui.close_menu();
}
if ui.button("VTable Probe").clicked() {
- self.show_tools_dialog = true;
- self.tools_dialog_title = "VTable Probe".into();
- self.tools_dialog_body = format!("Recovered RTTI classes: {}", self.rtti.classes.len());
+ let _ = self.tools_vtable_probe();
ui.close_menu();
}
if ui.button("Decode Support").clicked() {
diff --git a/crates/ghidrust-gui/src/app/session_api.rs b/crates/ghidrust-gui/src/app/session_api.rs
index 9cb43cc..c02cbd1 100644
--- a/crates/ghidrust-gui/src/app/session_api.rs
+++ b/crates/ghidrust-gui/src/app/session_api.rs
@@ -26,7 +26,8 @@ use crate::nav::NavLocation;
use crate::panes::{Bookmark, BookmarkKind, PaneKind};
use eframe::egui::{self, Color32};
use ghidrust_core::{
- arch_mode_for_program, default_arch_mode, xrefs_from, xrefs_to, Arch, XRef,
+ arch_mode_for_program, create_function, default_arch_mode, process_vtable_probe, xrefs_from,
+ xrefs_to, Arch, FunctionInfo, XRef,
};
use std::collections::{BTreeMap, BTreeSet};
@@ -1158,6 +1159,145 @@ impl GhidrustApp {
Ok(())
}
+ /// Tools → Create Function: same mutation as `ghidrust function create`.
+ pub fn tools_create_function(&mut self) -> Result<FunctionInfo, String> {
+ let va = self
+ .listing_focus_va
+ .ok_or_else(|| "Select a listing address first".to_string())?;
+ self.tools_create_function_at(va)
+ }
+
+ /// Create/heal a function at `va` on the loaded program and persist it.
+ pub fn tools_create_function_at(&mut self, va: u64) -> Result<FunctionInfo, String> {
+ let prog = self
+ .program
+ .as_mut()
+ .ok_or_else(|| "no program loaded".to_string())?;
+ let f = create_function(prog, va, None);
+ if f.end <= f.entry {
+ return Err(format!(
+ "create_function produced empty range [{:#x}, {:#x})",
+ f.entry, f.end
+ ));
+ }
+ self.status = format!(
+ "Created {} [{:#x}, {:#x}) seed_kind={:?}",
+ f.name, f.entry, f.end, f.seed_kind
+ );
+ self.show_tools_dialog = true;
+ self.tools_dialog_title = "Create Function".into();
+ self.tools_dialog_body = self.status.clone();
+ self.log(self.status.clone());
+ Ok(f)
+ }
+
+ /// Tools → VTable Probe: live `process_vtable_probe` when a debug session exists.
+ pub fn tools_vtable_probe(&mut self) -> Result<String, String> {
+ let Some(sid) = self.debugger.session.as_ref().map(|s| s.session_id.clone()) else {
+ let msg = "VTable Probe: no debug session (attach or launch a process first)".to_string();
+ self.status = msg.clone();
+ self.show_tools_dialog = true;
+ self.tools_dialog_title = "VTable Probe".into();
+ self.tools_dialog_body = msg.clone();
+ self.log_error(msg.clone());
+ return Err(msg);
+ };
+ let va = match self
+ .listing_focus_va
+ .or_else(|| self.program.as_ref().and_then(|p| p.entry))
+ {
+ Some(va) => va,
+ None => {
+ let msg = "VTable Probe: select a listing address first".to_string();
+ self.status = msg.clone();
+ self.log_warn(msg.clone());
+ return Err(msg);
+ }
+ };
+ match process_vtable_probe(&sid, va, 32) {
+ Ok(r) => {
+ let body = serde_json::to_string_pretty(&r).unwrap_or_else(|_| format!("{r:?}"));
+ self.status = format!(
+ "VTable probe object={:#x} vtable={:#x} slots={}",
+ r.object_va,
+ r.vtable_va,
+ r.slots.len()
+ );
+ self.show_tools_dialog = true;
+ self.tools_dialog_title = "VTable Probe".into();
+ self.tools_dialog_body = body.clone();
+ self.log(self.status.clone());
+ Ok(body)
+ }
+ Err(e) => {
+ let msg = format!("VTable Probe: {e}");
+ self.status = msg.clone();
+ self.show_tools_dialog = true;
+ self.tools_dialog_title = "VTable Probe".into();
+ self.tools_dialog_body = msg.clone();
+ self.log_error(msg.clone());
+ Err(msg)
+ }
+ }
+ }
+
+ /// File → Parse C Source: hand-rolled header parser → Program types.
+ pub fn tools_parse_c_source_text(&mut self, src: &str) -> Result<usize, String> {
+ let types = ghidrust_core::parse_c_source(src).map_err(|e| e.to_string())?;
+ let n = types.len();
+ let prog = self
+ .program
+ .as_mut()
+ .ok_or_else(|| "no program loaded".to_string())?;
+ prog.edits.apply_parsed_c(types);
+ self.status = format!("Parse C Source: {n} type(s) into Program");
+ self.show_tools_dialog = true;
+ self.tools_dialog_title = "Parse C Source".into();
+ self.tools_dialog_body = self.status.clone();
+ self.log(self.status.clone());
+ Ok(n)
+ }
+
+ /// Tools → Compare / Program Differences.
+ pub fn tools_diff_with(&mut self, other: &ghidrust_core::Program) -> String {
+ let Some(prog) = self.program.as_ref() else {
+ let msg = "no program loaded".to_string();
+ self.status = msg.clone();
+ return msg;
+ };
+ let d = ghidrust_core::diff_programs(prog, other);
+ let text = d.render();
+ self.status = format!(
+ "Program diff: {} byte / {} fn / {} section deltas",
+ d.byte_diffs.len(),
+ d.unmatched_functions_left.len() + d.unmatched_functions_right.len(),
+ d.unmatched_sections_left.len() + d.unmatched_sections_right.len()
+ );
+ self.show_tools_dialog = true;
+ self.tools_dialog_title = "Program differences".into();
+ self.tools_dialog_body = text.clone();
+ self.log(self.status.clone());
+ text
+ }
+
+ pub fn persist_register(&mut self, name: &str, value: &str) -> Result<(), String> {
+ let prog = self
+ .program
+ .as_mut()
+ .ok_or_else(|| "no program loaded".to_string())?;
+ prog.edits.set_register(name, value);
+ self.register_manager.values.retain(|r| r.register != name);
+ self.register_manager.values.push(crate::register_manager::RegisterValueRow {
+ register: name.into(),
+ start_va: 0,
+ end_va: u64::MAX,
+ value: value.into(),
+ });
+ self.status = format!("Register {name} = {value} (persisted on program)");
+ self.log(self.status.clone());
+ Ok(())
+ }
+
pub(crate) fn direct_references(&mut self) {
let Some(va) = self.listing_focus_va else {
self.status = "Select a listing address first".into();
diff --git a/crates/ghidrust-gui/src/app/tests.rs b/crates/ghidrust-gui/src/app/tests.rs
index 056e0b7..48df5a9 100644
--- a/crates/ghidrust-gui/src/app/tests.rs
+++ b/crates/ghidrust-gui/src/app/tests.rs
@@ -502,6 +502,12 @@ fn shell_has_required_menus_and_panes() {
assert!(both.contains("run_search_text"));
assert!(both.contains("select_all_listing"));
assert!(both.contains("show_processor_dialog"));
+ assert!(both.contains("tools_create_function"));
+ assert!(both.contains("tools_vtable_probe"));
+ assert!(both.contains("tools_parse_c_source"));
+ assert!(!src.contains("Function creation requested"));
+ assert!(!src.contains("Recovered RTTI classes"));
+ assert!(!src.contains("Imported C source stub"));
}
#[test]
@@ -1507,3 +1513,101 @@ fn shell_has_required_menus_and_panes() {
assert_eq!(t.primary[0], 0xD0);
}
+ #[test]
+ fn tools_create_function_persists_entry_and_end_on_fixture() {
+ let mut app = GhidrustApp::headless();
+ app.load_binary(&fixture_path("tiny_x64.pe")).expect("load");
+ let entry = app.program.as_ref().and_then(|p| p.entry).expect("entry");
+ app.listing_focus_va = Some(entry);
+ let created = app.tools_create_function().expect("create function");
+ assert_eq!(created.entry, entry);
+ assert!(
+ created.end > created.entry,
+ "expected real end bound, got [{:#x}, {:#x})",
+ created.entry,
+ created.end
+ );
+ let stored = app
+ .program
+ .as_ref()
+ .and_then(|p| p.function_at(entry).cloned())
+ .expect("function persisted on Program.analysis");
+ assert_eq!(stored.entry, created.entry);
+ assert_eq!(stored.end, created.end);
+ assert!(stored.end > stored.entry);
+ assert!(app.tools_dialog_body.contains(&format!("{entry:#x}")));
+ assert!(!app.tools_dialog_body.contains("Function creation requested"));
+ }
+
+ #[test]
+ fn tools_vtable_probe_without_session_is_honest_error() {
+ let mut app = GhidrustApp::headless();
+ app.load_binary(&fixture_path("tiny_x64.pe")).expect("load");
+ let entry = app.program.as_ref().and_then(|p| p.entry).unwrap();
+ app.listing_focus_va = Some(entry);
+ let err = app.tools_vtable_probe().expect_err("no session");
+ assert!(
+ err.to_lowercase().contains("session"),
+ "honest no-session error, got {err}"
+ );
+ assert!(!err.contains("Recovered RTTI classes"));
+ assert!(!app.tools_dialog_body.contains("Recovered RTTI classes"));
+ assert!(app.tools_dialog_body.to_lowercase().contains("session"));
+ assert!(app.status.to_lowercase().contains("session"));
+ }
+
+ #[test]
+ fn parse_c_source_persists_named_types_with_fields() {
+ let mut app = GhidrustApp::headless();
+ app.load_binary(&fixture_path("tiny_x64.pe")).expect("load");
+ let src = r#"
+ struct Widget { int id; char *name; };
+ typedef unsigned int u32;
+ enum Color { RED, GREEN };
+ int draw(struct Widget *w, int flags);
+ "#;
+ let n = app.tools_parse_c_source_text(src).expect("parse");
+ assert!(n >= 4, "parsed {n}");
+ assert!(!app.status.contains("no C parser"));
+ let prog = app.program.as_ref().unwrap();
+ assert!(prog.edits.parsed_types.contains_key("Widget"));
+ let w = prog.edits.parsed_types.get("Widget").unwrap();
+ assert!(w.members.iter().any(|m| m.name == "id"));
+ assert!(prog.edits.user_types.get("Widget").unwrap().contains("id"));
+ assert!(prog.edits.parsed_types.contains_key("u32"));
+ assert!(prog.edits.parsed_types.contains_key("Color"));
+ let d = prog.edits.parsed_types.get("draw").unwrap();
+ assert!(!d.members.is_empty());
+ }
+
+ #[test]
+ fn tools_diff_is_not_count_only() {
+ let mut app = GhidrustApp::headless();
+ app.load_binary(&fixture_path("tiny_x64.pe")).expect("load");
+ let mut other = ghidrust_core::load_path(fixture_path("tiny_x64.pe")).unwrap();
+ if let Some(b) = other.blocks.first_mut() {
+ if !b.bytes.is_empty() {
+ b.bytes[0] ^= 0xff;
+ }
+ }
+ let text = app.tools_diff_with(&other);
+ assert!(!text.contains("Sections:"), "{text}");
+ assert!(
+ text.contains("0x") || text.contains("function") || text.contains("section"),
+ "{text}"
+ );
+ }
+
+ #[test]
+ fn persist_register_writes_program_edits() {
+ let mut app = GhidrustApp::headless();
+ app.load_binary(&fixture_path("tiny_x64.pe")).expect("load");
+ app.persist_register("RAX", "0xdead").expect("persist");
+ let v = app
+ .program
+ .as_ref()
+ .and_then(|p| p.edits.register("RAX"))
+ .expect("stored");
+ assert_eq!(v, "0xdead");
+ }
+
diff --git a/crates/ghidrust-gui/src/mcp_host.rs b/crates/ghidrust-gui/src/mcp_host.rs
index bc310e6..c11e828 100644
--- a/crates/ghidrust-gui/src/mcp_host.rs
+++ b/crates/ghidrust-gui/src/mcp_host.rs
@@ -232,13 +232,15 @@ pub fn cli_args_for_tool(tool: &str, args: &Value) -> Option<Vec<String>> {
"get_xrefs_to" => {
let p = path?;
let a = addr?;
- Some(vec![
- "xrefs".into(),
- p.into(),
- "--to".into(),
- a.into(),
- "--json".into(),
- ])
+ let mut v = vec!["xrefs".into(), p.into(), "--to".into(), a.into()];
+ if args.get("skip_stubs").and_then(|x| x.as_bool()) == Some(true) {
+ v.push("--skip-stubs".into());
+ }
+ if args.get("classify").and_then(|x| x.as_bool()) == Some(true) {
+ v.push("--classify".into());
+ }
+ push_json(&mut v);
+ Some(v)
}
"get_xrefs_from" => {
let p = path?;
@@ -746,4 +748,33 @@ mod tests {
assert!(reject_net("net_dig").is_err());
assert!(reject_net("load").is_ok());
}
+
+ #[test]
+ fn get_xrefs_to_forwards_skip_stubs_and_classify() {
+ let args = cli_args_for_tool(
+ "get_xrefs_to",
+ &json!({
+ "path": "x.pe",
+ "addr": "0x140001000",
+ "skip_stubs": true,
+ "classify": true
+ }),
+ )
+ .unwrap();
+ assert_eq!(args[0], "xrefs");
+ assert!(args.iter().any(|a| a == "--skip-stubs" || a == "-skip-stubs"));
+ assert!(args.iter().any(|a| a == "--classify" || a == "-classify"));
+ assert!(args.iter().any(|a| a == "--json" || a == "-json"));
+ }
+
+ #[test]
+ fn get_xrefs_to_omits_optional_flags_when_unset() {
+ let args = cli_args_for_tool(
+ "get_xrefs_to",
+ &json!({ "path": "x.pe", "addr": "0x140001000" }),
+ )
+ .unwrap();
+ assert!(!args.iter().any(|a| a.contains("skip-stubs")));
+ assert!(!args.iter().any(|a| a.contains("classify")));
+ }
}
diff --git a/crates/ghidrust-gui/src/network.rs b/crates/ghidrust-gui/src/network.rs
index f5609c0..45d4948 100644
--- a/crates/ghidrust-gui/src/network.rs
+++ b/crates/ghidrust-gui/src/network.rs
@@ -19,7 +19,9 @@ use ghidrust_net_correlate::{
};
use ghidrust_net_detect::{detect_capture_file, inline_block_allowed, last_alerts, load_rules};
use ghidrust_net_parse::extract_pivots;
-use ghidrust_net_rules::{load_rule_pack, CompiledRuleset, Rule};
+use ghidrust_net_rules::{
+ install_et_open, load_active_ruleset, load_rule_pack, CompiledRuleset, InstallOpts, Rule,
+};
use ghidrust_net_schema::{
Alert, AttributedFlow, CaptureSessionInfo, ClosedLoopConfig, Confidence, ConnectionView,
DigJob, DigPlan, DigResult, NetHint, NetworkInfo, Owner, PivotFields,
@@ -45,6 +47,8 @@ pub fn network_info() -> NetworkInfo {
"alerts".into(),
"rules".into(),
"pivots".into(),
+ "et-open".into(),
+ "ids".into(),
],
native: true,
capture: true,
@@ -771,6 +775,41 @@ impl NetworkState {
}
}
+ /// Same install function as `ghidrust net rules update et-open --from-archive`.
+ pub fn install_et_open_from_archive(&mut self, archive: &Path) -> Result<ghidrust_net_rules::InstallReport, String> {
+ let dest = ghidrust_net_rules::user_rules_root();
+ let rep = install_et_open(InstallOpts {
+ from_archive: Some(archive.display().to_string()),
+ dest_root: Some(dest.display().to_string()),
+ et_version: Some("mini".into()),
+ ..Default::default()
+ })?;
+ self.apply_et_report(&rep);
+ Ok(rep)
+ }
+
+ pub fn install_et_open_online(&mut self) -> Result<ghidrust_net_rules::InstallReport, String> {
+ let dest = ghidrust_net_rules::user_rules_root();
+ let rep = install_et_open(InstallOpts {
+ dest_root: Some(dest.display().to_string()),
+ ..Default::default()
+ })?;
+ self.apply_et_report(&rep);
+ Ok(rep)
+ }
+
+ fn apply_et_report(&mut self, rep: &ghidrust_net_rules::InstallReport) {
+ self.rules_path = rep.path.clone();
+ self.rules_warnings = rep.skip_reasons.clone();
+ self.rules_check_ok = Some(rep.ok);
+ self.status = format!(
+ "ET Open {} loaded={} skipped={}",
+ rep.version, rep.loaded, rep.skipped
+ );
+ self.last_error = rep.error.clone();
+ self.ruleset = load_active_ruleset().ok();
+ }
+
pub fn check_rules(&mut self) {
self.last_error = None;
match load_rule_pack(Path::new(&self.rules_path)) {
@@ -795,6 +834,12 @@ impl NetworkState {
if self.ruleset.is_some() {
return Ok(());
}
+ if let Ok(rs) = load_active_ruleset() {
+ self.rules_warnings = rs.warnings.clone();
+ self.rules_check_ok = Some(true);
+ self.ruleset = Some(rs);
+ return Ok(());
+ }
let rs = load_rules(Path::new(&self.rules_path)).map_err(|e| e.to_string())?;
self.rules_warnings = rs.warnings.clone();
self.rules_check_ok = Some(true);
@@ -814,7 +859,8 @@ impl NetworkState {
self.alerts.clear();
self.selected_alert = None;
if let Err(e) = self.begin_network_job("Detect", move || {
- let rules = load_rules(Path::new(&rules_path)).map_err(|e| e.to_string())?;
+ let rules = load_active_ruleset()
+ .or_else(|_| load_rules(Path::new(&rules_path)).map_err(|e| e.to_string()))?;
let mut alerts = detect_capture_file(&path, &rules, &[])?;
alerts.sort_by(|a, b| {
b.severity
@@ -1659,9 +1705,26 @@ fn render_alerts(state: &mut NetworkState, ui: &mut Ui, muted: Color32) {
fn render_rules(state: &mut NetworkState, ui: &mut Ui, muted: Color32) {
ui.small(
- RichText::new("GNR pack — Check before Detect. Default: rules/ghidrust-minimal.rules")
+ RichText::new("GNR / ET Open pack — Check before Detect. Default: rules/ghidrust-minimal.rules + active ET pack")
.color(muted),
);
+ ui.horizontal(|ui| {
+ if ui
+ .button("Download / update ET Open")
+ .on_hover_text("Fetch Proofpoint ET Open, verify md5, activate for Detect")
+ .clicked()
+ {
+ match state.install_et_open_online() {
+ Ok(rep) => {
+ state.status = format!(
+ "ET Open {} loaded={} skipped={}",
+ rep.version, rep.loaded, rep.skipped
+ );
+ }
+ Err(e) => state.last_error = Some(e),
+ }
+ }
+ });
ui.horizontal(|ui| {
ui.label("path");
ui.add(
@@ -2014,6 +2077,7 @@ mod tests {
sid: 1,
msg: "prior".into(),
severity: 1,
+ action: "alert".into(),
flow_key: None,
owner: None,
timestamp_ms: 0,
@@ -2042,6 +2106,7 @@ mod tests {
sid: 9,
msg: "keep?".into(),
severity: 3,
+ action: "alert".into(),
flow_key: None,
owner: None,
timestamp_ms: 0,
@@ -2319,6 +2384,7 @@ mod tests {
sid: 1,
msg: "t".into(),
severity: 2,
+ action: "alert".into(),
flow_key: None,
owner: Some(Owner {
pid: 1,
@@ -2347,6 +2413,7 @@ mod tests {
sid: 1,
msg: "t".into(),
severity: 1,
+ action: "alert".into(),
flow_key: None,
owner: None,
timestamp_ms: 0,
@@ -2425,6 +2492,47 @@ mod tests {
let _ = std::fs::remove_dir_all(&dir);
}
+ #[test]
+ fn download_et_open_button_is_wired_to_install() {
+ let src = include_str!("network.rs");
+ assert!(src.contains("Download / update ET Open"));
+ assert!(src.contains("install_et_open_online"));
+ assert!(src.contains("install_et_open_from_archive"));
+ let dir = std::env::temp_dir().join(format!(
+ "gui-et-{}-{}",
+ std::process::id(),
+ std::time::SystemTime::now()
+ .duration_since(std::time::UNIX_EPOCH)
+ .unwrap()
+ .as_nanos()
+ ));
+ let _ = std::fs::create_dir_all(dir.join("src"));
+ std::fs::write(dir.join("src").join("LICENSE"), b"test\n").unwrap();
+ std::fs::write(
+ dir.join("src").join("et.rules"),
+ b"alert tcp any any -> any any (msg:\"c\"; content:\"EVILCMD\"; sid:2000001;)\n",
+ )
+ .unwrap();
+ let bytes = ghidrust_net_rules::tar_gz_dir(&dir.join("src")).unwrap();
+ let arch = dir.join("mini.tar.gz");
+ std::fs::write(&arch, &bytes).unwrap();
+ let dest = dir.join("rules-root");
+ std::env::set_var("GHIDRUST_RULES_DIR", dest.display().to_string());
+ let mut st = NetworkState::default();
+ let rep = st.install_et_open_from_archive(&arch).expect("install");
+ assert!(rep.ok, "{:?}", rep.error);
+ assert!(
+ rep.loaded >= 1,
+ "loaded={} skipped={} reasons={:?}",
+ rep.loaded,
+ rep.skipped,
+ rep.skip_reasons
+ );
+ assert!(st.ruleset.as_ref().map(|r| !r.rules.is_empty()).unwrap_or(false));
+ std::env::remove_var("GHIDRUST_RULES_DIR");
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+
#[test]
fn refresh_connections_via_worker() {
let mut st = NetworkState::default();
@@ -2497,7 +2605,7 @@ mod tests {
assert!(info.native);
assert!(info.capture);
assert_eq!(info.wave, 5);
- for need in ["dig", "capture", "detect", "pivots", "connections"] {
+ for need in ["dig", "capture", "detect", "pivots", "connections", "et-open", "ids"] {
assert!(info.caps.iter().any(|c| c == need), "missing cap {need}");
}
}
diff --git a/crates/ghidrust-lift/src/lib.rs b/crates/ghidrust-lift/src/lib.rs
index c7b5f9d..071c09c 100644
--- a/crates/ghidrust-lift/src/lib.rs
+++ b/crates/ghidrust-lift/src/lib.rs
@@ -729,6 +729,29 @@ fn lift_with_ctx(ctx: &mut LiftCtx, insn: &Instruction) -> Vec<PcodeOp> {
PcodeOp::new(OpCode::Pop, Some(rbp), vec![]).with_note("leave"),
];
}
+ "pause" => {
+ return vec![PcodeOp::new(OpCode::Nop, None, vec![]).with_note("pause")];
+ }
+ "clc" | "stc" | "cld" | "std" | "cmc" => {
+ return vec![PcodeOp::new(OpCode::Nop, None, vec![]).with_note(mnem)];
+ }
+ "xchg" => {
+ if parts.len() >= 2 {
+ if let (Some((a, asz)), Some((b, bsz))) = (parse_reg(&parts[0]), parse_reg(&parts[1]))
+ {
+ let sz = asz.min(bsz);
+ let va = a.as_varnode(sz);
+ let vb = b.as_varnode(sz);
+ let tmp = ctx.take_unique(sz);
+ return vec![
+ PcodeOp::new(OpCode::Copy, Some(tmp.clone()), vec![va.clone()]),
+ PcodeOp::new(OpCode::Copy, Some(va), vec![vb.clone()]),
+ PcodeOp::new(OpCode::Copy, Some(vb), vec![tmp])
+ .with_note(format!("xchg {}", insn.operands)),
+ ];
+ }
+ }
+ }
"push" => {
if let Some(first) = parts.first() {
if let Some((reg, size)) = parse_reg(first) {
@@ -1079,22 +1102,6 @@ fn lift_with_ctx(ctx: &mut LiftCtx, insn: &Instruction) -> Vec<PcodeOp> {
return ops;
}
}
- "xchg" if parts.len() == 2 => {
- // xchg a, b → tmp = a; a = b; b = tmp.
- if let (Some((a, asz)), Some((b, bsz))) = (parse_reg(parts[0]), parse_reg(parts[1])) {
- let sz = asz.min(bsz);
- let a_vn = a.as_varnode(sz);
- let b_vn = b.as_varnode(sz);
- let tmp = ctx.take_unique(sz);
- return vec![
- PcodeOp::new(OpCode::Copy, Some(tmp.clone()), vec![a_vn.clone()])
- .with_note("xchg tmp"),
- PcodeOp::new(OpCode::Copy, Some(a_vn), vec![b_vn.clone()])
- .with_note(format!("xchg {}", insn.operands)),
- PcodeOp::new(OpCode::Copy, Some(b_vn), vec![tmp]).with_note("xchg tmp"),
- ];
- }
- }
"call" => {
if let Some(first) = parts.first() {
if let Some(target) = parse_imm(first) {
@@ -1790,6 +1797,50 @@ mod tests {
assert_eq!(seq.ops.last().unwrap().opcode, OpCode::Pop);
}
+ #[test]
+ fn common_x86_mnemonics_lift_or_are_explicitly_remaining() {
+ use ghidrust_decode::Instruction;
+ let cases: &[(&str, &str)] = &[
+ ("nop", ""),
+ ("pause", ""),
+ ("push", "rax"),
+ ("pop", "rax"),
+ ("ret", ""),
+ ("leave", ""),
+ ("mov", "rax, rbx"),
+ ("lea", "rax, [rbx]"),
+ ("add", "rax, rbx"),
+ ("sub", "rax, rbx"),
+ ("xor", "rax, rax"),
+ ("and", "rax, rbx"),
+ ("or", "rax, rbx"),
+ ("cmp", "rax, rbx"),
+ ("test", "rax, rax"),
+ ("xchg", "rax, rbx"),
+ ("bswap", "eax"),
+ ("int3", ""),
+ ("hlt", ""),
+ ("syscall", ""),
+ ("cpuid", ""),
+ ("clc", ""),
+ ("call", "rax"),
+ ("jmp", "rax"),
+ ];
+ let mut remaining = Vec::new();
+ for (m, opsnd) in cases {
+ let insn = Instruction::with_text(0x1000, vec![0x90], *m, *opsnd, 1);
+ let ops = lift_instruction(&insn);
+ if ops.iter().all(|o| o.opcode == OpCode::Unimplemented) {
+ remaining.push(*m);
+ }
+ }
+ const STILL_UNIMPLEMENTED: &[&str] = &[];
+ assert_eq!(
+ remaining, STILL_UNIMPLEMENTED,
+ "common mnemonic still Unimplemented: {remaining:?}"
+ );
+ }
+
#[test]
fn unhandled_becomes_unimplemented_and_coverage_reflects() {
let insn = Instruction::with_text(0, vec![0xff], "wibble", "", 1);
diff --git a/crates/ghidrust-net-capture/src/acq/ghidnetcap.rs b/crates/ghidrust-net-capture/src/acq/ghidnetcap.rs
new file mode 100644
index 0000000..9b7156a
--- /dev/null
+++ b/crates/ghidrust-net-capture/src/acq/ghidnetcap.rs
@@ -0,0 +1,627 @@
+//! Hand-rolled live acquisition (ghidnetcap).
+//!
+//! Reference (not linked): Npcap Packet32 `PacketOpenAdapter` / `PacketReceivePacket`,
+//! libpcap `pcap_findalldevs` / `pcap_open_live` / `pcap_next_ex`, WinPcap NPF
+//! `\\.\NPCAP\{guid}` + `ReadFile`, and Windows `SIO_RCVALL` raw sockets.
+//!
+//! Cargo does **not** link npcap/wpcap/Packet.dll. NPF is optional runtime
+//! `CreateFile` if a driver is installed; otherwise IPv4 raw sockets.
+
+use super::{CaptureBackend, CaptureError};
+use crate::acq::pkt::decode_packet;
+use ghidrust_net_flow::now_ms;
+use serde::{Deserialize, Serialize};
+
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct IfaceInfo {
+ pub name: String,
+ pub description: String,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub ipv4: Option<String>,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub guid: Option<String>,
+}
+
+/// OS adapters (empty on enum failure). Never includes the synthetic `replay` row.
+pub fn enumerate_os_ifaces() -> Vec<IfaceInfo> {
+ #[cfg(windows)]
+ {
+ win::enumerate()
+ }
+ #[cfg(not(windows))]
+ {
+ unix_sysfs_ifaces()
+ }
+}
+
+/// Open a live iface. `None` / `"live"` / empty → first IPv4 adapter.
+///
+/// Fail-closed: `privilege_required` or `backend_unavailable`. Never fabricates frames.
+pub fn open_live(iface: Option<&str>) -> Result<GhidnetcapBackend, CaptureError> {
+ if std::env::var("GHIDRUST_NET_LIVE_STUB").ok().as_deref() == Some("1") {
+ return Err(CaptureError::unavailable(
+ "GHIDRUST_NET_LIVE_STUB=1 selects LiveStubBackend, not ghidnetcap",
+ ));
+ }
+ let want = iface.map(str::trim).filter(|s| !s.is_empty() && *s != "live");
+ if matches!(want, Some("replay")) {
+ return Err(CaptureError::unavailable("replay iface requires replay_path"));
+ }
+ #[cfg(windows)]
+ {
+ win::open(want)
+ }
+ #[cfg(not(windows))]
+ {
+ unix_open(want)
+ }
+}
+
+pub struct GhidnetcapBackend {
+ label: String,
+ inner: Inner,
+}
+
+impl std::fmt::Debug for GhidnetcapBackend {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.debug_struct("GhidnetcapBackend")
+ .field("label", &self.label)
+ .finish()
+ }
+}
+
+enum Inner {
+ #[cfg(windows)]
+ Raw { sock: usize },
+ #[cfg(windows)]
+ Npf { handle: *mut std::ffi::c_void },
+ #[cfg(not(windows))]
+ Packet { fd: i32 },
+}
+
+// NPF HANDLE is used only on the capture thread.
+unsafe impl Send for GhidnetcapBackend {}
+
+impl CaptureBackend for GhidnetcapBackend {
+ fn poll_frames(&mut self) -> Result<Vec<FrameWrap>, CaptureError> {
+ self.poll_inner()
+ }
+ fn name(&self) -> &str {
+ &self.label
+ }
+}
+
+// Trait uses Frame — alias to keep this file compiling against CaptureBackend.
+use ghidrust_net_flow::Frame as FrameWrap;
+
+impl GhidnetcapBackend {
+ fn poll_inner(&mut self) -> Result<Vec<FrameWrap>, CaptureError> {
+ #[cfg(windows)]
+ {
+ win::poll(self)
+ }
+ #[cfg(not(windows))]
+ {
+ unix_poll(self)
+ }
+ }
+}
+
+impl Drop for GhidnetcapBackend {
+ fn drop(&mut self) {
+ #[cfg(windows)]
+ win::close(self);
+ #[cfg(not(windows))]
+ unix_close(self);
+ }
+}
+
+#[cfg(not(windows))]
+fn unix_sysfs_ifaces() -> Vec<IfaceInfo> {
+ let path = std::path::Path::new("/sys/class/net");
+ let Ok(rd) = std::fs::read_dir(path) else {
+ return Vec::new();
+ };
+ rd.filter_map(|e| e.ok())
+ .filter_map(|e| e.file_name().into_string().ok())
+ .map(|name| IfaceInfo {
+ description: format!("sysfs {name}"),
+ name: name.clone(),
+ ipv4: None,
+ guid: None,
+ })
+ .collect()
+}
+
+#[cfg(not(windows))]
+fn unix_open(_want: Option<&str>) -> Result<GhidnetcapBackend, CaptureError> {
+ Err(CaptureError::unavailable(
+ "AF_PACKET live capture is not enabled on this host; use replay",
+ ))
+}
+
+#[cfg(not(windows))]
+fn unix_poll(_b: &mut GhidnetcapBackend) -> Result<Vec<FrameWrap>, CaptureError> {
+ Ok(Vec::new())
+}
+
+#[cfg(not(windows))]
+fn unix_close(_b: &mut GhidnetcapBackend) {}
+
+#[cfg(windows)]
+mod win {
+ use super::*;
+ use std::mem::{size_of, zeroed};
+ use std::ptr::null_mut;
+
+ type Handle = *mut std::ffi::c_void;
+ type Dword = u32;
+ type Socket = usize;
+
+ const INVALID_SOCKET: Socket = !0;
+ const SOCKET_ERROR: i32 = -1;
+ const AF_INET: i32 = 2;
+ const SOCK_RAW: i32 = 3;
+ const IPPROTO_IP: i32 = 0;
+ const SIO_RCVALL: i32 = -1744830463; // 0x98000001 as i32
+ const RCVALL_ON: u32 = 1;
+ const FIONBIO: i32 = -2147195266; // 0x8004667E
+ const WSAEACCES: i32 = 10013;
+ const WSAEWOULDBLOCK: i32 = 10035;
+ const WSAEADDRNOTAVAIL: i32 = 10049;
+ const GENERIC_READ: Dword = 0x8000_0000;
+ const GENERIC_WRITE: Dword = 0x4000_0000;
+ const OPEN_EXISTING: Dword = 3;
+ const FILE_ATTRIBUTE_NORMAL: Dword = 0x80;
+ const INVALID_HANDLE_VALUE: Handle = -1isize as Handle;
+ const ERROR_ACCESS_DENIED: Dword = 5;
+
+ #[repr(C)]
+ struct WsaData {
+ w_version: u16,
+ w_high: u16,
+ sz_desc: [u8; 257],
+ sz_sys: [u8; 129],
+ max_sockets: u16,
+ max_udp: u16,
+ vendor: *mut u8,
+ }
+
+ #[repr(C)]
+ struct SockAddrIn {
+ family: u16,
+ port: u16,
+ addr: u32,
+ zero: [u8; 8],
+ }
+
+ #[repr(C)]
+ struct IpAddrString {
+ next: *mut IpAddrString,
+ ip_address: [u8; 16],
+ ip_mask: [u8; 16],
+ context: Dword,
+ }
+
+ #[repr(C)]
+ struct IpAdapterInfo {
+ next: *mut IpAdapterInfo,
+ combo_index: Dword,
+ adapter_name: [u8; 260],
+ description: [u8; 132],
+ address_length: u32,
+ address: [u8; 8],
+ index: Dword,
+ typ: u32,
+ dhcp_enabled: u32,
+ current_ip: *mut IpAddrString,
+ ip_address_list: IpAddrString,
+ gateway_list: IpAddrString,
+ dhcp_server: IpAddrString,
+ have_wins: i32,
+ primary_wins: IpAddrString,
+ secondary_wins: IpAddrString,
+ lease_obtained: i64,
+ lease_expires: i64,
+ }
+
+ #[link(name = "iphlpapi")]
+ extern "system" {
+ fn GetAdaptersInfo(p: *mut IpAdapterInfo, len: *mut Dword) -> Dword;
+ }
+
+ #[link(name = "ws2_32")]
+ extern "system" {
+ fn WSAStartup(ver: u16, data: *mut WsaData) -> i32;
+ fn socket(af: i32, typ: i32, proto: i32) -> Socket;
+ fn bind(s: Socket, addr: *const SockAddrIn, len: i32) -> i32;
+ fn closesocket(s: Socket) -> i32;
+ fn recv(s: Socket, buf: *mut u8, len: i32, flags: i32) -> i32;
+ fn ioctlsocket(s: Socket, cmd: i32, arg: *mut u32) -> i32;
+ fn WSAIoctl(
+ s: Socket,
+ code: i32,
+ inbuf: *const u8,
+ inlen: Dword,
+ outbuf: *mut u8,
+ outlen: Dword,
+ ret: *mut Dword,
+ overlapped: *mut u8,
+ completion: *mut u8,
+ ) -> i32;
+ fn WSAGetLastError() -> i32;
+ }
+
+ #[link(name = "kernel32")]
+ extern "system" {
+ fn CreateFileW(
+ name: *const u16,
+ access: Dword,
+ share: Dword,
+ sa: *mut u8,
+ disp: Dword,
+ flags: Dword,
+ tmpl: Handle,
+ ) -> Handle;
+ fn CloseHandle(h: Handle) -> i32;
+ fn ReadFile(
+ h: Handle,
+ buf: *mut u8,
+ n: Dword,
+ read: *mut Dword,
+ overlapped: *mut u8,
+ ) -> i32;
+ fn GetLastError() -> Dword;
+ }
+
+ fn wsa_once() -> Result<(), CaptureError> {
+ use std::sync::Once;
+ static ONCE: Once = Once::new();
+ let mut rc = 0i32;
+ ONCE.call_once(|| unsafe {
+ let mut d: WsaData = zeroed();
+ rc = WSAStartup(0x0202, &mut d);
+ });
+ if rc != 0 {
+ return Err(CaptureError::unavailable(format!("WSAStartup {rc}")));
+ }
+ Ok(())
+ }
+
+ fn cstr(buf: &[u8]) -> String {
+ let n = buf.iter().position(|&b| b == 0).unwrap_or(buf.len());
+ String::from_utf8_lossy(&buf[..n]).into_owned()
+ }
+
+ pub fn enumerate() -> Vec<IfaceInfo> {
+ unsafe {
+ let mut len: Dword = 0;
+ GetAdaptersInfo(null_mut(), &mut len);
+ if len == 0 {
+ return Vec::new();
+ }
+ let mut buf = vec![0u8; len as usize];
+ let rc = GetAdaptersInfo(buf.as_mut_ptr() as *mut IpAdapterInfo, &mut len);
+ if rc != 0 {
+ return Vec::new();
+ }
+ let mut out = Vec::new();
+ let base = buf.as_ptr() as usize;
+ let mut p = buf.as_ptr() as usize;
+ let end = base + buf.len();
+ // MSVC x64 IP_ADAPTER_INFO: Next@0, ComboIndex@8, AdapterName@12 (260),
+ // Description@272 (132), … IpAddressList.IpAddress after pointer-aligned hole.
+ const NAME: usize = 12;
+ const DESC: usize = 272;
+ const IP_ADDR: usize = 448; // IpAddressList.IpAddress after Next pointer
+ while p >= base && p + IP_ADDR + 16 <= end {
+ let off = p - base;
+ let guid = cstr(&buf[off + NAME..off + NAME + 260]);
+ let desc = cstr(&buf[off + DESC..off + DESC + 132]);
+ let ip = cstr(&buf[off + IP_ADDR..off + IP_ADDR + 16]);
+ let ipv4 = if ip.is_empty() || ip == "0.0.0.0" {
+ None
+ } else {
+ Some(ip)
+ };
+ let name = if guid.is_empty() {
+ desc.clone()
+ } else {
+ guid.clone()
+ };
+ if !name.is_empty() {
+ out.push(IfaceInfo {
+ name,
+ description: desc,
+ ipv4,
+ guid: if guid.is_empty() { None } else { Some(guid) },
+ });
+ }
+ let next = usize::from_le_bytes(buf[off..off + 8].try_into().unwrap());
+ if next == 0 || next == p {
+ break;
+ }
+ p = next;
+ }
+ out
+ }
+ }
+
+ fn pick(want: Option<&str>) -> Result<IfaceInfo, CaptureError> {
+ let ifaces = enumerate();
+ if ifaces.is_empty() {
+ return Err(CaptureError::unavailable("GetAdaptersInfo returned no adapters"));
+ }
+ if let Some(w) = want {
+ ifaces
+ .into_iter()
+ .find(|i| {
+ i.name.eq_ignore_ascii_case(w)
+ || i.description.to_ascii_lowercase().contains(&w.to_ascii_lowercase())
+ || i.guid.as_deref().is_some_and(|g| g.eq_ignore_ascii_case(w))
+ })
+ .ok_or_else(|| CaptureError::unavailable(format!("no such iface: {w}")))
+ } else {
+ ifaces
+ .into_iter()
+ .find(|i| i.ipv4.is_some())
+ .ok_or_else(|| CaptureError::unavailable("no IPv4 adapter"))
+ }
+ }
+
+ fn ipv4_to_s_addr(s: &str) -> Option<u32> {
+ let mut o = [0u8; 4];
+ let parts: Vec<_> = s.split('.').collect();
+ if parts.len() != 4 {
+ return None;
+ }
+ for (i, p) in parts.iter().enumerate() {
+ o[i] = p.parse().ok()?;
+ }
+ Some(u32::from_be_bytes(o).to_be()) // s_addr is in network order, stored as u32
+ }
+
+ fn try_npf(guid: &str) -> Result<Handle, CaptureError> {
+ let g = guid.trim_matches(|c| c == '{' || c == '}');
+ let paths = [
+ format!("\\\\.\\NPCAP\\{{{g}}}"),
+ format!("\\\\.\\NPF_{{{g}}}"),
+ format!("\\\\.\\NPCAP\\{guid}"),
+ format!("\\\\.\\NPF_{guid}"),
+ ];
+ unsafe {
+ for p in &paths {
+ let wide: Vec<u16> = p.encode_utf16().chain(std::iter::once(0)).collect();
+ let h = CreateFileW(
+ wide.as_ptr(),
+ GENERIC_READ | GENERIC_WRITE,
+ 0,
+ null_mut(),
+ OPEN_EXISTING,
+ FILE_ATTRIBUTE_NORMAL,
+ null_mut(),
+ );
+ if h != INVALID_HANDLE_VALUE && !h.is_null() {
+ return Ok(h);
+ }
+ let err = GetLastError();
+ if err == ERROR_ACCESS_DENIED {
+ return Err(CaptureError::privilege());
+ }
+ }
+ }
+ Err(CaptureError::unavailable("NPF/Npcap device not present"))
+ }
+
+ fn try_raw(ipv4: &str) -> Result<Socket, CaptureError> {
+ wsa_once()?;
+ let addr = ipv4_to_s_addr(ipv4).ok_or_else(|| {
+ CaptureError::unavailable(format!("bad ipv4 {ipv4}"))
+ })?;
+ unsafe {
+ let s = socket(AF_INET, SOCK_RAW, IPPROTO_IP);
+ if s == INVALID_SOCKET {
+ let e = WSAGetLastError();
+ return Err(if e == WSAEACCES {
+ CaptureError::privilege()
+ } else {
+ CaptureError::unavailable(format!("socket SOCK_RAW: WSA{e}"))
+ });
+ }
+ let sa = SockAddrIn {
+ family: AF_INET as u16,
+ port: 0,
+ addr,
+ zero: [0; 8],
+ };
+ if bind(s, &sa, size_of::<SockAddrIn>() as i32) == SOCKET_ERROR {
+ let e = WSAGetLastError();
+ closesocket(s);
+ return Err(if e == WSAEACCES || e == WSAEADDRNOTAVAIL {
+ CaptureError::privilege()
+ } else {
+ CaptureError::unavailable(format!("bind: WSA{e}"))
+ });
+ }
+ let on: u32 = RCVALL_ON;
+ let mut ret: Dword = 0;
+ if WSAIoctl(
+ s,
+ SIO_RCVALL,
+ &on as *const u32 as *const u8,
+ 4,
+ null_mut(),
+ 0,
+ &mut ret,
+ null_mut(),
+ null_mut(),
+ ) == SOCKET_ERROR
+ {
+ let e = WSAGetLastError();
+ closesocket(s);
+ return Err(if e == WSAEACCES {
+ CaptureError::privilege()
+ } else {
+ CaptureError::unavailable(format!("SIO_RCVALL: WSA{e}"))
+ });
+ }
+ let mut nb: u32 = 1;
+ let _ = ioctlsocket(s, FIONBIO, &mut nb);
+ Ok(s)
+ }
+ }
+
+ pub fn open(want: Option<&str>) -> Result<GhidnetcapBackend, CaptureError> {
+ let info = pick(want)?;
+ if let Some(g) = &info.guid {
+ match try_npf(g) {
+ Ok(h) => {
+ return Ok(GhidnetcapBackend {
+ label: format!("ghidnetcap-npf:{}", info.name),
+ inner: Inner::Npf { handle: h },
+ });
+ }
+ Err(e) if e.code == "privilege_required" => return Err(e),
+ Err(_) => {}
+ }
+ }
+ let ip = info
+ .ipv4
+ .as_deref()
+ .ok_or_else(|| CaptureError::unavailable("adapter has no IPv4 for SIO_RCVALL"))?;
+ let sock = try_raw(ip)?;
+ Ok(GhidnetcapBackend {
+ label: format!("ghidnetcap-raw:{}", info.name),
+ inner: Inner::Raw { sock },
+ })
+ }
+
+ pub fn poll(b: &mut GhidnetcapBackend) -> Result<Vec<FrameWrap>, CaptureError> {
+ let mut out = Vec::new();
+ let ts = now_ms();
+ match &mut b.inner {
+ Inner::Raw { sock } => unsafe {
+ let mut buf = [0u8; 65535];
+ for _ in 0..32 {
+ let n = recv(*sock, buf.as_mut_ptr(), buf.len() as i32, 0);
+ if n == SOCKET_ERROR {
+ let e = WSAGetLastError();
+ if e == WSAEWOULDBLOCK {
+ break;
+ }
+ if e == WSAEACCES {
+ return Err(CaptureError::privilege());
+ }
+ return Err(CaptureError::unavailable(format!("recv WSA{e}")));
+ }
+ if n <= 0 {
+ break;
+ }
+ if let Some(f) = decode_packet(ts, &buf[..n as usize]) {
+ out.push(f);
+ }
+ }
+ },
+ Inner::Npf { handle } => unsafe {
+ let mut buf = [0u8; 65535];
+ let mut read: Dword = 0;
+ let ok = ReadFile(*handle, buf.as_mut_ptr(), buf.len() as Dword, &mut read, null_mut());
+ if ok == 0 {
+ let e = GetLastError();
+ if e == 997 || e == 0 || e == 234 {
+ // pending / EOF-ish — no frames this poll
+ return Ok(out);
+ }
+ if e == ERROR_ACCESS_DENIED {
+ return Err(CaptureError::privilege());
+ }
+ return Err(CaptureError::unavailable(format!("NPF ReadFile {e}")));
+ }
+ // bpf_hdr: tv_sec i32, tv_usec i32, caplen u32, datalen u32, hdrlen u16 (+ pad)
+ if read >= 18 {
+ let caplen = u32::from_le_bytes(buf[8..12].try_into().unwrap()) as usize;
+ let hdrlen = u16::from_le_bytes(buf[16..18].try_into().unwrap()) as usize;
+ let start = if hdrlen >= 18 && hdrlen < read as usize {
+ hdrlen
+ } else {
+ 18
+ };
+ let end = (start + caplen).min(read as usize);
+ if end > start {
+ if let Some(f) = decode_packet(ts, &buf[start..end]) {
+ out.push(f);
+ }
+ }
+ }
+ },
+ }
+ Ok(out)
+ }
+
+ pub fn close(b: &mut GhidnetcapBackend) {
+ match &b.inner {
+ Inner::Raw { sock } => unsafe {
+ let _ = closesocket(*sock);
+ },
+ Inner::Npf { handle } => unsafe {
+ let _ = CloseHandle(*handle);
+ },
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn os_enum_is_not_only_placeholders() {
+ let os = enumerate_os_ifaces();
+ // When the OS returns adapters, none of this list is the synthetic pair.
+ for i in &os {
+ assert_ne!(i.name, "replay");
+ }
+ if cfg!(windows) {
+ // GetAdaptersInfo almost always yields at least Loopback on Windows.
+ // If it is empty, live-open must still be honest (covered below).
+ let names: Vec<_> = os.iter().map(|i| i.name.as_str()).collect();
+ assert!(
+ !names.is_empty() || open_live(None).is_err(),
+ "empty OS enum must not pretend live capture works"
+ );
+ }
+ }
+
+ #[test]
+ fn live_open_unknown_iface_is_unavailable() {
+ let err = open_live(Some("no-such-ghidnetcap-iface-xyz")).unwrap_err();
+ assert!(
+ err.code == "backend_unavailable" || err.code == "privilege_required",
+ "{}",
+ err.code
+ );
+ }
+
+ #[test]
+ fn live_open_does_not_emit_stub_payload() {
+ match open_live(None) {
+ Ok(mut b) => {
+ let frames = b.poll_frames().unwrap_or_default();
+ for f in frames {
+ assert_ne!(
+ f.payload.as_slice(),
+ b"live-stub",
+ "ghidnetcap must not fabricate the live-stub frame"
+ );
+ }
+ }
+ Err(e) => {
+ assert!(
+ e.code == "privilege_required" || e.code == "backend_unavailable",
+ "honest error, got {}",
+ e.code
+ );
+ }
+ }
+ }
+}
diff --git a/crates/ghidrust-net-capture/src/acq/mod.rs b/crates/ghidrust-net-capture/src/acq/mod.rs
index d99bcab..c9ebf70 100644
--- a/crates/ghidrust-net-capture/src/acq/mod.rs
+++ b/crates/ghidrust-net-capture/src/acq/mod.rs
@@ -1,9 +1,14 @@
//! Frame acquisition backends.
+pub mod ghidnetcap;
+pub mod pkt;
+
use ghidrust_net_flow::Frame;
use ghidrust_net_schema::Owner;
use serde::{Deserialize, Serialize};
+pub use ghidnetcap::{enumerate_os_ifaces, open_live, GhidnetcapBackend, IfaceInfo};
+
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CaptureError {
pub code: String,
diff --git a/crates/ghidrust-net-capture/src/acq/pkt.rs b/crates/ghidrust-net-capture/src/acq/pkt.rs
new file mode 100644
index 0000000..baf7aaf
--- /dev/null
+++ b/crates/ghidrust-net-capture/src/acq/pkt.rs
@@ -0,0 +1,133 @@
+//! L2/L3 frame decode (libpcap/Npcap packet layout as reference; no pcap crate).
+//!
+//! Ethernet (14) or raw IPv4 (SIO_RCVALL) → [`ghidrust_net_flow::Frame`].
+
+use ghidrust_net_flow::Frame;
+
+pub fn decode_packet(ts_ms: u64, bytes: &[u8]) -> Option<Frame> {
+ if bytes.len() >= 14 && is_ethertype_ip(&bytes[12..14]) {
+ return decode_l3(ts_ms, &bytes[14..]);
+ }
+ if bytes.len() >= 20 && bytes[0] >> 4 == 4 {
+ return decode_l3(ts_ms, bytes);
+ }
+ None
+}
+
+fn is_ethertype_ip(et: &[u8]) -> bool {
+ et == [0x08, 0x00] || et == [0x86, 0xdd]
+}
+
+fn decode_l3(ts_ms: u64, ip: &[u8]) -> Option<Frame> {
+ if ip.len() < 20 {
+ return None;
+ }
+ let ver = ip[0] >> 4;
+ if ver == 4 {
+ decode_ipv4(ts_ms, ip)
+ } else {
+ None
+ }
+}
+
+fn decode_ipv4(ts_ms: u64, ip: &[u8]) -> Option<Frame> {
+ let ihl = ((ip[0] & 0x0f) as usize) * 4;
+ if ihl < 20 || ip.len() < ihl {
+ return None;
+ }
+ let proto_n = ip[9];
+ let src = format!("{}.{}.{}.{}", ip[12], ip[13], ip[14], ip[15]);
+ let dst = format!("{}.{}.{}.{}", ip[16], ip[17], ip[18], ip[19]);
+ let rest = &ip[ihl..];
+ let (proto, src_port, dst_port, payload, tcp_seq, tcp_ack, tcp_flags) = match proto_n {
+ 6 if rest.len() >= 20 => {
+ let sp = u16::from_be_bytes([rest[0], rest[1]]);
+ let dp = u16::from_be_bytes([rest[2], rest[3]]);
+ let seq = u32::from_be_bytes([rest[4], rest[5], rest[6], rest[7]]);
+ let ack = u32::from_be_bytes([rest[8], rest[9], rest[10], rest[11]]);
+ let doff = ((rest[12] >> 4) as usize) * 4;
+ let flags = rest[13];
+ let pay = if rest.len() > doff {
+ rest[doff..].to_vec()
+ } else {
+ Vec::new()
+ };
+ ("tcp", sp, dp, pay, Some(seq), Some(ack), Some(flags))
+ }
+ 17 if rest.len() >= 8 => {
+ let sp = u16::from_be_bytes([rest[0], rest[1]]);
+ let dp = u16::from_be_bytes([rest[2], rest[3]]);
+ ("udp", sp, dp, rest[8..].to_vec(), None, None, None)
+ }
+ 1 => ("icmp", 0, 0, rest.to_vec(), None, None, None),
+ other => {
+ let name = match other {
+ 47 => "gre",
+ 50 => "esp",
+ _ => "ip",
+ };
+ (name, 0, 0, rest.to_vec(), None, None, None)
+ }
+ };
+ Some(Frame {
+ ts_ms,
+ proto: proto.into(),
+ src,
+ dst,
+ src_port,
+ dst_port,
+ payload,
+ tcp_seq,
+ tcp_ack,
+ tcp_flags,
+ owner: None,
+ })
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn decodes_raw_ipv4_tcp() {
+ // IPv4 20 + TCP 20 + "HI"
+ let mut pkt = vec![0u8; 42];
+ pkt[0] = 0x45;
+ pkt[9] = 6;
+ pkt[12..16].copy_from_slice(&[10, 0, 0, 1]);
+ pkt[16..20].copy_from_slice(&[10, 0, 0, 2]);
+ pkt[20] = 0x04;
+ pkt[21] = 0xd2; // 1234
+ pkt[22] = 0x00;
+ pkt[23] = 80;
+ pkt[32] = 0x50; // data offset 5
+ pkt[40] = b'H';
+ pkt[41] = b'I';
+ let f = decode_packet(1, &pkt).expect("frame");
+ assert_eq!(f.proto, "tcp");
+ assert_eq!(f.src, "10.0.0.1");
+ assert_eq!(f.dst, "10.0.0.2");
+ assert_eq!(f.src_port, 1234);
+ assert_eq!(f.dst_port, 80);
+ assert_eq!(f.payload, b"HI");
+ }
+
+ #[test]
+ fn decodes_ethernet_ipv4() {
+ let mut pkt = vec![0u8; 14 + 28];
+ pkt[12] = 0x08;
+ pkt[13] = 0x00;
+ pkt[14] = 0x45;
+ pkt[14 + 9] = 17; // UDP
+ pkt[14 + 12..14 + 16].copy_from_slice(&[1, 2, 3, 4]);
+ pkt[14 + 16..14 + 20].copy_from_slice(&[5, 6, 7, 8]);
+ pkt[14 + 20] = 0x00;
+ pkt[14 + 21] = 53;
+ pkt[14 + 22] = 0x00;
+ pkt[14 + 23] = 53;
+ let f = decode_packet(2, &pkt).expect("eth");
+ assert_eq!(f.proto, "udp");
+ assert_eq!(f.src_port, 53);
+ assert_eq!(f.dst_port, 53);
+ }
+}
diff --git a/crates/ghidrust-net-capture/src/lib.rs b/crates/ghidrust-net-capture/src/lib.rs
index a74504a..af14d0b 100644
--- a/crates/ghidrust-net-capture/src/lib.rs
+++ b/crates/ghidrust-net-capture/src/lib.rs
@@ -4,7 +4,10 @@ pub mod acq;
pub mod file;
pub mod session;
-pub use acq::{CaptureBackend, CaptureError, ReplayBackend};
+pub use acq::{
+ enumerate_os_ifaces, open_live, CaptureBackend, CaptureError, GhidnetcapBackend, IfaceInfo,
+ ReplayBackend,
+};
pub use file::{read_frames, write_frames, CAPTURE_MAGIC};
pub use session::{
capture_start, capture_status, capture_stop, flows, list_interfaces, CaptureStartRequest,
diff --git a/crates/ghidrust-net-capture/src/session.rs b/crates/ghidrust-net-capture/src/session.rs
index e84dac8..9382bf6 100644
--- a/crates/ghidrust-net-capture/src/session.rs
+++ b/crates/ghidrust-net-capture/src/session.rs
@@ -1,6 +1,8 @@
//! Capture session lifecycle.
-use crate::acq::{CaptureBackend, CaptureError, LiveStubBackend, ReplayBackend};
+use crate::acq::{
+ enumerate_os_ifaces, open_live, CaptureBackend, CaptureError, LiveStubBackend, ReplayBackend,
+};
use crate::file::{write_frames, CAPTURE_MAGIC};
use ghidrust_net_flow::{CaptureFilter, FlowTable, Frame};
use ghidrust_net_schema::{AttributedFlow, CaptureSessionInfo, Owner};
@@ -51,7 +53,7 @@ struct Session {
#[allow(dead_code)]
backend_name: String,
replay: Option<ReplayBackend>,
- live: Option<LiveStubBackend>,
+ live: Option<Box<dyn CaptureBackend + Send>>,
finished: bool,
}
@@ -66,10 +68,24 @@ fn with_sessions<R>(f: impl FnOnce(&mut HashMap<String, Session>) -> R) -> R {
}
pub fn list_interfaces() -> Vec<serde_json::Value> {
- vec![
- serde_json::json!({"name": "loopback", "description": "Local loopback"}),
- serde_json::json!({"name": "replay", "description": "Fixture replay backend"}),
- ]
+ let mut v: Vec<serde_json::Value> = enumerate_os_ifaces()
+ .into_iter()
+ .map(|i| {
+ serde_json::json!({
+ "name": i.name,
+ "description": i.description,
+ "ipv4": i.ipv4,
+ "guid": i.guid,
+ "kind": "os",
+ })
+ })
+ .collect();
+ v.push(serde_json::json!({
+ "name": "replay",
+ "description": "Fixture replay backend",
+ "kind": "replay",
+ }));
+ v
}
pub fn capture_start(req: CaptureStartRequest) -> Result<CaptureStartResult, CaptureError> {
@@ -111,8 +127,20 @@ pub fn capture_start(req: CaptureStartRequest) -> Result<CaptureStartResult, Cap
return Err(CaptureError::unavailable(
"replay iface requires replay_path",
));
+ } else if std::env::var("GHIDRUST_NET_LIVE_STUB").ok().as_deref() == Some("1") {
+ (
+ None,
+ Some(Box::new(LiveStubBackend::new()) as Box<dyn CaptureBackend + Send>),
+ "live_stub".to_string(),
+ )
} else {
- (None, Some(LiveStubBackend::new()), "live_stub".to_string())
+ let backend = open_live(req.iface.as_deref())?;
+ let name = backend.name().to_string();
+ (
+ None,
+ Some(Box::new(backend) as Box<dyn CaptureBackend + Send>),
+ name,
+ )
};
let info = CaptureSessionInfo {
@@ -164,11 +192,7 @@ fn capture_pump(session_id: &str) -> Result<(), CaptureError> {
let frames = if let Some(b) = s.replay.as_mut() {
b.poll_frames()?
} else if let Some(b) = s.live.as_mut() {
- match b.poll_frames() {
- Ok(f) => f,
- Err(e) if e.code == "backend_unavailable" => Vec::new(),
- Err(e) => return Err(e),
- }
+ b.poll_frames()?
} else {
Vec::new()
};
@@ -305,6 +329,46 @@ mod tests {
let _ = std::fs::remove_dir_all(&dir);
}
+ #[test]
+ fn list_interfaces_is_not_only_hardcoded_placeholders() {
+ let v = list_interfaces();
+ assert!(!v.is_empty());
+ let names: Vec<String> = v
+ .iter()
+ .filter_map(|x| x.get("name").and_then(|n| n.as_str()).map(|s| s.to_string()))
+ .collect();
+ assert!(names.iter().any(|n| n == "replay"), "replay stays listed");
+ let os_only: Vec<_> = names.iter().filter(|n| *n != "replay").cloned().collect();
+ if !os_only.is_empty() {
+ assert!(
+ os_only.iter().any(|n| n != "loopback"),
+ "OS enum must surface real adapters, got {os_only:?}"
+ );
+ } else {
+ // Enum failed — live open must be fail-closed, not live_stub success.
+ let err = crate::open_live(None).unwrap_err();
+ assert!(err.code == "privilege_required" || err.code == "backend_unavailable");
+ }
+ }
+
+ #[test]
+ fn live_capture_start_is_not_silent_stub() {
+ if std::env::var("GHIDRUST_NET_LIVE_STUB").ok().as_deref() == Some("1") {
+ return;
+ }
+ match capture_start(CaptureStartRequest {
+ iface: Some("no-such-ghidnetcap-iface-xyz".into()),
+ ..Default::default()
+ }) {
+ Ok(s) => panic!("unknown iface must not start a session (backend={})", s.backend),
+ Err(e) => assert!(
+ e.code == "backend_unavailable" || e.code == "privilege_required",
+ "{}",
+ e.code
+ ),
+ }
+ }
+
#[test]
fn limit_reached() {
let dir = std::env::temp_dir().join("grn-limit");
diff --git a/crates/ghidrust-net-correlate/src/lib.rs b/crates/ghidrust-net-correlate/src/lib.rs
index 8bbe744..29f4fc9 100644
--- a/crates/ghidrust-net-correlate/src/lib.rs
+++ b/crates/ghidrust-net-correlate/src/lib.rs
@@ -326,6 +326,7 @@ mod tests {
sid: 1000001,
msg: "test".into(),
severity: 2,
+ action: "alert".into(),
flow_key: None,
owner: None,
timestamp_ms: 0,
diff --git a/crates/ghidrust-net-detect/Cargo.toml b/crates/ghidrust-net-detect/Cargo.toml
index c26849d..0c2e7c5 100644
--- a/crates/ghidrust-net-detect/Cargo.toml
+++ b/crates/ghidrust-net-detect/Cargo.toml
@@ -9,5 +9,7 @@ description = "Native multi-pattern match pipeline and alert generation"
ghidrust-net-schema = { workspace = true }
ghidrust-net-flow = { workspace = true }
ghidrust-net-rules = { workspace = true }
+ghidrust-net-parse = { workspace = true }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
+regex = "1"
diff --git a/crates/ghidrust-net-detect/src/lib.rs b/crates/ghidrust-net-detect/src/lib.rs
index db347a9..d1cc66e 100644
--- a/crates/ghidrust-net-detect/src/lib.rs
+++ b/crates/ghidrust-net-detect/src/lib.rs
@@ -6,7 +6,11 @@ mod mpm;
pub use inline_gate::inline_block_allowed;
use ghidrust_net_flow::{FlowTable, Frame};
-use ghidrust_net_rules::{compile_rules, load_rule_pack, CompiledRuleset, ContentOpt, Rule};
+use ghidrust_net_parse::{parse_client_hello, parse_qnames, parse_request_ex};
+use ghidrust_net_rules::{
+ addr_matches, compile_rules, load_rule_pack, port_matches, CompiledRuleset, ContentOpt,
+ DetectOp, Rule,
+};
use ghidrust_net_schema::{Alert, FlowKey, Owner};
use std::path::Path;
use std::sync::Mutex;
@@ -75,76 +79,58 @@ pub fn detect_frames(
for f in frames {
table.ingest(f);
}
- let mut alerts = Vec::new();
- let flows = table.attributed();
+ let mut alerts: Vec<Alert> = Vec::new();
+ let _flows = table.attributed();
for rule in &rules.rules {
- if !rule.action.eq_ignore_ascii_case("alert") {
+ let action = rule.action.to_ascii_lowercase();
+ if action != "alert" && action != "pass" && action != "drop" {
continue;
}
- for flow in &flows {
- if !proto_matches(rule, &flow.key) {
- continue;
- }
- let payload = table.payload_combined(&flow.key);
- if rule.contents.is_empty() {
+ for f in frames {
+ if !rule_matches_frame(rule, f) {
continue;
}
- if contents_match(&rule.contents, &payload) {
- let owner = attr_owners
+ let key = FlowKey {
+ proto: f.proto.clone(),
+ src: f.src.clone(),
+ dst: f.dst.clone(),
+ src_port: f.src_port,
+ dst_port: f.dst_port,
+ };
+ let owner = f.owner.clone().or_else(|| {
+ attr_owners
.iter()
- .find(|(k, _)| k == &flow.key)
+ .find(|(k, _)| k.src == key.src || k.dst == key.dst)
.map(|(_, o)| o.clone())
- .or_else(|| flow.owner.clone());
- let id = format!("alert-{}-{}", rule.sid, alerts.len());
- alerts.push(Alert {
- id,
- sid: rule.sid,
- msg: rule.msg.clone(),
- severity: rule.severity,
- flow_key: Some(flow.key.clone()),
- owner,
- timestamp_ms: now_ms(),
- host: None,
- ioc: Some(String::from_utf8_lossy(&rule.contents[0].pattern).to_string()),
- });
- }
- }
- // Also scan raw frame payloads (for single-packet rules).
- for f in frames {
- if !rule.proto.eq_ignore_ascii_case("any") && !rule.proto.eq_ignore_ascii_case(&f.proto)
+ });
+ if alerts
+ .iter()
+ .any(|a| a.sid == rule.sid && a.flow_key.as_ref() == Some(&key))
{
continue;
}
- if contents_match(&rule.contents, &f.payload) {
- let key = FlowKey {
- proto: f.proto.clone(),
- src: f.src.clone(),
- dst: f.dst.clone(),
- src_port: f.src_port,
- dst_port: f.dst_port,
- };
- let owner = f.owner.clone().or_else(|| {
- attr_owners
- .iter()
- .find(|(k, _)| k.src == key.src || k.dst == key.dst)
- .map(|(_, o)| o.clone())
+ let ioc = rule
+ .ops
+ .iter()
+ .find(|o| !o.pattern.is_empty())
+ .map(|o| String::from_utf8_lossy(&o.pattern).into_owned())
+ .or_else(|| {
+ rule.contents
+ .first()
+ .map(|c| String::from_utf8_lossy(&c.pattern).into_owned())
});
- let id = format!("alert-{}-{}", rule.sid, alerts.len());
- if alerts.iter().any(|a| a.sid == rule.sid && a.flow_key.as_ref() == Some(&key)) {
- continue;
- }
- alerts.push(Alert {
- id,
- sid: rule.sid,
- msg: rule.msg.clone(),
- severity: rule.severity,
- flow_key: Some(key),
- owner,
- timestamp_ms: f.ts_ms,
- host: None,
- ioc: Some(String::from_utf8_lossy(&rule.contents[0].pattern).to_string()),
- });
- }
+ alerts.push(Alert {
+ id: format!("alert-{}-{}", rule.sid, alerts.len()),
+ sid: rule.sid,
+ msg: rule.msg.clone(),
+ severity: rule.severity,
+ action: rule.action.clone(),
+ flow_key: Some(key),
+ owner,
+ timestamp_ms: f.ts_ms,
+ host: None,
+ ioc,
+ });
}
}
let buf = LAST_ALERTS.get();
@@ -156,15 +142,216 @@ pub fn detect_frames(
}
pub fn compile_rules_text(text: &str) -> Result<CompiledRuleset, String> {
- compile_rules(text).map_err(|e| e.to_string())
+ Ok(compile_rules(text))
}
pub fn load_rules(path: &Path) -> Result<CompiledRuleset, String> {
load_rule_pack(path).map_err(|e| e.to_string())
}
-fn proto_matches(rule: &Rule, key: &FlowKey) -> bool {
- rule.proto.eq_ignore_ascii_case("any") || rule.proto.eq_ignore_ascii_case(&key.proto)
+fn proto_matches(rule: &Rule, proto: &str) -> bool {
+ if rule.proto.eq_ignore_ascii_case("any") || rule.proto.eq_ignore_ascii_case(proto) {
+ return true;
+ }
+ // alert http/tls stored as tcp; dns as udp
+ match rule.l7.as_deref() {
+ Some("http") | Some("tls") => proto.eq_ignore_ascii_case("tcp") || proto.eq_ignore_ascii_case("http"),
+ Some("dns") => proto.eq_ignore_ascii_case("udp") || proto.eq_ignore_ascii_case("dns"),
+ _ => false,
+ }
+}
+
+fn header_matches_frame(rule: &Rule, f: &Frame) -> bool {
+ if !proto_matches(rule, &f.proto) {
+ return false;
+ }
+ let fwd = addr_matches(&rule.src, &f.src)
+ && port_matches(&rule.src_port, f.src_port)
+ && addr_matches(&rule.dst, &f.dst)
+ && port_matches(&rule.dst_port, f.dst_port);
+ let rev = rule.direction.contains("<>")
+ && addr_matches(&rule.src, &f.dst)
+ && port_matches(&rule.src_port, f.dst_port)
+ && addr_matches(&rule.dst, &f.src)
+ && port_matches(&rule.dst_port, f.src_port);
+ fwd || rev
+}
+
+fn service_port(p: u16) -> bool {
+ matches!(p, 21 | 22 | 25 | 53 | 80 | 110 | 143 | 443 | 465 | 587 | 993 | 995 | 8000 | 8008 | 8080 | 8443 | 8888)
+}
+
+fn flow_ok(rule: &Rule, f: &Frame) -> bool {
+ if rule.flow_established {
+ if let Some(flags) = f.tcp_flags {
+ if flags & 0x10 == 0 {
+ return false;
+ }
+ }
+ }
+ if rule.flow_to_server && !rule.flow_to_client {
+ if !service_port(f.dst_port) && service_port(f.src_port) {
+ return false;
+ }
+ }
+ if rule.flow_to_client && !rule.flow_to_server {
+ if !service_port(f.src_port) && service_port(f.dst_port) {
+ return false;
+ }
+ }
+ true
+}
+
+fn l7_ok(rule: &Rule, f: &Frame) -> bool {
+ match rule.l7.as_deref() {
+ Some("http") => parse_request_ex(&f.payload).is_some(),
+ Some("tls") => parse_client_hello(&f.payload).is_some(),
+ Some("dns") => parse_qnames(&f.payload).is_some(),
+ _ => true,
+ }
+}
+
+fn buffer_bytes(op: &DetectOp, f: &Frame) -> Vec<u8> {
+ match op.buffer.as_str() {
+ "http.uri" => parse_request_ex(&f.payload)
+ .map(|r| r.uri.into_bytes())
+ .unwrap_or_default(),
+ "http.host" => parse_request_ex(&f.payload)
+ .and_then(|r| r.host)
+ .map(|h| h.into_bytes())
+ .unwrap_or_default(),
+ "http.method" => parse_request_ex(&f.payload)
+ .map(|r| r.method.into_bytes())
+ .unwrap_or_default(),
+ "http.header" => parse_request_ex(&f.payload)
+ .map(|r| r.headers.into_bytes())
+ .unwrap_or_default(),
+ "dns.query" => parse_qnames(&f.payload)
+ .map(|q| q.join("\n").into_bytes())
+ .unwrap_or_default(),
+ "tls.sni" => parse_client_hello(&f.payload)
+ .and_then(|(sni, _)| sni)
+ .map(|s| s.into_bytes())
+ .unwrap_or_default(),
+ _ => f.payload.clone(),
+ }
+}
+
+fn rule_matches_frame(rule: &Rule, f: &Frame) -> bool {
+ if !header_matches_frame(rule, f) || !flow_ok(rule, f) || !l7_ok(rule, f) {
+ return false;
+ }
+ let ops: Vec<&DetectOp> = if rule.ops.is_empty() {
+ return if rule.contents.is_empty() {
+ true
+ } else {
+ contents_match(&rule.contents, &f.payload)
+ };
+ } else {
+ rule.ops.iter().collect()
+ };
+ let mut last_end: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
+ for op in ops {
+ let buf = buffer_bytes(op, f);
+ if buf.is_empty() && op.kind != "pcre" && !op.pattern.is_empty() {
+ return false;
+ }
+ let start = op.offset.unwrap_or(0);
+ let from = if let Some(d) = op.distance {
+ let base = last_end.get(&op.buffer).copied().unwrap_or(0) as i64;
+ (base + d as i64).max(0) as usize
+ } else {
+ start
+ };
+ if from > buf.len() {
+ return false;
+ }
+ let end = if let Some(w) = op.within {
+ (from + w).min(buf.len())
+ } else if let Some(d) = op.depth {
+ (start + d).min(buf.len())
+ } else {
+ buf.len()
+ };
+ if end < from {
+ return false;
+ }
+ let window = &buf[from..end];
+ let hit_end = if op.kind == "pcre" {
+ let Some(re) = op.pcre.as_deref() else {
+ return false;
+ };
+ let Ok(rx) = regex::bytes::Regex::new(re) else {
+ return false;
+ };
+ rx.find(window).map(|m| from + m.end())
+ } else {
+ find_pattern(window, &op.pattern, op.nocase).map(|rel_end| from + rel_end)
+ };
+ let Some(abs_end) = hit_end else {
+ return false;
+ };
+ last_end.insert(op.buffer.clone(), abs_end.min(buf.len()));
+ }
+ true
+}
+
+/// Emit a Suricata-shaped rule that compiles and matches the given frames/alerts.
+/// Deterministic (no API key). Content is taken from the first non-empty payload.
+pub fn synthesize_rule_from_frames(frames: &[Frame]) -> Result<String, String> {
+ let f = frames
+ .iter()
+ .find(|f| !f.payload.is_empty())
+ .ok_or_else(|| "no payload to synthesize from".to_string())?;
+ synthesize_rule_for_frame(f)
+}
+
+pub fn synthesize_rule_for_frame(f: &Frame) -> Result<String, String> {
+ let snippet = content_snippet(&f.payload);
+ if snippet.is_empty() {
+ return Err("empty content snippet".into());
+ }
+ let sid = 2_000_000u32
+ + (f.payload.iter().fold(0u32, |a, b| a.wrapping_mul(16777619) ^ (*b as u32)) % 900_000);
+ let proto = if f.proto.is_empty() { "any" } else { f.proto.as_str() };
+ let src = if f.src.is_empty() { "any".into() } else { f.src.clone() };
+ let dst = if f.dst.is_empty() { "any".into() } else { f.dst.clone() };
+ let sport = if f.src_port == 0 {
+ "any".into()
+ } else {
+ f.src_port.to_string()
+ };
+ let dport = if f.dst_port == 0 {
+ "any".into()
+ } else {
+ f.dst_port.to_string()
+ };
+ Ok(format!(
+ r#"alert {proto} {src} {sport} -> {dst} {dport} (msg:"ghidnet synth"; content:"{snippet}"; sid:{sid};)"#
+ ))
+}
+
+fn content_snippet(payload: &[u8]) -> String {
+ // Contiguous payload bytes as Suricata `|HH HH|` so the compiler's
+ // `opts.split(';')` never sees a raw ';' / quote, and the pattern is a
+ // real substring (no concatenating across CRLF/NUL).
+ let n = payload.len().min(12);
+ if n == 0 {
+ return String::new();
+ }
+ hex_content(&payload[..n])
+}
+
+fn hex_content(bytes: &[u8]) -> String {
+ let mut hex = String::from("|");
+ for (i, b) in bytes.iter().enumerate() {
+ if i > 0 {
+ hex.push(' ');
+ }
+ hex.push_str(&format!("{b:02X}"));
+ }
+ hex.push('|');
+ hex
}
fn contents_match(opts: &[ContentOpt], hay: &[u8]) -> bool {
@@ -180,23 +367,30 @@ fn content_hit(c: &ContentOpt, hay: &[u8]) -> bool {
Some(d) => (start + d).min(hay.len()),
None => hay.len(),
};
- let window = &hay[start..end];
- if c.nocase {
- let pat: Vec<u8> = c.pattern.iter().map(|b| b.to_ascii_lowercase()).collect();
- let hay_l: Vec<u8> = window.iter().map(|b| b.to_ascii_lowercase()).collect();
- find_subslice(&hay_l, &pat)
- } else {
- find_subslice(window, &c.pattern)
- }
+ find_pattern(&hay[start..end], &c.pattern, c.nocase).is_some()
}
-fn find_subslice(hay: &[u8], needle: &[u8]) -> bool {
+/// End offset of the first match inside `hay` (relative to `hay`).
+fn find_pattern(hay: &[u8], needle: &[u8], nocase: bool) -> Option<usize> {
if needle.is_empty() {
- return true;
+ return Some(0);
+ }
+ if nocase {
+ let pat: Vec<u8> = needle.iter().map(|b| b.to_ascii_lowercase()).collect();
+ hay.windows(pat.len()).position(|w| {
+ w.iter()
+ .zip(pat.iter())
+ .all(|(a, b)| a.to_ascii_lowercase() == *b)
+ })
+ .map(|i| i + pat.len())
+ } else {
+ hay.windows(needle.len())
+ .position(|w| w == needle)
+ .map(|i| i + needle.len())
}
- hay.windows(needle.len()).any(|w| w == needle)
}
+#[allow(dead_code)]
fn now_ms() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
@@ -319,6 +513,277 @@ mod tests {
assert_eq!(hits.len(), 2);
}
+ #[test]
+ fn pass_and_drop_are_recorded_not_silent() {
+ let rules = compile_rules_text(
+ r#"pass tcp any any -> any any (msg:"allow"; content:"OKOK"; sid:2001;)
+drop tcp any any -> any any (msg:"block"; content:"BADBAD"; sid:2002;)"#,
+ )
+ .unwrap();
+ let frames = vec![
+ Frame {
+ ts_ms: 1,
+ proto: "tcp".into(),
+ src: "1.1.1.1".into(),
+ dst: "2.2.2.2".into(),
+ src_port: 9,
+ dst_port: 80,
+ payload: b"OKOK".to_vec(),
+ tcp_seq: None,
+ tcp_ack: None,
+ tcp_flags: None,
+ owner: None,
+ },
+ Frame {
+ ts_ms: 2,
+ proto: "tcp".into(),
+ src: "1.1.1.1".into(),
+ dst: "2.2.2.2".into(),
+ src_port: 9,
+ dst_port: 80,
+ payload: b"BADBAD".to_vec(),
+ tcp_seq: None,
+ tcp_ack: None,
+ tcp_flags: None,
+ owner: None,
+ },
+ ];
+ let alerts = detect_frames(&frames, &rules, &[]).unwrap();
+ assert!(alerts.iter().any(|a| a.sid == 2001 && a.action == "pass"));
+ assert!(alerts.iter().any(|a| a.sid == 2002 && a.action == "drop"));
+ }
+
+ #[test]
+ fn synthesize_then_compile_then_detect() {
+ let frames = vec![Frame {
+ ts_ms: 1,
+ proto: "tcp".into(),
+ src: "10.0.0.1".into(),
+ dst: "10.0.0.2".into(),
+ src_port: 4444,
+ dst_port: 80,
+ payload: b"header EVILCMD trailer".to_vec(),
+ tcp_seq: None,
+ tcp_ack: None,
+ tcp_flags: None,
+ owner: None,
+ }];
+ let text = synthesize_rule_from_frames(&frames).unwrap();
+ assert!(text.contains("content:"));
+ assert!(text.contains("sid:"));
+ let rules = compile_rules_text(&text).unwrap();
+ let alerts = detect_frames(&frames, &rules, &[]).unwrap();
+ assert!(!alerts.is_empty());
+ assert_eq!(alerts[0].sid, rules.rules[0].sid);
+ assert!(alerts[0].msg.contains("ghidnet"));
+ assert!(
+ frames[0]
+ .payload
+ .windows(rules.rules[0].contents[0].pattern.len())
+ .any(|w| w == rules.rules[0].contents[0].pattern.as_slice()),
+ "synth content must be a contiguous payload substring"
+ );
+ }
+
+ #[test]
+ fn synthesize_http_payload_with_semicolon_and_crlf() {
+ let frames = vec![Frame {
+ ts_ms: 1,
+ proto: "tcp".into(),
+ src: "10.0.0.8".into(),
+ dst: "10.0.0.9".into(),
+ src_port: 49152,
+ dst_port: 80,
+ payload: b"GET /x HTTP/1.1\r\nHost: a.example;evil\r\n\r\n".to_vec(),
+ tcp_seq: None,
+ tcp_ack: None,
+ tcp_flags: None,
+ owner: None,
+ }];
+ let text = synthesize_rule_from_frames(&frames).expect("synth");
+ assert!(
+ !text.contains("content:\"GET") && !text.contains("content:\"Host"),
+ "must not put raw HTTP (CRLF/';') inside content:\"...\": {text}"
+ );
+ assert!(text.contains("content:\"|"), "prefer |HH HH| hex: {text}");
+ let rules = compile_rules_text(&text).expect(&format!("compile {text}"));
+ assert!(!rules.rules.is_empty());
+ let pat = &rules.rules[0].contents[0].pattern;
+ assert!(
+ frames[0].payload.windows(pat.len()).any(|w| w == pat.as_slice()),
+ "pattern {pat:?} not a substring of payload"
+ );
+ let alerts = detect_frames(&frames, &rules, &[]).expect("detect");
+ assert!(!alerts.is_empty(), "synthesized rule must match originating HTTP frame");
+ assert_eq!(alerts[0].sid, rules.rules[0].sid);
+ }
+
+ #[test]
+ fn http_uri_matches_uri_not_body() {
+ let rules = compile_rules_text(
+ r#"
+alert http any any -> any any (msg:"uri"; http.uri; content:"/gate.php"; sid:2000002;)
+alert tls any any -> any any (msg:"sni"; tls.sni; content:"evil.example"; sid:2000003;)
+"#,
+ )
+ .unwrap();
+ assert_eq!(rules.rules.len(), 2, "{:?}", rules.skipped);
+ let http = Frame {
+ ts_ms: 1,
+ proto: "tcp".into(),
+ src: "10.0.0.2".into(),
+ dst: "10.0.0.1".into(),
+ src_port: 49152,
+ dst_port: 80,
+ payload: b"GET /gate.php HTTP/1.1\r\nHost: a.test\r\n\r\n".to_vec(),
+ tcp_seq: None,
+ tcp_ack: None,
+ tcp_flags: Some(0x18),
+ owner: None,
+ };
+ let body_only = Frame {
+ ts_ms: 2,
+ proto: "tcp".into(),
+ src: "10.0.0.2".into(),
+ dst: "10.0.0.1".into(),
+ src_port: 49152,
+ dst_port: 80,
+ payload: b"GET /index HTTP/1.1\r\nHost: a.test\r\n\r\n/gate.php".to_vec(),
+ tcp_seq: None,
+ tcp_ack: None,
+ tcp_flags: Some(0x18),
+ owner: None,
+ };
+ let hits = detect_frames(&[http], &rules, &[]).unwrap();
+ assert!(hits.iter().any(|a| a.sid == 2000002), "{hits:?}");
+ assert!(!hits.iter().any(|a| a.sid == 2000003));
+ let misses = detect_frames(&[body_only], &rules, &[]).unwrap();
+ assert!(
+ !misses.iter().any(|a| a.sid == 2000002),
+ "URI rule must not fire on body-only /gate.php: {misses:?}"
+ );
+ }
+
+ #[test]
+ fn dns_and_tls_sticky_buffers() {
+ let rules = compile_rules_text(
+ r#"
+alert dns any any -> any any (msg:"dns"; dns.query; content:"c2.example"; sid:2000004;)
+alert tls any any -> any any (msg:"sni"; tls.sni; content:"evil.example"; sid:2000003;)
+"#,
+ )
+ .unwrap();
+ let mut dns = vec![
+ 0x12, 0x34, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ ];
+ for label in [b"c2".as_slice(), b"example".as_slice()] {
+ dns.push(label.len() as u8);
+ dns.extend_from_slice(label);
+ }
+ dns.push(0);
+ dns.extend_from_slice(&[0x00, 0x01, 0x00, 0x01]);
+ let dns_f = Frame {
+ ts_ms: 1,
+ proto: "udp".into(),
+ src: "10.0.0.2".into(),
+ dst: "10.0.0.1".into(),
+ src_port: 5555,
+ dst_port: 53,
+ payload: dns,
+ tcp_seq: None,
+ tcp_ack: None,
+ tcp_flags: None,
+ owner: None,
+ };
+ let alerts = detect_frames(&[dns_f], &rules, &[]).unwrap();
+ assert!(alerts.iter().any(|a| a.sid == 2000004), "{alerts:?}");
+ assert!(!alerts.iter().any(|a| a.sid == 2000003));
+
+ let tls_payload = tls_client_hello_sni(b"evil.example");
+ let tls_f = Frame {
+ ts_ms: 2,
+ proto: "tcp".into(),
+ src: "10.0.0.2".into(),
+ dst: "10.0.0.1".into(),
+ src_port: 49152,
+ dst_port: 443,
+ payload: tls_payload,
+ tcp_seq: None,
+ tcp_ack: None,
+ tcp_flags: Some(0x18),
+ owner: None,
+ };
+ let tls_hits = detect_frames(&[tls_f], &rules, &[]).unwrap();
+ assert!(
+ tls_hits.iter().any(|a| a.sid == 2000003),
+ "tls.sni SID must fire on ClientHello: {tls_hits:?}"
+ );
+ assert!(!tls_hits.iter().any(|a| a.sid == 2000004));
+ }
+
+ fn tls_client_hello_sni(sni: &[u8]) -> Vec<u8> {
+ let mut ext_sni = Vec::new();
+ ext_sni.extend_from_slice(&0x0000u16.to_be_bytes());
+ let mut list = Vec::new();
+ list.push(0x00);
+ list.extend_from_slice(&(sni.len() as u16).to_be_bytes());
+ list.extend_from_slice(sni);
+ let mut list_wrap = Vec::new();
+ list_wrap.extend_from_slice(&(list.len() as u16).to_be_bytes());
+ list_wrap.extend_from_slice(&list);
+ ext_sni.extend_from_slice(&(list_wrap.len() as u16).to_be_bytes());
+ ext_sni.extend_from_slice(&list_wrap);
+ let mut body = Vec::new();
+ body.extend_from_slice(&[0x03, 0x03]);
+ body.extend_from_slice(&[0u8; 32]);
+ body.push(0);
+ body.extend_from_slice(&2u16.to_be_bytes());
+ body.extend_from_slice(&0x1301u16.to_be_bytes());
+ body.push(1);
+ body.push(0);
+ body.extend_from_slice(&(ext_sni.len() as u16).to_be_bytes());
+ body.extend_from_slice(&ext_sni);
+ let bl = body.len();
+ let mut hs = vec![
+ 0x01,
+ ((bl >> 16) & 0xff) as u8,
+ ((bl >> 8) & 0xff) as u8,
+ (bl & 0xff) as u8,
+ ];
+ hs.extend_from_slice(&body);
+ let mut rec = vec![0x16, 0x03, 0x01];
+ rec.extend_from_slice(&(hs.len() as u16).to_be_bytes());
+ rec.extend_from_slice(&hs);
+ rec
+ }
+
+ #[test]
+ fn distance_within_uses_previous_hit_end() {
+ let rules = compile_rules_text(
+ r#"alert tcp any any -> any any (msg:"d"; content:"ABC"; content:"D"; distance:0; within:1; sid:42;)"#,
+ )
+ .unwrap();
+ assert_eq!(rules.rules.len(), 1, "{:?}", rules.skipped);
+ let f = Frame {
+ ts_ms: 1,
+ proto: "tcp".into(),
+ src: "1.1.1.1".into(),
+ dst: "2.2.2.2".into(),
+ src_port: 9,
+ dst_port: 80,
+ payload: b"xABCD".to_vec(),
+ tcp_seq: None,
+ tcp_ack: None,
+ tcp_flags: None,
+ owner: None,
+ };
+ let alerts = detect_frames(&[f], &rules, &[]).unwrap();
+ assert!(
+ alerts.iter().any(|a| a.sid == 42),
+ "Suricata cursor after ABC is offset 4, D at distance:0 within:1 must match: {alerts:?}"
+ );
+ }
+
#[test]
fn detect_does_not_spawn_external_analyzer() {
// Structural guarantee: this crate has no Command::new for foreign analyzers.
diff --git a/crates/ghidrust-net-parse/src/http.rs b/crates/ghidrust-net-parse/src/http.rs
index eaa968f..8b4c7da 100644
--- a/crates/ghidrust-net-parse/src/http.rs
+++ b/crates/ghidrust-net-parse/src/http.rs
@@ -1,30 +1,59 @@
//! Minimal HTTP/1.x request pivot parse.
+#[derive(Debug, Clone, Default)]
+pub struct HttpRequest {
+ pub method: String,
+ pub uri: String,
+ pub host: Option<String>,
+ /// Raw header block (name: value lines, no request-line).
+ pub headers: String,
+}
+
pub fn parse_request(data: &[u8]) -> Option<(Option<String>, Option<String>)> {
+ let r = parse_request_ex(data)?;
+ let host = r.host.clone();
+ let uri = if r.uri.is_empty() {
+ None
+ } else {
+ Some(r.uri)
+ };
+ if host.is_none() && uri.is_none() {
+ None
+ } else {
+ Some((host, uri))
+ }
+}
+
+pub fn parse_request_ex(data: &[u8]) -> Option<HttpRequest> {
let s = std::str::from_utf8(data).ok()?;
- let mut uri = None;
- let mut host = None;
+ let mut out = HttpRequest::default();
+ let mut hdr = String::new();
for (idx, line) in s.split("\r\n").enumerate() {
if idx == 0 {
let mut parts = line.split_whitespace();
let method = parts.next()?;
- if matches!(
+ if !matches!(
method,
"GET" | "POST" | "PUT" | "HEAD" | "DELETE" | "OPTIONS" | "PATCH"
) {
- uri = parts.next().map(|u| u.to_string());
- } else {
return None;
}
- } else if let Some(rest) = line.strip_prefix("Host:") {
- host = Some(rest.trim().to_string());
+ out.method = method.to_string();
+ out.uri = parts.next().unwrap_or("").to_string();
} else if line.is_empty() {
break;
+ } else {
+ if let Some(rest) = line.strip_prefix("Host:") {
+ out.host = Some(rest.trim().to_string());
+ }
+ hdr.push_str(line);
+ hdr.push('\n');
}
}
- if host.is_none() && uri.is_none() {
+ out.headers = hdr;
+ if out.method.is_empty() {
None
} else {
- Some((host, uri))
+ Some(out)
}
}
diff --git a/crates/ghidrust-net-parse/src/lib.rs b/crates/ghidrust-net-parse/src/lib.rs
index ec59054..08f65b4 100644
--- a/crates/ghidrust-net-parse/src/lib.rs
+++ b/crates/ghidrust-net-parse/src/lib.rs
@@ -5,6 +5,10 @@ mod http;
mod smb;
mod tls;
+pub use dns::parse_qnames;
+pub use http::{parse_request, parse_request_ex, HttpRequest};
+pub use tls::parse_client_hello;
+
use ghidrust_net_schema::PivotFields;
/// Extract pivot fields from a payload or capture blob.
diff --git a/crates/ghidrust-net-rules/Cargo.toml b/crates/ghidrust-net-rules/Cargo.toml
index efeb2c4..b99a8cc 100644
--- a/crates/ghidrust-net-rules/Cargo.toml
+++ b/crates/ghidrust-net-rules/Cargo.toml
@@ -9,3 +9,8 @@ description = "Ghidrust Net Rule (GNR) dialect parse and compile"
ghidrust-net-schema = { workspace = true }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
+regex = "1"
+flate2 = "1"
+tar = "0.4"
+md-5 = "0.10"
+ureq = "2"
diff --git a/crates/ghidrust-net-rules/src/addr.rs b/crates/ghidrust-net-rules/src/addr.rs
new file mode 100644
index 0000000..c114d11
--- /dev/null
+++ b/crates/ghidrust-net-rules/src/addr.rs
@@ -0,0 +1,119 @@
+//! IPv4 / CIDR / list / port matching for Suricata-style headers.
+
+pub fn ipv4_u32(s: &str) -> Option<u32> {
+ let s = s.trim();
+ let mut o = [0u8; 4];
+ let parts: Vec<_> = s.split('.').collect();
+ if parts.len() != 4 {
+ return None;
+ }
+ for (i, p) in parts.iter().enumerate() {
+ o[i] = p.parse().ok()?;
+ }
+ Some(u32::from_be_bytes(o))
+}
+
+pub fn cidr_contains(cidr: &str, ip: &str) -> bool {
+ let (net, bits) = match cidr.split_once('/') {
+ Some((n, b)) => (n.trim(), b.trim().parse::<u32>().unwrap_or(32).min(32)),
+ None => return ipv4_u32(cidr) == ipv4_u32(ip),
+ };
+ let Some(n) = ipv4_u32(net) else {
+ return false;
+ };
+ let Some(a) = ipv4_u32(ip) else {
+ return false;
+ };
+ if bits == 0 {
+ return true;
+ }
+ let mask = !0u32 << (32 - bits);
+ (n & mask) == (a & mask)
+}
+
+/// Match a Suricata address atom: `any`, IP, CIDR, `!atom`, `[a,b,c]`.
+pub fn addr_matches(pat: &str, addr: &str) -> bool {
+ let pat = pat.trim();
+ if pat.is_empty() || pat.eq_ignore_ascii_case("any") {
+ return true;
+ }
+ if let Some(rest) = pat.strip_prefix('!') {
+ return !addr_matches(rest.trim(), addr);
+ }
+ if pat.starts_with('[') && pat.ends_with(']') {
+ let inner = &pat[1..pat.len() - 1];
+ return split_list(inner).iter().any(|t| addr_matches(t, addr));
+ }
+ if pat.contains('/') {
+ return cidr_contains(pat, addr);
+ }
+ pat == addr || ipv4_u32(pat) == ipv4_u32(addr)
+}
+
+pub fn port_matches(pat: &str, port: u16) -> bool {
+ let pat = pat.trim();
+ if pat.is_empty() || pat.eq_ignore_ascii_case("any") {
+ return true;
+ }
+ if let Some(rest) = pat.strip_prefix('!') {
+ return !port_matches(rest.trim(), port);
+ }
+ if pat.starts_with('[') && pat.ends_with(']') {
+ let inner = &pat[1..pat.len() - 1];
+ return split_list(inner).iter().any(|t| port_matches(t, port));
+ }
+ if let Some((a, b)) = pat.split_once(':') {
+ let lo: u16 = a.parse().unwrap_or(0);
+ let hi: u16 = b.parse().unwrap_or(65535);
+ return port >= lo && port <= hi;
+ }
+ pat.parse::<u16>().ok() == Some(port)
+}
+
+pub fn split_list(inner: &str) -> Vec<String> {
+ let mut out = Vec::new();
+ let mut depth = 0i32;
+ let mut cur = String::new();
+ for c in inner.chars() {
+ match c {
+ '[' => {
+ depth += 1;
+ cur.push(c);
+ }
+ ']' => {
+ depth -= 1;
+ cur.push(c);
+ }
+ ',' if depth == 0 => {
+ let t = cur.trim();
+ if !t.is_empty() {
+ out.push(t.to_string());
+ }
+ cur.clear();
+ }
+ _ => cur.push(c),
+ }
+ }
+ let t = cur.trim();
+ if !t.is_empty() {
+ out.push(t.to_string());
+ }
+ out
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn cidr_and_list_and_negation() {
+ assert!(addr_matches("10.0.0.0/8", "10.1.2.3"));
+ assert!(!addr_matches("10.0.0.0/8", "11.0.0.1"));
+ assert!(addr_matches("[1.2.3.4, 10.0.0.0/8]", "10.9.9.9"));
+ assert!(!addr_matches("!10.0.0.1", "10.0.0.1"));
+ assert!(addr_matches("any", "8.8.8.8"));
+ assert!(port_matches("[80,443]", 443));
+ assert!(port_matches("8000:8010", 8005));
+ assert!(!port_matches("80", 81));
+ }
+}
diff --git a/crates/ghidrust-net-rules/src/install.rs b/crates/ghidrust-net-rules/src/install.rs
new file mode 100644
index 0000000..78ac0eb
--- /dev/null
+++ b/crates/ghidrust-net-rules/src/install.rs
@@ -0,0 +1,493 @@
+//! ET Open pack fetch/extract/activate. I/O lives here; compile stays in parse.rs.
+
+use crate::parse::{compile_rules_with, CompiledRuleset};
+use serde::{Deserialize, Serialize};
+use std::fs;
+use std::io::{Read, Write};
+use std::path::{Path, PathBuf};
+
+pub const DEFAULT_ET_VERSION: &str = "7.0.3";
+pub const ET_OPEN_BASE: &str = "https://rules.emergingthreats.net/open";
+
+#[derive(Debug, Clone, Default, Serialize, Deserialize)]
+pub struct InstallOpts {
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub from_archive: Option<String>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub url: Option<String>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub et_version: Option<String>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub dest_root: Option<String>,
+ /// Optional ET Pro oink code — never logged.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub et_code: Option<String>,
+}
+
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct InstallReport {
+ pub ok: bool,
+ pub path: String,
+ pub version: String,
+ pub loaded: usize,
+ pub skipped: usize,
+ pub skip_reasons: Vec<String>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub error: Option<String>,
+ #[serde(default)]
+ pub active: bool,
+}
+
+#[derive(Debug, Clone, Serialize, Deserialize)]
+struct ActivePtr {
+ pack: String,
+ version: String,
+ path: String,
+}
+
+pub fn user_rules_root() -> PathBuf {
+ if let Ok(p) = std::env::var("GHIDRUST_RULES_DIR") {
+ return PathBuf::from(p);
+ }
+ if let Ok(app) = std::env::var("APPDATA") {
+ return PathBuf::from(app).join("ghidrust").join("rules");
+ }
+ if let Ok(xdg) = std::env::var("XDG_CONFIG_HOME") {
+ return PathBuf::from(xdg).join("ghidrust").join("rules");
+ }
+ if let Ok(home) = std::env::var("HOME").or_else(|_| std::env::var("USERPROFILE")) {
+ return PathBuf::from(home).join(".ghidrust").join("rules");
+ }
+ std::env::temp_dir().join("ghidrust-rules")
+}
+
+fn active_ptr_path(root: &Path) -> PathBuf {
+ root.join("active.json")
+}
+
+pub fn active_pack_path() -> Option<PathBuf> {
+ let root = user_rules_root();
+ let p = active_ptr_path(&root);
+ let text = fs::read_to_string(p).ok()?;
+ let ptr: ActivePtr = serde_json::from_str(&text).ok()?;
+ let path = PathBuf::from(ptr.path);
+ if path.is_dir() || path.is_file() {
+ Some(path)
+ } else {
+ None
+ }
+}
+
+pub fn set_active_pack(path: &Path, version: &str) -> Result<(), String> {
+ let root = user_rules_root();
+ fs::create_dir_all(&root).map_err(|e| e.to_string())?;
+ let ptr = ActivePtr {
+ pack: "et-open".into(),
+ version: version.into(),
+ path: path.display().to_string(),
+ };
+ fs::write(active_ptr_path(&root), serde_json::to_vec_pretty(&ptr).unwrap())
+ .map_err(|e| e.to_string())
+}
+
+pub fn bundled_minimal_rules() -> PathBuf {
+ let cands = [
+ PathBuf::from("rules/ghidrust-minimal.rules"),
+ PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../rules/ghidrust-minimal.rules"),
+ ];
+ for c in cands {
+ if c.is_file() {
+ return c;
+ }
+ }
+ PathBuf::from("rules/ghidrust-minimal.rules")
+}
+
+/// Compile active ET pack (all `*.rules` in the dir) plus bundled minimal rules.
+pub fn load_active_ruleset() -> Result<CompiledRuleset, String> {
+ let mut texts = Vec::new();
+ if let Some(p) = active_pack_path() {
+ if p.is_dir() {
+ collect_rules_dir(&p, &mut texts)?;
+ } else if p.is_file() {
+ texts.push(fs::read_to_string(&p).map_err(|e| e.to_string())?);
+ }
+ }
+ let mini = bundled_minimal_rules();
+ if mini.is_file() {
+ texts.push(fs::read_to_string(&mini).map_err(|e| e.to_string())?);
+ }
+ if texts.is_empty() {
+ return Err("no active rules pack".into());
+ }
+ let vars = {
+ let pack = active_pack_path();
+ let vp = pack.as_ref().map(|p| {
+ if p.is_dir() {
+ p.join("ghidrust.vars")
+ } else {
+ p.parent()
+ .unwrap_or(p)
+ .join("ghidrust.vars")
+ }
+ });
+ match vp {
+ Some(p) if p.is_file() => crate::parse::load_vars_file(&p),
+ _ => crate::parse::default_vars(),
+ }
+ };
+ Ok(compile_rules_with(&texts.join("\n"), &vars))
+}
+
+fn collect_rules_dir(dir: &Path, texts: &mut Vec<String>) -> Result<(), String> {
+ let mut files: Vec<_> = fs::read_dir(dir)
+ .map_err(|e| e.to_string())?
+ .filter_map(|e| e.ok())
+ .map(|e| e.path())
+ .filter(|p| {
+ p.extension().and_then(|x| x.to_str()) == Some("rules")
+ || p.file_name().and_then(|x| x.to_str()) == Some("emerging-all.rules")
+ })
+ .collect();
+ files.sort();
+ for p in files {
+ texts.push(fs::read_to_string(&p).map_err(|e| e.to_string())?);
+ }
+ // recurse one level (rules/ subdirectory in ET tarball)
+ if let Ok(rd) = fs::read_dir(dir) {
+ for e in rd.filter_map(|e| e.ok()) {
+ let p = e.path();
+ if p.is_dir() {
+ collect_rules_dir(&p, texts)?;
+ }
+ }
+ }
+ Ok(())
+}
+
+pub fn list_rules_json() -> serde_json::Value {
+ let active = active_pack_path();
+ let set = load_active_ruleset().ok();
+ serde_json::json!({
+ "packs": {
+ "minimal": bundled_minimal_rules().display().to_string(),
+ "active": active.as_ref().map(|p| p.display().to_string()),
+ },
+ "path": active.as_ref().map(|p| p.display().to_string()),
+ "version": read_active_version(),
+ "loaded": set.as_ref().map(|s| s.rules.len()).unwrap_or(0),
+ "skipped": set.as_ref().map(|s| s.skipped.len()).unwrap_or(0),
+ "skip_reasons": set.as_ref().map(|s| {
+ s.skipped.iter().take(64).map(|x| format!("sid {}: {}", x.sid, x.reason)).collect::<Vec<_>>()
+ }).unwrap_or_default(),
+ "native": true,
+ })
+}
+
+fn read_active_version() -> Option<String> {
+ let root = user_rules_root();
+ let text = fs::read_to_string(active_ptr_path(&root)).ok()?;
+ let ptr: ActivePtr = serde_json::from_str(&text).ok()?;
+ Some(ptr.version)
+}
+
+/// Install ET Open from a local archive (CI) or URL.
+pub fn install_et_open(opts: InstallOpts) -> Result<InstallReport, String> {
+ let version = opts
+ .et_version
+ .clone()
+ .unwrap_or_else(|| DEFAULT_ET_VERSION.into());
+ let root = opts
+ .dest_root
+ .as_deref()
+ .map(PathBuf::from)
+ .unwrap_or_else(user_rules_root);
+ fs::create_dir_all(&root).map_err(|e| e.to_string())?;
+ let dest = root.join("et-open").join(&version);
+
+ let archive_bytes = if let Some(p) = &opts.from_archive {
+ fs::read(p).map_err(|e| format!("archive: {e}"))?
+ } else {
+ let url = opts.url.clone().unwrap_or_else(|| {
+ if let Some(code) = &opts.et_code {
+ format!(
+ "https://rules.emergingthreatspro.com/{code}/suricata-{version}/etpro.rules.tar.gz"
+ )
+ } else {
+ format!("{ET_OPEN_BASE}/suricata-{version}/emerging.rules.tar.gz")
+ }
+ });
+ http_get(&url)?
+ };
+
+ if let Some(p) = &opts.from_archive {
+ let md5_path = PathBuf::from(format!("{p}.md5"));
+ if md5_path.is_file() {
+ verify_md5(&archive_bytes, &md5_path)?;
+ }
+ } else if opts.from_archive.is_none() {
+ let url = opts.url.clone().unwrap_or_else(|| {
+ format!("{ET_OPEN_BASE}/suricata-{version}/emerging.rules.tar.gz")
+ });
+ let md5_url = format!("{url}.md5");
+ match http_get(&md5_url) {
+ Ok(sum) => verify_md5_bytes(&archive_bytes, &sum)?,
+ Err(e) => {
+ return Err(format!("md5 download failed (pack not activated): {e}"));
+ }
+ }
+ }
+
+ if dest.exists() {
+ let _ = fs::remove_dir_all(&dest);
+ }
+ fs::create_dir_all(&dest).map_err(|e| e.to_string())?;
+ extract_tar_gz(&archive_bytes, &dest)?;
+
+ let mut texts = Vec::new();
+ collect_rules_dir(&dest, &mut texts)?;
+ if texts.is_empty() {
+ let _ = fs::remove_dir_all(&dest);
+ return Err("archive contained no .rules files".into());
+ }
+ let vp = dest.join("ghidrust.vars");
+ if !vp.is_file() {
+ let _ = fs::write(
+ &vp,
+ "HOME_NET: [10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,127.0.0.0/8]\nEXTERNAL_NET: any\nHTTP_SERVERS: $HOME_NET\nSQL_SERVERS: $HOME_NET\nSMTP_SERVERS: $HOME_NET\n",
+ );
+ }
+ let vars = crate::parse::load_vars_file(&vp);
+ let set = compile_rules_with(&texts.join("\n"), &vars);
+ let skip_reasons: Vec<String> = set
+ .skipped
+ .iter()
+ .take(200)
+ .map(|s| format!("sid {}: {}", s.sid, s.reason))
+ .collect();
+ let report = InstallReport {
+ ok: true,
+ path: dest.display().to_string(),
+ version: version.clone(),
+ loaded: set.rules.len(),
+ skipped: set.skipped.len(),
+ skip_reasons,
+ error: None,
+ active: true,
+ };
+ fs::write(
+ dest.join("import-report.json"),
+ serde_json::to_vec_pretty(&report).unwrap(),
+ )
+ .ok();
+ fs::create_dir_all(&root).map_err(|e| e.to_string())?;
+ let ptr = ActivePtr {
+ pack: "et-open".into(),
+ version: version.clone(),
+ path: dest.display().to_string(),
+ };
+ fs::write(active_ptr_path(&root), serde_json::to_vec_pretty(&ptr).unwrap())
+ .map_err(|e| e.to_string())?;
+ Ok(report)
+}
+
+fn verify_md5(bytes: &[u8], md5_path: &Path) -> Result<(), String> {
+ let sum = fs::read(md5_path).map_err(|e| e.to_string())?;
+ verify_md5_bytes(bytes, &sum)
+}
+
+fn verify_md5_bytes(bytes: &[u8], sum_file: &[u8]) -> Result<(), String> {
+ let got = md5_hex(bytes);
+ let expect = String::from_utf8_lossy(sum_file)
+ .split_whitespace()
+ .next()
+ .unwrap_or("")
+ .trim()
+ .to_ascii_lowercase();
+ if expect.len() != 32 {
+ return Err("md5 file malformed".into());
+ }
+ if got != expect {
+ return Err(format!("hash mismatch (got {got}, expected {expect}); pack not activated"));
+ }
+ Ok(())
+}
+
+fn md5_hex(bytes: &[u8]) -> String {
+ use md5::{Digest, Md5};
+ let mut h = Md5::new();
+ h.update(bytes);
+ let d = h.finalize();
+ d.iter().map(|b| format!("{b:02x}")).collect()
+}
+
+pub fn md5_hex_pub(bytes: &[u8]) -> String {
+ md5_hex(bytes)
+}
+
+fn http_get(url: &str) -> Result<Vec<u8>, String> {
+ if let Some(rest) = url.strip_prefix("file://") {
+ return fs::read(rest).map_err(|e| e.to_string());
+ }
+ let resp = ureq::get(url)
+ .timeout(std::time::Duration::from_secs(60))
+ .call()
+ .map_err(|e| {
+ let s = e.to_string();
+ if s.contains("429") {
+ "429 rate limited".into()
+ } else {
+ s
+ }
+ })?;
+ if resp.status() == 429 {
+ return Err("429 rate limited".into());
+ }
+ if resp.status() >= 400 {
+ return Err(format!("HTTP {}", resp.status()));
+ }
+ let mut v = Vec::new();
+ resp.into_reader()
+ .take(80 * 1024 * 1024)
+ .read_to_end(&mut v)
+ .map_err(|e| e.to_string())?;
+ Ok(v)
+}
+
+fn extract_tar_gz(bytes: &[u8], dest: &Path) -> Result<(), String> {
+ let dec = flate2::read::GzDecoder::new(std::io::Cursor::new(bytes));
+ let mut ar = tar::Archive::new(dec);
+ for ent in ar.entries().map_err(|e| e.to_string())? {
+ let mut ent = ent.map_err(|e| e.to_string())?;
+ let path = ent.path().map_err(|e| e.to_string())?.into_owned();
+ if path.components().any(|c| matches!(c, std::path::Component::ParentDir)) {
+ continue;
+ }
+ let out = dest.join(&path);
+ if ent.header().entry_type().is_dir() {
+ fs::create_dir_all(&out).map_err(|e| e.to_string())?;
+ continue;
+ }
+ if let Some(parent) = out.parent() {
+ fs::create_dir_all(parent).map_err(|e| e.to_string())?;
+ }
+ let mut f = fs::File::create(&out).map_err(|e| e.to_string())?;
+ std::io::copy(&mut ent, &mut f).map_err(|e| e.to_string())?;
+ let _ = f.flush();
+ }
+ Ok(())
+}
+
+/// ET-shaped mini pack used by CI (`--from-archive`).
+pub fn write_mini_et_tree(dir: &Path) -> Result<(), String> {
+ fs::create_dir_all(dir.join("rules")).map_err(|e| e.to_string())?;
+ fs::write(dir.join("LICENSE"), b"ET Open test fixture - not the Proofpoint ruleset\n")
+ .map_err(|e| e.to_string())?;
+ fs::write(dir.join("BSD-License.txt"), b"BSD\n").map_err(|e| e.to_string())?;
+ fs::write(dir.join("version.txt"), b"mini\n").map_err(|e| e.to_string())?;
+ fs::write(
+ dir.join("rules").join("et-test.rules"),
+ br#"
+alert tcp any any -> any any (msg:"ET TEST content"; content:"EVILCMD"; sid:2000001; rev:1;)
+alert http any any -> any any (msg:"ET TEST uri"; http.uri; content:"/gate.php"; sid:2000002; rev:1;)
+alert tls any any -> any any (msg:"ET TEST sni"; tls.sni; content:"evil.example"; sid:2000003; rev:1;)
+alert dns any any -> any any (msg:"ET TEST dns"; dns.query; content:"c2.example"; sid:2000004; rev:1;)
+alert tcp any any -> any any (msg:"ET TEST pcre"; pcre:"/PAY[0-9]{3}/"; sid:2000005; rev:1;)
+alert tcp any any -> any any (msg:"ET TEST flowbits"; content:"X"; flowbits:set,ET.foo; sid:2000006; rev:1;)
+"#,
+ )
+ .map_err(|e| e.to_string())?;
+ Ok(())
+}
+
+/// Build a gzip+tar from a directory (tests / fixture generation).
+pub fn tar_gz_dir(dir: &Path) -> Result<Vec<u8>, String> {
+ let mut buf = Vec::new();
+ {
+ let enc = flate2::write::GzEncoder::new(&mut buf, flate2::Compression::default());
+ let mut bld = tar::Builder::new(enc);
+ bld.append_dir_all(".", dir).map_err(|e| e.to_string())?;
+ bld.finish().map_err(|e| e.to_string())?;
+ }
+ Ok(buf)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn mini_dir() -> PathBuf {
+ let d = std::env::temp_dir().join(format!(
+ "et-mini-{}",
+ std::time::SystemTime::now()
+ .duration_since(std::time::UNIX_EPOCH)
+ .unwrap()
+ .as_nanos()
+ ));
+ fs::create_dir_all(d.join("rules")).unwrap();
+ fs::write(
+ d.join("LICENSE"),
+ b"ET Open test fixture - not the Proofpoint ruleset\n",
+ )
+ .unwrap();
+ fs::write(d.join("BSD-License.txt"), b"BSD\n").unwrap();
+ fs::write(d.join("version.txt"), b"test1\n").unwrap();
+ fs::write(
+ d.join("rules").join("et-test.rules"),
+ br#"
+alert tcp any any -> any any (msg:"ET TEST content"; content:"EVILCMD"; sid:2000001; rev:1;)
+alert http any any -> any any (msg:"ET TEST uri"; http.uri; content:"/gate.php"; sid:2000002; rev:1;)
+alert tls any any -> any any (msg:"ET TEST sni"; tls.sni; content:"evil.example"; sid:2000003; rev:1;)
+alert dns any any -> any any (msg:"ET TEST dns"; dns.query; content:"c2.example"; sid:2000004; rev:1;)
+alert tcp any any -> any any (msg:"ET TEST pcre"; pcre:"/PAY[0-9]{3}/"; sid:2000005; rev:1;)
+alert tcp any any -> any any (msg:"ET TEST flowbits"; content:"X"; flowbits:set,ET.foo; sid:2000006; rev:1;)
+"#,
+ )
+ .unwrap();
+ d
+ }
+
+ #[test]
+ fn from_archive_loads_and_skips_flowbits() {
+ let dir = mini_dir();
+ let bytes = tar_gz_dir(&dir).unwrap();
+ let arch = dir.join("et-open-mini.tar.gz");
+ fs::write(&arch, &bytes).unwrap();
+ let md5 = md5_hex(&bytes);
+ fs::write(dir.join("et-open-mini.tar.gz.md5"), md5.as_bytes()).unwrap();
+ let dest = dir.join("out");
+ let rep = install_et_open(InstallOpts {
+ from_archive: Some(arch.display().to_string()),
+ dest_root: Some(dest.display().to_string()),
+ et_version: Some("mini".into()),
+ ..Default::default()
+ })
+ .unwrap();
+ assert!(rep.ok);
+ assert_eq!(rep.loaded, 5, "flowbits skipped, others loaded: {:?}", rep.skip_reasons);
+ assert_eq!(rep.skipped, 1);
+ assert!(rep.skip_reasons.iter().any(|s| s.contains("2000006") && s.contains("flowbits")));
+ assert!(Path::new(&rep.path).join("LICENSE").is_file() || Path::new(&rep.path).join("rules").is_dir());
+ let _ = fs::remove_dir_all(&dir);
+ }
+
+ #[test]
+ fn bad_md5_does_not_activate() {
+ let dir = mini_dir();
+ let bytes = tar_gz_dir(&dir).unwrap();
+ let arch = dir.join("bad.tar.gz");
+ fs::write(&arch, &bytes).unwrap();
+ fs::write(dir.join("bad.tar.gz.md5"), b"00000000000000000000000000000000\n").unwrap();
+ let dest = dir.join("out-bad");
+ let err = install_et_open(InstallOpts {
+ from_archive: Some(arch.display().to_string()),
+ dest_root: Some(dest.display().to_string()),
+ et_version: Some("bad".into()),
+ ..Default::default()
+ })
+ .unwrap_err();
+ assert!(err.contains("hash mismatch"), "{err}");
+ assert!(!dest.join("et-open").join("bad").join("import-report.json").is_file());
+ let _ = fs::remove_dir_all(&dir);
+ }
+}
diff --git a/crates/ghidrust-net-rules/src/lib.rs b/crates/ghidrust-net-rules/src/lib.rs
index c9d9db9..80b509b 100644
--- a/crates/ghidrust-net-rules/src/lib.rs
+++ b/crates/ghidrust-net-rules/src/lib.rs
@@ -1,262 +1,17 @@
-//! Ghidrust Net Rule (GNR) dialect — parse and compile.
-
-use serde::{Deserialize, Serialize};
-use std::path::Path;
-
-#[derive(Debug, Clone, Serialize, Deserialize)]
-pub struct Rule {
- pub action: String,
- pub proto: String,
- pub src: String,
- pub src_port: String,
- pub direction: String,
- pub dst: String,
- pub dst_port: String,
- pub sid: u32,
- pub msg: String,
- pub severity: u8,
- pub contents: Vec<ContentOpt>,
- #[serde(default)]
- pub skipped_options: Vec<String>,
-}
-
-#[derive(Debug, Clone, Serialize, Deserialize)]
-pub struct ContentOpt {
- pub pattern: Vec<u8>,
- pub nocase: bool,
- pub offset: Option<usize>,
- pub depth: Option<usize>,
-}
-
-#[derive(Debug, Clone, Default, Serialize, Deserialize)]
-pub struct CompiledRuleset {
- pub rules: Vec<Rule>,
- pub warnings: Vec<String>,
-}
-
-#[derive(Debug, Clone, Serialize, Deserialize)]
-pub struct RuleError {
- pub code: String,
- pub message: String,
-}
-
-impl std::fmt::Display for RuleError {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- write!(f, "{}: {}", self.code, self.message)
- }
-}
-impl std::error::Error for RuleError {}
-
-/// Load and compile a rules file.
-pub fn load_rule_pack(path: &Path) -> Result<CompiledRuleset, RuleError> {
- let text = std::fs::read_to_string(path).map_err(|e| RuleError {
- code: "io".into(),
- message: e.to_string(),
- })?;
- compile_rules(&text)
-}
-
-/// Compile GNR text (one rule per line; `#` comments).
-pub fn compile_rules(text: &str) -> Result<CompiledRuleset, RuleError> {
- let mut rules = Vec::new();
- let mut warnings = Vec::new();
- for (lineno, raw) in text.lines().enumerate() {
- let line = raw.trim();
- if line.is_empty() || line.starts_with('#') {
- continue;
- }
- match parse_rule_line(line) {
- Ok(r) => {
- for s in &r.skipped_options {
- warnings.push(format!("line {}: skipped option '{s}'", lineno + 1));
- }
- rules.push(r);
- }
- Err(e) => {
- return Err(RuleError {
- code: "parse".into(),
- message: format!("line {}: {}", lineno + 1, e.message),
- });
- }
- }
- }
- Ok(CompiledRuleset { rules, warnings })
-}
-
-fn parse_rule_line(line: &str) -> Result<Rule, RuleError> {
- // header (options)
- let (header, opts) = if let Some(i) = line.find('(') {
- let end = line.rfind(')').ok_or_else(|| RuleError {
- code: "parse".into(),
- message: "missing closing ')'".into(),
- })?;
- (&line[..i], &line[i + 1..end])
- } else {
- return Err(RuleError {
- code: "parse".into(),
- message: "missing options block".into(),
- });
- };
- let parts: Vec<_> = header.split_whitespace().collect();
- if parts.len() < 7 {
- return Err(RuleError {
- code: "parse".into(),
- message: "expected: action proto src sport dir dst dport".into(),
- });
- }
- let mut sid = 0u32;
- let mut msg = String::new();
- let mut severity = 2u8;
- let mut contents = Vec::new();
- let mut skipped = Vec::new();
- for opt in opts.split(';') {
- let opt = opt.trim();
- if opt.is_empty() {
- continue;
- }
- if let Some(rest) = opt.strip_prefix("msg:") {
- msg = unquote(rest.trim());
- } else if let Some(rest) = opt.strip_prefix("sid:") {
- sid = rest.trim().parse().unwrap_or(0);
- } else if let Some(rest) = opt.strip_prefix("severity:") {
- severity = rest.trim().parse().unwrap_or(2);
- } else if let Some(rest) = opt.strip_prefix("content:") {
- let (pat, flags) = split_content(rest.trim());
- let mut c = ContentOpt {
- pattern: unescape_content(&unquote(&pat)),
- nocase: false,
- offset: None,
- depth: None,
- };
- for f in flags {
- match f {
- "nocase" => c.nocase = true,
- other if other.starts_with("offset:") => {
- c.offset = other["offset:".len()..].parse().ok();
- }
- other if other.starts_with("depth:") => {
- c.depth = other["depth:".len()..].parse().ok();
- }
- other => skipped.push(other.to_string()),
- }
- }
- contents.push(c);
- } else if opt == "nocase" {
- if let Some(c) = contents.last_mut() {
- c.nocase = true;
- }
- } else if let Some(rest) = opt.strip_prefix("offset:") {
- if let Some(c) = contents.last_mut() {
- c.offset = rest.trim().parse().ok();
- }
- } else if let Some(rest) = opt.strip_prefix("depth:") {
- if let Some(c) = contents.last_mut() {
- c.depth = rest.trim().parse().ok();
- }
- } else if opt.starts_with("classtype:")
- || opt.starts_with("rev:")
- || opt.starts_with("reference:")
- || opt == "flow:to_server"
- || opt == "flow:established"
- {
- // recognized no-ops for Wave 3
- } else {
- skipped.push(opt.to_string());
- }
- }
- if sid == 0 {
- return Err(RuleError {
- code: "parse".into(),
- message: "sid required".into(),
- });
- }
- Ok(Rule {
- action: parts[0].into(),
- proto: parts[1].into(),
- src: parts[2].into(),
- src_port: parts[3].into(),
- direction: parts[4].into(),
- dst: parts[5].into(),
- dst_port: parts[6].into(),
- sid,
- msg,
- severity,
- contents,
- skipped_options: skipped,
- })
-}
-
-fn unquote(s: &str) -> String {
- let s = s.trim();
- if s.starts_with('"') && s.ends_with('"') && s.len() >= 2 {
- s[1..s.len() - 1].to_string()
- } else {
- s.to_string()
- }
-}
-
-fn split_content(s: &str) -> (String, Vec<&str>) {
- // "pattern"[,flags...]
- let s = s.trim();
- if let Some(rest) = s.strip_prefix('"') {
- if let Some(end) = rest.find('"') {
- let pat = format!("\"{}\"", &rest[..end]);
- let after = rest[end + 1..].trim().trim_start_matches(',').trim();
- let flags: Vec<_> = if after.is_empty() {
- Vec::new()
- } else {
- after.split(',').map(|x| x.trim()).filter(|x| !x.is_empty()).collect()
- };
- return (pat, flags);
- }
- }
- (s.to_string(), Vec::new())
-}
-
-fn unescape_content(s: &str) -> Vec<u8> {
- // Support |HH HH| hex runs mixed with text.
- let mut out = Vec::new();
- let mut chars = s.chars().peekable();
- while let Some(c) = chars.next() {
- if c == '|' {
- let mut hex = String::new();
- while let Some(&n) = chars.peek() {
- if n == '|' {
- chars.next();
- break;
- }
- hex.push(n);
- chars.next();
- }
- for part in hex.split_whitespace() {
- if let Ok(b) = u8::from_str_radix(part, 16) {
- out.push(b);
- }
- }
- } else {
- out.push(c as u8);
- }
- }
- out
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- fn parse_minimal_content_rule() {
- let text = r#"alert tcp any any -> any any (msg:"probe"; content:"EVIL"; nocase; sid:1000001; severity:2;)"#;
- let set = compile_rules(text).unwrap();
- assert_eq!(set.rules.len(), 1);
- assert_eq!(set.rules[0].sid, 1000001);
- assert_eq!(set.rules[0].contents[0].pattern, b"EVIL");
- assert!(set.rules[0].contents[0].nocase);
- }
-
- #[test]
- fn malformed_missing_sid() {
- let text = r#"alert tcp any any -> any any (msg:"x"; content:"EVIL";)"#;
- assert!(compile_rules(text).is_err());
- }
-}
+//! Ghidrust Net Rule (GNR) dialect — Suricata/ET-shaped parse, compile, ET Open install.
+
+mod addr;
+mod install;
+mod parse;
+
+pub use addr::{addr_matches, port_matches};
+pub use install::{
+ active_pack_path, bundled_minimal_rules, install_et_open, list_rules_json, load_active_ruleset,
+ md5_hex_pub, set_active_pack, tar_gz_dir, user_rules_root, write_mini_et_tree, InstallOpts,
+ InstallReport,
+ DEFAULT_ET_VERSION,
+};
+pub use parse::{
+ compile_rules, compile_rules_with, default_vars, load_rule_pack, load_vars_file, CompiledRuleset,
+ ContentOpt, DetectOp, Rule, RuleError, SkippedSid, Vars,
+};
diff --git a/crates/ghidrust-net-rules/src/parse.rs b/crates/ghidrust-net-rules/src/parse.rs
new file mode 100644
index 0000000..49d573b
--- /dev/null
+++ b/crates/ghidrust-net-rules/src/parse.rs
@@ -0,0 +1,748 @@
+//! Suricata/ET-shaped rule parse — fail-closed per SID.
+
+use serde::{Deserialize, Serialize};
+use std::collections::BTreeMap;
+use std::path::Path;
+
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct Rule {
+ pub action: String,
+ pub proto: String,
+ pub src: String,
+ pub src_port: String,
+ pub direction: String,
+ pub dst: String,
+ pub dst_port: String,
+ pub sid: u32,
+ pub msg: String,
+ pub severity: u8,
+ pub contents: Vec<ContentOpt>,
+ #[serde(default)]
+ pub skipped_options: Vec<String>,
+ #[serde(default)]
+ pub ops: Vec<DetectOp>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub l7: Option<String>,
+ #[serde(default)]
+ pub flow_established: bool,
+ #[serde(default)]
+ pub flow_to_server: bool,
+ #[serde(default)]
+ pub flow_to_client: bool,
+}
+
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct ContentOpt {
+ pub pattern: Vec<u8>,
+ pub nocase: bool,
+ pub offset: Option<usize>,
+ pub depth: Option<usize>,
+}
+
+#[derive(Debug, Clone, Serialize, Deserialize, Default)]
+pub struct DetectOp {
+ /// payload | http.uri | http.host | http.method | http.header | dns.query | tls.sni
+ pub buffer: String,
+ /// content | pcre
+ pub kind: String,
+ #[serde(default)]
+ pub pattern: Vec<u8>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub pcre: Option<String>,
+ #[serde(default)]
+ pub nocase: bool,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub offset: Option<usize>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub depth: Option<usize>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub distance: Option<i32>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub within: Option<usize>,
+}
+
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct SkippedSid {
+ pub sid: u32,
+ pub reason: String,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub msg: Option<String>,
+}
+
+#[derive(Debug, Clone, Default, Serialize, Deserialize)]
+pub struct CompiledRuleset {
+ pub rules: Vec<Rule>,
+ pub warnings: Vec<String>,
+ #[serde(default)]
+ pub skipped: Vec<SkippedSid>,
+}
+
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct RuleError {
+ pub code: String,
+ pub message: String,
+}
+
+impl std::fmt::Display for RuleError {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "{}: {}", self.code, self.message)
+ }
+}
+impl std::error::Error for RuleError {}
+
+pub type Vars = BTreeMap<String, String>;
+
+pub fn default_vars() -> Vars {
+ let mut v = Vars::new();
+ let home = "[10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,127.0.0.0/8]";
+ v.insert("HOME_NET".into(), home.into());
+ v.insert("EXTERNAL_NET".into(), "any".into());
+ v.insert("HTTP_PORTS".into(), "[80,443,8080,8000,8008,8888]".into());
+ v.insert("SMTP_PORTS".into(), "[25,587,465]".into());
+ v.insert("FTP_PORTS".into(), "21".into());
+ v.insert("SSH_PORTS".into(), "22".into());
+ v.insert("ORACLE_PORTS".into(), "1521".into());
+ v.insert("DNS_PORTS".into(), "53".into());
+ v.insert("SIP_PORTS".into(), "[5060,5061]".into());
+ v.insert("FILE_DATA_PORTS".into(), "[80,443,8080]".into());
+ v.insert("SHELLCODE_PORTS".into(), "!80".into());
+ apply_suricata_aliases(&mut v);
+ v
+}
+
+/// Suricata.yaml defaults: HTTP_SERVERS/SQL_SERVERS/… alias HOME_NET unless set.
+pub fn apply_suricata_aliases(v: &mut Vars) {
+ let home = v
+ .get("HOME_NET")
+ .cloned()
+ .unwrap_or_else(|| "[10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,127.0.0.0/8]".into());
+ let ext = v
+ .get("EXTERNAL_NET")
+ .cloned()
+ .unwrap_or_else(|| "any".into());
+ for k in [
+ "HTTP_SERVERS",
+ "SMTP_SERVERS",
+ "SQL_SERVERS",
+ "DNS_SERVERS",
+ "TELNET_SERVERS",
+ "DNP3_SERVER",
+ "DNP3_CLIENT",
+ "MODBUS_CLIENT",
+ "MODBUS_SERVER",
+ "ENIP_CLIENT",
+ "ENIP_SERVER",
+ ] {
+ v.entry(k.into()).or_insert_with(|| home.clone());
+ }
+ v.entry("AIM_SERVERS".into()).or_insert_with(|| ext.clone());
+ // Resolve `$HOME_NET` / `$EXTERNAL_NET` copies from a vars file.
+ let keys: Vec<String> = v.keys().cloned().collect();
+ for k in keys {
+ if let Some(val) = v.get(&k).cloned() {
+ if let Some(name) = val.trim().strip_prefix('$') {
+ if let Some(inner) = v.get(name).cloned() {
+ v.insert(k, inner);
+ }
+ }
+ }
+ }
+}
+
+pub fn load_vars_file(path: &Path) -> Vars {
+ let mut v = default_vars();
+ let Ok(text) = std::fs::read_to_string(path) else {
+ return v;
+ };
+ for line in text.lines() {
+ let line = line.trim();
+ if line.is_empty() || line.starts_with('#') {
+ continue;
+ }
+ if let Some((k, val)) = line.split_once(':') {
+ let k = k.trim().trim_start_matches('$');
+ v.insert(k.to_string(), val.trim().to_string());
+ } else if let Some((k, val)) = line.split_once('=') {
+ let k = k.trim().trim_start_matches('$');
+ v.insert(k.to_string(), val.trim().to_string());
+ }
+ }
+ apply_suricata_aliases(&mut v);
+ v
+}
+
+pub fn load_rule_pack(path: &Path) -> Result<CompiledRuleset, RuleError> {
+ let text = std::fs::read_to_string(path).map_err(|e| RuleError {
+ code: "io".into(),
+ message: e.to_string(),
+ })?;
+ Ok(compile_rules(&text))
+}
+
+pub fn compile_rules(text: &str) -> CompiledRuleset {
+ compile_rules_with(text, &default_vars())
+}
+
+pub fn compile_rules_with(text: &str, vars: &Vars) -> CompiledRuleset {
+ let mut rules = Vec::new();
+ let mut warnings = Vec::new();
+ let mut skipped = Vec::new();
+ for (lineno, raw) in text.lines().enumerate() {
+ let line = raw.trim();
+ if line.is_empty() || line.starts_with('#') || line.starts_with("alert ip any any -> any any (msg:\"GPL") {
+ // keep GPL lines; they parse
+ }
+ if line.is_empty() || line.starts_with('#') {
+ continue;
+ }
+ match parse_rule_line(line, vars) {
+ Ok(ParseOut::Rule(r)) => {
+ for s in &r.skipped_options {
+ warnings.push(format!("sid {}: skipped option '{s}'", r.sid));
+ }
+ rules.push(r);
+ }
+ Ok(ParseOut::Skip { sid, reason, msg }) => {
+ skipped.push(SkippedSid { sid, reason, msg });
+ }
+ Err(e) => {
+ skipped.push(SkippedSid {
+ sid: 0,
+ reason: format!("line {}: {}", lineno + 1, e.message),
+ msg: None,
+ });
+ }
+ }
+ }
+ CompiledRuleset {
+ rules,
+ warnings,
+ skipped,
+ }
+}
+
+enum ParseOut {
+ Rule(Rule),
+ Skip {
+ sid: u32,
+ reason: String,
+ msg: Option<String>,
+ },
+}
+
+fn parse_rule_line(line: &str, vars: &Vars) -> Result<ParseOut, RuleError> {
+ let (header, opts) = if let Some(i) = line.find('(') {
+ let end = line.rfind(')').ok_or_else(|| RuleError {
+ code: "parse".into(),
+ message: "missing closing ')'".into(),
+ })?;
+ (&line[..i], &line[i + 1..end])
+ } else {
+ return Err(RuleError {
+ code: "parse".into(),
+ message: "missing options block".into(),
+ });
+ };
+ let parts: Vec<_> = header.split_whitespace().collect();
+ if parts.len() < 7 {
+ return Err(RuleError {
+ code: "parse".into(),
+ message: "expected: action proto src sport dir dst dport".into(),
+ });
+ }
+ let mut src = expand_token(parts[2], vars)?;
+ let mut src_port = expand_token(parts[3], vars)?;
+ let mut dst = expand_token(parts[5], vars)?;
+ let mut dst_port = expand_token(parts[6], vars)?;
+ let proto_raw = parts[1];
+ let l7 = match proto_raw.to_ascii_lowercase().as_str() {
+ "http" => Some("http".into()),
+ "dns" => Some("dns".into()),
+ "tls" | "ssl" => Some("tls".into()),
+ _ => None,
+ };
+ let proto = match proto_raw.to_ascii_lowercase().as_str() {
+ "http" | "tls" | "ssl" => "tcp".into(),
+ "dns" => "udp".into(),
+ other => other.to_string(),
+ };
+
+ let mut sid = 0u32;
+ let mut msg = String::new();
+ let mut severity = 2u8;
+ let mut contents = Vec::new();
+ let mut skipped_opts = Vec::new();
+ let mut ops: Vec<DetectOp> = Vec::new();
+ let mut buffer = "payload".to_string();
+ let mut skip_reason: Option<String> = None;
+ let mut flow_established = false;
+ let mut flow_to_server = false;
+ let mut flow_to_client = false;
+
+ for opt in split_options(opts) {
+ let opt = opt.trim();
+ if opt.is_empty() {
+ continue;
+ }
+ let lower = opt.to_ascii_lowercase();
+ if lower.starts_with("msg:") {
+ msg = unquote(opt[4..].trim());
+ } else if lower.starts_with("sid:") {
+ sid = opt[4..].trim().parse().unwrap_or(0);
+ } else if lower.starts_with("severity:") || lower.starts_with("priority:") {
+ severity = opt.split(':').nth(1).and_then(|s| s.trim().parse().ok()).unwrap_or(2);
+ } else if is_sticky(&lower) {
+ buffer = sticky_name(&lower);
+ } else if lower.starts_with("content:") {
+ let (pat, flags) = split_content(opt[8..].trim());
+ let mut op = DetectOp {
+ buffer: buffer.clone(),
+ kind: "content".into(),
+ pattern: unescape_content(&unquote(&pat)),
+ pcre: None,
+ nocase: false,
+ offset: None,
+ depth: None,
+ distance: None,
+ within: None,
+ };
+ for f in flags {
+ apply_flag(&mut op, f);
+ }
+ if op.buffer == "payload" {
+ contents.push(ContentOpt {
+ pattern: op.pattern.clone(),
+ nocase: op.nocase,
+ offset: op.offset,
+ depth: op.depth,
+ });
+ }
+ ops.push(op);
+ } else if lower.starts_with("pcre:") {
+ let raw = unquote(opt[5..].trim());
+ match rust_pcre(&raw) {
+ Ok((re, nocase)) => {
+ ops.push(DetectOp {
+ buffer: buffer.clone(),
+ kind: "pcre".into(),
+ pattern: Vec::new(),
+ pcre: Some(re),
+ nocase,
+ ..Default::default()
+ });
+ }
+ Err(e) => skip_reason = Some(format!("invalid pcre: {e}")),
+ }
+ } else if lower == "nocase" {
+ if let Some(c) = ops.last_mut() {
+ c.nocase = true;
+ }
+ if let Some(c) = contents.last_mut() {
+ c.nocase = true;
+ }
+ } else if lower.starts_with("offset:") {
+ let v = opt[7..].trim().parse().ok();
+ if let Some(c) = ops.last_mut() {
+ c.offset = v;
+ }
+ if let Some(c) = contents.last_mut() {
+ c.offset = v;
+ }
+ } else if lower.starts_with("depth:") {
+ let v = opt[6..].trim().parse().ok();
+ if let Some(c) = ops.last_mut() {
+ c.depth = v;
+ }
+ if let Some(c) = contents.last_mut() {
+ c.depth = v;
+ }
+ } else if lower.starts_with("distance:") {
+ if let Some(c) = ops.last_mut() {
+ c.distance = opt[9..].trim().parse().ok();
+ }
+ } else if lower.starts_with("within:") {
+ if let Some(c) = ops.last_mut() {
+ c.within = opt[7..].trim().parse().ok();
+ }
+ } else if lower.starts_with("flow:") {
+ for p in opt[5..].split(',') {
+ match p.trim() {
+ "established" => flow_established = true,
+ "to_server" | "from_client" => flow_to_server = true,
+ "to_client" | "from_server" => flow_to_client = true,
+ "stateless" | "no_stream" | "only_stream" => {}
+ other => skipped_opts.push(format!("flow:{other}")),
+ }
+ }
+ } else if lower.starts_with("flowbits:")
+ || lower.starts_with("xbits:")
+ || lower.starts_with("flowint:")
+ {
+ skip_reason = Some("flowbits".into());
+ } else if lower.starts_with("file-data")
+ || lower.starts_with("file.data")
+ || lower.starts_with("filemagic")
+ || lower.starts_with("file.md5")
+ {
+ skip_reason = Some("file-data".into());
+ } else if lower.starts_with("ja3") || lower.starts_with("ja3s") || lower == "ja3.hash" {
+ skip_reason = Some("ja3".into());
+ } else if lower.starts_with("lua:") || lower.starts_with("luajit:") {
+ skip_reason = Some("lua".into());
+ } else if lower.starts_with("threshold:")
+ || lower.starts_with("detection_filter:")
+ || lower.starts_with("limit:")
+ {
+ skip_reason = Some("threshold".into());
+ } else if lower.starts_with("classtype:")
+ || lower.starts_with("rev:")
+ || lower.starts_with("reference:")
+ || lower.starts_with("metadata:")
+ || lower.starts_with("tag:")
+ || lower.starts_with("priority:")
+ || lower == "fast_pattern"
+ || lower.starts_with("fast_pattern:")
+ || lower.starts_with("target:")
+ || lower.starts_with("gid:")
+ {
+ // recorded no-ops
+ } else if lower.starts_with("http.")
+ || lower.starts_with("dns.")
+ || lower.starts_with("tls.")
+ || lower.starts_with("ssl_")
+ {
+ skip_reason = Some(format!("buffer {opt}"));
+ } else if lower.starts_with("byte_test")
+ || lower.starts_with("byte_jump")
+ || lower.starts_with("byte_extract")
+ || lower.starts_with("isdataat")
+ || lower.starts_with("dsize")
+ || lower.starts_with("stream_size")
+ || lower.starts_with("itype")
+ || lower.starts_with("icode")
+ || lower.starts_with("icmp_")
+ || lower.starts_with("flags:")
+ || lower.starts_with("window:")
+ || lower.starts_with("seq:")
+ || lower.starts_with("ack:")
+ || lower.starts_with("id:")
+ || lower.starts_with("geoip")
+ || lower.starts_with("xbits")
+ || lower.starts_with("app-layer")
+ || lower.starts_with("urilen")
+ || lower.starts_with("bsize")
+ {
+ skip_reason = Some(format!("keyword {opt}"));
+ } else {
+ skipped_opts.push(opt.to_string());
+ }
+ }
+
+ if sid == 0 {
+ return Err(RuleError {
+ code: "parse".into(),
+ message: "sid required".into(),
+ });
+ }
+ if let Some(reason) = skip_reason {
+ return Ok(ParseOut::Skip {
+ sid,
+ reason,
+ msg: if msg.is_empty() { None } else { Some(msg) },
+ });
+ }
+ // Unknown leftover options that look like matchers → skip (don't half-match).
+ for s in &skipped_opts {
+ let sl = s.to_ascii_lowercase();
+ if sl.contains(':') && !sl.starts_with("classtype") {
+ return Ok(ParseOut::Skip {
+ sid,
+ reason: format!("unknown option {s}"),
+ msg: if msg.is_empty() { None } else { Some(msg) },
+ });
+ }
+ }
+
+ let _ = (&mut src, &mut src_port, &mut dst, &mut dst_port);
+ Ok(ParseOut::Rule(Rule {
+ action: parts[0].into(),
+ proto,
+ src,
+ src_port,
+ direction: parts[4].into(),
+ dst,
+ dst_port,
+ sid,
+ msg,
+ severity,
+ contents,
+ skipped_options: skipped_opts,
+ ops,
+ l7,
+ flow_established,
+ flow_to_server,
+ flow_to_client,
+ }))
+}
+
+fn expand_token(tok: &str, vars: &Vars) -> Result<String, RuleError> {
+ if let Some(name) = tok.strip_prefix('$') {
+ let name = name.trim();
+ vars.get(name).cloned().ok_or_else(|| RuleError {
+ code: "var".into(),
+ message: format!("undefined var ${name}"),
+ })
+ } else if let Some(rest) = tok.strip_prefix("!$") {
+ let name = rest.trim();
+ let inner = vars.get(name).ok_or_else(|| RuleError {
+ code: "var".into(),
+ message: format!("undefined var ${name}"),
+ })?;
+ Ok(format!("!{inner}"))
+ } else {
+ Ok(tok.to_string())
+ }
+}
+
+fn is_sticky(lower: &str) -> bool {
+ matches!(
+ lower.trim_end_matches(':'),
+ "http.uri"
+ | "http_uri"
+ | "http.uri.raw"
+ | "http.host"
+ | "http_host"
+ | "http.method"
+ | "http_method"
+ | "http.header"
+ | "http_header"
+ | "http.header.raw"
+ | "dns.query"
+ | "dns_query"
+ | "dns.rrname"
+ | "tls.sni"
+ | "tls_sni"
+ | "tls.sni.raw"
+ )
+}
+
+fn sticky_name(lower: &str) -> String {
+ match lower.trim_end_matches(':') {
+ "http.uri" | "http_uri" | "http.uri.raw" => "http.uri",
+ "http.host" | "http_host" => "http.host",
+ "http.method" | "http_method" => "http.method",
+ "http.header" | "http_header" | "http.header.raw" => "http.header",
+ "dns.query" | "dns_query" | "dns.rrname" => "dns.query",
+ "tls.sni" | "tls_sni" | "tls.sni.raw" => "tls.sni",
+ other => other,
+ }
+ .into()
+}
+
+fn apply_flag(op: &mut DetectOp, f: &str) {
+ match f {
+ "nocase" => op.nocase = true,
+ other if other.starts_with("offset:") => {
+ op.offset = other["offset:".len()..].parse().ok();
+ }
+ other if other.starts_with("depth:") => {
+ op.depth = other["depth:".len()..].parse().ok();
+ }
+ other if other.starts_with("distance:") => {
+ op.distance = other["distance:".len()..].parse().ok();
+ }
+ other if other.starts_with("within:") => {
+ op.within = other["within:".len()..].parse().ok();
+ }
+ _ => {}
+ }
+}
+
+fn split_options(opts: &str) -> Vec<String> {
+ let mut out = Vec::new();
+ let mut cur = String::new();
+ let mut in_dq = false;
+ let mut esc = false;
+ for c in opts.chars() {
+ if esc {
+ cur.push(c);
+ esc = false;
+ continue;
+ }
+ if c == '\\' && in_dq {
+ cur.push(c);
+ esc = true;
+ continue;
+ }
+ if c == '"' {
+ in_dq = !in_dq;
+ cur.push(c);
+ continue;
+ }
+ if c == ';' && !in_dq {
+ let t = cur.trim();
+ if !t.is_empty() {
+ out.push(t.to_string());
+ }
+ cur.clear();
+ continue;
+ }
+ cur.push(c);
+ }
+ let t = cur.trim();
+ if !t.is_empty() {
+ out.push(t.to_string());
+ }
+ out
+}
+
+fn unquote(s: &str) -> String {
+ let s = s.trim();
+ if s.starts_with('"') && s.ends_with('"') && s.len() >= 2 {
+ s[1..s.len() - 1].to_string()
+ } else {
+ s.to_string()
+ }
+}
+
+fn split_content(s: &str) -> (String, Vec<&str>) {
+ let s = s.trim();
+ if let Some(rest) = s.strip_prefix('"') {
+ if let Some(end) = rest.find('"') {
+ let pat = format!("\"{}\"", &rest[..end]);
+ let after = rest[end + 1..].trim().trim_start_matches(',').trim();
+ let flags: Vec<_> = if after.is_empty() {
+ Vec::new()
+ } else {
+ after.split(',').map(|x| x.trim()).filter(|x| !x.is_empty()).collect()
+ };
+ return (pat, flags);
+ }
+ }
+ (s.to_string(), Vec::new())
+}
+
+fn unescape_content(s: &str) -> Vec<u8> {
+ let mut out = Vec::new();
+ let mut chars = s.chars().peekable();
+ while let Some(c) = chars.next() {
+ if c == '|' {
+ let mut hex = String::new();
+ while let Some(&n) = chars.peek() {
+ if n == '|' {
+ chars.next();
+ break;
+ }
+ hex.push(n);
+ chars.next();
+ }
+ for part in hex.split_whitespace() {
+ if let Ok(b) = u8::from_str_radix(part, 16) {
+ out.push(b);
+ }
+ }
+ } else {
+ out.push(c as u8);
+ }
+ }
+ out
+}
+
+/// Convert Suricata `pcre:"/pat/flags"` to a Rust regex string.
+fn rust_pcre(raw: &str) -> Result<(String, bool), String> {
+ let s = raw.trim();
+ let body = if let Some(rest) = s.strip_prefix('/') {
+ if let Some(end) = rest.rfind('/') {
+ let pat = &rest[..end];
+ let flags = &rest[end + 1..];
+ let mut pfx = String::new();
+ let mut nocase = false;
+ if flags.contains('i') {
+ pfx.push_str("(?i)");
+ nocase = true;
+ }
+ if flags.contains('s') {
+ pfx.push_str("(?s)");
+ }
+ if flags.contains('m') {
+ pfx.push_str("(?m)");
+ }
+ (format!("{pfx}{pat}"), nocase)
+ } else {
+ (s.to_string(), false)
+ }
+ } else {
+ (s.to_string(), false)
+ };
+ regex::Regex::new(&body.0).map_err(|e| e.to_string())?;
+ Ok(body)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn parse_minimal_content_rule() {
+ let text = r#"alert tcp any any -> any any (msg:"probe"; content:"EVIL"; nocase; sid:1000001; severity:2;)"#;
+ let set = compile_rules(text);
+ assert_eq!(set.rules.len(), 1);
+ assert_eq!(set.rules[0].sid, 1000001);
+ assert_eq!(set.rules[0].contents[0].pattern, b"EVIL");
+ assert!(set.rules[0].contents[0].nocase);
+ }
+
+ #[test]
+ fn malformed_missing_sid_is_skipped() {
+ let text = r#"alert tcp any any -> any any (msg:"x"; content:"EVIL";)"#;
+ let set = compile_rules(text);
+ assert!(set.rules.is_empty());
+ assert!(set.skipped.iter().any(|s| s.reason.contains("sid")));
+ }
+
+ #[test]
+ fn quote_aware_semicolon_in_content() {
+ let text = r#"alert tcp any any -> any any (msg:"x"; content:"a;b"; sid:9;)"#;
+ let set = compile_rules(text);
+ assert_eq!(set.rules.len(), 1);
+ assert_eq!(set.rules[0].contents[0].pattern, b"a;b");
+ }
+
+ #[test]
+ fn flowbits_skipped_not_loaded() {
+ let text = r#"alert tcp any any -> any any (msg:"fb"; content:"X"; flowbits:set,ET.foo; sid:2000006;)"#;
+ let set = compile_rules(text);
+ assert!(set.rules.is_empty());
+ assert_eq!(set.skipped[0].sid, 2000006);
+ assert!(set.skipped[0].reason.contains("flowbits"));
+ }
+
+ #[test]
+ fn http_uri_sticky_and_pcre_load() {
+ let text = r#"
+alert http any any -> any any (msg:"uri"; http.uri; content:"/gate.php"; sid:2000002; rev:1;)
+alert tcp any any -> any any (msg:"p"; pcre:"/PAY[0-9]{3}/"; sid:2000005;)
+"#;
+ let set = compile_rules(text);
+ assert_eq!(set.rules.len(), 2, "{:?}", set.skipped);
+ assert_eq!(set.rules[0].l7.as_deref(), Some("http"));
+ assert_eq!(set.rules[0].ops[0].buffer, "http.uri");
+ assert_eq!(set.rules[1].ops[0].kind, "pcre");
+ }
+
+ #[test]
+ fn undefined_var_skips() {
+ let text = r#"alert tcp $NO_SUCH_NET any -> any any (msg:"v"; content:"X"; sid:3;)"#;
+ let set = compile_rules(text);
+ assert!(set.rules.is_empty());
+ assert!(set.skipped.iter().any(|s| s.reason.contains("NO_SUCH_NET") || s.reason.contains("var")));
+ }
+
+ #[test]
+ fn http_servers_alias_loads() {
+ let text = r#"alert tcp $HTTP_SERVERS any -> $EXTERNAL_NET any (msg:"h"; content:"X"; sid:77;)"#;
+ let set = compile_rules(text);
+ assert_eq!(set.rules.len(), 1, "{:?}", set.skipped);
+ assert!(set.rules[0].src.contains("10.0.0.0/8"));
+ }
+}
diff --git a/crates/ghidrust-net-schema/src/lib.rs b/crates/ghidrust-net-schema/src/lib.rs
index b4f7c67..2693f7e 100644
--- a/crates/ghidrust-net-schema/src/lib.rs
+++ b/crates/ghidrust-net-schema/src/lib.rs
@@ -153,6 +153,9 @@ pub struct Alert {
pub sid: u32,
pub msg: String,
pub severity: u8,
+ /// Suricata action that fired (`alert` / `pass` / `drop`). Recorded, not silently ignored.
+ #[serde(default = "default_alert_action")]
+ pub action: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub flow_key: Option<FlowKey>,
#[serde(default, skip_serializing_if = "Option::is_none")]
@@ -164,6 +167,10 @@ pub struct Alert {
pub ioc: Option<String>,
}
+fn default_alert_action() -> String {
+ "alert".into()
+}
+
/// Closed-loop dig job.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DigJob {
diff --git a/fixtures/et-http-gate.bin b/fixtures/et-http-gate.bin
new file mode 100644
index 0000000..d19201e
--- /dev/null
+++ b/fixtures/et-http-gate.bin
@@ -0,0 +1,3 @@
+GET /gate.php HTTP/1.1
+Host: a.test
+
diff --git a/fixtures/et-open-mini-tree/BSD-License.txt b/fixtures/et-open-mini-tree/BSD-License.txt
new file mode 100644
index 0000000..9000ab8
--- /dev/null
+++ b/fixtures/et-open-mini-tree/BSD-License.txt
@@ -0,0 +1 @@
+BSD
diff --git a/fixtures/et-open-mini-tree/LICENSE b/fixtures/et-open-mini-tree/LICENSE
new file mode 100644
index 0000000..4d14eb8
--- /dev/null
+++ b/fixtures/et-open-mini-tree/LICENSE
@@ -0,0 +1 @@
+ET Open test fixture - not the Proofpoint ruleset
diff --git a/fixtures/et-open-mini-tree/rules/et-test.rules b/fixtures/et-open-mini-tree/rules/et-test.rules
new file mode 100644
index 0000000..651ac35
--- /dev/null
+++ b/fixtures/et-open-mini-tree/rules/et-test.rules
@@ -0,0 +1,7 @@
+
+alert tcp any any -> any any (msg:"ET TEST content"; content:"EVILCMD"; sid:2000001; rev:1;)
+alert http any any -> any any (msg:"ET TEST uri"; http.uri; content:"/gate.php"; sid:2000002; rev:1;)
+alert tls any any -> any any (msg:"ET TEST sni"; tls.sni; content:"evil.example"; sid:2000003; rev:1;)
+alert dns any any -> any any (msg:"ET TEST dns"; dns.query; content:"c2.example"; sid:2000004; rev:1;)
+alert tcp any any -> any any (msg:"ET TEST pcre"; pcre:"/PAY[0-9]{3}/"; sid:2000005; rev:1;)
+alert tcp any any -> any any (msg:"ET TEST flowbits"; content:"X"; flowbits:set,ET.foo; sid:2000006; rev:1;)
diff --git a/fixtures/et-open-mini-tree/version.txt b/fixtures/et-open-mini-tree/version.txt
new file mode 100644
index 0000000..627d2d2
--- /dev/null
+++ b/fixtures/et-open-mini-tree/version.txt
@@ -0,0 +1 @@
+mini
diff --git a/fixtures/et-open-mini.tar.gz b/fixtures/et-open-mini.tar.gz
new file mode 100644
index 0000000..91d640e
Binary files /dev/null and b/fixtures/et-open-mini.tar.gz differ
diff --git a/fixtures/et-open-mini.tar.gz.md5 b/fixtures/et-open-mini.tar.gz.md5
new file mode 100644
index 0000000..48b08ae
--- /dev/null
+++ b/fixtures/et-open-mini.tar.gz.md5
@@ -0,0 +1 @@
+66cf0c5cc904b14d699987d6d65a04fa
\ No newline at end of file
diff --git a/fixtures/tiny_x64.macho b/fixtures/tiny_x64.macho
new file mode 100644
index 0000000..db6e94b
Binary files /dev/null and b/fixtures/tiny_x64.macho differ
diff --git a/skill/README.md b/skill/README.md
index 6a2e2f1..008c77d 100644
--- a/skill/README.md
+++ b/skill/README.md
@@ -33,7 +33,16 @@ cargo run -p ghidrust-gui --release
Windows: `.\target\release\ghidrust.exe` / `.\target\release\ghidrust-gui.exe`. Friction surfaces (`inventory`, `tree`, `artifact`, `process`, `rtti --filter`) match MCP tool names in `tool_defs()` — see [`SKILL.md`](SKILL.md).
-Check build identity: `ghidrust --version` (same package version as MCP `server_info` and egui About). Agents need MCP `tool_surface >= 3` (touch-map / body_class / function_create); prefer `>= 4` for bounded disasm / `get_calls_from`; `>= 5` for decode tools; **`>= 6`** for crypto recover/bake; **`>= 7`** for live process debug; **`>= 8`** for Ghidnet (`net_*`, `server_info.network`; see [ghidnet.md](ghidnet.md)). Restart MCP after rebuild if those tools are missing.
+Check build identity: `ghidrust --version` (same package version as MCP `server_info` and egui About). Agents need MCP `tool_surface >= 3` (touch-map / body_class / function_create); prefer `>= 4` for bounded disasm / `get_calls_from`; `>= 5` for decode tools; **`>= 6`** for crypto recover/bake; **`>= 7`** for live process debug; **`>= 8`** for Ghidnet (`net_*`, `net_rules_update`, `server_info.network`; see [ghidnet.md](ghidnet.md)). Restart MCP after rebuild if those tools are missing.
+
+Ghidnet / ET Open smoke (IDS alerts, not kernel IPS):
+
+```bash
+./target/release/ghidrust net help
+./target/release/ghidrust net rules update et-open --from-archive fixtures/et-open-mini.tar.gz --et-version mini --json
+./target/release/ghidrust net rules list --json
+./target/release/ghidrust net detect --payload-file fixtures/et-http-gate.bin --json
+```
Smoke crypto surfaces:
diff --git a/skill/SKILL.md b/skill/SKILL.md
index 988307d..4f0ba5a 100644
--- a/skill/SKILL.md
+++ b/skill/SKILL.md
@@ -3,13 +3,15 @@ name: ghidrust
description: >
x86-64 Auto Analysis, projects, CLI/MCP, egui GUI, IL2CPP metadata, Unity player inventory,
GPU analyzer kernels + multipass decompile, crypt-constants / recover-strings / decode bake /
- crypto-capabilities, Ghidnet native network dig/connections/capture/detect/pivots/rules.
+ crypto-capabilities, Ghidnet native network dig/connections/capture/detect/pivots/rules,
+ ET Open, IDS/IPS-class alerts.
Exhaustive feature catalog with when-to-use guidance.
Triggers: /ghidrust, reverse engineer, RE a PE/ELF, disassemble, decode-support, decode-query,
MCP ghidrust, strings/functions, crypt-constants, recover-strings, decode bake, decode magic,
crypto-capabilities, GPU decompile, IL2CPP, global-metadata, unity-inventory, GameAssembly,
analyzer-bench, rtti-gpu-bench, bulk-bench, Ghidnet, net dig, phone home, network pivot, IOC dig,
- net_connections, net_detect, net_capture, net_rules_check, GNR, ghidrust-netcap.
+ net_connections, net_detect, net_capture, net_rules_check, net_rules_update, ET Open, emerging
+ threats, IDS, IPS, GNR, ghidrust-netcap.
metadata:
short-description: "Ghidrust RE — decode, crypto, Ghidnet, CLI, MCP, IL2CPP, Unity, analyzers, GPU"
---
@@ -26,8 +28,9 @@ metadata:
| CLI | `cargo run -p ghidrust-cli --release -- <cmd>` or `target/release/ghidrust.exe` |
| GUI | `cargo run -p ghidrust-gui --release` |
| Ghidnet helper | `target/release/ghidrust-netcap` |
-| GNR rules | `rules/ghidrust-minimal.rules` |
-| Ghidnet SOP | [ghidnet.md](ghidnet.md) (`tool_surface >= 8`) |
+| GNR rules | `rules/ghidrust-minimal.rules` (merged into the active ET pack) |
+| ET Open pack | `GHIDRUST_RULES_DIR` or `%APPDATA%/ghidrust/rules/et-open/<version>/` (`net rules update et-open`) |
+| Ghidnet SOP | [ghidnet.md](ghidnet.md) (`tool_surface >= 8`; ET Open + IDS) |
| Fixtures | `fixtures/tiny_x64.pe`, `fixtures/analysis_lab.pe`, `fixtures/tiny_x64.elf`, `fixtures/il2cpp/*` |
| Docs | `README.md`, `docs/IL2CPP.md`, `docs/GPU_ANALYZER_MATRIX.md`, `docs/PARALLEL_RE_RESEARCH.md` |
| Decode API | `ghidrust-decode::Engine` — `open`, `disasm`, `disasm_one`, `option`, `insn_name`, `reg_name`, `group_name`, `regs_access`; re-exported from `ghidrust-core` |
@@ -280,7 +283,7 @@ Brief line (`--brief` / MCP `listing_text`): `{addr:#x}: {mnemonic} {operands}`.
- **RTTI catalog**: Prefer `rtti_query` (`--filter`/`--exact`) before mangled `.?AV` string archaeology. Multi-vtable types report `vtable_vas[]` honestly.
- **UTF-16 xrefs**: If `search_strings` returns `utf16le`, query `get_string_xrefs` with `encoding=all` (or `utf16le`) before concluding “no refs”.
- **Live process (Windows, `tool_surface >= 7`)**: Multi-step live work **must** use MCP (or one long-lived process). Default attach is **observe** (read-only). Pass `mode: "debug"` for break/step/registers/stack. Loop: `process_list` → `process_attach|process_launch` → `process_modules` → `process_resolve` → optional `process_break_set` / `process_scan` / `process_watch_expr` → `process_continue`/`process_wait` → regs/stack/`process_read` → `process_export_snapshot` → `process_detach`. Observe launch still uses CREATE_SUSPENDED + `process_resume`. Debug launch uses Windows debug APIs (initial break). Never chain separate CLI `ghidrust process` spawns expecting the same `session_id`. Bytes ≠ types. Hardware BP / instrument mode not enabled.
-- **Ghidnet (native, `tool_surface >= 8`)**: Prefer `net_dig` / `net_playbook` over hand-chained load/analyze for phone-home / IOC digs. Check `server_info.network`. Default pack: `rules/ghidrust-minimal.rules`. Multi-step capture/detect **must** use MCP or GUI Network host (`session_id` process-local). GUI: **Network** / **Window → Network** (Connections\|Capture\|Alerts\|Rules\|Dig); Dig → **Open in Listing**; Capture **Export path**; inline block only if `GHIDRUST_NET_INLINE=1`. Airgap: path-only dig; refuse live observe/capture/detect. Never invent hosts; `pid_confidence=unknown` ≠ ground truth; no foreign IDS. Full matrix/workflows: [ghidnet.md](ghidnet.md).
+- **Ghidnet (native, `tool_surface >= 8`)**: Prefer `net_dig` / `net_playbook` over hand-chained load/analyze for phone-home / IOC digs. Check `server_info.network` (`caps` include `et-open` / `ids`). **ET Open:** `net_rules_update` / CLI `net rules update et-open` / GUI **Download / update ET Open** (same `install_et_open`). Detect without `rules` uses the active pack + `rules/ghidrust-minimal.rules`. IDS: `alert`/`pass`/`drop` recorded — **not** kernel IPS. Unimplemented ET keywords skip that SID (`net_rules_list` `skipped`). Live capture is ghidnetcap (fail-closed `privilege_required`). Multi-step capture/detect **must** use MCP or GUI (`session_id` process-local). Airgap: path-only dig; ET via `from_archive`. Never invent hosts; `pid_confidence=unknown` ≠ ground truth; do not install Suricata. Full matrix: [ghidnet.md](ghidnet.md).
- **IL2CPP offline → live**: (1) `il2cpp_touch_map` / `il2cpp_meta` / `il2cpp_map` for names + method RVAs (null = unknown offline; encrypted → `next_steps` with meta-sections/touch-map). (2) Require `body_class` not `shared_stub` / `semantics_mismatch` before treating a name as a hook target. (3) `decompile` + `follow_stub` for resolve stubs with mapped slots. (4) On `runtime_unresolved` / `trampoline_or_invoker` → live attach → `process_resolve(module, rva)` → `process_read`. (5) Multi-build work: `il2cpp map --baseline PREV.json` → inspect `build_skew` (stale **map catalog**, not only stale MCP binary).
- **Skill bootstrap**: GUI project open / Start writes `.grok/skills/ghidrust/SKILL.md` (disk or embedded fallback) and shows a fail-loud checklist (mcp/skill/agents/context + hash).
- **Do not**: invent enum ordinals; treat `section_notes` as proof of hooks; read `.gdecomp` dumps as text (metrics JSON only); hook shared stubs as unique gameplay methods.
@@ -327,11 +330,13 @@ Need crypto constants / obfuscated strings / peel a blob?
Need network dig / phone-home / IOC → binary (Ghidnet, tool_surface >= 8)?
→ server_info.network first; full matrix: skill/ghidnet.md
+ → ET Open: net_rules_update (or CLI net rules update et-open / GUI Download)
→ MCP: net_connections → net_dig (execute) OR net_capture_* → net_flows → dig
- → Detect: net_rules_check → net_detect → net_dig (alert_id)
+ → Detect: net_rules_list → net_detect (omit rules = active pack) → net_dig (alert_id)
→ Pivots: net_pivots → net_dig host/ioc
- → CLI: ghidrust net … ; GUI: Network host tabs
- → Airgap: path-only net_dig; no foreign IDS
+ → CLI: ghidrust net help ; GUI: Network host tabs
+ → IDS not kernel IPS; skipped SIDs are honest; live NIC may return privilege_required
+ → Airgap: path-only net_dig; ET from_archive; no Suricata/Npcap crates
Need GPU decompile at a VA?
→ gpu-decompile <path> --addr HEX (metrics JSON; .gdecomp opaque)
@@ -485,9 +490,9 @@ Requires **`tool_surface >= 3`** (prefer **`>= 4`** for bounded disasm / `get_ca
| `process_scan` / `process_watch_expr` / `process_vtable_probe` / `process_export_snapshot` | session / aob / expr / addr | Live data discovery (`tool_surface >= 7`) |
| `net_dig` / `net_playbook` | path / pid / host / ioc / alert_id / execute / live | Dig playbooks + closed-loop (`tool_surface >= 8`; see [ghidnet.md](ghidnet.md)) |
| `net_connections` / `net_owners` | pid / path / proto / max | Host socket → owner (airgap refuses) |
-| `net_ifaces` / `net_capture_start` / `_stop` / `_status` / `net_flows` | iface / filter / session_id / replay_path | Capture sessions (airgap refuses) |
-| `net_detect` / `net_alerts` | pcap / rules / max | GNR detect + alert queue |
-| `net_rules_list` / `net_rules_load` / `net_rules_check` | path | GNR pack list/load/check (`rules/ghidrust-minimal.rules`) |
+| `net_ifaces` / `net_capture_start` / `_stop` / `_status` / `net_flows` | iface / filter / session_id / replay_path | ghidnetcap live or replay (airgap refuses; live `privilege_required`) |
+| `net_detect` / `net_alerts` | pcap / optional `rules` / max | GNR/ET detect; omit `rules` → active pack + minimal; IDS records only |
+| `net_rules_list` / `net_rules_load` / `net_rules_check` / `net_rules_update` | path / `from_archive` / `url` / `et_version` / `dest` | List/check packs; one-click ET Open install (same as CLI/GUI) |
| `net_pivots` | pcap | DNS / TLS SNI / HTTP / SMB pivots |
| `net_job_get` | job_id | **Stub** — do not rely on async dig jobs yet |
@@ -672,6 +677,7 @@ Add `--json` for structured stdout.
| Tree | `ghidrust tree <path> [--max-depth N] [--ext LIST] [--name GLOB]` |
| Artifact | `ghidrust artifact get\|query\|list …` |
| Process (Windows) | `ghidrust process list\|attach\|launch\|resume\|detach\|modules\|read\|resolve\|regions\|break\|continue\|wait\|step\|threads\|regs\|stack\|scan\|watch\|vtable\|snapshot …` |
+| **Ghidnet** | `ghidrust net help` — `dig\|playbook\|connections\|owners\|ifaces\|capture\|flows\|detect\|alerts\|rules\|pivots` (ET Open: `net rules update et-open`; detect default = active pack; see [ghidnet.md](ghidnet.md)) |
| IL2CPP | `ghidrust il2cpp meta\|map\|touch-map\|stubs\|icalls …` (`--baseline` / `--meta-sections`; see `docs/IL2CPP.md`) |
| Unity inventory | `ghidrust unity-inventory <game-dir>` |
| RTTI catalog | `ghidrust rtti <path> [--filter\|--name\|--exact] [--match MODE]` |
@@ -742,6 +748,15 @@ ghidrust process watch SESSION "module.exe+0x1234->*+0x10"
ghidrust process read SESSION --addr LIVE_VA --size 64 --json
ghidrust process detach SESSION
+# Ghidnet / ET Open / IDS (tool_surface >= 8; see skill/ghidnet.md)
+ghidrust net help
+ghidrust net rules update et-open --json
+ghidrust net rules list --json
+ghidrust net detect --payload-file fixtures/et-http-gate.bin --json
+ghidrust net connections --json
+ghidrust net ifaces --json
+ghidrust net dig --path PATH --execute --json
+
# Unity / IL2CPP
ghidrust unity-inventory GAME_DIR --json
ghidrust il2cpp touch-map --meta GAME_DIR/*_Data/il2cpp_data/Metadata/global-metadata.dat --filter Camera --json
@@ -883,6 +898,7 @@ Guardrails to respect:
| Progress | One analyzer per frame |
| Project tree | Open / Analyze / Delete |
| Decompiler pane | Placeholder / GPU dump via CLI preferred |
+| **Network host** | Connections / Capture / Alerts / Rules / Dig. Rules: **Download / update ET Open**. Detect uses active pack. Live capture: ghidnetcap (`privilege_required` if unprivileged). |
---
@@ -907,4 +923,6 @@ ghidrust il2cpp stubs --binary fixtures/il2cpp/il2cpp_stub_lab.pe --filter Camer
ghidrust decompile fixtures/il2cpp/il2cpp_stub_lab.pe --addr 0x140001000 --follow-stub --json
ghidrust bytes fixtures/il2cpp/il2cpp_stub_lab.pe --addr 0x140001000 --count 32 --json
ghidrust strings fixtures/il2cpp/meta_v31.dat --raw --filter Camera --match token --limit 5 --json
+ghidrust net help
+ghidrust net rules list --json
```
diff --git a/skill/ghidnet.md b/skill/ghidnet.md
index f021662..eec396d 100644
--- a/skill/ghidnet.md
+++ b/skill/ghidnet.md
@@ -1,16 +1,19 @@
# Ghidnet — native network investigation (`tool_surface >= 8`)
-In-tree attribute → detect → dig. No foreign capture/IDS products. Prefer MCP or GUI for multi-step sessions (`session_id` is process-local; CLI one-shot cannot reuse it).
+In-tree **attribute → detect → dig**. No Suricata binary, no `suricata-update`, no Npcap crate, no vendored ET tarball. Prefer MCP or GUI for multi-step sessions (`session_id` is process-local; CLI one-shot cannot reuse it). Check `server_info.network` (`native`, `wave`, `caps` include `et-open` / `ids`, `capture`).
## Surfaces
| Surface | Entry |
|---------|--------|
-| MCP | `net_*` tools; check `server_info.network` (`native`, `wave`, `caps`, `capture`) |
-| CLI | `ghidrust net dig\|playbook\|connections\|owners\|ifaces\|capture\|flows\|detect\|alerts\|rules\|pivots … [--json]` |
+| MCP | `net_*` tools; `net_rules_update` for ET Open |
+| CLI | `ghidrust net help` — `dig\|playbook\|connections\|owners\|ifaces\|capture\|flows\|detect\|alerts\|rules\|pivots … [--json]` |
| GUI | **Network** menu / **Window → Network** — tabs Connections \| Capture \| Alerts \| Rules \| Dig |
| Helper bin | `target/release/ghidrust-netcap` |
-| Rules pack | `rules/ghidrust-minimal.rules` (default for detect / GUI Rules **Check**) |
+| Bundled GNR | `rules/ghidrust-minimal.rules` (always merged into the active pack) |
+| Active pack | `GHIDRUST_RULES_DIR` or `%APPDATA%/ghidrust/rules` / `~/.ghidrust/rules` (`et-open/<version>/` + `active.json`) |
+
+CLI, GUI **Download / update ET Open**, and MCP `net_rules_update` share **`install_et_open`**. Detect without an explicit rules path uses **`load_active_ruleset`** (ET pack + minimal).
## MCP tools
@@ -20,28 +23,62 @@ In-tree attribute → detect → dig. No foreign capture/IDS products. Prefer MC
| `net_playbook` | same hint fields | Plan only (MCP chain shape) |
| `net_connections` | `pid`, `path`, `proto`, `max` | Socket → owner; airgap refuses |
| `net_owners` | `pid` | Owner detail |
-| `net_ifaces` | — | Interface list (loopback/replay stubs ok) |
-| `net_capture_start` | `iface`, `filter`, `pid`, `path`, `out`, `replay_path` | Returns `session_id` |
+| `net_ifaces` | — | OS adapters (ghidnetcap) + replay |
+| `net_capture_start` | `iface`, `filter`, `pid`, `path`, `out`, `replay_path` | Returns `session_id`. Live: fail-closed `privilege_required` |
| `net_capture_stop` / `net_capture_status` | `session_id` | |
| `net_flows` | `session_id`, `max` | Attributed flows |
-| `net_detect` | `pcap`, `rules` | GNR → alerts |
+| `net_detect` | `pcap`, optional `rules` | Omit `rules` → active ET pack + minimal. IDS records only |
| `net_alerts` | `max` | Severity queue |
-| `net_rules_list` | — | Loaded/default pack summary |
-| `net_rules_load` / `net_rules_check` | `path` | Compile-check GNR |
+| `net_rules_list` | — | `packs.minimal` / `packs.active`, `loaded`, `skipped`, `skip_reasons` |
+| `net_rules_update` | `from_archive`, `url`, `et_version`, `dest` | Fetch/install ET Open; activate pack |
+| `net_rules_load` / `net_rules_check` | `path` | Compile-check one file (`rule_count`, `skipped`) |
| `net_pivots` | `pcap` | DNS / TLS SNI / HTTP / SMB fields |
| `net_job_get` | `job_id` | **Stub** today (`status: unknown`) — do not rely on async jobs |
+## ET Open
+
+Proofpoint Emerging Threats Open rules are compiled **as written**. Unimplemented keywords skip that SID (honest `skipped` count) — do not rewrite ET text.
+
+```bash
+ghidrust net rules update et-open --json
+ghidrust net rules update et-open --from-archive fixtures/et-open-mini.tar.gz --et-version mini --dest DIR --json
+ghidrust net rules list --json
+```
+
+MCP: `net_rules_update` `{}` (online) or `{ "from_archive": "…", "et_version": "mini", "dest": "…" }`. GUI: Rules tab **Download / update ET Open**.
+
+Default fetch: `https://rules.emergingthreats.net/open` (`emerging.rules.tar.gz` + `.md5`, version `7.0.3` unless `et_version` set). Optional `et_code` is an ET Pro oink — never log it.
+
+## Detect / IDS vs IPS
+
+Actions `alert`, `pass`, and `drop` are **recorded as alerts**. `drop` is not a kernel/NFQUEUE/WFP block. Inline block stays off unless `GHIDRUST_NET_INLINE=1`.
+
+**Loaded:** `content` / `pcre`, `nocase`, `distance`/`within` (cursor = previous hit end), sticky buffers `http.uri`/`http.host`/`http.method`/`http.header` / `dns.query` / `tls.sni`, `flow:established|to_server|to_client`, `$HOME_NET` / `$HTTP_SERVERS` / CIDR.
+
+**Skipped SIDs:** `flowbits`/`xbits`/`flowint`, `ja3`/`ja3s`, `lua`, `threshold`/`detection_filter`, `file-data`, `byte_test`/`byte_jump`, unknown `http.*`/`tls.*` buffers.
+
+```bash
+ghidrust net detect --pcap capture.grncap --json
+ghidrust net detect --payload-file fixtures/et-http-gate.bin --json
+ghidrust net detect --pcap capture.grncap --rules rules/ghidrust-minimal.rules --json
+```
+
+## Capture (ghidnetcap)
+
+`net ifaces` lists real OS adapters plus `replay`. Live open uses Windows NPF `CreateFile` when a driver is present, else `SIO_RCVALL` raw sockets; Unix packet sockets. Cargo does not link Npcap. Unprivileged live open returns `privilege_required` — never empty fabricated frames. `GHIDRUST_NET_LIVE_STUB=1` errors on live open (tests only). Prefer `replay_path` in CI.
+
## Workflows
1. **Connections → Dig:** `net_connections` → pick remote/image → `net_dig` with `path` + `host`/`ioc` + `execute`.
2. **Capture → Dig:** `net_ifaces` → `net_capture_start` (`replay_path` in CI) → `net_flows` → `net_capture_stop` → dig owner `path`.
-3. **Detect → Dig:** `net_rules_check` on pack → `net_detect` → `net_dig` with `alert_id` (+ `path` if alert has no owner).
+3. **ET Open → Detect → Dig:** `net_rules_update` → `net_rules_list` (inspect `skipped`) → `net_detect` (no `rules`) → `net_dig` with `alert_id` (+ `path` if alert has no owner).
4. **Pivots → Dig:** `net_pivots` → feed DNS/SNI/HTTP/SMB host into `net_dig` `host`/`ioc`.
-5. **GUI:** Connections/Capture **Dig** → Dig tab **Compile**/**Execute** → **Open in Listing**. Capture **Export path** reveals `out_path`. Host header / Help→About show NetworkInfo. **Inline block** disabled unless `GHIDRUST_NET_INLINE=1|true`.
+5. **GUI:** Rules **Download / update ET Open**; Connections/Capture **Dig** → Dig tab **Compile**/**Execute** → **Open in Listing**. Capture **Export path** reveals `out_path`. **Inline block** disabled unless `GHIDRUST_NET_INLINE=1|true`.
## Honesty
- Never invent hosts/IOCs; `pid_confidence=unknown` is not ground truth.
-- Airgap: refuses `net_connections` / `net_owners` / `net_ifaces` / `net_capture_*` / `net_flows` / `net_detect`; path-only `net_dig` / `net_playbook` allowed.
-- Live NIC may be stub/replay-first; prefer `replay_path` for proofs.
-- Do not install Suricata/Wireshark/etc. as a substitute — engines are `ghidrust-net-*`.
+- Airgap: refuses `net_connections` / `net_owners` / `net_ifaces` / `net_capture_*` / `net_flows` / `net_detect`; path-only `net_dig` / `net_playbook` allowed. ET install in airgap: `from_archive` only.
+- Do not install Suricata/Wireshark/Npcap crates as a substitute — engines are `ghidrust-net-*`.
+- Do not claim Suricata-complete matching or kernel IPS. Empty detect after a large `skipped` count is honest, not a silent miss of those SIDs.
+- `net_job_get` is a stub.