0xnald/pharos-audit-skillpack
GitHub: 0xnald/pharos-audit-skillpack
为 Pharos Network 生态构建的两个可复用 Solidity 智能合约安全审计 AI Agent 技能,提供静态分析与威胁建模能力。
Stars: 0 | Forks: 0
# Pharos Agent 嘉年华:可复用的智能合约审计技能(第一阶段)
欢迎查看我们为 **Pharos Network Agent 嘉年华:技能到 Agent 双重级联黑客松** 提交的第一阶段项目。
本代码仓库包含两个功能完备的生产级、可复用的 **AI Agent 技能**,专为对 Solidity 智能合约进行安全审计而构建:
1. **Solidity 静态分析技能**:标准化工具,通过解析源代码来识别结构性缺陷(重入、可见性、不安全的身份验证以及未检查的返回值)。
2. **Solidity 威胁建模技能**:标准化工具,用于映射访问控制边界、合约角色以及架构威胁风险。
这些技能严格遵守 **Anvita Flow** 清单规范,使 Pharos 生态系统中的任何自主 Agent 都能够发现、触发并支付合约审计服务。
## 技能架构与清单
### 1. Solidity 静态分析技能
* **路径**:[skills/static_analysis/](./skills/static_analysis/)
* **定价**:每次调用 `0.01 PHRS`
* **核心能力**:
* *浮动版本检查*:标记未固定的编译器版本。
* *默认可见性检查*:扫描缺失的函数可见性修饰符。
* *tx.origin 身份验证*:检测危险的网络钓鱼访问向量。
* *未检查的底层调用*:识别 `.call` 和 `.send` 操作中缺失的检查。
* *重入检查*:对函数行进行词法分析,检查状态赋值是否发生在外部调用之后。
#### 清单 (`manifest.json`)
```
{
"name": "solidity_static_analysis",
"display_name": "Solidity Static Analysis",
"description": "Scans Solidity source code for structural security vulnerabilities...",
"version": "1.0.0",
"category": "Security",
"pricing": {
"token": "PHRS",
"price_per_call": "0.01"
},
"inputs": {
"source_code": {
"type": "string",
"description": "The raw Solidity smart contract source code to audit.",
"required": true
}
},
"outputs": {
"vulnerabilities": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": { "type": "string" },
"name": { "type": "string" },
"severity": { "type": "string" },
"line": { "type": "integer" },
"snippet": { "type": "string" },
"description": { "type": "string" },
"remediation": { "type": "string" }
}
}
},
"score": { "type": "integer" }
}
}
```
### 2. Solidity 威胁建模技能
* **路径**:[skills/threat_modeling/](./skills/threat_modeling/)
* **定价**:每次调用 `0.015 PHRS`
* **核心能力**:
* *用户角色提取*:发现已定义的管理员状态变量和自定义权限类(Owner、Admin、自定义角色)。
* *特权函数注册表*:映射所有受限制的访问控制修饰符(`onlyOwner`、自定义修饰符约束、require-auth)。
* *未受保护的敏感更新*:标记那些在没有访问修饰符的情况下更改费用、费率和限制等变量的公共函数。
* *算术溢出风险*:针对 0.8.0 之前版本的 solidity 编译器,对 SafeMath 的遗漏发出警告。
* *预言机操纵风险*:识别合约计算流程中的现货价格依赖漏洞模式。
#### 清单 (`manifest.json`)
```
{
"name": "solidity_threat_modeling",
"display_name": "Solidity Threat Modeling",
"description": "Analyzes smart contract functions, privilege levels, and modifiers to model access control boundaries...",
"version": "1.0.0",
"category": "Security",
"pricing": {
"token": "PHRS",
"price_per_call": "0.015"
},
"inputs": {
"source_code": {
"type": "string",
"description": "The Solidity contract code to analyze.",
"required": true
}
},
"outputs": {
"roles": { "type": "array", "items": { "type": "string" } },
"privileged_functions": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": { "type": "string" },
"modifier": { "type": "string" },
"line": { "type": "integer" }
}
}
},
"threats": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": { "type": "string" },
"title": { "type": "string" },
"severity": { "type": "string" },
"description": { "type": "string" },
"remediation": { "type": "string" }
}
}
}
}
}
```
## 快速入门与测试
请通过我们的单元测试套件和 CLI 审计测试工具来验证这两个技能。
### 1. 克隆代码仓库
```
git clone https://github.com/0xnald/pharos-audit-skillpack.git
cd pharos-audit-skillpack
```
### 2. 前置条件
- Python 3.8+(无需任何外部第三方依赖。解析器使用 Python 标准的 `re` 和编译器工具原生运行)。
### 2. 运行单元测试
运行静态分析技能的单元测试:
```
python skills/static_analysis/test_skill.py
```
运行威胁建模技能的单元测试:
```
python skills/threat_modeling/test_skill.py
```
### 3. 运行审计测试工具
运行 CLI 测试工具,它会扫描两个示例智能合约(`VulnerableBank.sol` 和 `SecureBank.sol`)并打印格式化的报告:
```
python test_harness.py
---
## Programmatic Integration & API 使用
For other builders and agents in the Pharos Network / Anvita Flow ecosystem, here is how you can invoke and integrate these skills:
### 1. Python Integration
If you are developing a Python-based agent, you can import and call the handlers directly:
```python
from skills.static_analysis import handler as static_analyzer
from skills.threat_modeling import handler as threat_modeler
source_code = """
pragma solidity 0.8.20;
contract Simple {
address public owner;
}
"""
# 运行 static analysis
static_report = static_analyzer.analyze(source_code)
print("Static Score:", static_report["score"])
# 运行 threat modeling
threat_report = threat_modeler.analyze(source_code)
print("Found Roles:", threat_report["roles"])
```
### 2. HTTP API 集成(A2A 网关)
当部署在 Anvita Flow 容器内时,技能通过标准的 HTTP POST 请求进行调用。
**请求:**
* **方法**:`POST`
* **标头**:`Content-Type: application/json`
* **主体**:
{
"source_code": "pragma solidity ^0.8.0; contract Test {}"
}
**JSON 响应示例(静态分析)**:
```
{
"vulnerabilities": [
{
"id": "SOL-PRAGMA-001",
"name": "Floating Pragma",
"severity": "Low",
"line": 2,
"snippet": "pragma solidity ^0.8.0;",
"description": "The contract uses a floating pragma (^0.8.0)...",
"remediation": "Lock the compiler version..."
}
],
"score": 97
}
```
## 第二阶段进阶(预览)
在这些技能被选中进入 Skill Hub 注册表后,即可使用 **Anvita Flow Infra** 对其进行部署。在**第二阶段(Agent 竞技场)**中,我们将构建一个 **Lead Auditor Orchestrator Agent**,该 Agent 将:
1. 动态接受审计请求。
2. 在 **Pharos Testnet**(RPC:`https://testnet.dplabs-internal.com`,Chain ID:`688688`)上,使用原生代币 `PHRS` 结算与子代理技能之间的 A2A 支付。
3. 将输出整合为一份单一报告,并通过一个高级的深色主题 Web 界面提供服务。
标签:AI智能体, Solidity, 区块链安全, 威胁建模, 智能合约审计, 逆向工具, 错误基检测, 静态代码分析