by nubinetwork · 2y ago · 0 comments
In a startup / init script / systemd unit file:
# IPv4
ipset flush bots 2>/dev/null
ipset create bots hash:ip hashsize 2048 maxelem 65536 timeout 604800 netmask 24 2>/dev/null
# IPv6
ipset flush bots6 2>/dev/null
ipset create bots6 hash:ip hashsize 2048 maxelem 65536 timeout 604800 netmask 64 family inet6 2>/dev/null
In this example I am using a bigger netmask much in the way name servers rrl rate limit.In the raw table, drop bots we saw for a week:
-A PREROUTING -i eth0 -p tcp -m tcp --dport 22:80 -d [server ip] -m set --match-set bots src,dst -j DROP
-A PREROUTING -i eth0 -p tcp -m tcp --dport 22 -d [server ip] -m string --string "SSH-2.0-libssh" --algo bm --from 10 --to 60 -j SET --add-set bots src --exist --timeout 604800
In the filter table outbound rules: -A OUTPUT -o eth0 -p tcp -m tcp --sport 22 -m set --match-set bots dst -j REJECT --reject-with tcp-reset
This should only be performed on servers that one has console / out-of-band access to, after exhaustive testing.