|
| 1 | +# Architecture |
| 2 | + |
| 3 | +## System Overview |
| 4 | + |
| 5 | +``` |
| 6 | +Claude Code / Claude Desktop |
| 7 | + | |
| 8 | + | MCP (stdio) |
| 9 | + v |
| 10 | ++------------------+ +------------------------------------+ |
| 11 | +| wairz-mcp |---->| FastAPI Backend | |
| 12 | +| (MCP server) | | | |
| 13 | +| 60+ tools | | Services: firmware, analysis, | |
| 14 | ++------------------+ | emulation, fuzzing, sbom, uart | |
| 15 | + | | |
| 16 | + | Ghidra headless - QEMU - AFL++ | |
| 17 | + +-----------+--------------------------| |
| 18 | + | |
| 19 | ++--------------+ +----------------+----------------+ |
| 20 | +| React SPA |--->| PostgreSQL | Redis | |
| 21 | +| (Frontend) | | | | |
| 22 | ++--------------+ +----------------+----------------+ |
| 23 | +
|
| 24 | +Optional: |
| 25 | + wairz-uart-bridge.py (host) <-- TCP:9999 --> Docker backend |
| 26 | +``` |
| 27 | + |
| 28 | +## Tech Stack |
| 29 | + |
| 30 | +| Layer | Technology | |
| 31 | +|-------|------------| |
| 32 | +| Frontend | React 19, Vite, TypeScript, Tailwind CSS, shadcn/ui | |
| 33 | +| Code Viewer | Monaco Editor | |
| 34 | +| Component Graph | ReactFlow + Dagre | |
| 35 | +| Terminal | xterm.js | |
| 36 | +| State Management | Zustand | |
| 37 | +| Backend | Python 3.12, FastAPI, SQLAlchemy 2.0 (async), Alembic | |
| 38 | +| Database | PostgreSQL 16 | |
| 39 | +| Cache | Redis 7 | |
| 40 | +| Firmware Extraction | binwalk, sasquatch, jefferson, ubi_reader, cramfs-tools | |
| 41 | +| Binary Analysis | radare2 (r2pipe), pyelftools | |
| 42 | +| Decompilation | Ghidra 11.3.1 (headless) with custom analysis scripts | |
| 43 | +| Emulation | QEMU user-mode + system-mode (ARM, MIPS, MIPSel, AArch64) | |
| 44 | +| Fuzzing | AFL++ with QEMU mode | |
| 45 | +| SBOM | CycloneDX, NVD API (nvdlib) | |
| 46 | +| UART | pyserial (host-side bridge) | |
| 47 | +| AI Integration | MCP (Model Context Protocol) | |
| 48 | +| Containers | Docker + Docker Compose | |
| 49 | + |
| 50 | +## Project Structure |
| 51 | + |
| 52 | +``` |
| 53 | +wairz/ |
| 54 | +├── backend/ |
| 55 | +│ ├── app/ |
| 56 | +│ │ ├── main.py # FastAPI application |
| 57 | +│ │ ├── config.py # Settings (pydantic-settings) |
| 58 | +│ │ ├── database.py # Async SQLAlchemy engine/session |
| 59 | +│ │ ├── mcp_server.py # MCP server with dynamic project switching |
| 60 | +│ │ ├── models/ # SQLAlchemy ORM models |
| 61 | +│ │ ├── schemas/ # Pydantic request/response schemas |
| 62 | +│ │ ├── routers/ # REST API endpoints |
| 63 | +│ │ ├── services/ # Business logic |
| 64 | +│ │ ├── ai/ # MCP tool registry + 60+ tool implementations |
| 65 | +│ │ │ └── tools/ # Organized by category |
| 66 | +│ │ └── utils/ # Path sandboxing, output truncation |
| 67 | +│ ├── alembic/ # Database migrations |
| 68 | +│ └── pyproject.toml |
| 69 | +├── frontend/ |
| 70 | +│ ├── src/ |
| 71 | +│ │ ├── pages/ # Route pages |
| 72 | +│ │ ├── components/ # UI components |
| 73 | +│ │ ├── api/ # API client functions |
| 74 | +│ │ ├── stores/ # Zustand state management |
| 75 | +│ │ └── types/ # TypeScript types |
| 76 | +│ └── package.json |
| 77 | +├── ghidra/ |
| 78 | +│ ├── Dockerfile # Ghidra headless container |
| 79 | +│ └── scripts/ # Custom Java analysis scripts |
| 80 | +├── emulation/ |
| 81 | +│ ├── Dockerfile # QEMU container |
| 82 | +│ └── scripts/ # Emulation helper scripts |
| 83 | +├── fuzzing/ |
| 84 | +│ └── Dockerfile # AFL++ container with QEMU mode |
| 85 | +├── scripts/ |
| 86 | +│ └── wairz-uart-bridge.py # Host-side UART serial bridge |
| 87 | +├── docker-compose.yml |
| 88 | +├── launch.sh |
| 89 | +├── .env.example |
| 90 | +└── CLAUDE.md |
| 91 | +``` |
| 92 | + |
| 93 | +## Key Design Decisions |
| 94 | + |
| 95 | +### MCP as the AI Interface |
| 96 | + |
| 97 | +Rather than embedding an LLM in the backend, Wairz exposes analysis tools through MCP. This means: |
| 98 | + |
| 99 | +- Users bring their own Claude subscription (no API keys stored server-side) |
| 100 | +- The AI assistant runs in the user's Claude Code or Claude Desktop |
| 101 | +- Tools are composable — Claude can chain them together for complex analysis workflows |
| 102 | + |
| 103 | +### Isolated Execution Environments |
| 104 | + |
| 105 | +Firmware binaries are never executed on the host. All execution happens in isolated Docker containers: |
| 106 | + |
| 107 | +- **Emulation** — QEMU runs inside a dedicated container with resource limits |
| 108 | +- **Fuzzing** — AFL++ runs in a separate container |
| 109 | +- Both are on an isolated Docker network |
| 110 | + |
| 111 | +### Async Everything |
| 112 | + |
| 113 | +The backend is fully async: |
| 114 | + |
| 115 | +- SQLAlchemy async sessions with asyncpg |
| 116 | +- `asyncio.create_subprocess_exec` for running Ghidra, binwalk, etc. |
| 117 | +- Background tasks for long-running operations (firmware unpacking) |
| 118 | +- Non-blocking API endpoints |
| 119 | + |
| 120 | +### Caching Strategy |
| 121 | + |
| 122 | +Analysis results are cached aggressively: |
| 123 | + |
| 124 | +- **Ghidra decompilation** — Cached by binary hash + function name in PostgreSQL |
| 125 | +- **SBOM data** — Cached after first generation |
| 126 | +- **Firmware metadata** — Extracted once during unpacking |
| 127 | + |
| 128 | +### Security Boundaries |
| 129 | + |
| 130 | +- **Path traversal prevention** — All file access validated against the extracted firmware root via `sandbox.py` |
| 131 | +- **Output truncation** — MCP tool outputs capped at 30KB to prevent client issues |
| 132 | +- **Resource limits** — Emulation and fuzzing containers have memory and CPU limits |
0 commit comments