Logging and Security Monitoring: Detecting and Responding to Security Incidents

Logging and Security Monitoring: Detecting and Responding to Security Incidents

Whitespots Team ·
logging
monitoring
siem
incident-response

Introduction

Effective security logging and monitoring are essential for detecting threats, investigating incidents, and maintaining compliance. This guide covers practical implementation of security logging with best practices for monitoring and alerting.

Key Security Events to Log

  • Authentication attempts (success and failure)
  • Authorization failures
  • Input validation errors
  • Session anomalies
  • Administrative actions
  • Data access and modifications
  • Security configuration changes
  • Rate limit violations

Structured Logging Implementation

javascript
const winston = require('winston'); const securityLogger = winston.createLogger({ format: winston.format.combine( winston.format.timestamp(), winston.format.json() ), transports: [ new winston.transports.File({ filename: 'security.log' }) ] }); function logSecurityEvent(event, details) { securityLogger.info({ event, timestamp: new Date().toISOString(), ...details }); } // Login monitoring app.post('/login', async (req, res) => { const result = await authenticate(req.body); logSecurityEvent(result.success ? 'LOGIN_SUCCESS' : 'LOGIN_FAILURE', { username: req.body.username, ip: req.ip, userAgent: req.headers['user-agent'] }); });

Centralized Log Management

yaml
# ELK Stack with Filebeat filebeat.inputs: - type: log enabled: true paths: - /var/log/application/*.log json.keys_under_root: true output.elasticsearch: hosts: ["elasticsearch:9200"] index: "security-logs-%{+yyyy.MM.dd}" # Alert on suspicious patterns processors: - drop_event: when: not: or: - contains: message: "SECURITY" - contains: message: "ERROR"

Real-Time Alerting

javascript
// Prometheus metrics for security events const prometheus = require('prom-client'); const loginFailures = new prometheus.Counter({ name: 'login_failures_total', help: 'Total failed login attempts', labelNames: ['username', 'ip'] }); const securityEvents = new prometheus.Counter({ name: 'security_events_total', help: 'Security events by type', labelNames: ['event_type'] }); // Alert on threshold function checkThreshold(ip, threshold = 5) { const failures = getFailureCount(ip); if (failures >= threshold) { sendAlert({ severity: 'high', message: `Multiple login failures from ${ip}`, count: failures }); } }

Security Monitoring Best Practices

  • ✅ Log all security-relevant events
  • ✅ Use structured logging (JSON)
  • ✅ Include context (IP, user, timestamp)
  • ✅ Centralize logs for analysis
  • ✅ Set up real-time alerting
  • ✅ Implement log retention policies
  • ✅ Protect logs from tampering
  • ✅ Regular log review and analysis
  • ✅ Correlate events across systems
  • ✅ Monitor for anomalies

Conclusion

Effective security logging and monitoring enable rapid threat detection and incident response. Implement comprehensive logging, centralized management, and real-time alerting to maintain security visibility across your infrastructure.