Debian attack blocking
Jump to navigation
Jump to search
Spotting Attacks
From ssh run:
tail /var/log/auth.log
Look for repeating errors like these from ip 218.25.208.90:
Jan 4 02:55:09 vps58963 sshd[26586]: Disconnecting: Too many authentication failures for root from 218.25.208.90 port 29523 ssh2 [preauth] Jan 4 02:55:09 vps58963 sshd[26586]: PAM 5 more authentication failures; logname= uid=0 euid=0 tty=ssh ruser= rhost=218.25.208.90 user=root Jan 4 02:55:09 vps58963 sshd[26586]: PAM service(sshd) ignoring max retries; 6 > 3 Jan 4 02:55:13 vps58963 sshd[26588]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=218.25.208.90 user=root Jan 4 02:55:15 vps58963 sshd[26588]: Failed password for root from 218.25.208.90 port 54063 ssh2 Jan 4 02:55:18 vps58963 sshd[26588]: Failed password for root from 218.25.208.90 port 54063 ssh2 Jan 4 02:55:21 vps58963 sshd[26588]: Failed password for root from 218.25.208.90 port 54063 ssh2 Jan 4 02:55:24 vps58963 sshd[26588]: Failed password for root from 218.25.208.90 port 54063 ssh2 Jan 4 02:55:28 vps58963 sshd[26588]: Failed password for root from 218.25.208.90 port 54063 ssh2 Jan 4 02:55:30 vps58963 sshd[26588]: Failed password for root from 218.25.208.90 port 54063 ssh2 Jan 4 02:55:30 vps58963 sshd[26588]: Disconnecting: Too many authentication failures for root from 218.25.208.90 port 54063 ssh2 [preauth]
Blocking attacks
See http://stackoverflow.com/questions/30818931/debian-8-iptables-persistent .
See https://www.rackaid.com/blog/how-to-block-ssh-brute-force-attacks/ .
Now make sure iptables-persistent is installed, add an ip blocking rule to the iptables and save the rules:
apt-get install iptables-persistent iptables -A INPUT -s 218.25.208.90 -j DROP invoke-rc.d netfilter-persistent save
Verify that the auth attachs stopped:
tail /var/log/auth.log
The ip blocking rule will survive a reboot.
More iptable tips
Edit and reload iptables rules file:
nano /etc/iptables/rules.v4 invoke-rc.d netfilter-persistent reload
Better rules from https://rudd-o.com/linux-and-free-software/a-better-way-to-block-brute-force-attacks-on-your-ssh-server :
iptables -A INPUT -i eth0 -p tcp -m tcp --dport 22 -m state --state NEW -m recent --set --name SSH --rsource iptables -A INPUT -i eth0 -p tcp -m tcp --dport 22 -m recent --rcheck --seconds 30 --hitcount 4 --rttl --name SSH --rsource -j REJECT --reject-with tcp-reset iptables -A INPUT -i eth0 -p tcp -m tcp --dport 22 -m recent --rcheck --seconds 30 --hitcount 3 --rttl --name SSH --rsource -j LOG --log-prefix "SSH brute force " iptables -A INPUT -i eth0 -p tcp -m tcp --dport 22 -m recent --update --seconds 30 --hitcount 3 --rttl --name SSH --rsource -j REJECT --reject-with tcp-reset iptables -A INPUT -i eth0 -p tcp -m tcp --dport 22 -j ACCEPT