I just logged in on a GitLab server and noticed that it had 18.974 failed logins since I last checked the server - almost 5 days. I checked the Ip's and it seems that almost all of them were from China and tried to gain access with SSH and Brute Force. I started to block some Ip's but then i realized that it is a huge waste of time and a better idea would be to block the entire country.
Is there any way i can block ALL China or any other country with iptables?
I found some articles on the internet but almost all of them are bash scripts. I'm a newbie on Linux so i don't really understand all those scripts. I find iptables really interesting and i want to learn more about it.
Any ideas ? Thank you!
China block using ipset
You can't manually add a few thousand IP addresses to your iptables, and even doing it automatically is a bad idea because it can cause a lot of CPU load (or so I've read). Instead we can use ipset which is designed for this sort of thing. ipset handles big lists of ip addresses; you just create a list and then tell iptables to use that list in a rule.
Note; I assume that the entirety of the following is done as root. Adjust accordingly if your system is based on sudo.
Next, I wrote a small Bash script to do all the work, which you should be able to understand from the comments in it. Create a file:
Here's what you want to paste into it:
Save the file. Make it executable:
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:
Add the following line:
Save the file. To be clear, my full iptables.firewall.rules now looks like this:
Right now, nothing has changed with the server because no new rules have been applied; to do so, run the block-china.sh script:
This should show some output as it pulls a fresh list of Chinese based IPs and then, after a few seconds or so, it will complete and drop you back to a command prompt.
To test if it worked, run:
You should now see a new rule blocking China – the output ought to look like this:
Almost done! This works, and will continue to work on re-boots. But, IP addresses change and that list will grow stale over time. If you want to pull and apply an updated list of IPs you can just run the block-china.sh script again.
We can also set the machine to do that automatically via a cron job:
Add a line such as this:
This will run /etc/block-china.sh at 5am every day. To enable it upon reboot add another line such as this:
The user running the script will need to be root or have root privileges.
source
Using iptables to automatically identify, and thereafter block, bad guys for ssh can be done using the
recent
module. The following segment must come after your genericESTABLISHED,RELATED
line:Now, the recent (the last year or two) problem with China is that they have become very clever and very often once they get blocked from one IP address they simply switch to another on the same sub-net and continue. This runs the risk of running out of default recent table entries (I think the default is 200). I monitor this and then look up the actual IP segment, and permanently block the entire segment. In my case, I do not care about collateral damage, i.e. blocking someone innocent:
Where in the above:
You can get the entire list of IP addresses for China, or any country, in iptables, or other, format here. However the list is both surprisingly long and rather dynamic. Myself, I decided not to block the entire list.
You may want to install something like fail2ban so that it blocks ips that attempt to log into your server and fail.
You can use the geoip-module for iptables: https://linoxide.com/linux-how-to/block-ips-countries-geoip-addons/
You use to IP2Location Firewall List to generate iptables for China.
The file is in the following format. Run it in the shell and you should be blocking all China IP addresses.