|
| 1 | +# ptracehook Agent Notes |
| 2 | + |
| 3 | +Last updated: 2026-02-09 |
| 4 | + |
| 5 | +## 1) Project Positioning |
| 6 | + |
| 7 | +- Crate: `ptracehook` |
| 8 | +- Status: **new scaffold / design-first** |
| 9 | +- Goal: build a dedicated out-of-process hook framework on top of `ptrace`, focusing first on Linux `x86_64`. |
| 10 | +- Relationship to `sighook`: **complementary, not replacement**. |
| 11 | + - `sighook`: in-process signal/trap patching (BRK/INT3 handler inside target process) |
| 12 | + - `ptracehook`: external tracer model (controller process drives target execution) |
| 13 | + |
| 14 | +### Why separate crate |
| 15 | + |
| 16 | +- Semantic boundary is clear: `sighook` means signal-based in-process hook. |
| 17 | +- Runtime model, failure modes, and API ergonomics differ significantly (`fork/attach/waitpid` loop vs in-process callback). |
| 18 | +- Keeping them separate reduces conceptual coupling and prevents platform-specific complexity leakage. |
| 19 | + |
| 20 | +## 2) Ecosystem Survey (crates.io quick scan) |
| 21 | + |
| 22 | +The following crates already exist and should be treated as references or potential building blocks: |
| 23 | + |
| 24 | +- `pete` (`0.13.0`): friendly ptrace wrapper. |
| 25 | +- `ptrace-do` (`0.1.4`): featureful ptrace interaction library. |
| 26 | +- `ptrace` (`0.1.2`): low-level POSIX ptrace bindings. |
| 27 | +- `udbg` (`0.3.1`): broader debugging/memory-hacking framework. |
| 28 | + |
| 29 | +### Gap assessment |
| 30 | + |
| 31 | +There are ptrace crates, but there is no obvious crate with a `sighook`-like **hook-centric** API contract (breakpoint registration + callback action contract + deterministic hook event loop tuned for RE/CTF workflows). |
| 32 | + |
| 33 | +Therefore, `ptracehook` is still justified as: |
| 34 | + |
| 35 | +- a focused hook abstraction layer, |
| 36 | +- with explicit behavior contracts for breakpoint lifecycle, |
| 37 | +- and stable, compact API for scripted reverse workflows. |
| 38 | + |
| 39 | +## 3) Scope and Non-goals (MVP) |
| 40 | + |
| 41 | +### MVP scope (v0.1 -> v0.2) |
| 42 | + |
| 43 | +- Linux `x86_64` only. |
| 44 | +- Spawn and attach modes. |
| 45 | +- Software breakpoint (`int3`) management. |
| 46 | +- Restore-original -> optional single-step -> reinsert flow. |
| 47 | +- Register read/write (`user_regs_struct`-aligned abstraction). |
| 48 | +- Remote memory read/write helpers. |
| 49 | +- Event loop with callback-based hook actions. |
| 50 | + |
| 51 | +### Explicit non-goals (MVP) |
| 52 | + |
| 53 | +- Windows/macOS backend. |
| 54 | +- Hardware breakpoint abstraction. |
| 55 | +- Full debugger feature parity (symbol server, DWARF stepping, source-level UI). |
| 56 | +- Thread-wide advanced scheduling policy beyond basic correctness. |
| 57 | + |
| 58 | +## 4) Public API Draft (current scaffold) |
| 59 | + |
| 60 | +Current scaffold exports these key types from `src/api.rs`: |
| 61 | + |
| 62 | +- `SessionBuilder` |
| 63 | + - `spawn(path)` |
| 64 | + - `attach(pid)` |
| 65 | + - `arg(...)`, `args(...)` |
| 66 | + - `options(...)` |
| 67 | + - `build() -> Result<TraceSession, PtraceHookError>` |
| 68 | +- `TraceSession` |
| 69 | + - `add_breakpoint(spec, callback) -> Result<BreakpointId, ...>` |
| 70 | + - `remove_breakpoint(id) -> Result<(), ...>` |
| 71 | + - `run() -> Result<TraceExit, ...>` |
| 72 | + - `read_bytes(...)`, `write_bytes(...)` |
| 73 | + - `get_regs()`, `set_regs(...)` |
| 74 | +- `BreakpointSpec` |
| 75 | + - `address`, `mode`, `name` |
| 76 | +- `HookCallback` / `HookContext` / `HookAction` |
| 77 | +- `RegistersX86_64` |
| 78 | + |
| 79 | +### Hook action contract |
| 80 | + |
| 81 | +`HookAction` is designed to support deterministic control flow decisions: |
| 82 | + |
| 83 | +- `Continue` |
| 84 | +- `ContinueWithSignal(i32)` |
| 85 | +- `SingleStepThenContinue` |
| 86 | +- `Detach` |
| 87 | +- `Kill` |
| 88 | + |
| 89 | +### Breakpoint mode contract |
| 90 | + |
| 91 | +- `ExecuteOriginal`: run original instruction (restore byte + single-step + reinsert trap). |
| 92 | +- `SkipOriginal`: callback fully controls state transition; default path skips original instruction semantics. |
| 93 | + |
| 94 | +## 5) Internal Architecture Plan |
| 95 | + |
| 96 | +Target module split (planned, not fully implemented): |
| 97 | + |
| 98 | +- `src/lib.rs`: public exports and compile gates. |
| 99 | +- `src/session.rs` (or current `api.rs` split later): session lifecycle and state container. |
| 100 | +- `src/event_loop.rs`: wait/dispatch loop, signal pass-through rules. |
| 101 | +- `src/breakpoint.rs`: install/remove/reinsert breakpoint logic. |
| 102 | +- `src/memory.rs`: `PTRACE_PEEKDATA/POKEDATA` helpers with alignment-safe reads/writes. |
| 103 | +- `src/regs.rs`: register mapping and conversions. |
| 104 | +- `src/error.rs`: typed error model. |
| 105 | + |
| 106 | +### State model requirements |
| 107 | + |
| 108 | +At minimum, `TraceSession` should track: |
| 109 | + |
| 110 | +- launch mode (`spawn` / `attach`), |
| 111 | +- active breakpoints map (`BreakpointId -> metadata`), |
| 112 | +- original byte cache (`address -> original opcode byte`), |
| 113 | +- per-breakpoint callback registry, |
| 114 | +- stepping state (`which breakpoint is in single-step recovery`). |
| 115 | + |
| 116 | +## 6) Behavior Invariants (must keep) |
| 117 | + |
| 118 | +- Every installed breakpoint stores original first byte before writing `0xCC`. |
| 119 | +- On trap hit: |
| 120 | + 1. restore original byte, |
| 121 | + 2. set `rip = rip - 1`, |
| 122 | + 3. execute callback, |
| 123 | + 4. follow callback action, |
| 124 | + 5. if needed, single-step and reinsert trap. |
| 125 | +- Non-trap stop signals should be forwarded unless policy says otherwise. |
| 126 | +- `PTRACE_O_EXITKILL` should be enabled by default for safety. |
| 127 | +- Session teardown must attempt best-effort breakpoint restoration. |
| 128 | + |
| 129 | +## 7) Error Model Guidelines |
| 130 | + |
| 131 | +`PtraceHookError` should remain compact and user-facing: |
| 132 | + |
| 133 | +- platform incompatibility, |
| 134 | +- invalid pid/address, |
| 135 | +- ptrace syscall failure (with operation context + errno), |
| 136 | +- internal state errors (missing breakpoint, invalid lifecycle), |
| 137 | +- not-implemented placeholders during scaffold stage. |
| 138 | + |
| 139 | +Avoid leaking raw syscall details directly in API unless wrapped with actionable context. |
| 140 | + |
| 141 | +## 8) Roadmap (Detailed Plan) |
| 142 | + |
| 143 | +### Phase 0 — Scaffold (done in this commit) |
| 144 | + |
| 145 | +- Create crate. |
| 146 | +- Draft public API shape. |
| 147 | +- Add `AGENT.md` contract and implementation roadmap. |
| 148 | + |
| 149 | +### Phase 1 — Core runtime loop |
| 150 | + |
| 151 | +- Implement `spawn` mode. |
| 152 | +- Implement `add_breakpoint` + internal table. |
| 153 | +- Implement trap dispatch loop with single-thread tracee assumption. |
| 154 | +- Implement `run()` for one tracee process. |
| 155 | + |
| 156 | +### Phase 2 — Memory/regs surface |
| 157 | + |
| 158 | +- Implement `read_bytes`/`write_bytes` with unaligned handling. |
| 159 | +- Implement register get/set mapping for Linux x86_64. |
| 160 | +- Validate callback-controlled register mutation. |
| 161 | + |
| 162 | +### Phase 3 — Attach mode and resilience |
| 163 | + |
| 164 | +- Implement `attach(pid)`. |
| 165 | +- Handle already-stopped tracees and signal forwarding. |
| 166 | +- Add robust detach/kill/cleanup paths. |
| 167 | + |
| 168 | +### Phase 4 — API hardening |
| 169 | + |
| 170 | +- Add richer `StopReason` reporting and optional observer hooks. |
| 171 | +- Add configurable policies (signal pass-through, auto-reinsert behavior). |
| 172 | +- Freeze MVP signatures for `0.2.x`. |
| 173 | + |
| 174 | +### Phase 5 — Testing and examples |
| 175 | + |
| 176 | +- Add integration demos: |
| 177 | + - dump-only hook, |
| 178 | + - bypass-style control-flow redirection, |
| 179 | + - oracle-style byte-by-byte solver flow. |
| 180 | +- Add regression tests for breakpoint restore and reinsert logic. |
| 181 | + |
| 182 | +## 9) Validation Checklist |
| 183 | + |
| 184 | +Run before merge/release: |
| 185 | + |
| 186 | +```bash |
| 187 | +cargo fmt --all -- --check |
| 188 | +cargo check --all-targets |
| 189 | +cargo test |
| 190 | +cargo clippy --all-targets -- -D warnings |
| 191 | +``` |
| 192 | + |
| 193 | +Linux x86_64 runtime verification (manual during MVP): |
| 194 | + |
| 195 | +- spawn + breakpoint hit, |
| 196 | +- attach + breakpoint hit, |
| 197 | +- callback register mutation observed, |
| 198 | +- non-trap signal pass-through, |
| 199 | +- teardown restores original byte. |
| 200 | + |
| 201 | +## 10) Interop Guidance with sighook |
| 202 | + |
| 203 | +- Keep crate boundaries explicit (`sighook` vs `ptracehook`). |
| 204 | +- Shared helper code should move to a separate utility crate only when duplication becomes meaningful. |
| 205 | +- Future optional umbrella crate (`hookkit` style) can re-export both backends, but backends remain independent. |
| 206 | + |
| 207 | +## 11) Current Scaffold Limitations |
| 208 | + |
| 209 | +- `TraceSession::run` and many runtime methods currently return `NotImplemented`. |
| 210 | +- The scaffold is intentionally API-first to stabilize contracts before low-level ptrace engine work. |
| 211 | +- Treat this crate as design baseline for the next implementation iteration. |
0 commit comments