Caracal 是一个针对 Starknet 智能合约的 SIERRA 表示的静态分析工具。
预编译的二进制文件可在我们的发布页面获取。如果您使用的是 Cairo 编译器 1.x.x,请使用 v0.1.x 二进制文件;如果您使用的是 Cairo 编译器 2.x.x,请使用 v0.2.x。
您需要安装 Rust 编译器和 Cargo。 从 git 构建:
cargo install --git https://github.com/crytic/caracal --profile release --force
从本地副本构建:
git clone https://github.com/crytic/caracal
cd caracal
cargo install --path . --profile release --force
列出检测器:
caracal detectors
列出打印器:
caracal printers
要使用独立的 Cairo 文件,并且您有本地的 Cairo 编译器二进制文件,只需将其指向该文件即可。否则,将使用捆绑的编译器,并且您需要通过 --corelib 命令行选项或设置 CORELIB_PATH 环境变量来传递 corelib 库的路径。
运行检测器:
caracal detect path/file/to/analyze
caracal detect path/file/to/analyze --corelib path/to/corelib/src
运行打印器:
caracal print path/file/to/analyze --printer printer_to_use --corelib path/to/corelib/src
如果您有一个包含多个文件和合约的 Cairo 项目,可能需要通过 --contract-path 指定合约。如果可用,将使用本地的 Cairo 编译器二进制文件;否则将使用捆绑的编译器。在后一种情况下,您还需要按照独立使用情况中所述指定 corelib。路径是 cairo_project.toml 所在的目录。
运行检测器:
caracal detect path/to/dir
caracal detect path/to/dir --contract-path token::myerc20::... token::myerc721::...
运行打印器:
caracal print path/to/dir --printer printer_to_use
如果您有一个使用 Scarb 的项目,您需要在 Scarb.toml 中添加以下内容:
[[target.starknet-contract]]
sierra = true
[cairo]
sierra-replace-ids = true
然后传递 Scarb.toml 所在目录的路径。 运行检测器:
caracal detect path/to/dir
运行打印器:
caracal print path/to/dir --printer printer_to_use
Cairo 列表示该检测器适用的编译器版本。
cfg:将每个函数的 CFG 导出为 .dot 文件callgraph:将函数调用图导出为 .dot 文件请查看以下主题的 wiki:
| 编号 | 检测器 | 检测内容 | 影响 | 置信度 | Cairo 版本 |
|---|
| 1 | controlled-library-call | 使用用户控制的类哈希进行库调用 | 高 | 中 | 1 & 2 |
| 2 | unchecked-l1-handler-from | 检测未进行 from 地址检查的 L1 处理器 | 高 | 中 | 1 & 2 |
| 3 | felt252-unsafe-arithmetic | 检测用户控制的 felt252 类型操作,该类型不安全的溢出/下溢 | 中 | 中 | 1 & 2 |
| 4 | reentrancy | 检测在外部调用之前读取存储变量并在之后写入的情况 | 中 | 中 | 1 & 2 |
| 5 | read-only-reentrancy | 检测视图函数在外部调用之后读取存储变量的情况 | 中 | 中 | 1 & 2 |
| 6 | unused-events | 定义了但未发出的事件 | 中 | 中 | 1 & 2 |
| 7 | unused-return | 未使用的返回值 | 中 | 中 | 1 & 2 |
| 8 | unenforced-view | 函数具有视图修饰符但修改了状态 | 中 | 中 | 1 |
| 9 | tx-origin | 检测使用交易发起地址作为访问控制 | 中 | 中 | 2 |
| 10 | unused-arguments | 未使用的参数 | 低 | 中 | 1 & 2 |
| 11 | reentrancy-benign | 检测在外部调用之后写入存储变量但在之前未读取的情况 | 低 | 中 | 1 & 2 |
| 12 | reentrancy-events | 检测在外部调用之后发出事件导致事件乱序的情况 | 低 | 中 | 1 & 2 |
| 13 | dead-code | 从未使用过的私有函数 | 低 | 中 | 1 & 2 |
| 14 | use-after-pop-front | 检测在移除数组或 span 元素后使用该数组或 span 的情况 | 低 | 中 | 1 & 2 |