VPN logging and monitoring are critical for security auditing, troubleshooting, and compliance. Here's a comprehensive logging and monitoring solution:
Log Types
1. Connection Logs
Recorded Content:
- User identity (username, certificate DN)
- Connection time (start/end timestamps)
- Source IP address and port
- Destination server IP
- Protocol and encryption algorithm
- Connection duration
- Data transfer volume (upload/download)
Example Format:
shell2024-01-15 10:30:45 [INFO] User john.doe connected from 192.168.1.100:54321 2024-01-15 10:30:46 [INFO] Assigned IP 10.8.0.2 to john.doe 2024-01-15 10:35:22 [INFO] User john.doe disconnected, duration: 4m 37s, tx: 5.2MB, rx: 12.8MB
2. Authentication Logs
Recorded Content:
- Authentication attempts (success/failure)
- Authentication method (password, certificate, MFA)
- Failure reasons
- Abnormal login attempts
Example Format:
shell2024-01-15 10:30:45 [AUTH] SUCCESS: john.doe authenticated via certificate 2024-01-15 10:31:00 [AUTH] FAILED: invalid credentials from 192.168.1.200 2024-01-15 10:31:05 [AUTH] WARNING: Multiple failed attempts from 192.168.1.200
3. Error Logs
Recorded Content:
- Connection failures
- Timeout errors
- Configuration errors
- System errors
Example Format:
shell2024-01-15 10:32:00 [ERROR] Connection timeout for user alice.smith 2024-01-15 10:33:15 [ERROR] TLS handshake failed: certificate expired 2024-01-15 10:34:00 [ERROR] Unable to assign IP address: pool exhausted
4. Performance Logs
Recorded Content:
- Bandwidth usage
- Concurrent connections
- CPU and memory usage
- Latency and packet loss
Example Format:
shell2024-01-15 10:35:00 [PERF] Bandwidth: 125Mbps up, 450Mbps down 2024-01-15 10:35:00 [PERF] Active connections: 45, Peak: 52 2024-01-15 10:35:00 [PERF] CPU: 45%, Memory: 2.1GB/4GB
Log Configuration
OpenVPN Log Configuration
Server Configuration:
conf# Log level verb 3 mute 10 # Log file log /var/log/openvpn.log log-append /var/log/openvpn.log # Status file status /var/log/openvpn-status.log 10 # Client connection scripts script-security 2 client-connect /etc/openvpn/connect-script.sh client-disconnect /etc/openvpn/disconnect-script.sh
Connection Script Example (connect-script.sh):
bash#!/bin/bash LOG_FILE="/var/log/vpn-connections.log" TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S') echo "$TIMESTAMP CONNECT $common_name $trusted_ip $ifconfig_pool_remote_ip" >> $LOG_FILE
WireGuard Log Configuration
System Log Configuration:
bash# Enable WireGuard logging echo "module wireguard +p" | sudo tee /sys/kernel/debug/dynamic_debug/control # View logs sudo journalctl -u wg-quick@wg0 -f
Custom Logging Script:
bash#!/bin/bash # /etc/wireguard/log-connections.sh LOG_FILE="/var/log/wireguard-connections.log" while read line; do if [[ $line == *"AllowedIPs"* ]]; then echo "$(date '+%Y-%m-%d %H:%M:%S') $line" >> $LOG_FILE fi done < <(sudo wg show)
Log Management
1. Log Rotation
Logrotate Configuration (/etc/logrotate.d/openvpn):
shell/var/log/openvpn*.log { daily rotate 30 compress delaycompress missingok notifempty create 640 root adm sharedscripts postrotate systemctl reload openvpn > /dev/null 2>&1 || true endscript }
2. Log Archiving
Archive Script:
bash#!/bin/bash # /usr/local/bin/archive-vpn-logs.sh ARCHIVE_DIR="/backup/vpn-logs" DATE=$(date +%Y%m) mkdir -p $ARCHIVE_DIR # Compress last month's logs find /var/log -name "openvpn-*.log.*" -mtime +30 -exec gzip {} \; mv /var/log/openvpn-*.log.gz $ARCHIVE_DIR/ 2>/dev/null
3. Log Retention Policy
Recommended Retention Periods:
- Real-time logs: 7 days
- Compressed logs: 90 days
- Archived logs: 1-3 years (depending on compliance requirements)
- Audit logs: Permanent retention (critical events)
Monitoring Solutions
1. Real-time Monitoring
Using journalctl:
bash# Real-time OpenVPN logs sudo journalctl -u openvpn -f # View error logs sudo journalctl -u openvpn -p err # View today's logs sudo journalctl -u openvpn --since today
2. Performance Monitoring
Custom Monitoring Script:
bash#!/bin/bash # /usr/local/bin/monitor-vpn.sh LOG_FILE="/var/log/vpn-performance.log" while true; do TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S') # Get connection count CONNECTIONS=$(netstat -an | grep :1194 | grep ESTABLISHED | wc -l) # Get bandwidth BANDWIDTH=$(iftop -t -s 1 -n -i eth0 2>/dev/null | tail -n 3) # Get CPU and memory CPU=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}') MEMORY=$(free -m | awk '/Mem:/ {print $3}') echo "$TIMESTAMP Connections: $CONNECTIONS, CPU: $CPU%, Memory: ${MEMORY}MB" >> $LOG_FILE sleep 60 done
3. Alerting System
Using Nagios Monitoring:
bash#!/bin/bash # /usr/local/nagios/libexec/check_vpn.sh WARNING=40 CRITICAL=50 CONNECTIONS=$(netstat -an | grep :1194 | grep ESTABLISHED | wc -l) if [ $CONNECTIONS -ge $CRITICAL ]; then echo "CRITICAL: $CONNECTIONS active VPN connections" exit 2 elif [ $CONNECTIONS -ge $WARNING ]; then echo "WARNING: $CONNECTIONS active VPN connections" exit 1 else echo "OK: $CONNECTIONS active VPN connections" exit 0 fi
4. Centralized Log Management
Using ELK Stack:
yaml# Filebeat configuration filebeat.inputs: - type: log enabled: true paths: - /var/log/openvpn.log fields: service: vpn environment: production output.elasticsearch: hosts: ["elasticsearch:9200"] index: "vpn-logs-%{+yyyy.MM.dd}"
Security and Compliance
1. Log Protection
Access Control:
bash# Set log file permissions chmod 640 /var/log/openvpn.log chown root:adm /var/log/openvpn.log # Encrypt sensitive logs gpg --encrypt --recipient admin@company.com /var/log/vpn-connections.log
2. Audit Trail
Critical Event Recording:
- User login/logout
- Permission changes
- Configuration modifications
- Abnormal access patterns
3. Compliance Requirements
GDPR:
- Minimize log data
- Limit retention periods
- Provide data deletion mechanisms
HIPAA:
- Complete access logs
- Audit trails
- Secure storage
Analysis and Reporting
1. Using awk to Analyze Logs
bash# Count daily connections awk '/CONNECT/ {print $1}' /var/log/vpn-connections.log | sort | uniq -c # Find most active users awk '/CONNECT/ {print $4}' /var/log/vpn-connections.log | sort | uniq -c | sort -nr # Find abnormal connections awk '/CONNECT/ && $6 !~ /^10\.8\.0\./' /var/log/vpn-connections.log
2. Using GoAccess for Visualization
bash# Install GoAccess sudo apt install goaccess # Generate real-time report goaccess /var/log/openvpn.log -o /var/www/html/vpn-stats.html --log-format='%t %h %^[%^] %^ %^ %^ %s %b' --real-time-html
Best Practices
- Log Level: Use appropriate log levels (INFO or WARN for production)
- Regular Review: Regularly review logs to identify abnormal patterns
- Automation: Automate log collection, analysis, and alerting
- Backup: Regularly backup log files
- Testing: Test log recovery and analysis processes
- Documentation: Document log formats and analysis methods
- Privacy Protection: Comply with privacy regulations, minimize personal data