I'm stuck with Ethernaut Alien Codex problem. I understood underflow attack except that codex[2²⁵⁶ - 1 - uint(keccak256(1)) + 1]
corresponds to slot 0. I referred to the below tables.
Slot # | Variable |
---|---|
0 | contact bool(1 bytes] & owner address (20 bytes), both fit on one slot |
1 | codex.length |
keccak256(1) | codex[0] |
keccak256(1) + 1 | codex[1] |
2²⁵⁶ - 1 | codex[2²⁵⁶ - 1 - uint(keccak256(1))] |
0 | codex[2²⁵⁶ - 1 - uint(keccak256(1)) + 1] --> can write slot 0! |
All I know is Storage has 2²⁵⁶ slots and a dynamic array that had been attacked can control every slot in the Storage. What am I missing? The source code is below.
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.0;
import '../helpers/Ownable-05.sol';
contract AlienCodex is Ownable {
bool public contact;
bytes32[] public codex;
modifier contacted() {
assert(contact);
_;
}
function make_contact() public {
contact = true;
}
function record(bytes32 _content) contacted public {
codex.push(_content);
}
function retract() contacted public {
codex.length--;
}
function revise(uint i, bytes32 _content) contacted public {
codex[i] = _content;
}
}