

用 Slither 在合約升級之前做檢查. Slither 是 Solidity 的靜態分析器,由資安公司 tr...
source link: https://medium.com/taipei-ethereum-meetup/%E7%94%A8-slither-%E5%9C%A8%E5%90%88%E7%B4%84%E5%8D%87%E7%B4%9A%E4%B9%8B%E5%89%8D%E5%81%9A%E6%AA%A2%E6%9F%A5-350502aa8aba
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Usage
可升級合約要升級時,這時候的 codebase 則應該要有三個合約,Proxy, LogicContract_V1 和 LogicContract_V2 三個,範例合約如下:
這邊的合約都有問題,檢查 storage collision 時,可以抓出以下問題:
- Proxy 跟兩個 Logic Contract 之間:範例中,V1, V2 都和 Proxy 有 storage collision 的問題
- Logic Contract 之間:V1, V2 中 _val 變數存在不同的 slot 裡面
使用 slither-check-upgradeability 檢查
如果使用像是 OpenZeppelin 等套件,則需要帶入 remappings 進去。這是讓 solc compiler 可以知道引入的 solidity 檔案位於何處。一般使用 hardhat 等 framework 會幫你設定好 remappings 讓 solc 知道從 node_modules 抓。
以下為範例:
- 檢查 V1 升級至 V2 有沒有漏洞
# bash
slither-check-upgradeability \
./LogicV1.sol LogicV1 \
--new-contract-filename ./LogicV2.sol \
--new-contract-name LogicV2 \
--solc-remaps '@openzeppelin/=lib/openzeppelin-contracts/'
輸出如下,V1._val 和 V2._a 位在同樣的 slot 上但卻是不同的變數名稱
INFO:Slither:
Different variables between LogicV1 (src/LogicV1.sol#4-11) and LogicV2 (src/LogicV2.sol#4-12)
LogicV1._val (src/LogicV1.sol#5)
LogicV2._a (src/LogicV2.sol#5)
Reference: https://github.com/crytic/slither/wiki/Upgradeability-Checks#incorrect-variables-with-the-v2
- 檢查 V1 跟 Proxy
# bash
slither-check-upgradeability \
./LogicV1.sol LogicV1 \
--proxy-filename ./Proxy.sol \
--proxy-name SampleProxy \
--solc-remaps '@openzeppelin/=lib/openzeppelin-contracts/'
輸出如下,V1._val 和 Proxy._addr 位在同樣的 slot 上但卻是不同的變數名稱
INFO:Slither:
Different variables between LogicV1 (src/LogicV1.sol#4-11) and SampleProxy (src/upgrade/Proxy.sol#6-16)
LogicV1._val (src/LogicV1.sol#5)
SampleProxy._addr (src/upgrade/Proxy.sol#7)
Reference: https://github.com/crytic/slither/wiki/Upgradeability-Checks#incorrect-variables-with-the-proxy
- 也同時檢查 V1, V2 和 Proxy
# bash
slither-check-upgradeability \
./LogicV1.sol LogicV1 \
--new-contract-filename ./LogicV2.sol \
--new-contract-name LogicV2 \
--proxy-filename ./Proxy.sol \
--proxy-name SampleProxy \
--solc-remaps '@openzeppelin/=lib/openzeppelin-contracts/'
以下為 V1-V2,V1-Proxy 和 V2-Proxy 檢查的輸出
INFO:Slither:
Different variables between LogicV1 (src/LogicV1.sol#4-11) and SampleProxy (src/upgrade/Proxy.sol#6-16)
LogicV1._val (src/LogicV1.sol#5)
SampleProxy._addr (src/upgrade/Proxy.sol#7)
Reference: https://github.com/crytic/slither/wiki/Upgradeability-Checks#incorrect-variables-with-the-proxy
INFO:Slither:
Different variables between LogicV2 (src/LogicV2.sol#4-12) and SampleProxy (src/upgrade/Proxy.sol#6-16)
LogicV2._a (src/LogicV2.sol#5)
SampleProxy._addr (src/upgrade/Proxy.sol#7)
Reference: https://github.com/crytic/slither/wiki/Upgradeability-Checks#incorrect-variables-with-the-proxy
INFO:Slither:
Different variables between LogicV1 (src/LogicV1.sol#4-11) and LogicV2 (src/LogicV2.sol#4-12)
LogicV1._val (src/LogicV1.sol#5)
LogicV2._a (src/LogicV2.sol#5)
Reference: https://github.com/crytic/slither/wiki/Upgradeability-Checks#incorrect-variables-with-the-v2
Recommend
-
5
靜態分析詐欺術: Windows x86下IDA Pro混淆技巧 Adr
-
44
Slither是第一个开源的针对Solidity语言的静态分析框架。Slither速度非常快,准确性也非常高,它能够在不需要用户交互的情况下,在几秒钟之内找到真正的漏洞。该工具高度可配置,并且提供了多种API来帮助研究人员审计和分析Solidity代码。...
-
40
A denial-of-service (DoS) vulnerability, dubbed ‘Gridlock,’ was publicly reported on July 1st in one of Edgew...
-
8
使用 lock 共用靜態物件 vs 每次新建物件之效能比較-黑暗執行緒前天提到 JScript.NET 跑 Eval() 在多緒執行出錯崩潰的案例,問題根源在當初覺得反覆編譯 JScript 建立組件並建立物件...
-
22
使用 Slither 來檢測你的 Solidity 智能合約程式碼以太坊的智能合約程式一旦發佈便無法更改,因此程式的安全性尤為重要。因此,使用檢測工具對程式碼進行檢測,可以發現一些常見的安全隱患,避免因程式漏洞引致的各種損失。
-
10
嵌入 .html/.css/.js 靜態檔案徹底實現單檔部署-黑暗執行緒 ASP.NET Core 極簡風 - Minimal API 提到只需加一行 UseFileServer(),ASP.NET Core 空白網站就可以像 ASP.NET MVC 一樣,將 .h...
-
9
使用 ASP.NET Minimal API-黑暗執行緒 昨天發現 Python3 有個 http.server 模組,能將包含 .html、.css、.js 的資料夾快速轉成靜態網站,遇到臨時性或簡單的測試需求,相當...
-
5
在 Linux 將目錄快速轉為靜態網站 2022-02-27 03:13 PM
-
10
任選資料庫存放 ASP.NET Core 靜態檔案-黑暗執行緒 上回提到 ASP.NET Core 架構大幅革新,大量以介面取代直接引用類別,並
-
7
In 大玩家做為技術小白,我如何用靜態管理6天時間學會電腦組裝 兒子幼升小,全家緊鑼密鼓。我簡單收拾幾件衣物、搬裝學習書桌耗時整日。電腦做...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK