Skip to content

Latest commit

 

History

History
153 lines (122 loc) · 6.29 KB

File metadata and controls

153 lines (122 loc) · 6.29 KB

Svelte + Python Backend for Real-time Nmap Visualization

This document outlines the tasks required to implement a real-time nmap visualization system using Svelte for the frontend and a Python FastAPI backend.

1. Backend Setup

1.1 Initialize FastAPI Project

  • Create a requirements.txt file with FastAPI, uvicorn, websockets, and asyncio
  • Set up project structure with appropriate folders (routes, services, models)
  • Create main.py with FastAPI app initialization
  • Implement basic health check endpoint

1.2 Nmap Integration

  • Create NmapService class to handle subprocess interaction
  • Implement method to run nmap scan asynchronously
  • Add line-by-line output parsing for progress, ports, and phases
  • Implement timeout and error handling for nmap process

1.3 WebSocket Implementation

  • Create WebSocket endpoint for real-time nmap output streaming
  • Implement connection management (connect/disconnect handlers)
  • Add message formatting for different event types (output, progress, port discovery)
  • Implement broadcast mechanism for multiple clients

1.4 REST API Endpoints

  • Create endpoint to start nmap scan with parameters
  • Implement endpoint to retrieve scan history
  • Add endpoint to get current scan status
  • Create API for retrieving saved scan results

1.5 Data Persistence

  • Design database schema for scan results
  • Implement database connection (SQLite for simplicity)
  • Create data models and repositories
  • Add service to store and retrieve scan history

2. Frontend Setup

2.1 Initialize Svelte Project

  • Set up new Svelte project using SvelteKit or Vite
  • Configure build system and development environment
  • Set up basic routing structure
  • Add necessary dependencies (charting libraries, terminal emulator)

2.2 UI Components

  • Create layout components (header, sidebar, main content)
  • Implement scan configuration form
  • Build progress indicator components (progress bar, phase indicator)
  • Create terminal output component with syntax highlighting

2.3 Data Visualization Components

  • Implement network map visualization
  • Create port visualization component
  • Build vulnerability highlight system
  • Add timeline visualization for scan phases

2.4 WebSocket Integration

  • Set up WebSocket connection manager
  • Create stores for different data types (output, progress, ports)
  • Implement reconnection logic
  • Add message handlers for different event types

2.5 User Experience Enhancements

  • Add keyboard shortcuts for common actions
  • Implement dark/light theme support
  • Create responsive layout for different screen sizes
  • Add notifications for important events

3. Integration & Testing

3.1 API Integration

  • Connect frontend to backend REST endpoints
  • Implement authentication if needed
  • Add error handling for API requests
  • Create loading states for async operations

3.2 Testing Infrastructure

  • Set up backend testing with pytest
  • Implement frontend testing with Jest and Testing Library
  • Create mock data for testing visualizations
  • Add WebSocket testing utilities

3.3 End-to-End Testing

  • Create test for complete scan workflow
  • Test real-time updates during scan
  • Verify visualization accuracy with known scan results
  • Test error handling and edge cases

4. Deployment & Documentation

4.1 Build System

  • Configure production build process
  • Set up static asset optimization
  • Implement backend deployment scripts
  • Create Docker container if needed

4.2 Documentation

  • Write API documentation with Swagger/OpenAPI
  • Create user guide for the interface
  • Document architecture and design decisions
  • Add inline code documentation

4.3 Final Integration

  • Integrate with pentest-agent-system if required
  • Create transition plan from Streamlit
  • Implement data migration if needed
  • Configure CI/CD pipeline

5. Future Enhancements

5.1 Advanced Features

  • Implement comparison between multiple scans
  • Add scan scheduling and automation
  • Create scan templates for common scenarios
  • Implement reporting and export features

Implementation Details

Architecture Diagram

┌────────────────┐    WebSocket    ┌────────────────┐
│                │◄───Stream────┐  │                │
│  Svelte UI     │              │  │  Python        │
│  - Terminal    │    REST API  │  │  Backend       │◄────Subprocess──────┐
│  - Charts      │◄───JSON────────►│  (FastAPI)     │                     │
│  - Viz         │              │  │                │                     │
└────────────────┘              └──┴────────────────┘               ┌─────▼─────┐
                                                                    │   nmap    │
                                                                    └───────────┘

Key Benefits Over Streamlit Implementation

  1. True Real-time Updates: WebSocket connection provides instant updates without page refreshes
  2. Better Performance: Svelte's efficient DOM updates minimize UI lag during rapid updates
  3. Enhanced Visualizations: Rich client-side rendering capabilities for complex visualizations
  4. Improved User Experience: Responsive UI with proper loading states and error handling
  5. Separation of Concerns: Clear division between frontend and backend for better maintainability

Technical Implementation Notes

Backend

  • Use FastAPI for high-performance async API endpoints
  • Leverage Python asyncio for non-blocking nmap execution
  • Parse nmap output line-by-line for immediate transmission
  • Store scan results in SQLite (can be upgraded to PostgreSQL later)

Frontend

  • Utilize Svelte stores for reactive state management
  • Implement XTerm.js for terminal emulation with syntax highlighting
  • Use D3.js or similar for network and port visualizations
  • Maintain WebSocket connection with automatic reconnection