Template-Based Security Scanner for Smart Contracts & DApps
From Development to Deployment - Continuous Security at Every Stage
N3 (Nuclei for Web3) is a template-based security scanner inspired by ProjectDiscovery's Nuclei. Instead of rigid static analysis rules, N3 uses human-readable YAML templates to detect vulnerabilities in Solidity smart contracts.
Traditional scanners have fixed, hardcoded rules. N3 lets you:
- Create custom templates for new vulnerability patterns
- Share templates across teams and communities
- Update detection logic without changing code
- Combine multiple templates for comprehensive scans
- Calculate risk scores based on template matches
id: reentrancy-001
name: Reentrancy Vulnerability
severity: CRITICAL
category: smart-contract
description: Detects potential reentrancy attack patterns
patterns:
- pattern: "call.value"
before: "balance[msg.sender] -= amount"
message: "External call before state change"
- pattern: "withdraw"
missing: "nonReentrant"
message: "Missing reentrancy guard"
remediation: |
1. Use checks-effects-interactions pattern
2. Add ReentrancyGuard from OpenZeppelin
3. Update state before external calls
cvss: 9.8
references:
- https://swcregistry.io/docs/SWC-107
- https://consensys.github.io/smart-contract-best-practices/| Category | Templates | Examples |
|---|---|---|
| SWC (Smart Contract Weakness) | 20+ | Reentrancy, Access Control, Integer Overflow |
| DeFi Vulnerabilities | 10+ | Flash Loan Attacks, Oracle Manipulation, Price Manipulation |
| Smart Contract Patterns | 10+ | Initialization Issues, Upgrade Patterns, Gas Optimization |
| CVE Detection | 3+ | HTTP-based infrastructure scanning |
N3's template engine integrates seamlessly across the development workflow:
βββββββββββββββββββββββββββββββββββββββββββββββ
β N3 TEMPLATE-BASED SCANNER CORE β
β (YAML Templates + Risk Calculator) β
ββββββββββββββββ¬βββββββββββββββββββββββββββββββ
β
ββββββββββ΄βββββββββ
β Scan Contract β
β Match Templatesβ
β Calculate Risk β
ββββββββββ¬βββββββββ
β
ββββββββββββΌβββββββββββ
β β β
βββββΌββββ βββββΌββββ ββββΌβββββ
β CLI β βHardhatβ β MCP β
βScannerβ βPlugin β βServer β
βββββ¬ββββ βββββ¬ββββ βββββ¬ββββ
β β β
β βββββΌββββ β
β β Build β β
β β Time β β
β βββββββββ β
β β
βββββΌββββββββββββββββββββββΌββββ
β Blockscout Widget Display β
β + Envio Real-Time Index β
βββββββββββββββββββββββββββββββ
N3/
βββ packages/
β βββ core/ # TEMPLATE ENGINE (Core)
β β βββ templates/ # 40+ YAML templates
β β β βββ SWC/ # Smart Contract Weakness
β β β βββ defi/ # DeFi attack patterns
β β β βββ smart-contract/ # General patterns
β β βββ src/
β β β βββ engine.ts # Template matching engine
β β β βββ parser.ts # YAML template parser
β β β βββ risk-calculator.ts # CVSS-based scoring
β β βββ cve-templates/ # CVE definitions
β β
β βββ cli/ # Command-line scanner
β βββ hardhat-plugin/ # Hardhat 3 integration
β βββ mcp-server/ # AI Model Context Protocol
β βββ blockscout-widget/ # Explorer visualization
β βββ envio-indexer/ # Real-time event indexing
β
βββ contracts/
β βββ N3SecurityOracle.sol # On-chain scan result storage
β
βββ scripts/
βββ deploy-oracle.js # Contract deployment
| Component | Technology | Version | Purpose |
|---|---|---|---|
| Smart Contracts | Hardhat | 3.0.9 | Development & deployment |
| Indexing | Envio HyperIndex | 2.31.0 | Event monitoring |
| AI Integration | Blockscout SDK | 1.20.2 | AI-powered analysis |
| Explorer | Blockscout | API + Widget | Block explorer integration |
| Database | PostgreSQL | 17.5 | Event storage |
| API | Hasura GraphQL | 2.43.0 | Query interface |
| Runtime | Node.js | 22.21.0 | JavaScript execution |
| Package Manager | pnpm | 10.19.0 | Monorepo management |
IMPORTANT: Hardhat 3.x requires Node.js 22.10.0 or later. Node.js 20.x is not compatible.
# Install Node.js 22 (Ubuntu/Debian)
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt-get install -y nodejs
# Verify version
node --version # Should be 22.10.0 or higherOther requirements:
- pnpm 10.x or npm 10.x
- Docker & Docker Compose (for Envio)
- PostgreSQL 17+ (or use Docker)
# Clone repository
git clone https://github.com/intelligent-ears/N3.git
cd N3
# Install dependencies
pnpm install
# Build all packages
pnpm run build# Scan a single contract
n3 scan contracts/MyContract.sol
# Scan with specific templates
n3 scan MyContract.sol --template reentrancy-001
# Generate detailed report
n3 scan MyContract.sol --format html --output report.html
# Scan with severity filter
n3 scan MyContract.sol --severity critical,high// hardhat.config.js
require('@n3-security/hardhat-plugin');
module.exports = {
solidity: "0.8.20",
n3: {
templates: './n3-templates',
severity: ['critical', 'high', 'medium'],
failOnCritical: true,
reportFormat: 'json'
}
};# Run security scan
npx hardhat scan
# Run comprehensive audit
npx hardhat audit
# Generate security tests
npx hardhat test:generate
# Check template coverage
npx hardhat coverage:security# Start MCP server
node mcp-blockscout-server.mjs
# The server runs on http://localhost:3000
# AI assistants can query: /api/analyzeExample API call:
curl -X POST http://localhost:3000/api/analyze \
-H "Content-Type: application/json" \
-d '{
"contractAddress": "0xdAC17F958D2ee523a2206206994597C13D831ec7",
"chain": "ethereum"
}'# Navigate to indexer
cd packages/envio-indexer
# Start Envio development server
envio dev
# Access Hasura console at http://localhost:8080
# GraphQL endpoint: http://localhost:8080/v1/graphqlQuery vulnerability events:
query GetVulnerabilities {
VulnerabilityEvent(
where: { severity: { _eq: "CRITICAL" } }
order_by: { detectedAt: desc }
) {
id
contractAddress
vulnerabilityType
severity
detectedAt
}
}π Scanning VulnerableBank.sol...
π Found 7 vulnerabilities
π₯ Risk Score: 87.35/100 (CRITICAL)
β
Templates: reentrancy, access-control, math, oracle, flash-loan
π΄ Critical: 2 issues
- Reentrancy vulnerability in withdraw()
- Unprotected initialization function
π High: 2 issues
- Integer overflow in calculateReward()
- Missing access control on admin functions
π‘ Medium: 3 issues
- Unchecked external call return value
- Block timestamp dependency
- Gas optimization needed
β
Scan completed in 234ms
Compiling contracts...
β Compiled 3 contracts
Running N3 Security Scan...
β Scanned 3 contracts with 40 templates
SecurityToken.sol: β
PASSED (Risk: 15/100)
VulnerableBank.sol: β FAILED (Risk: 87/100)
- 2 CRITICAL issues found
- Build stopped (failOnCritical: true)
Error: Security scan failed. Fix critical issues before deployment.
N3 includes comprehensive template coverage across multiple categories:
- SWC-101: Integer Overflow/Underflow
- SWC-107: Reentrancy
- SWC-115: Authorization through tx.origin
- SWC-116: Timestamp Dependence
- SWC-120: Weak Sources of Randomness
- SWC-134: Message call with hardcoded gas
- ... and 15+ more
- Flash Loan Attacks
- Oracle Manipulation
- Price Manipulation
- Front-running vulnerabilities
- Sandwich attacks
- MEV exploitation
- ... and more
- Uninitialized storage pointers
- Delegate call issues
- Selfdestruct vulnerabilities
- Gas optimization patterns
- Upgrade pattern violations
N3 uses a CVSS-inspired risk calculation methodology:
Risk Score = Ξ£ (Template_Match_Score Γ Severity_Weight)
Severity Weights:
- CRITICAL: 10.0
- HIGH: 7.5
- MEDIUM: 5.0
- LOW: 2.5
- INFO: 1.0Risk Levels:
- π’ 0-30: LOW - Minor issues, safe to deploy
- π‘ 31-60: MEDIUM - Review recommended
- π 61-80: HIGH - Fix before deployment
- π΄ 81-100: CRITICAL - Do not deploy
We welcome contributions! See CONTRIBUTING.md for guidelines.
- Create a YAML file in
packages/core/templates/ - Follow the template schema (see examples)
- Test with sample vulnerable contracts
- Submit a PR with test cases
id: unique-identifier
name: Human-readable name
severity: CRITICAL|HIGH|MEDIUM|LOW|INFO
category: smart-contract|defi|infrastructure
description: What this template detects
patterns:
- pattern: "regex or string to match"
context: "where to look (optional)"
message: "Issue description"
remediation: |
Step-by-step fix instructions
cvss: 0.0-10.0
references:
- https://relevant-documentation.com- Quick Reference - Essential commands and workflows
- Integration Guide - Platform integrations (1006 lines)
- Architecture - System design and components
MIT License - see LICENSE for details
- Repository: https://github.com/intelligent-ears/N3
- Documentation: See
DOCUMENTATION_INDEX.md - Templates:
packages/core/templates/ - Examples:
examples/
- Inspired by Nuclei by ProjectDiscovery
- Built with Hardhat, Envio, and Blockscout
- Smart contract security patterns from SWC Registry