Skip to content

Instantly share code, notes, and snippets.

@maikgreubel
maikgreubel / harden-sshd.sh
Created February 5, 2023 16:13
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
for o in $(grep -A1 "failed mail authentication attempt for user" /var/log/maillog |grep "authentication fail" | perl -pe 's#.*unknown\[([^\]].*?)\].*#\1#' | grep -E "^[[:digit:]]+" | uniq); do whois $o | grep -E "([0-9]{1,3}[\.]){3}[0-9]{1,3}\/" | grep -v "^%" | awk '{print $2}'; done