Skip to content

Instantly share code, notes, and snippets.

@maikgreubel
Created February 5, 2023 16:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maikgreubel/e1a101932ca828a6f52eed06d55713c0 to your computer and use it in GitHub Desktop.
Save maikgreubel/e1a101932ca828a6f52eed06d55713c0 to your computer and use it in GitHub Desktop.
harden-sshd
#!/bin/bash
if [ $UID -ne 0 ]; then
echo "root only"
exit 1
fi
restart=0
for i in $(grep "Failed password" /var/log/secure | grep -oE '((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])' | uniq)
do
if [ `grep -c " $i\$" /etc/hosts.deny` -eq 0 ]; then
echo "sshd: $i" >> /etc/hosts.deny
echo "Added $i"
restart=1
fi
done
if [ $restart -gt 0 ]; then
service sshd restart
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment