智慧合約檢測工具Mythril掃描合約教學以及如何安裝到docker

更新 發佈閱讀 11 分鐘

Mythril 是可以對Solidity 程式碼及EVM 字節碼的安全分析工具,使用符號執行等方式檢測合約,可以幫助開發和安全人員發現合約中潛在的漏洞和弱點。

Mythril是由ConsenSys Diligence所開發,檢測為乙太坊、Hedera、Quorum、Vechain、Roostock、Tron 和其他與 EVM 兼容的區塊鏈構建的智慧合約中的安全漏洞,目前為免費的開源工具,可以在GitHub找到相關的原始碼。

raw-image


安裝方式:

docker

docker pull mythril/myth

pip

pip3 install mythril

其他安裝方式可以查看文件
使用docker 運行mythril並分析本地合約的方式:

docker run -v $(pwd): (path) mythril/myth analyze /(path)/(contract name).sol

這邊提供一個有重入漏洞的合約範例

// 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;
  }
}
raw-image


完整掃描結果如下:

==== External Call To User-Supplied Address ====
SWC ID: 107
Severity: Low
Contract: EtherStore
Function name: withdraw()
PC address: 368
Estimated Gas Usage: 7278 - 62222
A call to a user-supplied address is executed.
An external message call to an address specified by the caller is executed. Note that the callee account might contain arbitrary code and could re-enter any function within this contract. Reentering the contract in an intermediate state may lead to unexpected behaviour. Make sure that no state modifications are executed after this call and/or reentrancy guards are in place.
--------------------
In file: /Downloads/reentrancy.sol:42
msg.sender.call{value: bal}("")
--------------------
Initial State:
Account: [CREATOR], balance: 0x21000537b4, nonce:0, storage:{}
Account: [ATTACKER], balance: 0x20200000000000000, nonce:0, storage:{}
Transaction Sequence:
Caller: [CREATOR], calldata: , value: 0x0
Caller: [ATTACKER], function: deposit(), txdata: 0xd0e30db0, value: 0x1
Caller: [ATTACKER], function: withdraw(), txdata: 0x3ccfd60b, value: 0x0
==== State access after external call ====
SWC ID: 107
Severity: Medium
Contract: EtherStore
Function name: withdraw()
PC address: 556
Estimated Gas Usage: 7278 - 62222
Write to persistent state following external call
The contract account state is accessed after an external call to a user defined address. To prevent reentrancy issues, consider accessing the state only before the call, especially if the callee is untrusted. Alternatively, a reentrancy lock can be used to prevent untrusted callees from re-entering the contract in an intermediate state.
--------------------
In file: /Downloads/reentrancy.sol:45
balances[msg.sender] = 0
--------------------
Initial State:
Account: [CREATOR], balance: 0x21200082d2d9, nonce:0, storage:{}
Account: [ATTACKER], balance: 0x4801000000228c000, nonce:0, storage:{}
Transaction Sequence:
Caller: [CREATOR], calldata: , value: 0x0
Caller: [ATTACKER], function: deposit(), txdata: 0xd0e30db0, value: 0x1
Caller: [ATTACKER], function: withdraw(), txdata: 0x3ccfd60b, value: 0x0


掃描鏈上合約:

mythril可以藉由設定rpc掃描鏈上的智慧合約。

掃描主網(mainnet)合約

Mythril 默認會嘗試查詢infura,因此需要前往infura註冊帳號。

Step 1 : create new api key

raw-image

Step 2 : manage key

raw-image

Step 3 :  api key

raw-image
docker run --rm mythril/myth analyze -a [contract address] --rpc infura-mainnet https://mainnet.infura.io/v3/0df6422189fd41f7bf22587cb3d9bda2 --rpctls True --infura-id [yourApiKey]

掃描BSC(Binance Smart Chain)合約

docker run --rm mythril/myth analyze -a [contract address] --rpc bsc-dataseed1.binance.org:443 --rpctls True --infura-id [yourApiKey]

掃描本地鏈上的合約 (以下以Ganache舉例)

在docker中使用時,與文件有些許不同,需要使用以下指令。

Step 1 : 下載Ganache並在Remix上設定環境

raw-image


Step 2 : 發布合約後執行以下指令

docker run --rm mythril/myth analyze -a [contract address] --rpc host.docker.internal:[portNumber]


輸出格式

Mythril預設將掃描結果呈現在終端機,可以使用 -o 修改輸出格式。

輸出的檔案格式為text、markdown、json和jsonv2。

結論

Mythril旨在發現常見漏洞,並無法精確發現應用程序業務邏輯中的問題,自動化工具有助於檢查常見的 Solidity 漏洞或缺少​​的智慧合約最佳實踐的案例,檢測工具容易受到誤報的影響,需要藉由手動驗證進行協助。



作者Alice目前為區塊鏈安全從業人員,將來也會持續在Vocus以及medium上分享相關的研究,如果喜歡我的文章歡迎追蹤我的帳號喔!

另外,我已經加入由趨勢科技防詐達人所成立的方格子專題-《區塊鏈生存守則》,在那裡我會跟其他優質的創作者一起帶大家深入瞭解區塊鏈,並隨時向大家更新區塊鏈資安事件

> 追蹤《區塊鏈生存守則》學習如何在區塊鏈的世界保護自己

> 關注防詐達人獲得其他最新詐騙情報

留言
avatar-img
防詐達人的沙龍
37會員
175內容數
我們整理了web3相關的熱門資安問題,包含加密貨幣投資詐騙、盜版NFT、空投釣魚和區塊鏈重大資安事件懶人包等等,並提供最完整的辨識方法教學,讓大家從0到1學習如何保護自己
你可能也想看
Thumbnail
在 vocus 與你一起探索內容、發掘靈感的路上,我們又將啟動新的冒險——vocus App 正式推出! 現在起,你可以在 iOS App Store 下載全新上架的 vocus App。 無論是在通勤路上、日常空檔,或一天結束後的放鬆時刻,都能自在沈浸在內容宇宙中。
Thumbnail
在 vocus 與你一起探索內容、發掘靈感的路上,我們又將啟動新的冒險——vocus App 正式推出! 現在起,你可以在 iOS App Store 下載全新上架的 vocus App。 無論是在通勤路上、日常空檔,或一天結束後的放鬆時刻,都能自在沈浸在內容宇宙中。
Thumbnail
vocus 慶祝推出 App,舉辦 2026 全站慶。推出精選內容與數位商品折扣,訂單免費與紅包抽獎、新註冊會員專屬活動、Boba Boost 贊助抽紅包,以及全站徵文,並邀請你一起來回顧過去的一年, vocus 與創作者共同留下了哪些精彩創作。
Thumbnail
vocus 慶祝推出 App,舉辦 2026 全站慶。推出精選內容與數位商品折扣,訂單免費與紅包抽獎、新註冊會員專屬活動、Boba Boost 贊助抽紅包,以及全站徵文,並邀請你一起來回顧過去的一年, vocus 與創作者共同留下了哪些精彩創作。
Thumbnail
  Soildity是一種風格類似JavaScript的強型別高階語言,專門用來進行智能合約的開發及編程。本文敘述solidity的獨特之處。   
Thumbnail
  Soildity是一種風格類似JavaScript的強型別高階語言,專門用來進行智能合約的開發及編程。本文敘述solidity的獨特之處。   
Thumbnail
本篇章會以實際的程式碼來說明開發Solidity的組成脈絡與重要的區塊,讓我們不再害怕閱讀艱澀難懂的程式碼,並且具備基礎知識以後也能看懂外面的合約。 開源程式碼常常會面臨到法律的問題,因此License的宣告就變得非常重要,而Solidity也考慮到這一塊,因此特別規劃了一塊License宣告的區塊
Thumbnail
本篇章會以實際的程式碼來說明開發Solidity的組成脈絡與重要的區塊,讓我們不再害怕閱讀艱澀難懂的程式碼,並且具備基礎知識以後也能看懂外面的合約。 開源程式碼常常會面臨到法律的問題,因此License的宣告就變得非常重要,而Solidity也考慮到這一塊,因此特別規劃了一塊License宣告的區塊
Thumbnail
上一篇我們介紹了智能合約的基本概念,而在開發智能合約之前, 建議先對智能合約具備基礎的概念, 往後進行開發時較容易融會貫通, 還沒閱讀的朋友可以參閱此篇「【開發智能合約 — Solidity系列】 淺談智能合約」。 理解完「智能合約」之後,相信大家已經開始手癢了吧! 應該很想開始動手完成第一個自己專
Thumbnail
上一篇我們介紹了智能合約的基本概念,而在開發智能合約之前, 建議先對智能合約具備基礎的概念, 往後進行開發時較容易融會貫通, 還沒閱讀的朋友可以參閱此篇「【開發智能合約 — Solidity系列】 淺談智能合約」。 理解完「智能合約」之後,相信大家已經開始手癢了吧! 應該很想開始動手完成第一個自己專
Thumbnail
Mythril 是可以對Solidity 程式碼及EVM 字節碼的安全分析工具,使用符號執行等方式檢測合約,可以幫助開發和安全人員發現合約中潛在的漏洞和弱點。
Thumbnail
Mythril 是可以對Solidity 程式碼及EVM 字節碼的安全分析工具,使用符號執行等方式檢測合約,可以幫助開發和安全人員發現合約中潛在的漏洞和弱點。
Thumbnail
下面我們就來講解Import該怎麼使用及可以調用外部在GitHub上面寫好的合約,比如說OpenZeppelin的ERC20合約。
Thumbnail
下面我們就來講解Import該怎麼使用及可以調用外部在GitHub上面寫好的合約,比如說OpenZeppelin的ERC20合約。
Thumbnail
此篇文是在練習並熟悉solidity與remix的操作
Thumbnail
此篇文是在練習並熟悉solidity與remix的操作
Thumbnail
介紹了這麼多Solidity的函數和方法,今天來寫一個簡單的錢包,順便介紹一下payable。
Thumbnail
介紹了這麼多Solidity的函數和方法,今天來寫一個簡單的錢包,順便介紹一下payable。
Thumbnail
Solidity:用來撰寫智能合約的程式語言之一,也是最多人用的 Vyper:用來撰寫智能合約的程式語言之二,Vyper 是一種面向合約的Pythonic編程語言
Thumbnail
Solidity:用來撰寫智能合約的程式語言之一,也是最多人用的 Vyper:用來撰寫智能合約的程式語言之二,Vyper 是一種面向合約的Pythonic編程語言
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News