regast
1.0.0
警告
自2023年3月以來一直沒有維護Regast 。如果您想運行它,它很可能在解析階段會出現錯誤。
但是,請隨時查看代碼,以了解如何實現自己的靜態分析儀。
Remast是一種靜態分析儀,用於識別固體代碼庫中的安全漏洞和氣體優化。
它的靈感來自Slither,Solstat和4naly3er等工具,但具有以下差異:
Recast需要Python 3.10或更高。
首先,克隆該存儲庫及其子模型:
git clone --recurse-submodules https://github.com/MiloTruck/regast.git
cd regast使用pip或setuptools安裝重新安裝重新安裝:
# Using pip
pip3 install .
# Using setuptools
python3 setup.py install安裝後,可以刪除存儲庫:
cd ..
rm -r regastregast命令可以在.sol文件或包含堅固文件的文件夾上使用:
$ regast --help
usage: __main__.py [-h] [-d <detector>] [-c <classifications>] [-s <scope>] [-r <filename>] <contract>
Scan for vulnerabilities based on regex or AST queries.
positional arguments:
<contract> .sol file or folder containing .sol files to scan
options:
-h, --help show this help message and exit
-d <detector>, --detectors <detector>
.py file or folder containing .py files which implement detectors
-c <classifications>, --classifications <classifications>
Comma-separated list of classifications: GAS, NC, LOW, MEDIUM, HIGH
-s <scope>, --scope <scope>
Text file containing a list of contracts in scope
-r <filename>, --report <filename>
Generate a markdown report in <filename>.md
以下是當前實現的檢測器,該檢測器默認運行。來自4naly3er和Solstat的大多數檢測器將來都將包括在內。
| 探測器 | 描述 | 分類 |
|---|---|---|
address_balance | 使用selfbalance()代替address(this).balance 。 | 氣體 |
address_zero | 使用組件檢查address(0) 。 | 氣體 |
assign_update_array_value | 使用arr[i] += n而不是arr[i] = arr[i] + n更新數組值。 | 氣體 |
bool_comparison | 不要將布爾人與true或false進行比較。 | 氣體 |
bool_storage | 使用bool來存儲開銷。 | 氣體 |
byte_constant | bytes常數比string常數更有效。 | 氣體 |
cache_array_length | 緩存陣列長度在前面。 | 氣體 |
custom_error | 使用自定義錯誤而不是require語句。 | 氣體 |
initialize_default_value | 具有默認值的變量的不必要的初始化 | 氣體 |
long_revert_string | require帶有錯誤消息的語句。 | 氣體 |
post_increment | ++i價格低於i++或i += 1 。 | 氣體 |
private_constant | 宣布常數為private ,而不是非公共的,以節省天然氣。 | 氣體 |
shift_arithmetic | 在可能的情況下使用<<和>>而不是乘法/除法。 | 氣體 |
split_require_statements | 使用單獨的require語句而不是&& 。 | 氣體 |
unchecked_increment | “可以在未循環中宣布unchecked增量”。 | 氣體 |
unsigned_comparison | 使用!= 0而不是> 0用於無符號整數比較。 | 氣體 |
有關如何編寫自定義檢測器的信息,請參閱docs/writing-custom-detectors.md 。
Regast能夠生成有關發現問題的Markdown報告。例如,請參閱以下示例報告:
Regast建立在樹頂派森(Tree-Sitter-Python)的頂部,該樹木為樹木解析庫提供了Python綁定。固體的語法取自樹特性的態度。
tree-sitter首先將堅固源代碼轉換為多個抽象語法樹(AST)。然後,將這些AST中的每個節點解析為相應的Python類。解析完成後,各個檢測器通過這些Python類查詢AST,以識別共同的漏洞模式。
Regast的大多數代碼都在以下目錄中:
regast/core包含代表AST一部分的Python類。regast/detectors包含默認情況下Recast運行的檢測器。regast/parsing包含將AST從tree-sitter解析為Python類的邏輯。