Visto che il Forum in questo periodo soffre, anche per un motivo diverso, posto uno script per openwrt che fa un bel DROP degli ip mascalzoni che tentano di entrare in ssh nel nostro router o, quasi peggio, ciucciarci il credito Voip dal nostro asterisk installato.
Da puro scemo, ma proprio scemo, ho lasciato per provauna settimana un peer collegato dall'esterno sul mio asterisk con una pass ridicola e dopo essersi autenticati mi han ciucciato tutto il credito da Eutelia (25 ?... se ce ne fossero stati 25.000 ero fritto
), autenticarsi ad un asterisk gestito male ? cosa, e lo sapevo, semplice semplice.
Vabb?, oltre a sistemare la cosa, cambiare le pass, bloccare gli ip sui peer e le altre cose base da FARE per avere un asterisk, per quello che si pu?, sicuro ho guardato se si poteva installare fail2ban.
Per chi non lo conosce fail2ban legge i file di log e se trova ip che fa il furbo, a seconda di determinate regole, lo blocca.
Fail2ban non ? stato implementato su openwrt, gli serve python e per un povero router sarebbe troppo.
Allora ho fatto un povero script che fa (dovrebbe fare
) la stessa cosa.
Lo script (bloccaip.sh), lanciato da cron ogni 20 minuti esegue le seguenti operazioni:
assegna una serie di ip sicuri
va a leggere i log con "logread"
e il file /var/log/asterisk/messages
imposta dopo quanti tentativi un ip dev'essere droppato
imposta l'email dell'ammministratore
poi va a leggere i file di log, comtrolla le righe che contengono info significative, individua l'ip e se non ? gi? stato inserito lo inserische nel file /etc/firewall.user facendo ripartire il firewall. Se viene bloccato un ip manda un'emai all'amministratore per avvisarlo. Alla fine rif? partire il firewall.
Per dire, echo in due giorni i tentativi (nel file del firewall vengono riportati anche i dettagli del perch? son stati droppati:
# This file is interpreted as shell script.
# Put your custom iptables rules here, they will
# be executed with each firewall (re-)start.
iptables -A input_wan -s 41.238.237.161/16 -j DROP # Asterik Device does not match ACL
iptables -A input_wan -s 188.161.2# This file is interpreted as shell script.
# Put your custom iptables rules here, they will
# be executed with each firewall (re-)start.
iptables -A input_wan -s 41.238.237.161/16 -j DROP # Device does not match ACL
iptables -A input_wan -s 188.161.231.32/16 -j DROP # Device does not match ACL
iptables -A input_wan -s 41.239.177.13/16 -j DROP # Device does not match ACL
iptables -A input_wan -s 41.199.177.151/16 -j DROP # Device does not match ACL
iptables -A input_wan -s 41.234.126.237/16 -j DROP # Device does not match ACL
iptables -A input_wan -s 196.221.185.120/16 -j DROP # Device does not match ACL
iptables -A input_wan -s 119.237.166.210/16 -j DROP # Asterisk Wrong password
iptables -A input_wan -s 184.172.196.132/16 -j DROP # ssh bad password attempt for
iptables -A input_wan -s 124.207.65.146/16 -j DROP # ssh bad password attempt for
ecco lo script (naturalmente lo stesso gioco pu? essere fatto con altri servizi e log)
#!/bin/sh
#
#########################################################################
# File: /usr/sbin/bloccaip.sh #
# Descrizione: droppa Ip su log (ssh, asterisk) #
# #
# truthahn - Nabuk Family - vonkosta@gmail.com #
#########################################################################
#
ipsicuri="xxx:xxx:xxx:xxx xxx:xxx:xxx:xxx xxx:xxx:xxx:xxx" # distanziati da uno spazio
log=`logread`
file=/var/log/asterisk/messages
filefirewall=/etc/firewall.user
numerolimite=2 #limite per essere droppato
emailadmin="pippo@pippo.com"
#ssh
for a in "bad password attempt for" "login attempt for nonexistent user" ;do
attack="$(echo $log | grep "$a" | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' | sort | uniq -c | sort -nr )"
numtot="$(($(echo $attack | tr ' ' '\n' | sed -n '$ =') -1))"
for numero in $(seq 1 2 $numtot); do
volte="$(echo $attack | awk '{print $'$numero'}')"
if [ "$volte" != "" ];then
if [ "$volte" -gt "$numerolimite" ];then
numero1="$(($numero +1))"
ip="$(echo $attack | awk '{print $'$numero1'}')"
giainserito="$(cat $filefirewall | grep "iptables -A input_wan -s $ip/16 -j DROP")"
controlsicuri="$(echo "$ipsicuri" | grep "$ip")"
if [ "$giainserito" == "" ] && [ "$controlsicuri" == "" ];then
echo "iptables -A input_wan -s $ip/16 -j DROP # ssh $a" >> $filefirewall
testo="Ho bloccato l'ip $ip/16 per: ssh ($a , n. tentativi: $volte)"
echo -e "Subject: Blocco ip Covo\n
$(date +%d/%m/%Y) - $(date +%H:%M:%S)\n
$testo" | msmtp $emailadmin
fi
fi
fi
done
done
# asterisk
for a in "Device does not match ACL" "Wrong password" "No matching peer found" "name mismatch" "peer is not supposed to register" ;do
attack="$(cat $file | grep "$a" | grep -Eo "'"'([0-9]{1,3}\.){3}[0-9]{1,3}'"'" | sort | uniq -c | sort -nr | sed "s/'/\\ /g")"
numtot="$(($(echo $attack | tr ' ' '\n' | sed -n '$ =') -1))"
for numero in $(seq 1 2 $numtot); do
volte="$(echo $attack | awk '{print $'$numero'}')"
if [ "$volte" != "" ];then
if [ "$volte" -gt "$numerolimite" ];then
numero1="$(($numero +1))"
ip="$(echo $attack | awk '{print $'$numero1'}')"
giainserito="$(cat $filefirewall | grep "iptables -A input_wan -s $ip/16 -j DROP")"
controlsicuri="$(echo "$ipsicuri" | grep "$ip")"
if [ "$giainserito" == "" ] && [ "$controlsicuri" == "" ];then
echo "iptables -A input_wan -s $ip/16 -j DROP # Asterisk $a" >> $filefirewall
testo="Ho bloccato l'ip $ip/16 per: asterisk ($a , n. tentativi: $volte)"
echo -e "Subject: Blocco ip Covo\n
$(date +%d/%m/%Y) - $(date +%H:%M:%S)\n
$testo" | msmtp $emailadmin
fi
fi
fi
done
done
/etc/init.d/firewall restart > /dev/null
exit 0 > /dev/null
naturalmente affinch? venga mandata un'email all'amministratore dev'essere installato e configurato "msmtp"
Lo sto testando... vediamo se ha qualche problema... che casomai riporter?.
Saluti a tutti gli utenti preoccupati per il nostro amato forum.