ingest: Blockchain Security Auditor + 4 entities + 2 concepts
- Source: blockchain-security-auditor.md (The Agency Specialized, smart contract security audit agent) - Entities: The-DAO-2016, Euler-Finance, Nomad-Bridge, Curve-Finance - Concepts: Reentrancy, Oracle-Manipulation - Updated: index.md (消除了source missing标记), overview.md, log.md
This commit is contained in:
79
wiki/concepts/Oracle-Manipulation.md
Normal file
79
wiki/concepts/Oracle-Manipulation.md
Normal file
@@ -0,0 +1,79 @@
|
||||
---
|
||||
title: "Oracle Manipulation"
|
||||
type: concept
|
||||
tags: [smart-contract, vulnerability, defi, oracle, flash-loan]
|
||||
sources: [blockchain-security-auditor]
|
||||
last_updated: 2026-04-25
|
||||
---
|
||||
|
||||
## 中文定义
|
||||
|
||||
**预言机操纵(Oracle Manipulation)**:攻击者利用 DeFi 协议对价格预言机输入的信任,通过在单笔交易内(通常借助闪电贷)操纵市场价格,从而在借贷、清算、DEX 交易等场景中获取非法收益的攻击类型。
|
||||
|
||||
## 问题描述
|
||||
|
||||
许多 DeFi 协议依赖链上价格预言机(直接从 AMM 池获取价格)来计算抵押品价值、清算阈值、交易价格等关键参数。由于 AMM 的"即时价格"可以被单笔交易内的操作所操纵,攻击者可以在无需长期持仓的情况下:
|
||||
1. 在操纵价格时执行借贷/清算获取超额资产
|
||||
2. 在价格恢复后平仓获利
|
||||
|
||||
## 操纵模式
|
||||
|
||||
### 1. AMM 即时价格操纵(Spot Price Manipulation)
|
||||
```solidity
|
||||
// 有漏洞的代码:使用 AMM 即时储备计算价格
|
||||
(uint112 reserve0, uint112 reserve1,) = pair.getReserves();
|
||||
uint256 price = (uint256(reserve1) * 1e18) / reserve0;
|
||||
// 攻击者:在同一笔交易中先Swap大量代币改变储备,再执行借贷
|
||||
```
|
||||
|
||||
**攻击步骤**:
|
||||
1. Flash Loan 借入资产 A
|
||||
2. 在 DEX 中用资产 A 兑换资产 B(推动 B 的价格)
|
||||
3. 操纵后的价格被借贷协议读取,攻击者以虚高抵押品价值借款
|
||||
4. 归还 Flash Loan
|
||||
5. 偿还部分借款,保留利润
|
||||
|
||||
### 2. 时间加权平均价格(TWAP)操纵
|
||||
Uniswap V3 的 TWAP 预言机理论上比即时价格更安全,但仍可在以下条件下被操纵:
|
||||
- 操纵成本 < 攻击收益
|
||||
- 资金池流动性不足(攻击成本低)
|
||||
- TWAP 时间窗口过短
|
||||
|
||||
### 3. Chainlink 聚合价格操纵
|
||||
Chainlink 的去中心化价格源在极端市场条件下(如流动性枯竭)可能出现价格偏差:
|
||||
- 预言机更新延迟
|
||||
- 小币种流动性不足导致聚合价格失真
|
||||
- Chainlink 的 `staleness` 检查缺失或配置不当
|
||||
|
||||
## 防御机制
|
||||
|
||||
### TWAP(Time-Weighted Average Price)
|
||||
```solidity
|
||||
// Uniswap V3 的 TWAP:使用历史时间加权平均而非即时价格
|
||||
(uint256 price, ) = OracleLibrary.getQuoteAtTick(
|
||||
currentTick,
|
||||
1000000, // 1 AMM token
|
||||
token0,
|
||||
token1
|
||||
);
|
||||
```
|
||||
|
||||
### Chainlink 预言机 + 合法性验证
|
||||
```solidity
|
||||
(, int256 price, , uint256 updatedAt, ) = priceFeed.latestRoundData();
|
||||
require(price > 0, "Invalid price");
|
||||
require(updatedAt > block.timestamp - MAX_STALENESS, "Stale price");
|
||||
require(answeredInRound >= roundId, "Incomplete round");
|
||||
```
|
||||
|
||||
### Chainlink Off-Chain 监控
|
||||
设置价格异常波动告警,在 TWAP 偏离超过阈值时暂停协议。
|
||||
|
||||
## 关键案例
|
||||
- [[Euler-Finance]]:donate-to-reserves 攻击结合了预言机操纵(虽然主要是内部会计逻辑漏洞)
|
||||
- [[blockchain-security-auditor]] 的 Source Page 示例代码:展示了完整的有漏洞的借贷合约和修复方案
|
||||
|
||||
## 关联概念
|
||||
- [[Flash-Loan-Attack]]:预言机操纵几乎总是借助闪电贷实现(单笔交易内完成借贷和操纵)
|
||||
- [[Reentrancy]]:两者常被组合使用(操纵价格 + 重入执行操作)
|
||||
- Uniswap V2 / V3:主要 AMM 平台,也是大多数预言机操纵攻击的目标
|
||||
65
wiki/concepts/Reentrancy.md
Normal file
65
wiki/concepts/Reentrancy.md
Normal file
@@ -0,0 +1,65 @@
|
||||
---
|
||||
title: "Reentrancy"
|
||||
type: concept
|
||||
tags: [smart-contract, vulnerability, defi, solidity, security]
|
||||
sources: [blockchain-security-auditor]
|
||||
last_updated: 2026-04-25
|
||||
---
|
||||
|
||||
## 中文定义
|
||||
|
||||
**重入攻击(Reentrancy)**:智能合约在执行外部调用后、在状态更新前,允许攻击者通过回调函数再次进入合约逻辑,从而在状态被正确更新前回滚执行、重复提取资金或操纵合约状态的漏洞类型。
|
||||
|
||||
## 问题描述
|
||||
|
||||
当合约 A 调用合约 B 的函数时,合约 B 可以在其 `receive()`/`fallback()` 函数中回调合约 A 的函数。如果合约 A 在外部调用之前读取了状态(如用户余额),但在该调用返回后才更新状态(清零余额),攻击者合约 B 可以在余额清零前回拨合约 A 的函数,再次触发提取逻辑。
|
||||
|
||||
## 攻击模式
|
||||
|
||||
### 1. 单函数重入(Single-Function Reentrancy)
|
||||
最简单的模式:同一个函数在执行中被再次调用。The DAO 和 Curve Finance 属于此类。
|
||||
|
||||
### 2. 跨函数重入(Cross-Function Reentrancy)
|
||||
攻击者通过不同的函数路径重入合约,利用状态不一致进行攻击。例如:`withdraw()` 读取的余额与 `transfer()` 检查的余额不同步。
|
||||
|
||||
### 3. 只读重入(Read-Only Reentrancy)
|
||||
攻击者通过 `view` 函数读取合约状态(在外部调用期间),利用该状态作为预言机输入进行攻击,无需写入合约。
|
||||
|
||||
### 4. ERC-777 / ERC-1155 钩子重入
|
||||
即使合约没有 `receive()` 函数,使用 ERC-777 代币标准时,`transfer()` 会触发 `tokensReceived()` 钩子,攻击者可在钩子中重入。
|
||||
|
||||
## 防御机制
|
||||
|
||||
### Checks-Effects-Interactions Pattern
|
||||
```solidity
|
||||
// ✅ 正确顺序:先更新状态,再执行外部调用
|
||||
function withdraw() external {
|
||||
uint256 amount = balances[msg.sender];
|
||||
require(amount > 0);
|
||||
balances[msg.sender] = 0; // 先更新状态
|
||||
(bool success,) = msg.sender.call{value: amount}(""); // 后外部调用
|
||||
require(success);
|
||||
}
|
||||
```
|
||||
|
||||
### Reentrancy Guard
|
||||
```solidity
|
||||
import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
|
||||
|
||||
contract SecureVault is ReentrancyGuard {
|
||||
function withdraw() external nonReentrant {
|
||||
// nonReentrant 修饰符防止任何重入
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 关键案例
|
||||
- [[The-DAO-2016]]:历史上首个大规模重入攻击,损失 360 万 ETH,直接导致 ETH/ETC 硬分叉
|
||||
- [[Curve-Finance]]:Vyper 编译器 bug 导致 nonreentrant 失效,损失超 7000 万美元
|
||||
- [[blockchain-security-auditor]] 的 Source Page 示例代码:展示了完整的有漏洞合约和修复后合约
|
||||
|
||||
## 关联概念
|
||||
- [[Access-Control]]:权限控制缺失会加剧重入攻击的影响
|
||||
- [[Flash-Loan-Attack]]:重入攻击常与闪电贷结合使用
|
||||
- Checks-Effects-Interactions Pattern:标准防御模式
|
||||
- [[Slither]]:Slither 的 `reentrancy-eth` 检测器可发现大多数经典重入漏洞
|
||||
Reference in New Issue
Block a user