0

Suppose I have a contract below:

contract Hack {

    Switch private target;

    constructor(address _target) {
        target = Switch(_target);
    }

    function test() external pure returns (bytes memory){

       // 'hashOfFunction' is calculated via generateData() function.
        bytes memory hashOfFunction = hex"20606e15b70f0894e0e83ae9593ae406a94abb5adcfcf0d169c6784f02198dc3";
        return abi.encodeWithSignature("flipSwitch(bytes)", hashOfFunction);
    
    }
    
    function generateData() external pure returns (bytes32){
        return keccak256("turnSwitchOff()"); 
    }



    function turnSwitchOff() public {
         // extra code....
    }

    function flipSwitch(bytes memory _data) public {
         // extra code....
    }


}

The calldata output obtained by calling the test function is :---

// OutPut from test 
    // 0x
    // 30c13ade - function signature of flipSwitch(bytes) function.
    // 0000000000000000000000000000000000000000000000000000000000000020 - Offset of 32 bytes
    // 0000000000000000000000000000000000000000000000000000000000000020 - Size of parameter turnSwitchOff() 32 bytes
    // 20606e15b70f0894e0e83ae9593ae406a94abb5adcfcf0d169c6784f02198dc3 - signature of turnSwitchOff()   

From the Output we can see that Offset is 32 bytes, but if we want to change it to 64 bytes or 96 bytes is it possible to do that? I mean is it possible to only change the Offset and keep all the calldata output same?

1 Answer 1

1
function test() external pure returns (bytes memory){
    bytes memory hashOfFunction = hex"20606e15b70f0894e0e83ae9593ae406a94abb5adcfcf0d169c6784f02198dc3";
    bytes memory a = abi.encodeWithSignature("flipSwitch(bytes)", hashOfFunction);
    a[0x23] = 0x40;
    return a;
}

The calldata output:

0x30c13ade
0000000000000000000000000000000000000000000000000000000000000040
0000000000000000000000000000000000000000000000000000000000000020
20606e15b70f0894e0e83ae9593ae406a94abb5adcfcf0d169c6784f02198dc3
2
  • I think this the only way to achieve this, just want to confirm you are updating the 35th index of bytes array a. Right?
    – Shubham
    Jan 10 at 7:42
  • Yeah, and in this case. Jan 10 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.