Protect your Ubuntu 24.04 server from zero-day exploits and brute-force attacks by setting up ModSecurity and configuring CSF with LFD.
6 min read
Running a Linux server exposed to the public internet means dealing with automated threats around the clock. Automated bots constantly scan open ports, test default administrator credentials, and probe web applications for known and unknown vulnerabilities. For sysadmins managing websites, applications, and client data on a cloud VPS or dedicated server, maintaining a proactive security posture is non-negotiable.
Relying solely on default application settings leaves your infrastructure exposed. To protect your server effectively, you need a layered defense strategy that combines web application firewall rules with robust network-level intrusion detection. In this guide, we will walk through setting up essential ModSecurity rules and configuring ConfigServer Security & Firewall (CSF) with Login Failure Daemon (LFD) on Ubuntu 24.04 LTS to mitigate brute-force attacks and zero-day exploits.
Understanding the Threat Landscape on Modern Linux Servers
Every server with a public IP address attracts unwanted traffic within minutes of deployment. Brute-force attacks target exposed services like SSH, FTP, and mail servers, attempting thousands of password combinations until they gain unauthorized access. At the same time, web applications face SQL injection, cross-site scripting (XSS), and automated probes looking for recently disclosed vulnerabilities.
While patched software handles known bugs, zero-day exploits—vulnerabilities that are unknown to the software vendor and have no official patch—pose a severe risk. Defending against these requires behavioral monitoring and anomaly detection. By implementing a Web Application Firewall (WAF) alongside an intelligent firewall daemon, you can identify malicious patterns, block aggressive scanners, and limit unauthorized access attempts before they reach your core application logic.
Installing and Configuring ModSecurity on Ubuntu 24.04 LTS
ModSecurity is an open-source Web Application Firewall (WAF) that operates as an embedded module for web servers like Apache or Nginx. It inspects incoming HTTP traffic in real time, looking for malicious payloads, SQL injection attempts, and unauthorized data access.
To install ModSecurity and the OWASP Core Rule Set (CRS) on your Ubuntu 24.04 LTS server, follow these steps:
Update your package list to ensure you pull the latest available versions:
sudo apt update
This command refreshes the local package index so your system knows about the latest available software versions.
Install the Nginx web server and the ModSecurity module connector:
sudo apt install nginx libmodsecurity3 -y
This command installs the Nginx web server and the core ModSecurity library, answering "yes" to all installation prompts automatically.
Download the official OWASP ModSecurity Core Rule Set (CRS) to provide a baseline of security rules for common web vulnerabilities:
cd /etc/nginx
sudo git clone https://github.com/coreruleset/coreruleset.git modsecurity/crs
These commands change your working directory to the Nginx configuration folder and clone the official OWASP rule repository into a local directory.
Rename the sample configuration file and include it in your main ModSecurity configuration:
This command duplicates the template setup file so it can be customized for your server environment.
Once installed, ModSecurity must be switched from detection-only mode to blocking mode. Open your ModSecurity configuration file and locate the directive that controls rule engine behavior:
SecRuleEngine On
Setting this parameter to On ensures that ModSecurity actively blocks requests that violate your defined security rules rather than simply logging them.
Deploying Essential ModSecurity Rules to Stop Zero-Day Exploits
The OWASP Core Rule Set provides hundreds of rules covering common attack vectors. However, to defend against zero-day exploits and abnormal request patterns, sysadmins should fine-tune specific rule groups:
Protocol Enforcement: Rules in this category check for illegal HTTP requests, missing headers, or malformed user agents, blocking automated scanners instantly.
Request Body Inspection: Ensures that POST and PUT payloads are thoroughly scanned for script tags, shell commands, and known exploit signatures.
Outbound Content Inspection: Scans server responses to prevent data leaks, such as accidentally exposing database error messages or system configuration paths to end users.
To create a custom rule for blocking specific suspicious parameters often targeted by zero-day probes, you can add a local rule file. For instance, to block requests containing anomalous character strings in query parameters, create /etc/nginx/modsecurity/conf.d/custom-rules.conf and add:
This rule evaluates request arguments during phase 2 of the transaction lifecycle and returns an HTTP 403 Forbidden status if a potential SQL injection pattern is matched.
Securing Network Access with ConfigServer Security & Firewall (CSF)
While ModSecurity protects your web applications, ConfigServer Security & Firewall (CSF) manages your server's packet filtering rules and integrates tightly with LFD (Login Failure Daemon) to stop brute-force attacks at the network layer.
To install CSF on Ubuntu 24.04 LTS, download the installation package and run the installation script:
cd /usr/src
sudo wget https://download.configserver.com/csf.tar.gz
sudo tar -xzf csf.tar.gz
cd csf
sudo sh install.sh
These commands navigate to the source directory, download the compressed CSF archive, extract its contents, and execute the installation script.
After installation, test whether your server meets all required iptables modules:
sudo perl /usr/local/csf/bin/csftest.pl
This command runs a diagnostic script to verify that your kernel supports all the firewall features required by CSF.
If the test passes successfully, you can open the main configuration file located at /etc/csf/csf.conf using your preferred text editor to adjust the security settings.
Fine-Tuning LFD to Block Brute-Force Attacks Automatically
Login Failure Daemon (LFD) runs as a background service, constantly scanning log files for repeated authentication failures across services like SSH, webmail, and FTP. When a threshold is breached, LFD automatically adds the offending IP address to the firewall block list.
Open /etc/csf/csf.conf and locate the following parameters to adjust brute-force thresholds:
LF_SSH: Set the maximum number of failed SSH login attempts allowed before LFD blocks the IP address. A setting of 3 or 5 is generally recommended for production environments.
LF_INTERVAL: Defines the timeframe in seconds during which failed login attempts are counted.
LF_BLOCK: Specifies how long (in seconds) an offending IP address should remain blocked by the firewall. Setting this to 86400 ensures a 24-hour ban for repeat offenders.
By default, CSF runs in testing mode. Once you have verified your configuration and ensured that your own management IP address is added to /etc/csf/csf.allow to prevent accidental lockouts, disable testing mode by changing this line in /etc/csf/csf.conf:
TESTING = "0"
Save the file and restart both CSF and LFD to apply the changes:
sudo csf -r
This command restarts the firewall engine and reloads your updated configuration rules into the kernel.
Conclusion
Securing a Linux server against modern threats requires a multi-layered approach. By deploying ModSecurity alongside the OWASP Core Rule Set, you add an intelligent inspection layer capable of identifying malicious web traffic and mitigating zero-day exploits. Simultaneously, combining CSF and LFD provides a reliable defense against automated brute-force attacks at the network and service layers.
Maintaining security is an ongoing operational process. Regularly review your firewall logs, keep your software packages updated, and adjust your detection thresholds to match your application's specific traffic patterns. Taking these steps helps ensure your websites and apps remain stable, secure, and available to legitimate users.