Alright, I've not gotten any good responses and it took trial and error, as well as monitoring to determine what works to achieve this.
I found some things were said to be needed, so the example up above might not work on all systems because a full path should be used on the executables. Also, when specifying a range of ports, you need to add in --match multiport otherwise it will ignore the rule entirely. Lastly, I added in a shebang at the top to ensure that the script will be run correctly by shell.
So here is the final version:
/usr/local/csf/bin/csfpre.sh
#!/bin/sh /sbin/iptables -A INPUT -p tcp --dport 80 -j ACCEPT /sbin/iptables -A INPUT -p tcp --dport 443 -j ACCEPT /sbin/iptables -A INPUT -p all -s 60.168.112.0/20 -j DROP /sbin/iptables -A INPUT -p tcp --match multiport --dport 21:26 -s 1.0.0.0/8 -j DROP /sbin/iptables -A INPUT -p tcp --match multiport --dport 21:26 -s 112.0.0.0/7 -j DROP /sbin/iptables -A INPUT -p tcp --match multiport --dport 21:26 -s 116.96.0.0/12 -j DROP /sbin/iptables -A INPUT -p tcp --match multiport --dport 21:26 -s 116.118.0.0/16 -j DROP /sbin/iptables -A INPUT -p tcp --match multiport --dport 21:26,110,465,587,995 -s 117.0.0.0/8 -j DROP
Now for the breakdown of what is going on on this cPanel server with CSF installed for a firewall.
CSF allows adding custom rules that run in separate groups. All groups ultimately are run by iptables. First, csfpre.sh, then CSF, then csfpost.sh.
Create a csfpre.sh file if it doesn't exist. You can put this in the /etc/ folder somewhere too, but it will always take the version in /usr/local/csf/bin/ with priority.
Add the shebang at the top:
#!/bin/sh
My plan is to do some port blocking via csfpre.sh, but rather than have it run through all the rules, I have it first detect whether the connection is for a webpage visit. By checking this first, it reduces the latency/response time.
Ports 80 and 443 are for HTTP and HTTPS protocols, before anything else, if the input is for either of those ports, ACCEPT and stop checking rules for this csfpre group:
/sbin/iptables -A INPUT -p tcp --dport 80 -j ACCEPT /sbin/iptables -A INPUT -p tcp --dport 443 -j ACCEPT
Now, we could just block entirely all other ports except for 80 and 443 if the next line is as such:
/sbin/iptables -A INPUT -p all -s 60.168.112.0/20 -j DROP
Since all web traffic is accepted already, this will not block the website traffic for that range. If this line came first, then it would block all traffic, including web traffic. I don't want to block good users from viewing websites by blocking all an entire subnet they are in.
If blocking is reduced to specific ports, then we can block a specific port, a range, a list, or a combination of these.
To only block FTP:
/sbin/iptables -A INPUT -p tcp --dport 21 -s 1.0.0.0/8 -j DROP
FTP actually uses a few different ports to establish a connection, and there also is SFTP/SSH which standardly is port 22 so better to block a range by using the starting port separated by a colon and then the ending port:
/sbin/iptables -A INPUT -p tcp --match multiport --dport 21:26 -s 1.0.0.0/8 -j DROP
You must use the --match multiport if you are using a range or a list. Lists can have no more than 15 ports, and each port in a range counts against the 15 total ports.
You can also block the standard SMTP email ports:
/sbin/iptables -A INPUT -p tcp --match multiport --dport 110,465,587,995 -s 117.0.0.0/8 -j DROP
And, you can indeed use ranges in your list to block the FTP and mail ports in one rule:
/sbin/iptables -A INPUT -p tcp --match multiport --dport 21:26,110,465,587,995 -s 117.0.0.0/8 -j DROP
Save your script, and restart the firewall.
Let CSF and cpHulk block individual IP addresses as needed.
You can use your smartphone while not using your local connection to test with. Get the IP address of your phone, and be sure it is not the same as the computer you'll be working from. You can then run through all the scenarios, assuming you have the phone set up to check or send email through your server and an FTP program as well.
For the blocking, I've decided to restrict entire subnets from accessing FTP, and for some, SMTP. In order to whittle it down, I've analyzed all the incoming alerts and then compared the worst subnets with the countries listed on this website: http://www.tcpiputils.com/browse/ip-address
The end goal is to reduce the number of individual IPs being blocked by CSF. Blocking thousands of IPs can cause a latency issue, so by having some standard rules to block countries rife with malicious users reduces the need to manage such a large number of individual IPs.
To recalculate valid subnet ranges, use this tool: http://www.subnet-calculator.com/cidr.php
112.0.0.0/7 spans 112.0.0.0 to 113.255.255.255, but 111.0.0.0/7 is invalid blocking, so it would be 110.0.0.0 to 111.255.255.255. It's important that you verify your subnet ranges so that you don't wind up blocking the wrong IPs.