Skip to content

Commit

Permalink
Update Jackchou.md
Browse files Browse the repository at this point in the history
WTF solidity 101 ,1-4
  • Loading branch information
jackcooljackG authored Sep 22, 2024
1 parent fa8124e commit 7da47e0
Showing 1 changed file with 34 additions and 37 deletions.
71 changes: 34 additions & 37 deletions Jackchou.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
```
###
<!-- Content_END -->

0 comments on commit 7da47e0

Please sign in to comment.