Wednesday February 8th 2012

Ssh brute force bloccato con pf e regole di filtering su OpenBSD

Poco tempo fa stavo stavo dando un occhiata ai miei log e mi sono subito accorto di un attacco brute force sulla porta ssh.

#tail /var/log/authlog

Dec 26 19:22:31 maxbsd sshd[12620]: Failed password for root from 218.23.105.25 port 45219 ssh2
Dec 26 19:22:31 maxbsd sshd[13293]: Received disconnect from 218.23.105.25: 11: Bye Bye
Dec 26 19:22:35 maxbsd sshd[6301]: reverse mapping checking getaddrinfo for 25.105.23.218.broad.static.hf.ah.cndata.com [218.23.105.25] failed - POSSIBLE BREAK-IN ATTEMPT!
Dec 26 19:22:35 maxbsd sshd[6301]: Failed password for root from 218.23.105.25 port 45474 ssh2
Dec 26 19:22:35 maxbsd sshd[2351]: Received disconnect from 218.23.105.25: 11: Bye Bye
Dec 26 19:22:39 maxbsd sshd[3801]: reverse mapping checking getaddrinfo for 25.105.23.218.bro

Per eliminare questo genere di attacchi possiamo:

  • Abilitare ssh in modo che dopo 3 tentativi sbagliati cada la connessione
  • Inserire una regola in pf in modo che non accetti più di 3 connessioni contemporaneamente dallo stesso Ip, per poi bannarlo in una blacklist
  • Creare uno script in modo che legga l’ip dell’attaccante dai log e lo inserisca nella blacklist.

Per risolvere il primo punto è sufficiente modificare il file di configurazione di ssh, sshd_config, decommentando l’opzione:

# nano /etc/ssh/sshd_config

MaxAuthTries 3

Per il secondo punto è necessario definire una tabella che contenga gli ip bloccati (la nostra blacklist). Dunque editiamo il file di configurazione di pf che si trova in /etc/pf.conf:

#nano /etc/pf.conf

table  persist file "/etc/spammers"

Ora invece scriviamo la regola in modo che non accetti più di 3 connessioni contemporaneamente dallo stesso Ip e lo inserisca nella blacklist:

int="fxp0"
ssh_limit="(max-src-conn-rate 3/30, overload  flush global)"
pass in log quick on $int inet proto tcp from any to $int port ssh flags S/SA keep state $ssh_limit

Infine per il terzo punto è sufficiente creare uno script, ssh_block_attack, come il seguente, in modo che legga il file di log e metta in blacklist l’indirizzo ip che ha sbagliato la password o l’username:

# nano /root/ssh_block_attack

#!/bin/sh
 
while read mm dd hms localhostname sshd word1 word2 word3 word4 host1 host2 rest;
do
if [ "$word1 $word2 $word4" = "Invalid user from" ]; then
data=`date`
pfctl -t spammers -T add $host1
echo "Aggiunto $host1 a spammers il $data" >> /root/ssh_log
 
elif [ "$word1 $word2 $word3 $host1" = "Failed password for from" ]; then
pfctl -t spammers -T add $host2
echo "Aggiunto $host2 a spammers il $data" >> /root/ssh_log
fi
done

Per lanciarlo avvio dandogli in pasto il log da monitorare aggiungiamo questa riga in /etc/rc.local .

tail -f /var/log/authlog | sh ssh_block_attack &

Infine se vogliamo vedere la tabella degli ip bloccati è necessario utilizzare questo comando

pfctl -t spammers -T show

Ovviamente per non saturare la nostra macchina ogni tanto andrebbe svuotata:

pfctl -t spammers -T delete indirizzo_ip
Related Tags: , , , ,

7 Comments for “Ssh brute force bloccato con pf e regole di filtering su OpenBSD”


Leave a Comment

More Topics

Fake AP in 2 seconds
Fake AP in 2 seconds

Making an fake access point in Windows 7 it’s now really simple. Thanks to the new Wireless Hosted Networks [Read More]

Tutorial write an exploit part 3 SEH
Tutorial write an exploit part 3 SEH

In the previous tutorial we have seen some technique of buffer overflow, in most cases with the aim to overwrite the [Read More]

Tutorial write an exploit Part 2

After having fully understood the tutorial part 1 let’s go to read the second one. In this tutorial we will see [Read More]

Would you be white hat if it paid more?
Would you be white hat if it paid more?

If this is true or not no one knows but it is interesting to have an idea about cyber market. You can read the full [Read More]

Tutorial write an exploit Part 1 JMP to ESP

This article begins a small series of tutorials that aims to make you understand in an easier and more detailed way how [Read More]

Twitter