Protect your server with a strong iptables rules and ipset lists.
apt install ipset
apt install iptables-persistent
# iptables fails on real SSH port will be blocked 24 hours
ipset create ssh-real hash:ip timeout 86400
# iptables INPUT connections on default SSH port will be blocked forever
ipset create ssh hash:ip
# iptables INPUT connections on FTP port will be blocked forever
ipset create ftp hash:ip
# iptables INPUT connections on MySQL, PostgreSQL and MongoDB ports will be blocked forever
ipset create mysql hash:ip
ipset create postgresql hash:ip
ipset create mongodb hash:ip
# iptables INPUT connections on any mail related port will be blocked forever
ipset create mail hash:ip
# iptables INPUT connections on Plesk and WHM/cPanel ports will be blocked forever
ipset create plesk hash:ip
ipset create cpanel hash:ip
# register rules
ipset save -file /etc/iptables/ipset
- Replace the
XXX.XXX.XXX.XXX
ip with a safe to connect IP, it will be your lifeguard. - Replace the port 987 with your real SSH port (always different than 22).
iptables-apply -t 60 /etc/iptables/rules.v4
iptables-restore < /etc/iptables/rules.v4
ipset list
Create file /etc/systemd/system/ipset-persistent.service
with:
[Unit]
Description=ipset persistent configuration
Before=network.target
# ipset sets should be loaded before iptables
# Because creating iptables rules with names of non-existent sets is not possible
Before=netfilter-persistent.service
Before=ufw.service
ConditionFileNotEmpty=/etc/iptables/ipset
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/sbin/ipset restore -file /etc/iptables/ipset
ExecStop=/usr/sbin/ipset save -file /etc/iptables/ipset
ExecStop=/usr/sbin/ipset flush
ExecStopPost=/usr/sbin/ipset destroy
[Install]
WantedBy=multi-user.target
RequiredBy=netfilter-persistent.service
RequiredBy=ufw.service
Enable service with
systemctl daemon-reload
systemctl enable ipset-persistent.service
Thanks to https://selivan.github.io/2018/07/27/ipset-save-with-ufw-and-iptables-persistent-and.html
https://gist.github.com/eusonlito/5afa0d42f1aff3cd2b82a8c0a8a3b75d
The default INPUT rule is DROP, then you can not flush rules with iptables -F
or your access will be blocked forever.
You need to execute:
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -F
iptables -X