Metadata-Version: 2.5
Name: rikune
Version: 2.3.3
Summary: Verified reverse-engineering core engine: static analysis, call-graph slicing, multi-backend decompilation, and micro-emulation/symbolic verification via CLI and MCP.
Project-URL: Homepage, https://github.com/Last-emo-boy/rikune
Project-URL: Repository, https://github.com/Last-emo-boy/rikune
Project-URL: Issues, https://github.com/Last-emo-boy/rikune/issues
Project-URL: Changelog, https://github.com/Last-emo-boy/rikune/blob/main/CHANGELOG.md
Author: Last-emo-boy
License-Expression: MIT
License-File: LICENSE
Keywords: angr,binary-analysis,decompiler,ghidra,mcp,reverse-engineering,rizin,unicorn
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Information Technology
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: MacOS
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Security
Classifier: Topic :: Software Development :: Disassemblers
Requires-Python: <3.14,>=3.11
Requires-Dist: capstone>=5.0.3
Requires-Dist: lief>=0.15.1
Requires-Dist: mcp>=1.9.0
Requires-Dist: pydantic>=2.10.0
Requires-Dist: rich>=13.9.0
Requires-Dist: typer>=0.15.0
Requires-Dist: unicorn>=2.1.1
Requires-Dist: yara-x>=0.12.0
Provides-Extra: dev
Requires-Dist: mypy>=1.14.0; extra == 'dev'
Requires-Dist: pytest-asyncio>=0.25.0; extra == 'dev'
Requires-Dist: pytest>=8.3.0; extra == 'dev'
Requires-Dist: ruff>=0.9.0; extra == 'dev'
Provides-Extra: symbolic
Requires-Dist: angr>=9.2.120; extra == 'symbolic'
Requires-Dist: z3-solver>=4.13.0; extra == 'symbolic'
Description-Content-Type: text/markdown

<div align="center">

# ⚡ Rikune

### Verified Reverse-Engineering Core Engine for AI Agents & Analysts

*Stop guessing what binaries do — slice, decompile, micro-emulate, solve, and prove it.*

<p>
  <a href="https://github.com/Last-emo-boy/rikune/actions/workflows/ci.yml">
    <img src="https://github.com/Last-emo-boy/rikune/actions/workflows/ci.yml/badge.svg?branch=main" alt="CI" />
  </a>
  <a href="https://github.com/Last-emo-boy/rikune/releases/latest">
    <img src="https://img.shields.io/github/v/release/Last-emo-boy/rikune?color=0ea5e9&label=release" alt="Latest Release" />
  </a>
  <a href="./pyproject.toml">
    <img src="https://img.shields.io/badge/python-3.11%20%7C%203.12%20%7C%203.13-3776ab" alt="Python 3.11+" />
  </a>
  <a href="./LICENSE">
    <img src="https://img.shields.io/badge/license-MIT-22c55e" alt="License MIT" />
  </a>
  <a href="#mcp-tool-surface-36-real-primitives">
    <img src="https://img.shields.io/badge/MCP%20Tools-36%20Real%20Primitives-8b5cf6" alt="36 MCP Tools" />
  </a>
  <a href="#verified-benchmark-suite-1010-passing">
    <img src="https://img.shields.io/badge/Benchmarks-10%2F10%20Passing-10b981" alt="10/10 Benchmarks" />
  </a>
</p>

<p>
  <strong>English</strong> · <a href="./README_zh.md">简体中文</a> ·
  <a href="./docs/ARCHITECTURE.md">Architecture</a> ·
  <a href="./SKILL.md">Agent Skill</a> ·
  <a href="./bridges/README.md">IDA / Ghidra Bridges</a> ·
  <a href="./CHANGELOG.md">Changelog</a>
</p>

</div>

---

> **v2.0+ is now `main`!** The legacy v1 TypeScript/Node monolith has been retired to the [`deprecated`](https://github.com/Last-emo-boy/rikune/tree/deprecated) branch (and frozen on tag [`v1-legacy`](https://github.com/Last-emo-boy/rikune/releases/tag/v1-legacy)).

## Why Rikune v2?

Single-tool MCP bridges (`ida-mcp`, Binary Ninja MCP, `GhidraMCP`, `r2mcp`) are remote controls for static GUI databases — leaving AI agents to **mentally guess** crypto, obfuscation, packers, and branch logic.

**Rikune v2** is a ground-up, native Python reverse-engineering engine built around a **deterministic verification loop**:

```mermaid
flowchart LR
    A[Binary / Script Intake<br/>SHA-256 Read-Only Vault] --> B[Static Triage & Forensics<br/>PE/ELF/Mach-O/WASM/.NET/APK<br/>Crypto · Go/Rust · Data Xrefs]
    B --> C[Token-Aware Graph Slice<br/>Entry ↔ API/String/Crypto Anchors<br/>Cached Rizin · Ghidra · IDE Bridge]
    C --> D[Verification Loop<br/>Unicorn Ring-3 PEB/BCrypt/Stubs<br/>SMC/OEP Unpack · Batch Decrypt<br/>API-Hash · Z3 / angr]
    D --> E[Live Overlay & Lineage<br/>Verified Renames · Types · Values<br/>Derived Child Payloads]
    E --> C
    E --> F[Two-Way Export<br/>IDA · Ghidra · Rizin<br/>Markdown · JSON · Live GUI Sync]
```

### What Makes It Different

| Pillar | What Rikune v2 Delivers |
| :--- | :--- |
| **🔬 Verification-First** | Map real PE/ELF/Mach-O segments into **Unicorn** with Win64 Ring-3 GDT (`CS=0x33`/`SS=0x2B`), TLS, `KUSER_SHARED_DATA`, synthetic `ntdll`/`kernel32`/`bcrypt` PE export tables (real SHA-256/HMAC + Win10 syscall prologues), non-invasive `hook_returns`/`hook_skips`, and automatic **SMC/OEP unpacking** with **auto-persisted IAT annotations**. |
| **🧠 Token Economics** | Never dump 5,000 functions into an LLM context. Rank high-signal code, slice shortest **Entry → Sink** paths from string, direct-IAT API, or detected crypto-constant anchors (`AES`/`RC5`/`ChaCha20`/`SHA256`/`CRC32`/`SM4`/`TEA`), and auto-spill oversized outputs to paginated artifacts. |
| **🔁 Live Overlay & Two-Way IDE Interop** | Verified renames, C prototypes/structs (`type`), and decoded strings automatically overlay onto every subsequent read — and can be exported safely to **IDA Python / Ghidra / Rizin / Markdown / JSON** or pushed directly back into live **IDA Pro / Ghidra GUI** sessions. |
| **🌳 Non-Destructive Lineage** | Unpack UPX, dump dirty Unicorn sections (`--derive-image`), carve embedded PE/ELF/ZIP/Base85+Gzip payloads, or apply `nop`/`invert_branch` patches into immutable parent→child lineage trees. |
| **🛡️ Native, Lean & Safe** | Runs natively on **macOS (Apple Silicon & Intel)**, **Linux**, and **Windows** via Python + `uv`. Built-in untrusted-binary **prompt-injection guard** in `ResponseGuard`. Optional Docker container for untrusted malware isolation. |

---

## v1 (`deprecated`) vs. v2 (`main`)

| Dimension | v1 (`deprecated` / `v1-legacy`) | v2 (`main`) |
| :--- | :--- | :--- |
| **Codebase & Architecture** | ~435k LOC TypeScript + 7.7k-line Python worker + 117 plugins | Clean, modular native Python (`uv`), zero IPC glue |
| **Tool Surface** | ~355 tools (many `plan-only` stubs) + 4 meta-discovery proxies | **32 real executable primitives** + lightweight `tool_search` + 3 Prompts + 3 Resources |
| **Persistence** | 24 SQLite tables + heartbeat lease locks | **5 clean SQLite tables** (incl. persistent `backend_cache`) + `.rzdb` cache |
| **Platform Support** | Linux-kernel / `linux/amd64` Docker only | **Native macOS (arm64/x64), Linux, Windows** + optional Docker |
| **Packaging** | Heavy multi-stage Node + Python + 21 Dockerfiles | **216 KB pure-Python wheel** + single `ghcr.io` core image |

---

## Quick Start

### 1. Install (Zero-Venv, Out-of-the-Box)

```bash
# Option A: Official PyPI via uvx / uv tool / pip (recommended, zero manual venv!)
uvx rikune status                 # Run immediately without installing
uv tool install rikune            # Install globally via uv
pip install rikune                # Or standard pip

# Optional: include Z3 symbolic constraint solving
uv tool install --with z3-solver rikune

# Option B: Standalone single-file binary (zero Python / zero venv — download & run!)
#   Linux x86_64 : https://github.com/Last-emo-boy/rikune/releases/download/v2.3.3/rikune-linux-x86_64
#   macOS arm64  : https://github.com/Last-emo-boy/rikune/releases/download/v2.3.3/rikune-macos-arm64
#   Windows x64  : https://github.com/Last-emo-boy/rikune/releases/download/v2.3.3/rikune-windows-x86_64.exe

# Verify detected backends (fast built-in, rizin, ghidra, bridge), unpackers, and verification engines
rikune status
```

<details>
<summary><strong>Option B: Docker (isolated analysis with Rizin 0.9.1 + UPX 5.3.1 pre-installed)</strong></summary>

```bash
docker pull ghcr.io/last-emo-boy/rikune:2.3.3
docker run --rm ghcr.io/last-emo-boy/rikune:2.3.3 status
```

</details>

<details>
<summary><strong>Option C: From source (development)</strong></summary>

```bash
git clone https://github.com/Last-emo-boy/rikune.git
cd rikune
uv sync --python 3.12 --extra dev
uv run rikune status
```

</details>

> **Recommended backend**: Install [`rizin`](https://rizin.re/) (`brew install rizin` on macOS) for fast `.rzdb`-cached function indexing and disassembly. For full decompilation, set `GHIDRA_HOME` or connect a live IDE session via [`bridges/`](./bridges/README.md).

---

### 2. Connect to Your AI Agent (MCP)

Add Rikune to **VS Code Copilot**, **Claude Code**, **Claude Desktop**, or **Cursor**:

```json
{
  "mcpServers": {
    "rikune": {
      "command": "rikune",
      "args": ["mcp", "--transport", "stdio"]
    }
  }
}
```

For best results, copy [`SKILL.md`](./SKILL.md) into your agent's skills directory so it automatically drives the **triage → anchor-slice → decompile → emulate/solve → annotate → export** loop.

---

### 3. CLI Essentials

Every CLI command supports `--json` for scripting and agent workflows:

```bash
# Intake & compact static triage (PE/ELF/Mach-O/WASM/.NET/APK, crypto constants, Go/Rust)
rikune open ./target.exe
rikune triage ./target.exe --json

# High-signal forensics: section-filtered strings, VA memory reader, wildcard search, data-xrefs, carving
rikune strings ./target.exe --section .rdata --exclude-exec -q "flag"
rikune mem ./target.exe 0x14001f000 --length 64 --format u64   # hexdump|hex|u32|u64|i8|i16|i32|f32|f64|ascii|utf16
rikune search-bytes ./target.exe "65 48 8b ?? 25 60 00 00 00"
rikune data-xrefs ./target.exe 0x140067760 --range 0x100
rikune carve ./dropper.py --derive

# Code navigation: ranked functions (.pdata + WASM + Go/Rust), anchor slicing, smart RIP disasm, diff
rikune functions ./target.exe
rikune slice ./target.exe --api-anchor suspicious --crypto-anchors --json
rikune decompile ./target.exe main
rikune disasm ./target.exe 0x140001bb0 --count 40
rikune diff ./v1.bin ./v2.bin --json

# Verification loop: auto-unpack to OEP + auto-persist IAT, batch decrypt, API hash, VM trace, Z3
rikune emulate ./target.exe 0x1400bc000 --stop-on-smc --smc-cross-section --derive-image unpacked.exe --max-insns 20000000
rikune emulate ./target.exe 0x140001f00 --pre-call 0x140002290 --hook-ret 0x140001bb0=0 --read-mem out:@rdx:64 --json
rikune trace ./target.exe 0x140001f00 --bp 0x140001f80 --regs all --json
rikune batch-decrypt ./target.exe emit_encoded --json
rikune api-hash ./target.exe --scan-fn 0x140509000 --json
rikune solve-z3 -v x -b 32 -a "(= (bvxor x #x5a5a5a5a) #x13371337)" --json

# Non-destructive patching, live annotations, lineage & two-way export
rikune patch ./target.exe 0x140001234 --kind invert_branch --filename patched.exe
rikune lineage ./target.exe
rikune annotate ./target.exe 0x140002290 --kind rename --value init_vm_table --verified
rikune export ./target.exe --format ida -o apply_symbols.py   # ida | ghidra | rizin | markdown | json
```

---

## MCP Tool Surface (36 Real Primitives)

All 36 tools are real executable primitives (zero `plan-only` stubs, zero hidden proxy layers), accompanied by **3 native MCP Prompts** (`triage-binary`, `decrypt-and-verify`, `diff-security-patch`) and **3 read-only MCP Resources** (`rikune://status`, `rikune://samples`, `rikune://sample/{sha256}/report.md`).

<details>
<summary><strong>Click to expand the full 36-tool matrix</strong></summary>

| Group | Tools |
| :--- | :--- |
| **Workspace & Lineage (6)** | `engine_status`, `sample_open`, `sample_list`, `sample_derive`, `sample_lineage`, `unpack_sample` |
| **Static, Business, Mobile, JS & Protocol (12)** | `static_triage`, `strings_search`, `memory_read`, `bytes_search`, `data_xrefs`, `carve_payloads`, `binary_patch`, `yara_scan`, `disassemble_bytes`, `managed_inspect`, `js_deobfuscate`, `protocol_inspect` |
| **Code, Graph & Export (8)** | `functions_list`, `call_graph_slice`, `function_decompile`, `function_xrefs`, `function_cfg`, `binary_diff`, `annotate_symbol`, `export_annotations` |
| **Verification & Crypto (7)** | `emulate_function`, `emulate_trace`, `emulate_batch_decrypt`, `resolve_api_hashes`, `solve_symbolic`, `solve_z3`, `crypto_transform` |
| **Discovery & Async (3)** | `tool_search`, `job_manage`, `artifact_read` |

</details>

---

## Live IDE Bridges (IDA Pro & Ghidra GUI)

Combine commercial decompilers with Rikune's Unicorn/Z3 verification engine:

1. Run [`bridges/ida_rikune_bridge.py`](./bridges/ida_rikune_bridge.py) inside IDA Pro or [`bridges/ghidra_rikune_bridge.py`](./bridges/ghidra_rikune_bridge.py) inside Ghidra.
2. Point Rikune at the live session:
   ```bash
   export RIKUNE_BRIDGE_URL="http://127.0.0.1:18765"
   ```
3. Decompile via `--backend bridge`, verify decryption/hashes in Unicorn, and push `[VERIFIED]` renames, types, and decoded values straight back into your GUI with `rikune export <sample> --push-bridge`. See [`bridges/README.md`](./bridges/README.md).

---

## Battle-Tested on Real CrackMes & Public 2026 RE Benchmarks

Rikune v2 has been hardened end-to-end against **8 real-world `crackmes.one` challenges** and **4 public 2026 RE benchmarks**:

- **Public Benchmarks (`AgentRE-Bench`, `CREBench`, `BinaryAudit`, `BIOPSY-Bench`)**:
  - **CREBench (`constxor` + PRNG/fragmented keys)**: Auto-detects XOR-masked AES/SM4 S-boxes & inline ChaCha20 constants statically, extracts keys via Unicorn `@arg0` reads, and recovers byte-exact verified flags (`AES-128-CBC` & `SM4-CBC`).
  - **BinaryAudit (400 KB stripped `dnsmasq`)**: `rikune xrefs` / `slice --api-anchor execl` pinpoints the exact obfuscated backdoor function (`0x2e0d0`, `dhcp_reply`) in <1s with **0 false positives** on the clean negative control.
  - **AgentRE-Bench (V3.1 stripped PE32+)**: 100% encryption-key recovery across stripped Levels 15/16/18/19/22 (RC4, XOR, custom AES S-box, PEB/Hell's Gate/Reflective DLL/Code Cave).
  - **BIOPSY-Bench (Router Firmware)**: Carves embedded `SquashFS`, CRC-verified `U-Boot uImage`, `FDT`, `CPIO`, and `XZ` partitions directly from 7–14 MB router images.
- **Hard `crackmes.one` Corpus (8/8 Analyzed)**:
  - **Nested Encrypted VMs & Crypto**: 159 KB MSVC PE64 double-nested VM + anomalous-curve Schnorr (`ouroboros.exe`), and **4.8 MB Aegis v1.44** (97 virtualized functions, 11 encrypted sections unpacked in 13s, verified across **97.6M Unicorn instructions**).
  - **Custom PEB Crypters & Quantized Neural Nets**: 774 KB custom anti-debug PEB packer + 4,096-neuron QNN (`NNcrackme-t.exe`) — **1-command `--stop-on-smc --derive-image` auto-unpacked to OEP and auto-persisted 134 dynamic `imp_*` IAT annotations**.
  - **5.25 MB Multi-Engine XYVM**: Sub-second `.rzdb`-cached workflow, 13/13 `.xyvm1` ROR13 + UTF-16LE DLL hashes resolved in `0.58s`, Ring-3 PEB/TLS/`bcrypt.dll` SHA-256 & HMAC verified natively from CLI.
  - **Hybrid WebAssembly, JS & Python Droppers**: Pure-Python WASM 1.0 export parsing + Z3 MBA solving (`syferit-defender`, `syfer-drift`), and 1-command `carve --derive` unpacking a 5.33 MB Base85+Gzip obfuscated Python VM.

---

## Verified Benchmark Suite (10/10 Passing)

Measured locally on Apple Silicon (macOS ARM64, Python 3.12, Rizin 0.9, Unicorn 2.1.4 — **352 unit/E2E tests** + **10/10 benchmarks in ~2.0s**):

| Case | Result | Time | Est. Tokens | Verified Outcome |
| :--- | :---: | ---: | ---: | :--- |
| `crackme_full_loop` | PASS | ~300 ms | ~2,906 | 3/3 hidden messages batch-decrypted + `validate_candidate` proven `1`/`0` + report |
| `xor_batch_string_decrypt` | PASS | ~105 ms | ~310 | 3/3 encrypted IOCs recovered from call-sites + IDAPython export |
| `stage2_emulate_to_derive_lineage` | PASS | ~115 ms | ~231 | 26-byte stage-2 ELF decrypted in Unicorn & registered in sample lineage |
| `patch_binary_diff` | PASS | ~170 ms | ~205 | Byte-aware diff pinpointing added `validate_bounds` & modified functions |
| `api_hash_static_and_unicorn` | PASS | ~100 ms | ~213 | Static ROR13 (`VirtualAlloc`/`LoadLibraryA`) + Unicorn dynamic custom hash (`GetProcAddress`) |
| `crypto_anchor_aes_slice` | PASS | ~800 ms | ~1,744 | AES S-Box anchor slice `entry0 -> encrypt_block -> aes_sub_byte` (0 decoys) |
| `win64_peb_dynamic_api_chain` | PASS | ~2 ms | ~349 | `gs:[0x60]` PEB + `LoadLibraryA -> GetProcAddress -> VirtualAlloc` → `"IREV"` |
| `smc_self_decrypt_oep` | PASS | ~2 ms | ~180 | Self-decrypting x86_64 stub paused at OEP; stage-2 re-emulated to `0x1337` |
| `indirect_trace_and_branch_patch` | PASS | ~6 ms | ~225 | Indirect function-table trace (3 targets) + branch-flip patch verified `0 -> 1` |
| `pattern_data_xref_and_carve` | PASS | ~4 ms | ~145 | Wildcard byte search + static data-xref + embedded gzip carved into lineage |

---

## Development & Contributing

```bash
uv run ruff check src tests benchmarks bridges
uv run mypy src
uv run pytest -v
uv run python benchmarks/runner.py
uv run python benchmarks/agent_eval.py --self-test
```

See [`docs/ARCHITECTURE.md`](./docs/ARCHITECTURE.md), [`CHANGELOG.md`](./CHANGELOG.md), and [`SECURITY.md`](./SECURITY.md). Licensed under [MIT](./LICENSE).
