-- ============================================================================= -- Table audit_logs - Dashboard Bot Detector -- ============================================================================= -- Stocke tous les logs d'activité des utilisateurs pour audit et conformité -- -- Usage: -- clickhouse-client --host test-sdv-anubis.sdv.fr --port 8123 \ -- --user admin --password SuperPassword123! < deploy_audit_logs_table.sql -- -- ============================================================================= USE mabase_prod; -- ============================================================================= -- Table pour stocker les logs d'audit -- ============================================================================= CREATE TABLE IF NOT EXISTS mabase_prod.audit_logs ( -- Identification timestamp DateTime DEFAULT now(), user_name String, -- Nom de l'utilisateur action LowCardinality(String), -- Action effectuée -- Entité concernée entity_type LowCardinality(String), -- Type: ip, ja4, incident, classification entity_id String, -- ID de l'entité entity_count UInt32 DEFAULT 0, -- Nombre d'entités (pour bulk operations) -- Détails details String, -- JSON avec détails de l'action client_ip String, -- IP du client -- Métadonnées session_id String DEFAULT '', -- ID de session user_agent String DEFAULT '' -- User-Agent du navigateur ) ENGINE = MergeTree() PARTITION BY toYYYYMMDD(timestamp) ORDER BY (timestamp, user_name, action) TTL timestamp + INTERVAL 90 DAY -- Garder 90 jours de logs SETTINGS index_granularity = 8192; -- ============================================================================= -- Index pour accélérer les recherches -- ============================================================================= CREATE INDEX IF NOT EXISTS idx_audit_logs_user ON TABLE mabase_prod.audit_logs (user_name) TYPE minmax GRANULARITY 1; CREATE INDEX IF NOT EXISTS idx_audit_logs_action ON TABLE mabase_prod.audit_logs (action) TYPE minmax GRANULARITY 1; CREATE INDEX IF NOT EXISTS idx_audit_logs_entity ON TABLE mabase_prod.audit_logs (entity_type, entity_id) TYPE minmax GRANULARITY 1; CREATE INDEX IF NOT EXISTS idx_audit_logs_timestamp ON TABLE mabase_prod.audit_logs (timestamp) TYPE minmax GRANULARITY 1; -- ============================================================================= -- Vue pour les statistiques d'audit -- ============================================================================= CREATE VIEW IF NOT EXISTS mabase_prod.view_audit_stats AS SELECT toDate(timestamp) AS log_date, user_name, action, count() AS total_actions, uniq(entity_id) AS unique_entities, sum(entity_count) AS total_entity_count FROM mabase_prod.audit_logs GROUP BY log_date, user_name, action; -- ============================================================================= -- Vue pour l'activité par utilisateur -- ============================================================================= CREATE VIEW IF NOT EXISTS mabase_prod.view_user_activity AS SELECT user_name, toDate(timestamp) AS activity_date, count() AS actions, uniq(action) AS action_types, min(timestamp) AS first_action, max(timestamp) AS last_action, dateDiff('hour', min(timestamp), max(timestamp)) AS session_duration_hours FROM mabase_prod.audit_logs GROUP BY user_name, activity_date; -- ============================================================================= -- Actions d'audit standardisées -- ============================================================================= -- -- CLASSIFICATION: -- - CLASSIFICATION_CREATE -- - CLASSIFICATION_UPDATE -- - CLASSIFICATION_DELETE -- - BULK_CLASSIFICATION -- -- INVESTIGATION: -- - INVESTIGATION_START -- - INVESTIGATION_COMPLETE -- - CORRELATION_GRAPH_VIEW -- - TIMELINE_VIEW -- -- EXPORT: -- - EXPORT_CSV -- - EXPORT_JSON -- - EXPORT_STIX -- - EXPORT_MISP -- -- INCIDENT: -- - INCIDENT_CREATE -- - INCIDENT_UPDATE -- - INCIDENT_CLOSE -- -- ADMIN: -- - USER_LOGIN -- - USER_LOGOUT -- - PERMISSION_CHANGE -- - CONFIG_UPDATE -- -- ============================================================================= -- ============================================================================= -- Exemples d'insertion -- ============================================================================= -- Classification simple -- INSERT INTO mabase_prod.audit_logs -- (user_name, action, entity_type, entity_id, details) -- VALUES -- ('analyst1', 'CLASSIFICATION_CREATE', 'ip', '192.168.1.100', -- '{"label": "malicious", "tags": ["scraping", "bot-network"], "confidence": 0.95}'); -- Classification en masse -- INSERT INTO mabase_prod.audit_logs -- (user_name, action, entity_type, entity_count, details) -- VALUES -- ('analyst1', 'BULK_CLASSIFICATION', 'ip', 50, -- '{"label": "suspicious", "tags": ["scanner"], "confidence": 0.7}'); -- Export STIX -- INSERT INTO mabase_prod.audit_logs -- (user_name, action, entity_type, entity_count, details) -- VALUES -- ('analyst2', 'EXPORT_STIX', 'incident', 1, -- '{"incident_id": "INC-20240314-001", "format": "stix-2.1"}'); -- ============================================================================= -- FIN -- ============================================================================= -- -- Vérifier que la table est créée : -- SELECT count() FROM mabase_prod.audit_logs; -- -- Voir les dernières actions : -- SELECT * FROM mabase_prod.audit_logs ORDER BY timestamp DESC LIMIT 10; -- -- Statistiques par utilisateur : -- SELECT user_name, count() AS actions FROM mabase_prod.audit_logs -- WHERE timestamp >= now() - INTERVAL 24 HOUR GROUP BY user_name; -- -- =============================================================================