Chuyển đến nội dung
Skillpedia
Bảo mật6.5k · #14007

flash-loan

Skill này làm gì

Playbook này giải thích cách flash loan khuếch đại các điểm yếu khác thay vì tự gây ra chúng. Playbook dành cho kiểm toán viên rà soát hợp đồng DeFi dùng callback flash loan hoặc dữ liệu giá.

Khi nào nên dùng

  • ✓Khi một hợp đồng DeFi chấp nhận callback flash loan và bạn cần rà soát chúng.
  • ✓Khi giá hoặc biểu quyết của một giao thức phụ thuộc vào số dư có thể bị khoản vay thay đổi tạm thời.
  • ✓Khi bạn muốn kiểm tra xem một callback có thể được gọi mà không có khoản vay thật không.

Liên kết skill

https://github.com/BitterSecurity/Decepticon/blob/main/packages/decepticon/decepticon/skills/standard/contracts/flash-loan/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/BitterSecurity/Decepticon/blob/main/packages/decepticon/decepticon/skills/standard/contracts/flash-loan/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Hợp đồng cho vay của chúng tôi xử lý callback flash loan. Bạn có thể kiểm tra xem nó có thể bị lạm dụng không?”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Agent kiểm tra việc xác thực callback, tìm các lần đọc giá hoặc số dư mà khoản vay có thể làm thay đổi, rồi xác nhận kết quả bằng kiểm thử.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

Flash Loan Attack Playbook

Flash loans give the attacker uncollateralized capital for a single transaction. They're not vulnerabilities themselves — they're a force multiplier for existing bugs. Sources: Aave, Balancer, Uniswap V2/V3, Maker, dYdX (deprecated).

Common attack patterns

1. Oracle amplification (most common)

Use loan to push a price, then trigger price-dependent action. Cross-reference: see oracle-manipulation/SKILL.md.

2. Unauthenticated callback handler

function executeOperation(
    address[] calldata assets,
    uint256[] calldata amounts,
    uint256[] calldata premiums,
    address initiator,                     // ← UNVALIDATED in many contracts
    bytes calldata params
) external returns (bool) {
    // Anyone can call this with fake `initiator`
    // and have the contract do whatever the params say
}

Bug: initiator and msg.sender == pool checks are missing. Attack: call executeOperation directly with no actual loan, malicious params → contract does the operation anyway.

3. Governance attack via flash loan

// Vulnerable governance:
function propose() external {
    require(getVotes(msg.sender) > THRESHOLD);
    // ...
}
function getVotes(address user) public view returns (uint256) {
    return token.balanceOf(user);  // ← reads SPOT balance, not snapshot
}

Attack: flash-loan governance tokens, propose malicious change (e.g., upgrade contract to drain), vote with the loaned tokens, execute, repay loan. MakerDAO had this pattern; mitigated by checkpoint-based voting.

4. Liquidity manipulation

Pool that uses totalSupply() or balanceOf(pool) for share math:

function deposit(uint256 amt) external {
    uint256 shares = (amt * totalSupply()) / underlying.balanceOf(address(this));
    _mint(msg.sender, shares);
    underlying.transferFrom(msg.sender, address(this), amt);
}

Attacker:

  1. Flash-loan and deposit (becomes 99% of pool)
  2. Donate underlying to inflate underlying.balanceOf artificially
  3. Next depositor's shares = amt * totalSupply / inflatedBalance ≈ 0
  4. Withdraw → drain This is the ERC4626 inflation attack (also covered in reentrancy/ overlap).

5. Liquidation arbitrage gone wrong

A liquidator that uses flash loans to repay debt before claiming collateral — usually benign. The bug: liquidation health check happens before the seizure, but the seizure happens via callback into a manipulable function. Reentrancy + oracle dependent.

Audit steps

1. Locate flash-loan integrations

grep -rn 'executeOperation\|flashLoan\|onFlashLoan\|flashCallback\|aaveFlashLoan' src/

2. For each handler

Check:

  • msg.sender == known_pool (whitelist callback origin)
  • initiator == address(this) (only respond to your own loans)
  • ReentrancyGuard on the public function that takes the loan
  • The fee is correctly accounted (amounts[i] + premiums[i] is repaid)

3. For each price-reading function

See oracle-manipulation/SKILL.md.

4. For each share-mint / share-burn function

Check ERC4626 virtual-shares mitigation:

// OpenZeppelin ERC4626 v4.7+
function _convertToShares(uint256 assets, MathUpgradeable.Rounding rounding) internal view virtual override returns (uint256) {
    return _initialConvertToShares(assets, rounding);  // adds 10**18 to total supply for share calc
}

If using legacy ERC4626 or custom vault math, write a Foundry test that:

  1. Attacker deposits 1 wei → gets 1 share
  2. Attacker direct-transfers 1e18 underlying to vault
  3. Victim deposits 1e18 → gets 0 shares (rounding)
  4. Attacker withdraws 1 share → gets 100% of pool
foundry_inflation_test(vault_address="...", underlying="...", target_path="src/Vault.sol")

Severity calibration

Pattern Severity if confirmed
Unauth callback handler → arbitrary execution Critical 10.0
Flash-loan oracle manipulation drains protocol Critical 9-10
Governance attack possible Critical 9-10 (if exec results in fund loss)
ERC4626 inflation attack on live vault Critical 9
Liquidation reentrancy via callback High 8
Theoretical (low liquidity, manipulation cost > attack profit) Medium-Low

PoC template (Foundry)

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "forge-std/Test.sol";
import "@aave-v3/interfaces/IPool.sol";

contract Test_FlashAttack is Test {
    IPool constant AAVE = IPool(0x87870Bca3F3fD63
Câu hỏi thường gặp

Bản thân flash loan có phải là lỗi không?

Không. Playbook nói flash loan là công cụ khuếch đại cho các lỗi đã có.

Lỗi callback phổ biến là gì?

Không kiểm tra người gọi có đúng là pool cho vay không, và người khởi tạo có hợp lệ không.

Các nhóm phòng tránh bằng cách nào?

Bằng cách xác thực người gọi và tránh đưa ra quyết định quan trọng dựa trên số dư hoặc giá tức thời (spot).

Chi tiết

Mô tả gốc của tác giả

Flash-loan exploit patterns — callback reentrancy, oracle amplification, governance attacks, unauthenticated callback handlers.

repo: BitterSecurity/Decepticon

Độ phổ biến

Sao GitHub
6.5k
Vị trí xếp hạng
#14007
Giấy phép
Apache-2.0
Cập nhật
1/11/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự