# Slither — Smart Contract Security के लिए Static Analysis
Smart contract development में security सबसे ज़रूरी है। Traditional software के विपरीत, deployed contracts immutable होते हैं और अक्सर millions of dollars को handle करते हैं। Slither, जिसे Trail of Bits ने develop किया है, Solidity के लिए सबसे powerful open-source static analysis framework है। यह automatically vulnerabilities, code smells, और optimization opportunities को detect करता है deploy करने से पहले।
Slither क्या है?
Slither एक static analysis tool है जो आपके Solidity code को execute किए बिना examine करता है। यह एक custom intermediate representation (SlithIR) का उपयोग करके deep analysis करता है और 90 से ज़्यादा vulnerability patterns को detect करता है।
Key Features
- 90+ vulnerability detectors जो common और advanced security issues को cover करते हैं
- Zero false positives अधिकतर critical vulnerabilities पर
- Fast execution — पूरे project को seconds में analyze करें
- Detailed output line numbers और suggested fixes के साथ
- Custom detector support project-specific rules के लिए
- CI/CD integration automated security checks के लिए
- Multiple output formats (text, JSON, GitHub Actions)
Slither क्या Detect करता है
Critical vulnerabilities:
- Reentrancy attacks
- Unprotected self-destruct
- Arbitrary storage writes
- Delegatecall to untrusted contracts
High severity issues:
- Integer overflow/underflow (pre-0.8.0)
- Uninitialized storage variables
- Incorrect access controls
- Shadowing state variables
Medium severity issues:
- Block timestamp manipulation
- Incorrect event parameters
- Missing return values
- Dangerous strict equalities
Gas optimizations:
- State variables जो constant/immutable हो सकते हैं
- Loops जिन्हें optimize किया जा सकता है
- Redundant storage reads
- Expensive operations in loops
Code quality:
- Naming conventions violations
- Dead code detection
- Unused variables
- Missing NatSpec documentation
Installation
Prerequisites
Slither को Python 3.8+ और solc (Solidity compiler) की ज़रूरत है।
Python install करें (अगर पहले से installed नहीं है):
# macOS
brew install python3
# Ubuntu/Debian
sudo apt install python3 python3-pip
# Windows
# python.org से download करें
solc install करें:
# solc-select का उपयोग करें (versions manage करने के लिए recommended)
pip3 install solc-select
solc-select install 0.8.26
solc-select use 0.8.26
Slither Install करें
pip3 install slither-analyzer
Installation verify करें:
slither --version
# Output: 0.10.4 (या later)
अपना First Analysis Run करें
Basic Usage
एक single contract को analyze करें:
slither MyContract.sol
पूरे Hardhat/Foundry project को analyze करें:
slither .
Slither automatically आपके project structure और configuration को detect करता है।
Example Output
चलिए एक vulnerable contract को analyze करते हैं:
// VulnerableBank.sol
pragma solidity ^0.8.26;
contract VulnerableBank {
mapping(address => uint256) public balances;
function deposit() public payable {
balances[msg.sender] += msg.value;
}
// VULNERABLE: Reentrancy attack
function withdraw(uint256 amount) public {
require(balances[msg.sender] >= amount);
(bool success, ) = msg.sender.call{value: amount}("");
require(success);
balances[msg.sender] -= amount;
}
}
Slither run करें:
slither VulnerableBank.sol
Output:
Reentrancy in VulnerableBank.withdraw(uint256) (VulnerableBank.sol#10-15):
External calls:
- (success) = msg.sender.call{value: amount}() (VulnerableBank.sol#12)
State variables written after the call(s):
- balances[msg.sender] -= amount (VulnerableBank.sol#14)
Reference: https://github.com/crytic/slither/wiki/Detector-Documentation#reentrancy-vulnerabilities
Slither reentrancy vulnerability को identify करता है और exact line को point करता है जहाँ state को external call के बाद modify किया गया है।
Slither Results को समझें
Severity Levels
Slither findings को impact और confidence के आधार पर categorize करता है:
Impact:
- High: Critical vulnerabilities जो funds के loss की ओर ले जा सकती हैं
- Medium: Issues जिन्हें certain conditions के तहत exploit किया जा सकता है
- Low: Code quality issues या minor optimizations
- Informational: Best practice recommendations
Confidence:
- High: बहुत संभावना है कि यह real issue है (कम false positives)
- Medium: Probable issue लेकिन manual verification की ज़रूरत है
- Low: Possible issue लेकिन intentional हो सकता है
Slither Report को पढ़ना
हर finding में शामिल है:
Results को Filter करना
केवल high/medium severity दिखाएं:
slither . --exclude-informational --exclude-low
Specific detectors को exclude करें:
slither . --exclude naming-convention,solc-version
केवल specific detectors include करें:
slither . --detect reentrancy-eth,arbitrary-send-eth
JSON output parsing के लिए:
slither . --json results.json
Common Vulnerabilities को Fix करना
Reentrancy (Critical)
Bad:
function withdraw(uint256 amount) public {
require(balances[msg.sender] >= amount);
(bool success, ) = msg.sender.call{value: amount}("");
require(success);
balances[msg.sender] -= amount; // External call के बाद state change
}
Good (Checks-Effects-Interactions pattern):
function withdraw(uint256 amount) public {
require(balances[msg.sender] >= amount);
balances[msg.sender] -= amount; // External call से पहले state change
(bool success, ) = msg.sender.call{value: amount}("");
require(success);
}
Better (ReentrancyGuard):
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
contract Bank is ReentrancyGuard {
function withdraw(uint256 amount) public nonReentrant {
require(balances[msg.sender] >= amount);
balances[msg.sender] -= amount;
(bool success, ) = msg.sender.call{value: amount}("");
require(success);
}
}
Uninitialized Storage Variables (High)
Bad:
function processData() internal {
Data storage data; // Uninitialized, slot 0 को point करता है!
data.value = 100; // Contract state को overwrite करता है
}
Good:
function processData() internal {
Data memory data; // Memory का उपयोग करें
data.value = 100;
}
Dangerous Strict Equalities (Medium)
Bad:
require(address(this).balance == 10 ether); // selfdestruct से bypass किया जा सकता है
Good:
require(address(this).balance >= 10 ether);
Custom Detectors
Slither आपको project-specific requirements के लिए custom detectors लिखने की अनुमति देता है।
Example: Missing Events का पता लगाना
from slither.detectors.abstract_detector import AbstractDetector, DetectorClassification
class MissingEvent(AbstractDetector):
ARGUMENT = 'missing-event'
HELP = 'State-changing functions should emit events'
IMPACT = DetectorClassification.LOW
CONFIDENCE = DetectorClassification.MEDIUM
def _detect(self):
results = []
for contract in self.contracts:
for function in contract.functions:
if function.is_public and function.can_send_eth():
if not function.events_called:
results.append(self.generate_result([
function, " should emit an event\n"
]))
return results
अपना custom detector run करें:
slither . --detect missing-event
CI/CD Integration
GitHub Actions
.github/workflows/slither.yml create करें:
name: Slither Analysis
on: [push, pull_request]
jobs:
analyze:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install Slither
run: pip3 install slither-analyzer
- name: Install dependencies
run: npm install
- name: Run Slither
run: slither . --exclude-informational --exclude-low --json slither-report.json
- name: Upload results
uses: actions/upload-artifact@v3
with:
name: slither-report
path: slither-report.json
Pre-commit Hook
.git/hooks/pre-commit में add करें:
#!/bin/bash
echo "Running Slither analysis..."
slither . --exclude-informational --exclude-low
if [ $? -ne 0 ]; then
echo "Slither found issues. Commit aborted."
exit 1
fi
Executable बनाएं:
chmod +x .git/hooks/pre-commit
Slither Configuration
अपने project root में slither.config.json create करें:
{
"detectors_to_exclude": "naming-convention,solc-version",
"exclude_informational": true,
"exclude_low": true,
"exclude_medium": false,
"exclude_high": false,
"solc": "solc",
"solc_args": "--optimize --optimize-runs 200"
}
Config के साथ run करें:
slither . --config-file slither.config.json
Best Practices
1. Slither को जल्दी और अक्सर Run करें
- हर commit पर pre-commit hooks के माध्यम से run करें
- CI/CD pipeline में integrate करें
- हर deployment से पहले analyze करें
2. पहले High-Confidence Issues को Fix करें
High impact + High confidence findings से शुरुआत करें। ये लगभग हमेशा real vulnerabilities होती हैं।
3. False Positives को समझें
कुछ detectors intentional patterns को flag कर सकते हैं। slither-disable-next-line का उपयोग करके document करें कि ये safe क्यों हैं:
// slither-disable-next-line reentrancy-eth
function withdraw() public {
// Intentional pattern, mutex से protected है
}
4. अन्य Tools के साथ Combine करें
Slither powerful है लेकिन exhaustive नहीं। इसके साथ उपयोग करें:
- MythX deeper symbolic analysis के लिए
- Echidna fuzzing के लिए
- Manticore symbolic execution के लिए
- Manual audits business logic के लिए
5. Slither को Updated रखें
pip3 install --upgrade slither-analyzer
नए detectors regularly add किए जाते हैं।
Conclusion
Slither smart contract security के लिए एक essential tool है। इसका fast, accurate analysis उन vulnerabilities को पकड़ता है जो exploits और funds के loss की ओर ले जा सकती हैं। Slither को अपने development workflow में integrate करके, आप vulnerable contracts को deploy करने के risk को significantly reduce कर देते हैं।
आज ही अपने contracts को secure करना शुरू करें — Slither install करें और अपना पहला analysis run करें। फिर Solingo के security-focused challenges के साथ secure coding patterns practice करें ताकि bulletproof smart contracts बना सकें।
अगले steps:
- अपने existing projects पर Slither run करें
- Automated analysis के लिए GitHub Actions set up करें
- Project-specific rules के लिए custom detectors के बारे में सीखें
- Slither को Echidna जैसे fuzzing tools के साथ combine करें