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 48 hours
ipset create ssh hash:ip timeout 172800
# iptables INPUT connections on FTP port will be blocked 48 hours
ipset create ftp hash:ip timeout 172800
# 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 Redis port will be blocked forever
ipset create redis hash:ip
# iptables INPUT connections on Plesk and WHM/cPanel ports will be blocked 48 hours
ipset create plesk hash:ip timeout 172800
ipset create cpanel hash:ip timeout 172800
# 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
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
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
I have a question and I'm not quite sure if it's even possible. My webserver is behind an ELB and I get the user IP via X-Forward-For header. So I can block IPs and it works with:
iptables -A INPUT -p tcp --dport 80 -m string --algo bm --string 'X-Forwarded-For: 123.123.123.123' -j DROP
This way I can block individual IPs without any problems. However, I would like to block all IPs in an ipset "blacklist" (list is updated daily).
But when I add an IP to my blacklist, it is ignored by iptables.
I have set up the following rule for my blacklist on a test basis
I guess the XFF header IP is ignored because iptables only gets the ELB IP by this rule and does not look into the XFF header.
Abstractly described I need something like this:
iptables -A INPUT -p tcp --dport 80 -m string --algo bm --string 'X-Forwarded-For: blacklist' -j DROP
I want iptables to check the XXF header against my blacklist and drop the IP if there is a match.
It would really be a blessing if someone could help me or even just tell me that it doesn't work this way or has another idea how I can implement this to just pass IP lists to iptables.