From 7da47e0047a3b987c92ad8169d1423c2edf99c8c Mon Sep 17 00:00:00 2001 From: jackcooljackG <85824334+jackcooljackG@users.noreply.github.com> Date: Mon, 23 Sep 2024 03:22:34 +0800 Subject: [PATCH] Update Jackchou.md WTF solidity 101 ,1-4 --- Jackchou.md | 71 +++++++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 37 deletions(-) diff --git a/Jackchou.md b/Jackchou.md index 21f7af5a..7ea38017 100644 --- a/Jackchou.md +++ b/Jackchou.md @@ -51,43 +51,40 @@ timezone: Australia/Sydney # 澳大利亚东部标准时间 (UTC+10) ### 2024.09.23 學習內容: -- A 系列的 Ethernaut CTF, 之前做了差不多了. POC: [ethernaut-foundry-solutions](https://github.com/SunWeb3Sec/ethernaut-foundry-solutions) -- A 系列的 QuillAudit CTF 題目的網站關掉了, 幫大家收集了[題目](./Writeup/SunSec/src/QuillCTF/), 不過還是有幾題沒找到. 有找到題目的人可以在發出來. -- A 系列的 DamnVulnerableDeFi 有持續更新, 題目也不錯. [Damn Vulnerable DeFi](https://github.com/theredguild/damn-vulnerable-defi/tree/v4.0.0). -- 使用 [Foundry](https://book.getfoundry.sh/) 在本地解題目, 可以參考下面 RoadClosed 為例子 -- ``forge test --match-teat testRoadClosedExploit -vvvv`` -#### [QuillAudit CTF - RoadClosed](./Writeup/SunSec/src/QuillCTF/RoadClosed.sol) -``` - function addToWhitelist(address addr) public { - require(!isContract(addr), "Contracts are not allowed"); - whitelistedMinters[addr] = true; - } - - function changeOwner(address addr) public { - require(whitelistedMinters[addr], "You are not whitelisted"); - require(msg.sender == addr, "address must be msg.sender"); - require(addr != address(0), "Zero address"); - owner = addr; - } - - function pwn(address addr) external payable { - require(!isContract(msg.sender), " Contracts are not allowed"); - require(msg.sender == addr, "address must be msg.sender"); - require(msg.sender == owner, "Must be owner"); - hacked = true; - } - - function pwn() external payable { - require(msg.sender == pwner); - hacked = true; - } -``` -- 解決這個題目需要成為合約的 owner 和 hacked = true. -- On-chain: 可以透過 ``cast send`` 或是 forge script 來解. -- Local: 透過 forge test 通常是在local解題, 方便 debug. -- RoadClosed 為例子我寫了2個解題方式. testRoadClosedExploit 和 testRoadClosedContractExploit (因為題目有檢查msg.sender是不是合約, 所以可以透過constructor來繞過 isContract) -- [POC](./Writeup/SunSec/test/QuillCTF/RoadClosed.t.sol) - +- For this week, I will target on finishing solidity 101 +- Finished solidity 101, 1-4 + - 3.function + ``` + // SPDX-License-Identifier: MIT + pragma solidity ^0.8.4; + contract Quiz3{ + // complete following funciton, let it return the sum of x and y + function sum(uint x, uint y) pure external returns (uint sumXY){ + sumXY = x+y; + } + } + ``` + output + ``` + decoded input { + "uint256 x": "2", + "uint256 y": "3" + } + decoded output { + "0": "uint256: sumXY 5" + } + ``` + - In Solidity, functions can be marked as `pure`, `view`, or `payable` to indicate their behavior. `pure` functions do not modify the contract's state, while `view` functions can read the state but not modify it. `payable` functions can receive Ether. + - 4.function Output + - There are two keywords related to function output: return and returns: + ``` + returns is added after the function name to declare variable type and variable name; + return is used in the function body and returns desired variables. + // returning multiple variables + function returnMultiple() public pure returns(uint256, bool, uint256[3] memory){ + return(1, true, [uint256(1),2,5]); + } + ``` ###