Jump to content

Featured Replies

Posted

You are reading Part 41 of the 57-part series: Harden and Secure Linux Servers. [Level 5]

This series covers progressive security measures, from fundamental hardening techniques to enterprise-grade defense strategies. Each article delves into a specific security practice, explaining its importance and providing step-by-step guidance for implementation.

To explore more security best practices, visit the main guide for a full breakdown of all levels and recommendations.

Log analysis is a critical part of security monitoring that helps detect unauthorized access attempts, system anomalies, and potential security threats before they escalate. Regular log reviews allow you to:

✅ Identify and respond to security incidents early.
✅ Detect brute-force login attempts, unauthorized file access, and system changes.
✅ Ensure compliance with security regulations (e.g., PCI-DSS, HIPAA, ISO 27001).

By automating log reviews and setting up alerts, you improve threat detection and response for your Linux server.

How to Review and Monitor Logs for Suspicious Activities

1. Identify Critical Logs to Monitor

Log files contain vital security and system information. Key logs to monitor include:

Log File

Purpose

/var/log/auth.log

Tracks authentication attempts, SSH logins, and sudo usage.

/var/log/syslog

Captures general system events and service logs.

/var/log/audit/audit.log

Records system audit events and policy violations (if auditd is enabled).

/var/log/nginx/access.log

Logs web server requests (useful for detecting attacks).

/var/log/mysql/error.log

Tracks database errors and unauthorized access attempts.

2. Install and Configure Log Analysis Tools

To efficiently analyze and visualize logs, use a centralized logging tool like Splunk or Graylog.

Install Splunk for Log Monitoring
  1. Download and install Splunk:

    wget -O splunk.deb https://download.splunk.com/products/splunk/releases/latest/linux/splunk-<version>-linux-2.6-amd64.deb
    sudo dpkg -i splunk.deb
    
  2. Enable and start Splunk:

    sudo /opt/splunk/bin/splunk enable boot-start
    sudo systemctl start splunk
  3. Access Splunk Web UI at:

    http://your-server-ip:8000
    
  4. Configure log forwarding from /var/log/auth.log and /var/log/syslog.

Install Graylog for Centralized Log Management
  1. Install dependencies (MongoDB, Elasticsearch, Java):

    sudo apt update && sudo apt install mongodb elasticsearch openjdk-11-jre -y
    
  2. Install Graylog:

    wget https://packages.graylog2.org/repo/packages/graylog-<version>.deb
    sudo dpkg -i graylog-<version>.deb
    
  3. Start Graylog and access the Web UI:

    http://your-server-ip:9000
    
  4. Configure log collection and create dashboards to monitor security events.

3. Set Up Automated Alerts for Suspicious Activities

To detect security threats in real time, configure automated alerts.

Monitor Repeated Failed Login Attempts

Use Fail2Ban to block brute-force attacks based on failed login logs.

  1. Install Fail2Ban:

    sudo apt install fail2ban -y
    
  2. Create a filter for SSH login failures:

    sudo nano /etc/fail2ban/jail.local
    
  3. Add the following rule:

    [sshd]
    enabled = true
    maxretry = 5
    findtime = 600
    bantime = 3600
    logpath = /var/log/auth.log
    

Restart Fail2Ban:

sudo systemctl restart fail2ban
Create a Custom Script to Detect Unauthorized File Access

This script monitors sensitive files and alerts when unauthorized access is detected.

  1. Create a monitoring script:

    sudo nano /usr/local/bin/monitor_logs.sh
    
  2. Add the following code:

    #!/bin/bash
    tail -F /var/log/auth.log | awk '/Failed password/ {print "ALERT: Failed SSH login detected on", $1, $2, $3}' | mail -s "Security Alert" admin@example.com
    
  3. Make it executable:

    sudo chmod +x /usr/local/bin/monitor_logs.sh
    
  4. Run the script in the background:

    nohup /usr/local/bin/monitor_logs.sh &
    

(This will send an email alert to admin@example.com for each failed login attempt.)

4. Regularly Review and Analyze Logs

Manually Check Logs for Suspicious Activity

Use grep to filter logs for security events:

  • Check failed SSH logins:

    sudo grep "Failed password" /var/log/auth.log
    
  • Find successful root logins:

    sudo grep "session opened for user root" /var/log/auth.log
    
  • Monitor sudo command usage:

    sudo cat /var/log/auth.log | grep "sudo"
    
  • List login attempts by IP address:

    sudo awk '{print $1, $2, $3, $11}' /var/log/auth.log | sort | uniq -c | sort -nr
    
Use Logwatch for Daily Log Summaries
  1. Install Logwatch:

    sudo apt install logwatch -y
    
  2. Generate a daily security report:

    sudo logwatch --detail high --service sshd --range yesterday
    
  3. Schedule daily email reports:

    sudo crontab -e
    

    Add:

    0 6 * * * /usr/sbin/logwatch --output mail --mailto admin@example.com --detail high
    

    (Sends a daily security report at 6 AM.)

5. Store Logs Securely for Auditing and Compliance

To prevent log tampering, store logs on a remote log server using rsyslog:

  1. Edit rsyslog configuration on the local server:

    sudo nano /etc/rsyslog.conf
    
  2. Add a rule to forward logs to a remote server:

    *.* @@remote-log-server-ip:514
    
  3. Restart rsyslog to apply changes:

    sudo systemctl restart rsyslog
    

(Now, logs are stored externally, even if the primary server is compromised.)

Best Practices for Log Review and Security Monitoring

✅ Automate log collection and analysis with tools like Splunk, Graylog, or ELK Stack.
✅ Monitor login attempts, file changes, and suspicious processes.
✅ Set up alerts for critical events (failed logins, privilege escalation, unauthorized access).
✅ Store logs securely on a remote system to prevent tampering.
✅ Conduct regular audits to ensure compliance and security best practices.

By regularly reviewing and analyzing logs, you proactively detect security threats, prevent system breaches, and maintain a secure Linux environment.

  • Views 71
  • Created
  • Last Reply

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

Important Information

Terms of Use Privacy Policy Guidelines We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.