Skip to content

intelligent-ears/N3

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

29 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

N3 - Nuclei for Web3 πŸ›‘οΈ

Template-Based Security Scanner for Smart Contracts & DApps
From Development to Deployment - Continuous Security at Every Stage

License: MIT Built for ETHGlobal


🎯 What is N3?

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.

Core Philosophy: Templates > Hardcoded Rules

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

Example Template

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/

πŸš€ Features

1. Template Library (40+ Pre-Built Templates)

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

2. Multi-Platform Integration

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   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“¦ Architecture

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

πŸ› οΈ Technology Stack

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

πŸ”§ Installation

Prerequisites

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 higher

Other requirements:

  • pnpm 10.x or npm 10.x
  • Docker & Docker Compose (for Envio)
  • PostgreSQL 17+ (or use Docker)

Quick Start

# Clone repository
git clone https://github.com/intelligent-ears/N3.git
cd N3

# Install dependencies
pnpm install

# Build all packages
pnpm run build

πŸ“š Usage

1. CLI Scanner

# 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

2. Hardhat Plugin

// 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

3. MCP Server (AI Integration)

# Start MCP server
node mcp-blockscout-server.mjs

# The server runs on http://localhost:3000
# AI assistants can query: /api/analyze

Example API call:

curl -X POST http://localhost:3000/api/analyze \
  -H "Content-Type: application/json" \
  -d '{
    "contractAddress": "0xdAC17F958D2ee523a2206206994597C13D831ec7",
    "chain": "ethereum"
  }'

4. Envio Indexer (Real-time Monitoring)

# 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/graphql

Query vulnerability events:

query GetVulnerabilities {
  VulnerabilityEvent(
    where: { severity: { _eq: "CRITICAL" } }
    order_by: { detectedAt: desc }
  ) {
    id
    contractAddress
    vulnerabilityType
    severity
    detectedAt
  }
}

πŸ§ͺ Example Output

CLI Scan Results

πŸ” 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

Hardhat Integration Output

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.

🎯 Template Coverage

N3 includes comprehensive template coverage across multiple categories:

Smart Contract Weaknesses (SWC)

  • 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

DeFi Attack Patterns

  • Flash Loan Attacks
  • Oracle Manipulation
  • Price Manipulation
  • Front-running vulnerabilities
  • Sandwich attacks
  • MEV exploitation
  • ... and more

General Smart Contract Issues

  • Uninitialized storage pointers
  • Delegate call issues
  • Selfdestruct vulnerabilities
  • Gas optimization patterns
  • Upgrade pattern violations

πŸ“Š Risk Scoring

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.0

Risk 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

🀝 Contributing

We welcome contributions! See CONTRIBUTING.md for guidelines.

Adding New Templates

  1. Create a YAML file in packages/core/templates/
  2. Follow the template schema (see examples)
  3. Test with sample vulnerable contracts
  4. Submit a PR with test cases

Template Schema

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

πŸ“– Documentation


πŸ“„ License

MIT License - see LICENSE for details


πŸ”— Links


πŸ™ Acknowledgments


N3: Securing Web3, Templates for security πŸ›‘οΈ

About

Template-Based Security Scanner for Smart Contracts & DApps

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors