awsmattermost

Deny connections from bots/attackers using NGINX

Sometimes, if you are experiencing poor performance, it is because you are being attacked by Internet bots. The reason for these attacks is that they are trying to find a security bug in your application code or in the software itself.

An example of a bot attack is attempting to check if the php.cgi binary is disabled. As this is disabled by default, attackers won’t be able to exploit your system, but you will have hundreds or even thousands of connections from the same IP address (or even different IP addresses) trying to “check” every few hours if those binaries or scripts are available.

Our stacks and cloud images come with the latest versions of their components but, even though you are safe from those attacks, your server could experience poor performance because of the traffic they generate.

To know if you are being attacked, run the command below:

$ cd /opt/bitnami/nginx/logs/
$ tail -n 10000 access.log | awk '{print $1}'| sort| uniq -c| sort -nr| head -n 10

This will show you the number of times that an IP address connected to your Web server. If you see that some IP addresses have many more connections than others, run the following command (remember to modify ATTACKER_IP with the correct IP):

$ cd /opt/bitnami/nginx/logs/
$ grep "ATTACKER_IP" access.log

If you see that the IP address is always attempting to connect to the same location, if it is a URL that you don’t know, or if it is trying to run binaries or scripts directly, it is likely that IP address is a bot.

Examples of log messages for this scenario are:

[21-Jun-2020 12:32:53] WARNING: [pool www] server reached max_children setting (5), consider raising it

To deny connections to these attackers, the easiest way is with your NGINX configuration file. As an example, follow the steps below to reject any connections from the 1.2.3.4 IP address in WordPress:

  • Edit the file at /opt/bitnami/nginx/conf/nginx.conf and add this to the end of the server block:

      ...
        deny 1.2.3.4;
      }
    

    To deny access to more than one IP, use the example below:

      ...
        deny 1.2.3.4;
        deny 5.6.7.8;
        deny 9.10.11.12;
      }
    
  • Restart the NGINX web server:

      $ sudo /opt/bitnami/ctlscript.sh restart nginx
    
Last modification December 21, 2022