HTB Blockchain Challenge - False Bid
False Bidding
Code
1 | // SPDX-License-Identifier: UNLICENSED |
分析
- 很明顯可以看到在
withdrawFromAuction
裡存在 Re-entrancy Attack
1 | function withdrawFromAuction() public topBidderOnly { |
- 可以透過
payable(withdrawer.addr).call{value: withdrawer.bid / 2}("");
進入 fallback function 執行 Re-entrancy - 但是無法直接新增bidder
- 觀察recieve()
1 | if ((uint64(msg.value) >= 2 * topBidder().bid) && (msg.sender != topBidder().addr) && (!blacklisted[msg.sender])&& (_isPayable(msg.sender))) { |
- 有以下幾個條件
- msg.valu >= 2* topBidder().bid
- 初始bid為 0.5 ether
- 也就是我們要先儲值1 ether 到exploit合約
- 然後花費 1 ether 去 bid
- msg.sender != topBidder().addr
- 新建立合約
- !blacklisted[msg.sender]
- 沒有被列入blacklist -> 沒有withdraw
- _isPayable(msg.sender)
- payable contract
- 有fallback function
- msg.valu >= 2* topBidder().bid
- withdrawFromAuction
- payable(withdrawer.addr).call{value: withdrawer.bid / 2}(“”);
- 但仔細觀察可以發現,這題不是只有要觸發reentrancy
- 而是在bid之後,會設定一個timeout
- 程式使用uint32,稍微計算一下之後可以發現,大約執行81次之後會integer overflow,也就會回到31556926,如此一來便可以執行ClaimPrize
- 但是題目給的account只有30ether
- 要想辦法再60次內讓 uint32 timeout integer overflow
- 但是!!!
- 但是!!!
- 題目的 timeout是3821613406(0x00e3c9315e)
- 所以執行15次就可以了
- 但由於執行integer overflow會導致contract無法再進行bid
- 因此我們需要生成另一個新的合約來transferKey
- 可以使用另一個合約來建構原本的Exploit合約來一次性地完成攻擊
Exploit
- Exploit.sol
1 | // SPDX-License-Identifier: UNLICENSED |
- Attacker.sol
1 |
|
- deploy contract
forge create src/Attacker.sol:Attacker --rpc-url http://159.65.20.166:32026/rpc --private-key {{private key}} --value 25ether --constructor-args "0x5557a3c564A9D35F2c3CCc3bC53Fc2617b117f64"
- Exploit
cast send --rpc-url http://159.65.20.166:32026/rpc --private-key {{private key}} 0x7828aF85c4C0638419a9e0564Ec7942A197bf279 "complete_attack(address)" 0x57Eea081F89f3924F8c429D0D5f4b4B53317dA62
- Check is solved
cast call --rpc-url http://159.65.20.166:32026/rpc --private-key {{private key}} 0x5557a3c564A9D35F2c3CCc3bC53Fc2617b117f64 "isSolved(address)" 0x57Eea081F89f3924F8c429D0D5f4b4B53317dA62
All articles on this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated.
Comments