• If you are still using CentOS 7.9, it's time to convert to Alma 8 with the free centos2alma tool by Plesk or Plesk Migrator. Please let us know your experiences or concerns in this thread:
    CentOS2Alma discussion

Question Blocking IP Ranges using Plesk Firewall or NGINX Deny

Mark12345

Basic Pleskian
Creating rules using the Plesk Firewall utility is painful. Especially when you want to block or allow hundreds of IP ranges. Let's take for example wanting to only allow USA IP addresses per this table Major IP Address Blocks For United States

Do I want to go through every row and add them to custom rules in Plesk? Not really. I'd much rather add them to a file. The cool thing is, this file seems to exist:

/usr/local/psa/var/modules/firewall/firewall-active.sh

You can create a custom rule via Plesk and see it stored in this file. Say I choose to block CIDR 5.188.208.0/20, I create a custom rule to block all incoming from this IP range. This is what the entry looks like in the file:

apply_rule /usr/sbin/iptables -A INPUT -p udp -s 5.188.208.0/20 -j DROP
apply_rule /usr/sbin/iptables -A INPUT -p tcp -s 5.188.208.0/20 -j DROP

If I add another range to the rule, say 194.84.96.0/19, this is what shows up in the file:

apply_rule /usr/sbin/iptables -A INPUT -p udp -s 194.84.96.0/19 -j DROP
apply_rule /usr/sbin/iptables -A INPUT -p udp -s 5.188.208.0/20 -j DROP
apply_rule /usr/sbin/iptables -A INPUT -p tcp -s 194.84.96.0/19 -j DROP
apply_rule /usr/sbin/iptables -A INPUT -p tcp -s 5.188.208.0/20 -j DROP

It would be cool if I could edit that file and restart the firewall but it doesn't work that way.

So I'm wondering, what is the most effective, efficient, easiest way to block incoming IP ranges/CIDR/subnets?

Should I add them to /etc/nginx/nginx.conf like this:

location / {
deny 5.188.208.0/20;
deny 1194.84.96.0/19;
deny 194.59.184.0/24;
deny 194.59.186.0/24;
deny 194.60.132.0/22;
deny 194.60.236.0/22;
deny 194.60.242.0/24;
deny 194.60.244.0/22;
}

Maybe I answered my own question. If someone objects, please let me know but it seems blocking using deny in nginx.conf file is the best approach.

Handy tool to generate a list of CDIR nginx deny entries by country Block Visitors by Country | IP2Location

interesting references: How to block requests from specific IP address in Nginx - Init Pals
 
I have found that I have ipset v7.1 installed.

The instructions here are useful but not exact to my system. Block China with iptables (see China block using ipset)

For instance, it says

This hasn't done anything yet, but it will in a minute when we run the script. First, we need to add a rule into iptables that refers to this new ipset list the script above defines:

nano /etc/iptables.firewall.rules

But I don't have that file. I do have the following files /etc/sysconfig/iptables-config and ip6tables-config. There is also the nftables.conf file in that folder.

Hopefully someone can straighten me out. Chasing my tail with something that should be easy.
 
I have tried like this:

NombreDescripciónOrden
Denegar24Denegar entrante de 104.149.171.0/24, 147.182.169.0/24, 159.223.74.0/24, 159.65.109.0/24, 178.90.229.0/24, 184.95.38.0/24, 184.95.46.0/24, 185.222.57.0/24, 185.222.58.0/24, 185.30.177.0/24, 185.30.179.0/24, 191.6.217.0/24, 194.76.247.0/24, 195.200.94.0/24, 195.200.95.0/24, 207.244.238.0/24, 209.151.153.0/24, 209.50.48.0/24, 23.251.226.0/24, 31.14.183.0/24, 39.46.6.0/24, 45.137.22.0/24, 45.152.150.0/24, 45.165.125.0/24, 45.58.190.0/24, 64.34.200.0/2, 74.208.48.0/24, 89.47.183.0/24 en todos los puertos
Denegar16Denegar entrante de 101.205.0.0/16, 101.91.0.0/16, 105.112.0.0/16, 106.83.0.0/16, 110.167.0.0/16, 111.88.0.0/16, 112.194.0.0/16, 114.96.0.0/16, 115.84.0.0/16, 118.41.0.0/16, 119.53.0.0/16, 121.202.0.0/16, 121.204.0.0/16, 123.13.0.0/16, 123.16.0.0/16, 124.236.0.0/16, 124.79.0.0/16, 134.249.0.0/16, 14.142.0.0/16, 153.37.0.0/16, 178.48.0.0/16, 178.90.0.0/16, 185.30.0.0/16, 45.137.0.0/16 en todos los puertos
PermitirGrupoPermitir entrante de 0.0.0.0/1, 128.0.0.0/1, 137.103.0.0/16, 138.186.0.0/16, 152.231.0.0/16, 165.98.0.0/16, 170.250.0.0/16, 177.228.0.0/14, 177.232.0.0/13, 177.240.0.0/13, 177.248.0.0/16, 181.174.0.0/16, 181.209.0.0/16, 186.0.0.0/8, 187.0.0.0/8, 189.0.0.0/8, 190.0.0.0/8, 192.141.0.0/16, 200.39.0.0/16, 200.68.0.0/16, 200.92.0.0/16, 201.0.0.0/8, 208.96.0.0/16, 34.227.0.0/16, 38.122.0.0/16, 54.205.0.0/16, 65.246.0.0/16, 71.135.0.0/16 en los puertos 21/tcp, 22/tcp, 25/tcp, 53/tcp, 80/tcp, 106/tcp, 110/tcp, 139/tcp, 143/tcp, 443/tcp, 445/tcp, 465/tcp, 587/tcp, 993/tcp, 995/tcp, 3306/tcp, 5432/tcp, 8443/tcp, 8447/tcp, 8880/tcp, 9025/tcp, 49152-65535/tcp

These rules have been active for a couple of days, but today I received spam from 159.223.74.99
The rules are supposed to cascade from top to bottom, so you shouldn't have received spam from this address.
That's right?, So I don't know what I'm doing wrong.
 
Back
Top