Once in a while my server was getting a lot of traffic. As I discovered that was unwanted traffic, different IP’s were doing POST calls to my WordPress login. Not only I didn’t feel secure (I was being attacked) but they put my CPU at I had the CPU at 80% all the time.
iptables to the rescue
I already had a bunch of iptables rules set on my Ubuntu. Thing was, I didn’t know how to block a specific IP.
Some people, including myself, have a set of firewall rules in a separate file so every time Ubuntu restarts uses that set of rules.
1 |
nano /etc/iptables.firewall.rules |
On that file I was supposed to add the blocking (DROP) IP rules. They look like this:
1 |
-A INPUT -s xx.xx.xxx.xx -j DROP |
Use the iptables Ban Generator
It’s critical to put the DROP rules at the beggining, otherwise it doesn’t work. It looks like if a packet meets a rule, none of the following rules will apply. Meaning, if you put the DROP rules at the end, the packets will probably have met a previous ACCEPT rule.
Range Ban
To block 116.10.191.* addresses:
1 |
-A INPUT -s 116.10.191.0/24 -j DROP |
To block 116.10.. addresses:
1 |
-A INPUT -s 116.10.0.0/16 -j DROP |
To block 116…* addresses:
1 |
-A INPUT -s 116.0.0.0/8 -j DROP |
Tips
Restore the iptables based on the firewall rules file:
1 |
iptables-restore < /etc/iptables.firewall.rules |
Print the iptables with the IPs:
1 |
iptables -L -n |
Starter file with some firewall rules:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
*filter # Allow all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0 -A INPUT -i lo -j ACCEPT -A INPUT -d 127.0.0.0/8 -j REJECT # Accept all established inbound connections -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # Allow all outbound traffic - you can modify this to only allow certain traffic -A OUTPUT -j ACCEPT # Allow HTTP and HTTPS connections from anywhere (the normal ports for websites and SSL). -A INPUT -p tcp --dport 80 -j ACCEPT -A INPUT -p tcp --dport 443 -j ACCEPT # Allow SSH connections # # The -dport number should be the same port number you set in sshd_config # -A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT # Allow ping -A INPUT -p icmp -j ACCEPT # Log iptables denied calls -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7 # Drop all other inbound - default deny unless explicitly allowed policy -A INPUT -j DROP -A FORWARD -j DROP COMMIT |
Further information in Linode’s Documentation.
Marcian
Thanks. There’s an army of bots POSTing to wp-login endpoints. Sometimes our app would get hit dozens of times per second.