Overview
The Database module (modules/database.py) provides SQLite-based data persistence for all scan results. It manages five normalized tables storing scans, ports, vulnerabilities, web findings, and exploit attempts.
Database Class
Defined atdatabase.py:12, this class handles all database operations.
Initialization
database.py:13-20
string
default:"database/autopentestx.db"
Path to SQLite database file
Database Schema
AutoPentestX uses a normalized schema with five tables:scans Table
Stores scan metadata and overall results.database.py:42-55
id: Unique scan identifier (auto-increment)target: IP address or domain scannedscan_date: Timestamp when scan startedos_detection: Detected operating systemscan_duration: Total scan time in secondstotal_ports: Ports scanned countopen_ports: Open ports discoveredvulnerabilities_found: Total vulnerabilitiesrisk_score: Overall risk level (CRITICAL/HIGH/MEDIUM/LOW)status: Scan status (completed/failed/interrupted)
ports Table
Stores discovered open ports and services.database.py:57-69
vulnerabilities Table
Stores general vulnerabilities and CVEs.database.py:71-86
web_vulnerabilities Table
Stores web-specific vulnerabilities from Nikto/SQLMap.database.py:101-111
exploits Table
Stores exploitation attempt results.database.py:88-100
CRUD Operations
Insert Operations
insert_scan()
Creates a new scan entry and returns the scan ID.database.py:120-135
insert_port()
database.py:155-170
insert_vulnerability()
database.py:172-195
Update Operations
update_scan()
Updates scan record with results.database.py:137-153
Query Operations
get_scan_by_id()
database.py:230-245
get_ports_for_scan()
database.py:247-260
get_vulnerabilities_for_scan()
database.py:262-275
Usage Example
Error Handling
The Database module handles these common errors:Connection Failed
Connection Failed
Returns error if database file cannot be created or accessed.
Insert Failed
Insert Failed
Returns
None instead of crashing if insert fails.Foreign Key Violation
Foreign Key Violation
SQLite enforces foreign key constraints. Ensure scan_id exists before inserting related records.
Database Maintenance
Backup
Vacuum
Clear Old Scans
Related Documentation
Database Queries
Common SQL queries for data analysis
Configuration
Database settings and options
PDF Reports
How reports retrieve data from database
Workflow
Phase 1: Database initialization