13 de dezembro de 2009

Invasão no ssh do seu Servidor

Imagina o seguinte. Você tá na boa e seu servidor também. Aí vc vê por acaso o seu /var/log/messages e avista várias tentativas na porta do seu ssh. Você fica desesperado né. Estão tentando invadir meu Servidor! Ai Meu Deus!

Mas, você pode bloquear uma possível tentativa de invasão ao seu servidor ssh com o seguinte script:



#!/bin/bash
# Script de Lucas Sabino




MODE="AUTO"
#MODE="MANUAL"

if [ -f /var/log/messages ] ; then
ips=$(cat -n /var/log/messages | grep -P "(Invalid)" | grep -i sshd | awk -F" " '{ print $11}')
attempts=1
for ip in $ips ; do
lastip=$ip
if [ "$lastip" == "$ip" ] ; then
attempts=$(expr $attempts + 1)
if [ $attempts -ge 5 ] ; then
echo "Brute force SSHD attack detected from $ip"
attempts=1
lastip=""
blocked=$(iptables -L INPUT | grep -i $ip | grep -i DROP)
if [ "$blocked" ] ; then
echo "> Ip Already Blocked. Continuing with scan"
echo " "
else
if [ $MODE == "MANUAL" ] ; then
echo "> Do You Want to add this IP to INPUT DROP in IPTABLES rules? (y/n)"
read resp
if [ "$resp" == "y" ] ; then
iptables -A INPUT -s $ip -j DROP
echo "> IP $ip ADDED TO IPTABLES INPUT DROP ruleset"
echo " "
fi
else
iptables -A INPUT -s $ip -j DROP
echo "> IP $ip ADDED TO IPTABLES INPUT DROP ruleset"
echo " "
fi
fi
fi
fi
done
fi

Nenhum comentário: