smart-contract-vulnerabilities-kex5
安装方式
先用 Prompt 核对来源与依赖。也可下载安装包。
用 AI 助手安装
复制提示词,让助手先核对固定版本和依赖。
查看 Prompt收起 Prompt
先检查当前 Agent 是否已安装 SkillPI Skill Finder。如果尚未安装,请按官方仓库的说明安装并启用:https://github.com/Findskills/skillpi。然后使用 Skill Finder 打开此 SkillPI 详情页并定位 "smart-contract-vulnerabilities-kex5":https://skillpi.com/zh/skills/deepskill-a11a1d3ff856b7f538b181222e57d436fedaf2bd20de668e8930efea8d714f09。安装前核对上游来源和版本:https://skillhub.cn/skills/user_3c6cb52e/smart-contract-vulnerabilities-kex5。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。
来源与配置
skillhub.cn
配置方式待核实
请查看原始来源中的适用工具、依赖和权限要求。
安装实测待补充
面向智能合约漏洞分析与处理的专用技能包,帮助AI Agent高效完成与智能合约漏洞相关的任务。
介绍来源: third_party · user_3c6cb52e · 1.0.0 参考来源
SkillPi 解读
- 能完成的任务
- 翻译待完成,请参阅来源文档。
- 适合谁
- 来源文档未说明。
- 输入和输出
- 来源文档未说明。
- 软件、账号及付费要求
- 来源文档未说明。
- 限制与区别
- 来源文档未说明。
- 实测状态
- 未测试
文件浏览
Frontmatter
name: 智能合约漏洞
slug: smart-contract-vulnerabilities
version: 1.0.0
displayName: 智能合约漏洞
description: >
智能合约漏洞专用技能,帮助AI Agent高效完成相关任务。
summary: "智能合约漏洞专用技能,帮助AI Agent高效完成相关任务。
"
license: MIT
category: 安全与渗透测试
framework:
- Claude Code
- Codex
- Hermes Agent
- OpenClaw
- QClaw
- WorkBuddy
platform: multi-platform
homepage: https://github.com/1991513ccie-png
repository: https://github.com/1991513ccie-pngSKILL: Smart Contract Vulnerabilities — Expert Attack Playbook
AI LOAD INSTRUCTION: Expert smart contract audit techniques. Covers reentrancy (single, cross-function, cross-contract, read-only), integer overflow, access control, delegatecall, randomness manipulation, flash loans, signature replay, front-running/MEV, and CREATE2 exploitation. Base models miss subtle cross-contract reentrancy and storage layout collisions in proxy patterns.
0. RELATED ROUTING
- defi-attack-patterns when the vulnerability is part of a DeFi protocol exploit (flash loans, oracle manipulation, governance attacks)
- deserialization-insecure when the target is off-chain infrastructure deserializing blockchain data
Advanced Reference
Also load SOLIDITY_VULN_PATTERNS.md when you need:
- Side-by-side vulnerable vs fixed code patterns for each vulnerability class
- Gas optimization traps that introduce vulnerabilities
- Proxy pattern storage collision examples with slot calculations
1. REENTRANCY
The most iconic smart contract vulnerability. External calls transfer execution control; if state is not updated before the call, the callee can re-enter.
1.1 Classic Reentrancy (Single-Function)
Victim.withdraw()
├── checks balance[msg.sender] > 0 ✓
├── msg.sender.call{value: balance}("") ← external call
│ └── Attacker.receive()
│ └── Victim.withdraw() ← re-enters before state update
│ ├── checks balance[msg.sender] ← still > 0!
│ └── sends ETH again
└── balance[msg.sender] = 0 ← too late
1.2 Cross-Function Reentrancy
Two functions share state; attacker re-enters a different function during callback:
| Step | Execution | State |
|---|---|---|
| 1 | Call withdraw() → external call |
balance still positive |
| 2 | Attacker fallback calls transfer(attacker2) |
balance used before reset |
| 3 | transfer reads stale balance → moves funds |
attacker2 receives tokens |
| 4 | Original withdraw completes, zeroes balance |
damage done |
1.3 Cross-Contract Reentrancy
Contract A calls Contract B, which calls back into Contract A (or Contract C that reads A's stale state). Especially dangerous in DeFi protocols where multiple contracts share state.
1.4 Read-Only Reentrancy
The re-entered function is a view function used by a third-party contract for price calculation. No state modification in the victim, but the stale intermediate state misleads the reader.
Real-world: Curve pool get_virtual_price() read during remove_liquidity() callback → inflated price → profit on dependent lending protocol.
Mitigations
| Pattern | Protection Level |
|---|---|
| Checks-Effects-Interactions (CEI) | Core defense; update state before external call |
ReentrancyGuard (OpenZeppelin) |
Mutex lock; prevents same-tx re-entry |
| Pull payment pattern | Eliminate external calls in state-changing functions |
| CEI + guard on all public functions | Defense-in-depth against cross-function |
2. INTEGER OVERFLOW / UNDERFLOW
Pre-Solidity 0.8
Arithmetic silently wraps: uint8(255) + 1 == 0, uint8(0) - 1 == 255.
| Attack | Example |
|---|---|
| Balance underflow | balances[attacker] -= amount when amount > balance → huge balance |
| Supply overflow | totalSupply + mintAmount wraps → bypass cap checks |
| Timelock bypass | lockTime[msg.sender] + extend wraps to past → early unlock |
Post-Solidity 0.8
Default checked arithmetic reverts on overflow. But unchecked{} blocks reintroduce risk:
unchecked {
// "gas optimization" — but if i can be influenced by user input, overflow returns
for (uint i = start; i < end; i++) { ... }
}
SafeMath Bypass Scenarios
- Casting:
uint256→uint128truncation before SafeMath check - Assembly blocks:
mstore/addbypass Solidity-level checks - Intermediate multiplication overflow before division:
(a * b) / cwherea * boverflows
3. ACCESS CONTROL
tx.origin vs msg.sender
| Property | msg.sender |
tx.origin |
|---|---|---|
| Value | Immediate caller | EOA that initiated the tx |
| Safe for auth | Yes | No — phishing contract can inherit tx.origin |
Attack: trick owner into calling attacker contract → attacker contract calls victim with owner's tx.origin.
Common Patterns
| Issue | Impact |
|---|---|
Missing onlyOwner on critical functions |
Anyone can call admin functions |
Unprotected selfdestruct |
Anyone can destroy the contract, force-send ETH |
Unprotected delegatecall |
Attacker executes arbitrary code in victim's context |
| Default visibility (pre-0.6.0) | Functions default to public |
| Missing zero-address checks | Ownership transferred to address(0) |
4. RANDOMNESS MANIPULATION
On-chain randomness sources are predictable to miners/validators:
| Source | Predictability |
|---|---|
block.timestamp |
Miner has ~15s window to manipulate |
blockhash(block.number - 1) |
Known to all at execution time |
blockhash(block.number) |
Always returns 0 (current block hash unknown) |
block.difficulty / block.prevrandao |
Post-merge: known beacon chain value |
Commit-reveal bypass: If reveal phase doesn't enforce timeout or bond, attacker can choose not to reveal unfavorable outcomes (selective abort attack).
5. DELEGATECALL VULNERABILITIES
delegatecall executes callee's code in caller's storage context. Storage slot layout must match exactly.
Storage Layout Collision
Proxy (storage): Implementation (code):
slot 0: owner slot 0: someVariable
slot 1: implementation slot 1: anotherVariable
Implementation writes to someVariable (slot 0) → overwrites proxy's owner. Attacker calls implementation function that writes slot 0 → becomes proxy owner.
Function Selector Collision
4-byte function selectors can collide. If proxy's admin() selector collides with implementation's transfer(), calling admin() on the proxy executes transfer() logic.
Tool: cast selectors <bytecode> (Foundry) to enumerate selectors.
6. FRONT-RUNNING / MEV
Transaction Ordering Manipulation
Victim submits DEX swap tx (visible in mempool)
├── Front-runner: buy token before victim (raise price)
├── Victim tx executes at worse price
└── Back-runner: sell token after victim (profit from spread)
= Sandwich attack
Protection Patterns
| Defense | Mechanism |
|---|---|
| Commit-reveal | Hide transaction intent until reveal |
| Flashbots / private mempool | Submit tx directly to block builder |
| Slippage protection | Set minAmountOut to limit MEV extraction |
| Time-lock | Delay execution to reduce predictability |
7. SIGNATURE REPLAY
Missing Nonce
Reuse a valid signature to repeat the action (e.g., transfer) multiple times.
Cross-Chain Replay
Same contract deployed on multiple chains with same address → signature valid on all chains. Must include block.chainid in signed message.
EIP-712 Implementation Errors
| Error | Consequence |
|---|---|
Missing DOMAIN_SEPARATOR with chainId |
Cross-chain replay |
| Domain separator cached at deploy | Breaks after hard fork changing chainId |
| Missing nonce in struct hash | Signature replay |
ecrecover returns address(0) on invalid sig |
Passes == address(0) owner check |
8. SELF-DESTRUCT & FORCE-SEND ETH
selfdestruct(recipient) force-sends all contract ETH to recipient — bypasses receive() and fallback(), cannot be rejected.
Breaks contracts that rely on address(this).balance for logic (e.g., require(balance == expected)).
Post-EIP-6780 (Dencun): selfdestruct only sends ETH; code/storage deletion only if called in same tx as creation.
9. CREATE2 & DETERMINISTIC ADDRESS EXPLOITATION
CREATE2 address = keccak256(0xff ++ deployer ++ salt ++ keccak256(initCode)).
| Attack | Method |
|---|---|
| Pre-fund exploitation | Predict address → send tokens/ETH before deployment → selfdestruct → redeploy different code at same address |
| Pre-approve exploitation | Predicted address gets token approvals → deploy malicious contract → drain approved tokens |
| Metamorphic contracts | CREATE2 → selfdestruct → CREATE2 with same salt but different initCode (pre-EIP-6780) |
10. FLASH LOAN ATTACK PATTERNS
Single transaction:
├── Borrow large amount (no collateral)
├── Manipulate state (price oracle, governance, etc.)
├── Extract profit from manipulated state
├── Repay loan + fee
└── Keep profit
Key: entire sequence must succeed atomically or the whole tx reverts.
11. SHORT ADDRESS ATTACK
EVM pads missing bytes in ABI-encoded calldata with zeros. If transfer(address, uint256) is called with a 19-byte address, the uint256 amount shifts left by 8 bits → multiplied by 256.
Mitigation: validate calldata length; modern Solidity compilers add checks.
12. TOOLS
| Tool | Purpose | Usage |
|---|---|---|
| Slither | Static analysis, vulnerability detection | slither . in project root |
| Mythril | Symbolic execution, path exploration | myth analyze contract.sol |
| Echidna | Property-based fuzzing | Define invariants, fuzz for violations |
| Foundry (Forge) | Test framework, fuzzing, gas analysis | forge test --fuzz-runs 10000 |
| Hardhat | Development, testing, deployment | npx hardhat test |
| Certora | Formal verification | Write specs, prove/disprove properties |
| 4naly3er | Automated gas optimization + vuln report | CI integration |
13. DECISION TREE
Auditing a smart contract?
├── Is it a proxy pattern?
│ ├── Yes → Check storage layout collision (Section 5)
│ │ ├── Compare slot assignments between proxy and implementation
│ │ ├── Check for function selector collision
│ │ └── Verify initializer cannot be called twice
│ └── No → Continue
├── Does it make external calls?
│ ├── Yes → Check reentrancy (Section 1)
│ │ ├── State updated before call? → CEI pattern OK
│ │ ├── ReentrancyGuard present? → Check all entry points
│ │ ├── Cross-function state sharing? → Cross-function reentrancy risk
│ │ └── View functions read during callback? → Read-only reentrancy
│ └── No → Continue
├── Does it handle tokens/ETH?
│ ├── Yes → Check integer overflow (Section 2)
│ │ ├── Solidity < 0.8? → All arithmetic suspect
│ │ ├── unchecked{} blocks? → Verify no user-influenced values
│ │ └── Casting between uint sizes? → Truncation risk
│ └── Also check self-destruct force-send (Section 8)
├── Does it use signatures?
│ ├── Yes → Check replay (Section 7)
│ │ ├── Nonce included? → Verify incremented
│ │ ├── ChainId included? → Cross-chain safe
│ │ └── ecrecover result checked for address(0)? → OK
│ └── No → Continue
├── Does it use on-chain randomness?
│ ├── Yes → Predictable (Section 4)
│ │ └── Recommend Chainlink VRF or commit-reveal with bond
│ └── No → Continue
├── Does it interact with DeFi protocols?
│ ├── Yes → Load [defi-attack-patterns](../defi-attack-patterns/SKILL.md)
│ │ ├── Flash loan vectors
│ │ ├── Oracle manipulation
│ │ └── MEV exposure
│ └── No → Continue
├── Does it use CREATE2?
│ ├── Yes → Check deterministic address exploitation (Section 9)
│ └── No → Continue
└── Run automated tools (Section 12)
├── Slither for static analysis
├── Mythril for symbolic execution
└── Echidna for fuzzing invariants当前收录版本
1.0.0
安全与评测信息
质量评测
满分100 · 来自 SkillPi LAB 实验室的评测报告。
亮点
- 覆盖智能合约核心漏洞类型(重入、溢出、权限控制、随机数操纵等)
- 提供实际攻击模式与防御方案,具备高实用价值
- 集成决策树与工具链,形成完整审计闭环
亮点
- 触发描述清晰明确,聚焦智能合约漏洞审计场景
- 无歧义表述,边界清晰
亮点
- 结构清晰,分12个漏洞类别系统讲解
- frontmatter完整,含版本、许可证、分类等信息
- 提供端到端决策树示例,便于执行
待提升项
-
引用文件缺失 原报告严重程度:重大
SKILL.md 中引用了 './SOLIDITY_VULN_PATTERNS.md' 文件,但该文件未包含在传入内容中
改进建议:请确保所有被引用的文件(如 SOLIDITY_VULN_PATTERNS.md)都包含在 Skill 包中,或移除对该文件的引用
亮点
- 工具来源明确,参数使用正确
- 覆盖主要异常场景,提供可靠防御建议
- 对不可逆操作(如 selfdestruct)有明确风险提示
取前四项原始分值的算术平均,四舍五入取整。可通过收录报告核对各项评分与说明,运行记录需单独查证。
正文静态检测
100 / 100 · skillpi-20261123-no-r3-v1
正文未命中当前规则。
分数仅反映正文中的规则命中,供使用前参考。附件与运行行为不在此评分范围内。
权限与使用前检查
根据当前主文档自动整理,命中内容可能是示例或防护说明;不是实际权限声明,也未覆盖附件、外部依赖或运行行为。
-
安装或获取外部依赖
使用官方来源并锁定版本,在隔离环境检查安装脚本。当前文档检查不覆盖后续下载的代码。
主文档提及 · 第 282 行
文档引用的域名或地址
这些地址可能用于文档链接或示例,不是已批准的联网清单。使用前核对实际目的地及发送的数据。
- github.com
安全评级与访问范围
safe · 静态证据核验,不代表运行安全保证。
来源文档未说明。
使用前请由宿主核对并授权实际文件读写、外部服务和联网目的地;本站的核验不会授予任何运行权限。
用户讨论
还没有已核实的点评。