slither是Solidity的静态分析工具,工具本身会自动检查合约代码并给出一些安全建议。
该工具还自带了一些print工具,对于合约的功能分析也非常有用。
此文简单说明slither工具的安装和使用。
原理
- 通过官方solc工具生成ast抽象语法树json文件(solc --ast-json contract.sol)
- 解析ast-json文件,生成dot格式的graph源文件
- 将dot文件转为图片格式(例如png)
安装
安装solc版本管理工具
pip3 install solc-select
solc-select use 0.5.17
安装分析工具
pip3 install slither-analyzer --no-use-pep517
安装graphviz绘图工具
brew install graphviz
分析
以Ronin跨链桥合约为例,合约地址:https://etherscan.io/address/0x1a2a1c938ce3ec39b6d47113c7955baa9dd454f2
将合约代码保存为一个proxy.sol
文件中。
查看合约间的继承关系:执行如下命令会生成 proxy.sol.inheritance-graph.dot
文件
slither proxy.sol --print inheritance-graph // 多个合约生成一个依赖关系图
将dot文件格式转换为png图片:
dot proxy.sol.inheritance-graph.dot -Tpng -o proxy.png
查看其它信息:
slither proxy.sol --print call-graph // 针对每个合约,生成多个dot文件
slither proxy.sol --print cfg // 针对每个函数,生成多个dot文件
如果合约比较复杂,绘制出的调用关系图会比较乱,所以一般就用于从整体的角度浏览各个合约间的继承关系。
./graph.sh
slither $1 --print inheritance-graph
dot $1.inheritance-graph.dot -Tpng -o $1.png
其它使用见官方wiki:
https://github.com/crytic/slither/wiki/Printer-documentation#data-dependencies