Skip to content

Instantly share code, notes, and snippets.

@gartnera
Last active January 6, 2016 17:35
Show Gist options
  • Save gartnera/251316db85097ed6c7af to your computer and use it in GitHub Desktop.
Save gartnera/251316db85097ed6c7af to your computer and use it in GitHub Desktop.
Ban people trying to login to your server as root
#!/bin/bash
#run to setup
#iptables -N root-ban
#iptables -A root-ban -j RETURN
#iptables -A INPUT -j root-ban
#uncomment if cron
#export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
if [ ! -f /var/log/root_banned ]; then
touch /var/log/root_banned;
fi
ipsToBan=$(cat /var/log/auth.log | perl -lne '/root from ([^ ]*)/ && print $1' | sort| uniq | comm -3 - /var/log/root_banned | tee -a /var/log/root_banned)
for ip in $ipsToBan; do
if [[ $ip != "127.0.0.1" ]]; then
iptables -I root-ban -s $ip -j DROP;
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment