louissarvin/InjectMe
GitHub: louissarvin/InjectMe
构建在 0G 链上的 AI 对抗性红队竞技场,让攻击者通过 prompt injection 攻破 AI 代理赢取奖励,防御者获得链上可验证的安全性证明,所有判定由 TEE 密封推理完成。
Stars: 0 | Forks: 0
InjectMe
0G 链上的 AI 对抗性红队竞技场
部署 AI 代理。使用 prompt injection 进行攻击。赢取奖池。
所有验证均由 0G Compute 上的 TEE 密封推理完成。
快速开始 • 功能 • 架构 • 运行机制 • API • 合约 • 0G 集成
| ### 攻击 **`/challenges/$id`** 尝试通过 prompt injection 攻破 AI 代理。支持流式响应、commit-reveal 防抢跑机制、以及 TEE 验证的判定。每次尝试均包含 OWASP LLM Top 10 分类。 | ### 防御 **`/challenges/create`** 部署带有系统提示词和奖池的 AI 代理。可选择锦标赛(由手续费注资)、悬赏(由防御者注资)或对齐模式(按攻击次数奖励)。 | ### 验证 **`/attestations`** 每次攻击的判定都经由 TEE 验证并存储在 0G Storage 上。可通过 chatID 验证任何结果。支持下载包含 Merkle 验证的完整存储证明。 | ### 赚取 **`/leaderboard`** 通过攻破代理赢取奖池。通过 ReputationRegistry 建立链上声誉。攀登排行榜。为研究发布对齐数据集。 |
ERC-7857] RR[ReputationRegistry] OS[OracleStaking] TO[TeeOracle] end UI --> TQ UI --> Wagmi TQ --> Routes Wagmi --> ZeroG_Chain Routes --> TEE Routes --> KV Routes --> Log Routes --> Prisma Prisma --> DB Workers --> ZeroG_Chain Routes --> ZeroG_Chain TEE --> Attest CF --> CH CF --> NFT OS --> TO ``` ### 攻击流程 (Commit-Reveal) ``` sequenceDiagram participant A as Attacker participant F as Frontend participant B as Backend participant TEE as 0G Compute (TEE) participant C as 0G Chain participant S as 0G Storage A->>F: Write attack message F->>B: POST /challenge/:addr/commit B-->>F: commitHash + salt F->>C: commitAttempt(commitHash) + messagePrice C-->>F: Commit recorded on-chain F->>B: POST /challenge/:addr/reveal B->>C: Verify commit exists on-chain B->>TEE: Evaluate message via sealed inference TEE-->>B: AI response + judgment + chatID B->>S: Archive attempt (encrypted) B->>C: revealAndRecord(messageHash, salt, storageRoot) alt Judgment = SUCCESS B->>C: claimVictory(attacker, chatID) B->>C: markAgentBreached(tokenId) B->>C: recordAttackerVictory (ReputationRegistry) C-->>A: Prize pool distributed end B->>S: Store TEE attestation B->>C: anchorAttestation(attestationHash) B-->>F: judgment, response, chatID, teeVerified ``` ### 挑战生命周期 ``` flowchart TD A[Defender creates challenge] --> B{Challenge Type?} B -->|Tournament| C[Pay messagePrice per attack
Fees grow the prize pool] B -->|Bounty| D[Defender funds prize upfront
Attacks are free] B -->|Alignment| E[Defender funds pool
Reward per attempt] C --> F[AgentNFT minted
ERC-7857 iNFT] D --> F E --> F F --> G[Attackers submit attempts] G --> H{TEE Evaluation} H -->|FAILED| I[Agent survives
Stats updated on iNFT] H -->|SUCCESS| J[Agent breached
Prize distributed to attacker] I --> K{Challenge expired?} K -->|No| G K -->|Yes| L[Defender calls claimExpiry
Withdraws remaining prize] J --> M[ReputationRegistry updated
Attacker + Defender scores] L --> M E --> N[Defender publishes
alignment dataset to 0G Storage] ``` ## 运行机制 ### 0G Compute:TEE 密封推理 AI 模型在 0G Compute 的**可信执行环境 (TEE)** 内运行。攻击者和防御者都无法观察或操纵推理过程。后端调用 `evaluateAttemptWithFallback()`,该函数通过 0G Compute 代理进行路由,并返回一个可通过 `verifyAttestation()` 独立验证的 `chatID`。 ### 0G Storage:数据可用性 每次对话、判定、挑战配置和对齐数据集都存储在 0G Storage 的 KV 和 Log 层上。这提供了一个可在后端重启后持续存在的防篡改数据可用性层。加密存档使用带有预言机派生密钥的 ECIES。对齐数据集以明文形式作为公共产品发布。 ### Commit-Reveal 防抢跑机制 | 步骤 | 操作 | 位置 | |------|--------|-------| | 1 | 攻击者提交 `keccak256(message, salt, attacker)` | 链上 | | 2 | 后端通过 TEE 处理实际消息 | 0G Compute | | 3 | 预言机通过 `revealAndRecord()` 揭示结果 | 链上 | 5 分钟的揭示窗口可防止矿工和验证者抢跑攻击结果。提交哈希在揭示被接受之前会在链上进行验证。 ### 预言机共识 多个预言机操作者通过 `OracleStaking` 合约质押原生的 0G 代币。判定在执行前需要来自活跃预言机的 M-of-N 确认。提交错误判定的操作者将被惩罚。诚实的操作者将获得奖励。活跃的预言机集合为按质押金额排名的前 N 名(最多 50 名)。取消质押设有 7 天的锁定期。 ### iNFT 代理 (ERC-7857) 每次挑战都会通过 `AgentNFT` 合约铸造一个代表 AI 代理的灵魂绑定 iNFT。该 iNFT 实现了 ERC-7857 标准,适用于带有 TEE 加密数据的智能 NFT。 | 字段 | 描述 | |-------|-------------| | `challengeAddress` | 关联的 Challenge 合约 | | `model` | 0G Compute 模型标识符 | | `totalAttempts` | 攻击尝试次数 | | `attemptsSurvived` | 代理成功抵御的攻击次数 | | `breached` | 代理是否已被攻破 | | `securityScore` | 根据存活率计算 | 支持 TEE 加密传输(通过 `TeeOracle` 进行重加密)、克隆以及执行权限的授权。 ### 挑战类型 | 类型 | 奖励来源 | 费用模型 | 使用场景 | |------|-------------|-----------|----------| | **锦标赛** | 攻击者费用累积(80% 奖池,10% 防御者,10% 协议) | 按消息付费(`minMessagePrice >= 0.001 0G`) | 竞技性红队测试 | | **悬赏** | 防御者预先注资(`bountyListingFee = 0.5 0G`) | 免费攻击 | 安全测试 | | **对齐** | 防御者注资奖池,按攻击次数奖励 | 按攻击次数奖励 | AI 对齐数据收集 | ### 声誉系统 链上 `ReputationRegistry` 跟踪攻击者和防御者的累积统计数据。分数以基点计算(10,000 = 100%)。 **攻击者统计:** totalAttempts、successfulBreaches、challengesParticipated、totalEarnings、lastActiveAt **防御者统计:** totalChallengesCreated、challengesSurvived、challengesBreached、totalPrizeDefended、totalPrizeLost、lastActiveAt ## 技术栈 | 层级 | 技术 | 用途 | |-------|-----------|---------| | **前端** | TanStack Start + React 19 | 支持渲染的同构框架 | | **路由** | TanStack Router | 基于文件的路由 | | **状态** | TanStack Query | 服务端状态管理 | | **样式** | Tailwind CSS 4 + HeroUI | 组件库 + 实用 CSS | | **动画** | GSAP + Motion + Lenis | 滚动动画,平滑滚动 | | **钱包** | wagmi v3 + viem | 0G 链钱包连接 | | **后端** | Fastify 5 + Bun | HTTP 服务器 + 运行时 | | **数据库** | PostgreSQL + Prisma 7 | 持久化 + ORM | | **认证** | 钱包签名 (EIP-191) + JWT | 基于钱包的无状态认证 | | **智能合约** | Solidity 0.8.20 + OpenZeppelin | 链上逻辑 | | **工具链** | Foundry | 合约测试 + 部署 | | **AI 推理** | 0G Compute (TEE) | 密封 AI 评估 | | **存储** | 0G Storage (KV + Log) | 数据可用性 | | **结算** | 0G Chain (Galileo Testnet) | 链上结算 | ## 项目结构 ``` injectme/ ├── web/ # Frontend application │ ├── src/ │ │ ├── routes/ # File-based routes (TanStack Router) │ │ ├── components/ # Shared UI components │ │ ├── lib/ │ │ │ ├── api/hooks.ts # TanStack Query hooks for backend │ │ │ ├── contracts/ # ABIs + contract hooks (wagmi) │ │ │ └── wagmi.ts # Chain config (0G Galileo) │ │ ├── config.ts # Contract addresses + feature flags │ │ └── styles.css # Global styles (Tailwind 4) │ └── package.json │ ├── backend/ # API server │ ├── index.ts # Fastify entry, route registration │ ├── src/ │ │ ├── routes/ │ │ │ ├── challengeRoutes.ts # Challenge CRUD, attack flow, leaderboard │ │ │ ├── agentRoutes.ts # iNFT / ERC-7857 endpoints │ │ │ ├── oracleRoutes.ts # Oracle staking + judgment endpoints │ │ │ ├── trainingRoutes.ts # Fine-tuning via 0G Compute │ │ │ └── healthRoutes.ts # Health check + 0G status │ │ ├── lib/ │ │ │ ├── og-chain/ # 0G Chain contract interactions │ │ │ ├── og-compute/ # 0G Compute TEE inference │ │ │ ├── og-storage/ # 0G Storage KV + Log │ │ │ └── encryption.ts # AES-256-GCM prompt encryption │ │ ├── workers/ │ │ │ ├── eventIndexer.ts # On-chain event indexing │ │ │ ├── challengeExpiry.ts # Auto-expire stale challenges │ │ │ ├── fineTuningMonitor.ts # Track fine-tuning job status │ │ │ └── errorLogCleanup.ts # Cap error logs at 10k │ │ └── middlewares/ │ │ ├── walletAuth.ts # Wallet signature verification │ │ └── rateLimit.ts # Attack rate limiting │ ├── prisma/ │ │ └── schema.prisma # DB schema (Challenge, Attempt, etc.) │ └── package.json │ ├── contract/ # Smart contracts │ ├── src/ │ │ ├── ChallengeFactory.sol # Main registry, creates challenges │ │ ├── Challenge.sol # Escrow + commit-reveal game logic │ │ ├── AgentNFT.sol # ERC-7857 iNFT for AI agents │ │ ├── ReputationRegistry.sol # On-chain reputation tracking │ │ ├── OracleStaking.sol # Decentralized oracle via staking │ │ ├── TeeOracle.sol # On-chain TEE proof verification │ │ └── interfaces/ # IChallenge, IERC7857, IOracle │ ├── script/ │ │ └── Deploy.s.sol # Deployment script │ ├── test/ # 306 Foundry tests │ └── foundry.toml │ └── README.md ``` ## 快速开始 ### 前置条件 - [Bun](https://bun.sh) (v1.1+) - [PostgreSQL](https://www.postgresql.org/) (v15+) - [Foundry](https://book.getfoundry.sh/) (用于合约) ### 1. 克隆并安装 ``` git clone https://github.com/injectme/injectme.git cd injectme # 安装 frontend cd web && bun install && cd .. # 安装 backend cd backend && bun install && cd .. # 安装 contract dependencies cd contract && forge install && cd .. ``` ### 2. 配置环境 ``` cp backend/.env.example backend/.env # 使用你的值编辑 backend/.env ``` ### 3. 设置数据库 ``` cd backend bun run db:push ``` ### 4. 运行开发服务器 ``` # 终端 1:Backend cd backend && bun dev # 终端 2:Frontend cd web && bun dev ``` 前端运行在 `http://localhost:3200`,后端运行在 `http://localhost:3700`。
环境变量参考
| 变量 | 必填 | 描述 | |----------|----------|-------------| | `APP_PORT` | 否 | 后端端口(默认:3700) | | `NODE_ENV` | 否 | `development` 或 `production` | | `DATABASE_URL` | 是 | PostgreSQL 连接字符串 | | `JWT_SECRET` | 是 | JWT 签名密钥 | | `JWT_EXPIRES_IN` | 否 | Token 有效期(默认:7d) | | `OG_RPC_URL` | 是 | 0G Chain RPC(默认:testnet) | | `OG_CHAIN_ID` | 是 | 链 ID(Galileo testnet 为 16602) | | `FACTORY_ADDRESS` | 是 | ChallengeFactory 合约地址 | | `AGENT_NFT_ADDRESS` | 是 | AgentNFT 合约地址 | | `TEE_ORACLE_ADDRESS` | 是 | TeeOracle 合约地址 | | `OG_COMPUTE_PROVIDER` | 是 | 0G Compute 提供者地址 | | `OG_STORAGE_INDEXER` | 是 | 0G Storage 索引器 URL | | `OG_FLOW_CONTRACT` | 是 | 用于存储上传的 0G Flow 合约 | | `OG_KV_STREAM_ID` | 否 | KV 流 ID(首次写入时创建) | | `OG_KV_NODE_URL` | 否 | KV 节点端点 | | `ORACLE_PRIVATE_KEY` | 是 | 预言机钱包私钥 | | `EVALUATOR_PRIVATE_KEY` | 是 | 评估器钱包私钥 | | `STORAGE_PRIVATE_KEY` | 是 | 存储钱包私钥 | | `CORS_ORIGINS` | 否 | 逗号分隔的允许源(生产环境) |命令参考
### 前端 ``` bun dev # Start dev server on port 3200 bun build # Production build bun preview # Preview production build bun lint # Run ESLint bun format # Run Prettier bun check # Format + lint fix bun test # Run Vitest tests ``` ### 后端 ``` bun dev # Start with watch mode on port 3700 bun start # Start without watch bun test # Run Bun test suite bun run typecheck # TypeScript type check bun run db:push # Push schema to DB + generate client bun run db:pull # Pull DB schema into Prisma bun run db:generate # Regenerate Prisma client ``` ### 合约 ``` forge build # Compile contracts forge test # Run all 306 tests forge test -vvvv # Verbose test output with traces forge test --gas-report # Gas usage report forge script script/Deploy.s.sol --rpc-url 0g_testnet --broadcast # Deploy forge fmt # Format Solidity ```
InjectMe
攻破 AI。在链上证明。赚取奖励。
基于 0G 构建。
MIT License
标签:0G Chain, AI安全, Bun, Chat Copilot, CISA项目, DApp, Fastify, Foundry, PostgreSQL, Prisma, React, Solidity, Syscalls, Tailwind CSS, TEE, TGT, TypeScript, Vite, Web3, 人工智能, 区块链, 去中心化应用, 去中心化计算, 可信执行环境, 奖金池, 安全插件, 密封推理, 对抗性攻击, 攻防演练, 智能合约, 测试用例, 用户模式Hook绕过, 竞赛平台, 网络安全, 足迹探测, 隐私保护, 黑客松