cosmos-vulnerability-scanner by trailofbits/skills
npx skills add https://github.com/trailofbits/skills --skill cosmos-vulnerability-scanner系统性地扫描 Cosmos SDK 区块链模块和 CosmWasm 智能合约,查找可能导致链停止、共识失败或资金损失的平台特定安全漏洞。此技能编码了 9 种基于 Cosmos 链特有的关键漏洞模式。
.go,.proto.rs(包含 cosmwasm 导入的 Rust 文件)// Cosmos SDK 标识符
import (
"github.com/cosmos/cosmos-sdk/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/..."
)
// 常见模式
keeper.Keeper
sdk.Msg, GetSigners()
BeginBlocker, EndBlocker
CheckTx, DeliverTx
protobuf 服务定义
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
// CosmWasm 标识符
use cosmwasm_std::*;
#[entry_point]
pub fn execute(deps: DepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg)
x/modulename/ - 自定义模块keeper/keeper.go - 状态管理types/msgs.go - 消息定义abci.go - BeginBlocker/EndBlockerhandler.go - 消息处理器(旧版)调用此技能时,我将:
当发现漏洞时,您将收到类似以下的报告:
=== COSMOS SDK 漏洞扫描结果 ===
项目:my-cosmos-chain
已扫描文件:6 (.go)
发现漏洞:2
---
[严重] 不正确的 GetSigners()
---
我检查 9 种 CosmWasm 特有的关键漏洞模式。有关详细的检测模式、代码示例、缓解措施和测试策略,请参阅 VULNERABILITY_PATTERNS.md。
有关包含代码示例的完整漏洞模式,请参阅 VULNERABILITY_PATTERNS.md。
go.mod)x/*/)abci.go, BeginBlocker, EndBlocker)types/msgs.go, .proto)专注于共识关键代码:
这是 Cosmos 链的最高优先级检查。
# 搜索非确定性模式
grep -r "range.*map\[" x/
grep -r "\bint\b\|\buint\b" x/ | grep -v "int32\|int64\|uint32\|uint64"
grep -r "float32\|float64" x/
grep -r "go func\|go routine" x/
grep -r "select {" x/
grep -r "time.Now()" x/
grep -r "rand\." x/
对于每个发现:
审查 BeginBlocker 和 EndBlocker:
对于每种消息类型:
## [严重] EndBlocker 中的非确定性 Map 迭代
**位置**:`x/dex/abci.go:45-52`
**描述**:
EndBlocker 遍历一个无序的 map 来分发奖励,导致不同的验证者以不同的顺序处理用户,并产生不同的状态根。当验证者无法达成共识时,这将导致链停止。
**易受攻击的代码**:
```go
// abci.go, 第 45 行
func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
rewards := k.GetPendingRewards(ctx) // 返回 map[string]sdk.Coins
for user, amount := range rewards { // 非确定性顺序
k.bankKeeper.SendCoins(ctx, moduleAcc, user, amount)
}
}
攻击场景:
建议:在迭代前对 map 键进行排序:
func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
rewards := k.GetPendingRewards(ctx)
// 收集并排序键以实现确定性迭代
users := make([]string, 0, len(rewards))
for user := range rewards {
users = append(users, user)
}
sort.Strings(users) // 确定性顺序
// 按排序后的顺序处理
for _, user := range users {
k.bankKeeper.SendCoins(ctx, moduleAcc, user, rewards[user])
}
}
参考:
---
## 7. 优先级指南
### 严重 - 链停止风险
- 非确定性(任何形式)
- ABCI 方法恐慌
- 缓慢的 ABCI 方法
- 不正确的 GetSigners(允许未经授权的操作)
### 高危 - 资金损失风险
- 缺少错误处理(bankKeeper.SendCoins)
- 簿记错误(会计不匹配)
- 缺少消息优先级(预言机/紧急消息)
### 中危 - 逻辑/DoS 风险
- 舍入错误(协议价值泄漏)
- 未注册的消息处理器(功能损坏)
---
## 8. 测试建议
### 非确定性测试
```bash
# 为不同架构构建
GOARCH=amd64 go build
GOARCH=arm64 go build
# 运行相同操作,比较状态根
# 必须在不同架构间保持一致
# 使用并发操作进行模糊测试
go test -fuzz=FuzzEndBlocker -parallel=10
func BenchmarkBeginBlocker(b *testing.B) {
ctx := setupMaximalState() // 最坏情况状态
b.ResetTimer()
for i := 0; i < b.N; i++ {
BeginBlocker(ctx, keeper)
}
// 必须在 < 1 秒内完成
require.Less(b, b.Elapsed()/time.Duration(b.N), time.Second)
}
// 在集成测试中运行不变量
func TestInvariants(t *testing.T) {
app := setupApp()
// 执行操作
app.DeliverTx(...)
// 检查不变量
_, broken := keeper.AllInvariants()(app.Ctx)
require.False(t, broken, "检测到不变量违规")
}
building-secure-contracts/not-so-smart-contracts/cosmos/完成 Cosmos 链审计前:
非确定性(严重):
ABCI 方法(严重):
消息处理(高危):
算术运算与会计(中危):
测试:
每周安装量
1.2K
代码仓库
GitHub 星标
4.0K
首次出现
2026年1月19日
安全审计
安装于
claude-code1.0K
opencode996
gemini-cli981
codex975
cursor952
github-copilot923
Systematically scan Cosmos SDK blockchain modules and CosmWasm smart contracts for platform-specific security vulnerabilities that can cause chain halts, consensus failures, or fund loss. This skill encodes 9 critical vulnerability patterns unique to Cosmos-based chains.
.go, .proto.rs (Rust with cosmwasm imports)// Cosmos SDK indicators
import (
"github.com/cosmos/cosmos-sdk/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/..."
)
// Common patterns
keeper.Keeper
sdk.Msg, GetSigners()
BeginBlocker, EndBlocker
CheckTx, DeliverTx
protobuf service definitions
// CosmWasm indicators
use cosmwasm_std::*;
#[entry_point]
pub fn execute(deps: DepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg)
x/modulename/ - Custom moduleskeeper/keeper.go - State managementtypes/msgs.go - Message definitionsabci.go - BeginBlocker/EndBlockerhandler.go - Message handlers (legacy)When invoked, I will:
When vulnerabilities are found, you'll get a report like this:
=== COSMOS SDK VULNERABILITY SCAN RESULTS ===
Project: my-cosmos-chain
Files Scanned: 6 (.go)
Vulnerabilities Found: 2
---
[CRITICAL] Incorrect GetSigners()
---
## 5. Vulnerability Patterns (9 Patterns)
I check for 9 critical vulnerability patterns unique to CosmWasm. For detailed detection patterns, code examples, mitigations, and testing strategies, see [VULNERABILITY_PATTERNS.md](resources/VULNERABILITY_PATTERNS.md).
### Pattern Summary:
1. **Missing Denom Validation** ⚠️ CRITICAL - Accepting arbitrary token denoms
2. **Insufficient Authorization** ⚠️ CRITICAL - Missing sender/admin validation
3. **Missing Balance Check** ⚠️ HIGH - Not verifying sufficient balances
4. **Improper Reply Handling** ⚠️ HIGH - Unsafe submessage reply processing
5. **Missing Reply ID Check** ⚠️ MEDIUM - Not validating reply IDs
6. **Improper IBC Packet Validation** ⚠️ CRITICAL - Unvalidated IBC packets
7. **Unvalidated Execute Message** ⚠️ HIGH - Missing message validation
8. **Integer Overflow** ⚠️ HIGH - Unchecked arithmetic operations
9. **Reentrancy via Submessages** ⚠️ MEDIUM - State changes before submessages
For complete vulnerability patterns with code examples, see [VULNERABILITY_PATTERNS.md](resources/VULNERABILITY_PATTERNS.md).
## 5. Scanning Workflow
### Step 1: Platform Identification
1. Identify Cosmos SDK version (`go.mod`)
2. Locate custom modules (`x/*/`)
3. Find ABCI methods (`abci.go`, BeginBlocker, EndBlocker)
4. Identify message types (`types/msgs.go`, `.proto`)
### Step 2: Critical Path Analysis
Focus on consensus-critical code:
- BeginBlocker / EndBlocker implementations
- Message handlers (execute, DeliverTx)
- Keeper methods that modify state
- CheckTx priority logic
### Step 3: Non-Determinism Sweep
**This is the highest priority check for Cosmos chains.**
```bash
# Search for non-deterministic patterns
grep -r "range.*map\[" x/
grep -r "\bint\b\|\buint\b" x/ | grep -v "int32\|int64\|uint32\|uint64"
grep -r "float32\|float64" x/
grep -r "go func\|go routine" x/
grep -r "select {" x/
grep -r "time.Now()" x/
grep -r "rand\." x/
For each finding:
Review BeginBlocker and EndBlocker:
For each message type:
## [CRITICAL] Non-Deterministic Map Iteration in EndBlocker
**Location**: `x/dex/abci.go:45-52`
**Description**:
The EndBlocker iterates over an unordered map to distribute rewards, causing different validators to process users in different orders and produce different state roots. This will halt the chain when validators fail to reach consensus.
**Vulnerable Code**:
```go
// abci.go, line 45
func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
rewards := k.GetPendingRewards(ctx) // Returns map[string]sdk.Coins
for user, amount := range rewards { // NON-DETERMINISTIC ORDER
k.bankKeeper.SendCoins(ctx, moduleAcc, user, amount)
}
}
Attack Scenario :
Recommendation : Sort map keys before iteration:
func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
rewards := k.GetPendingRewards(ctx)
// Collect and sort keys for deterministic iteration
users := make([]string, 0, len(rewards))
for user := range rewards {
users = append(users, user)
}
sort.Strings(users) // Deterministic order
// Process in sorted order
for _, user := range users {
k.bankKeeper.SendCoins(ctx, moduleAcc, user, rewards[user])
}
}
References :
building-secure-contracts/not-so-smart-contracts/cosmos/non_determinism
Cosmos SDK docs: Determinism
# Build for different architectures
GOARCH=amd64 go build
GOARCH=arm64 go build
# Run same operations, compare state roots
# Must be identical across architectures
# Fuzz test with concurrent operations
go test -fuzz=FuzzEndBlocker -parallel=10
func BenchmarkBeginBlocker(b *testing.B) {
ctx := setupMaximalState() // Worst-case state
b.ResetTimer()
for i := 0; i < b.N; i++ {
BeginBlocker(ctx, keeper)
}
// Must complete in < 1 second
require.Less(b, b.Elapsed()/time.Duration(b.N), time.Second)
}
// Run invariants in integration tests
func TestInvariants(t *testing.T) {
app := setupApp()
// Execute operations
app.DeliverTx(...)
// Check invariants
_, broken := keeper.AllInvariants()(app.Ctx)
require.False(t, broken, "invariant violation detected")
}
building-secure-contracts/not-so-smart-contracts/cosmos/Before completing Cosmos chain audit:
Non-Determinism (CRITICAL) :
ABCI Methods (CRITICAL) :
Message Handling (HIGH) :
Arithmetic & Accounting (MEDIUM):
Testing :
Weekly Installs
1.2K
Repository
GitHub Stars
4.0K
First Seen
Jan 19, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
claude-code1.0K
opencode996
gemini-cli981
codex975
cursor952
github-copilot923
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
102,200 周安装