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.
On that file I was supposed to add the blocking (DROP) IP rules. They look like this:
-A INPUT -s xx.xx.xxx.xx -j DROP
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.
To block 116.10.191.* addresses:
-A INPUT -s 22.214.171.124/24 -j DROP
To block 116.10.. addresses:
-A INPUT -s 126.96.36.199/16 -j DROP
To block 116...* addresses:
-A INPUT -s 188.8.131.52/8 -j DROP
Restore the iptables based on the firewall rules file:
iptables-restore < /etc/iptables.firewall.rules
Print the iptables with the IPs:
iptables -L -n
Starter file with some firewall rules:
*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.