Skip to content

About

Diff harness = what you asked for (Prompted), what it thought (it's reasoning), what it did (execution and attempts), what tools it used (tool calls and execs), and what it responded back to you (did it reply honestly, fabricate, hallucinate, expand, correct, divulge, etc...)

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

cc-harness — MITM Instrumentation Harness

Diff harness= what you asked for (Prompted), what it thought (it's reasoning), what it did (execution and attempts), what tools it used (tool calls and execs), and what it responded back to you (did it reply honestly, fabricate, hallucinate, expand, correct, divulge, etc...).

Captures Claude Code activity across three layers, then surfaces prompt-vs-execution mismatches.

Important READ THIS:

This is not intended for baremetal usage, it explicitly captures env in txt, it explicitly sets a ca-cert, it explicitly logs and tracks api, usage, straces, and more.

The post-refusal-fabrication corpus and any new finding work involves running prompts designed to make claude misbehave: fabricate tool output, ignore refusals, attempt sandbox escapes, write outside the workdir, etc. On a VM those attempts hit a throwaway filesystem and network. On baremetal they hit your real $HOME, your real keychain/agent, your real network. The harness is exactly the tool you'd use to trigger the worst-case behavior of claude — so running it on the machine you actually use is not smart

If you ignored the above, here's how to remove the now global cert that you hopefully didn't push yet:

sudo rm /etc/ca-certificates/trust-source/anchors/mitmproxy.pem && sudo trust extract-compat && sudo update-ca-trust

You are 100% responsible for knowing how and what this tool does. What it is used for. The potential exposure and telemitry that this tool may leave if you use it on your native machine

I am in no way responsible for your lack of reading, understanding, or awareness.

Use at your own risk and experience level

This tool is specifically designed to be used in disposable VM envs for a reason, if you did not rtfm, stop, read

Seriously

What it captures

Layer Tool Output
API channel mitmproxy + addon api.jsonl — every request/response to api.anthropic.com
OS syscalls strace -f strace.log — execve / openat / connect from claude + children
Terminal / CLI script(1) or stream-json tty.log (interactive) or stdout.jsonl (headless)

The triangulation: (what the model emitted) vs (what was actually run) vs (what the user was shown). Any pairwise mismatch is a candidate finding — manual review is the gate, never diff.py's output on its own.

Install (run once in VM)

cd ~/cc-harness
./install.sh           # system deps + mitm CA install
./install.sh --dev     # also bootstrap .venv with pytest / ruff / mypy

Installs mitmproxy, strace, jq, util-linux (for script), python deps. Generates and trusts the mitmproxy CA cert system-wide (required so claude's child processes also trust the injected CA). Idempotent.

The harness itself ships as standalone bash + Python scripts — nothing to pip install for normal use. The .venv from --dev exists only for running the test suite and linters.

Run a session

./cc-harness start <session-name>                       # interactive
./cc-harness start --headless --prompt "..." <name>     # headless one-shot

cc-harness is the only entry point users should call; start-session.sh is the underlying script. What happens:

  1. Mints a session id (lib/session_id.sh) and creates logs/<name>-<ts-uuid8-pid>/.
  2. Allocates a free TCP port from the bounded range 18800–18999 (lib/port_alloc.py), writes it to .mitm.port.
  3. Spawns mitmdump on that port with the Anthropic addon (tools/mitm-anthropic.py); PID lands in .mitm.pid. The script polls ss -lntp to confirm mitmdump bound the port before continuing (closes the port-allocator TOCTOU under swarm runs).
  4. Exports HTTPS_PROXY + NODE_EXTRA_CA_CERTS so claude routes through the proxy and trusts the injected CA.
  5. Launches claude under strace -f plus either script(1) (interactive) or direct stdin/stdout pipes (headless --print).
  6. Exit claude → trap-driven cleanup tears down mitmdump and seals the log dir.

Multiple sessions run in parallel safely: each gets its own port, its own findings.db, and its own per-session .mitm.pid. Cleanup matches PIDs by /proc/<pid>/cmdline so cc-harness stop only kills its own mitmdump processes.

Analyze

After a session, run the full pipeline through the dispatcher:

./cc-harness analyze logs/<session-dir>/

This runs the analyzer chain in order:

  1. analyze/parse-api.py — extracts every tool_use from api.jsonl.
  2. analyze/parse-strace.py — extracts execve/openat/connect from strace.log.
  3. analyze/diff.py — pairs tool_uses to execs via score-sorted bipartite assignment; emits mismatches.
  4. analyze/focus-areas.py — flags heredoc/eval/background/curl-pipe-sh and other suspicious shell patterns.
  5. analyze/parse-stdout.py — headless only; cross-checks the wire (API) view against the stream (CLI) view to surface CLI-side rewrites or fabrications.
  6. analyze/index.py — ingests every JSONL into a per-session findings.db (SQLite).

Then:

./cc-harness list                  # per-session counts (execs / refused / mismatch / unmatched)
./cc-harness findings <session>    # dump analysis/findings.json
./cc-harness compare <a> <b>       # side-by-side counts via ATTACH DATABASE
./cc-harness scrub <session>       # produce *-scrubbed/ sibling for external sharing

Output structure

logs/<name>-<ts-uuid8-pid>/
├── api.jsonl              # raw mitmproxy capture of Anthropic API
├── strace.log             # raw strace output (execve/openat/connect)
├── tty.log                # raw terminal capture (interactive mode)
├── tty.timing             # script(1) timing file (asciinema replay)
├── stdout.jsonl           # claude --output-format stream-json (headless only)
├── stderr.log             # claude stderr (headless only)
├── prompt.txt             # the prompt that drove the headless run
├── claude-exit.txt        # claude's exit code (headless only)
├── env.txt                # full env snapshot at session start (chmod 600)
├── claude-version.txt     # Claude Code / Node / npm / uname / os-release
├── mitm-stderr.log        # mitmdump stderr
├── .mitm.pid              # mitmdump PID (so stop-session can find it)
├── .mitm.port             # allocated port (so cc-harness list can report it)
├── findings.db            # per-session SQLite (built atomically by index.py)
└── analysis/              # populated by `cc-harness analyze`
    ├── tool-uses.jsonl        # API-side tool_use blocks (parse-api.py)
    ├── model-tool-uses.jsonl  # CLI-stream tool_use blocks (parse-stdout.py)
    ├── tool-results.jsonl     # tool_result content + post_refusal_text
    ├── execs.jsonl            # parsed execve calls
    ├── file-writes.jsonl      # openat calls with write flags
    ├── network.jsonl          # connect() syscalls
    ├── mismatches.jsonl       # paired tool_use ↔ exec rows (one per call)
    ├── focus-flags.jsonl      # focus-area pattern hits (structured)
    ├── diff-report.md         # human-readable mismatch + truncation report
    ├── focus-flags.md         # human-readable focus-pattern report
    ├── stream-vs-wire.md      # headless-only: CLI vs API divergence
    └── findings.json          # consolidated machine-readable export

After cc-harness scrub, a sibling *-scrubbed/ directory holds a redacted copy with api.jsonl, env.txt, strace.log, tty.log and other identity-leaking files dropped, and the rest scrubbed of usernames, hostnames, paths, and secret tokens (Anthropic, OpenAI, GitHub, AWS, Google OAuth, JWT, basic-auth URLs, generic Bearer). See the docstring at the top of analyze/scrub.py for the full drop/keep list. Always run this before sharing artifacts externally.

Replay a session

The tty.log + tty.timing pair can be replayed:

scriptreplay --timing=logs/<session>/tty.timing logs/<session>/tty.log

Useful for re-watching exactly what the user saw without re-running claude.

Known limitations

  • mitmproxy intercepts TLS via injected CA. If a future Claude Code release pins certs, the API capture stops working. Surface this as a finding if it happens — but first, check NODE_EXTRA_CA_CERTS.
  • strace is noisy. Filters are in start-session.sh; widen them if you need more visibility. -s 4096 truncates very long string args; the parser detects strace's "..."... marker and diff.py classifies truncation-induced disagreement as strace_truncated rather than mismatch.
  • Correlation is heuristic. diff.py pairs tool_uses to execs via score-sorted bipartite assignment over a [ts−1s, ts+30s] window: word-bounded content match (100) > truncated-prefix match (75) > first-token (binary basename) match (50). Score-0 pairs are reported as unmatched — never silently matched to nearest-in-time. Manual review of every flagged row is still required before claiming a finding.
  • MCP tool calls ride the same API channel — parse-api.py is tool-agnostic and captures them, but diff.py filters to Bash-class tool names when correlating against execve. MCP / slash-command correlation against process activity is a future extension.

Workflow

After each meaningful session:

  1. cc-harness analyze logs/<session>/ to run the full pipeline.
  2. Open diff-report.md, focus-flags.md, and (headless) stream-vs-wire.md.
  3. Anything suspicious → copy raw evidence into a working _findings.md using findings-template.md.
  4. cc-harness scrub <session> before sharing the artifact bundle.

Extending

  • New mitmproxy addon hooks: edit tools/mitm-anthropic.py.
  • New focus patterns: add detectors to analyze/focus-areas.py.
  • New diff heuristics or scoring tiers: edit analyze/diff.py.
  • New tool families to correlate: parse-api.py already captures them; extend the BASH_TOOL_NAMES filter (or replace it) in diff.py.
  • Schema changes: edit analyze/schema.sql. index.py writes a fresh findings.db.tmp and os.replace()s it on success, so no migrations are needed — but bump anything downstream that reads the DB shape.
  • New scrub patterns (e.g. an extra secret regex): add to the _RE_SECRETS list in analyze/scrub.py and document the marker in SCRUBBED.md's preamble.

The codebase is intentionally small. Read it before extending.

About

Diff harness = what you asked for (Prompted), what it thought (it's reasoning), what it did (execution and attempts), what tools it used (tool calls and execs), and what it responded back to you (did it reply honestly, fabricate, hallucinate, expand, correct, divulge, etc...)

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages