How theabdullahraja/database-intrusion-detection-system works
Purpose and execution surfaces
DIDS is a MySQL-backed intrusion detection/prevention layer that inspects SQL before execution, applies role authorization, records security events, and exposes operational reporting. Primary entry points are the interactive CLI in dids_engine.py, read-only Flask dashboard in dashboard.py, and localhost-only browser query console in webconsole.py.
Database security foundation
MySQL owns the persistent security model: protected target tables, users, rules, query/alert/audit records, blacklist and brute-force state. Schema creation and seed state are separated in 01_schema.sql and 02_sample_data.sql; database roles, reporting views, stored procedures, and triggers are defined in 03_roles_views_triggers.sql. The Python layer depends on this initialized schema rather than owning a separate persistence model.
Protected-query engine
dids_engine.py is the central policy boundary. It authenticates application users, resolves their roles, analyzes submitted SQL against injection, DDL, privilege-escalation, command-injection, and data-dump patterns, including MySQL versioned-comment evasion. It computes risk, fails closed on authorization/security violations, and only sends approved SQL to MySQL. Both interactive interfaces route through this engine rather than directly exposing database access.
Prevention and incident flow
A query enters from CLI or web console with a client IP and authenticated identity. The engine evaluates IP blacklist/brute-force state, RBAC and role mismatch, then threat signatures and risk. Blocked activity produces alerts/audit records and may update incident or blacklist state; allowed activity is executed and logged. Critical events flow to SMTP when configured, otherwise to console notification. Core implementation is dids_engine.py, with database-side logging/enforcement support in 03_roles_views_triggers.sql.
Reporting and administrative operations
Administrative CLI actions export alert, incident, and audit CSVs and generate standalone HTML security reports from persisted security data. Database vw_* views form the reporting contract consumed by the dashboard, avoiding a separate analytics store. Relevant paths: dids_engine.py, 03_roles_views_triggers.sql, and 04_detection_queries.sql.
Web boundaries and runtimes
Python is the application runtime; MySQL is required infrastructure. Flask serves the dashboard and web console. dashboard.py is read-only and displays security statistics, open critical alerts, brute-force sources, activity, and attack categories from the database views. webconsole.py is intentionally bound to localhost and submits SQL to the engine, preserving its inspection and authorization boundary.
Configuration and external services
Environment configuration supplies mandatory MySQL credentials and optional dashboard/web-console ports. SMTP is optional and config-gated for critical alert delivery; absent SMTP configuration uses safe console fallback. .env.example documents the expected environment contract, while requirements.txt supplies Python dependencies.
Main entry points: dids_engine.py Protected-query engine · .env.example Environment contract · 03_roles_views_triggers.sql vw_* reporting views · webconsole.py Local web console
Components 16
Execution surfaces
- Interactive CLI · Runs query and administrative operations.
dids_engine.py - Local web console · Localhost-only SQL submission interface.
webconsole.py - Read-only dashboard · Displays operational security reporting.
dashboard.py
Python security layer
- Protected-query engine · Authenticates, authorizes, inspects, and executes approved SQL.
dids_engine.py - RBAC and threat analysis · Checks identity, role mismatch, blacklist state, signatures, and risk.
dids_engine.py - Incident handling · Records blocked activity and updates alert or blacklist state.
dids_engine.py - Exports and HTML reports · Generates CSV exports and standalone security reports.
dids_engine.py
MySQL security model
- Schema and seed data · Creates persistent security records, rules, users, and protected targets.
01_schema.sql - Sample security data
02_sample_data.sql - Roles, views, procedures, triggers · Provides database-side enforcement, logging, and reporting views.
03_roles_views_triggers.sql - MySQL security database · Stores protected data, identities, rules, audits, alerts, and incident state.
- vw_* reporting views · Supplies security statistics and activity data to reporting consumers.
03_roles_views_triggers.sql - Detection queries
04_detection_queries.sql
External services
- SMTP or console fallback · Critical events use SMTP when configured, otherwise console notification.
- Environment contract · Provides required MySQL credentials and optional service settings.
.env.example - Python dependencies
requirements.txt
Connections 20
- Interactive CLI → Protected-query engine: submits SQL / admin actions
- Local web console → Protected-query engine: submits inspected SQL
- Read-only dashboard → vw_* reporting views: reads reporting data
- Protected-query engine → RBAC and threat analysis: evaluates request
- RBAC and threat analysis → MySQL security database: reads identity and security state
- RBAC and threat analysis → Incident handling: blocked or critical activity
- Protected-query engine → MySQL security database: executes approved SQL and logs activity
- Incident handling → MySQL security database: persists alerts and incident state
- Incident handling → SMTP or console fallback: critical notifications
- Exports and HTML reports → vw_* reporting views: reads persisted security data
- Interactive CLI → Exports and HTML reports: administrative reporting
- Schema and seed data → MySQL security database: creates security model
- Sample security data → MySQL security database: loads sample state
- Roles, views, procedures, triggers → MySQL security database: installs database logic
- Roles, views, procedures, triggers → vw_* reporting views: defines
- Detection queries → MySQL security database: queries security data
- Environment contract → Protected-query engine: MySQL and runtime settings
- Environment contract → Read-only dashboard: port settings
- Environment contract → Local web console: port settings
- Python dependencies → Protected-query engine: provides runtime libraries