Bonjour, en lisant il y a quelques jours un article sur FireHOL (https://korben.info/firehol-protection-ip-pare-feu.html), je me suis re-penché sur ce script, actif sur mon NAS et routeur depuis plusieurs années. voici une version qui intègre les listes niveaux 2 et 3 (j'ai cru comprendre que la liste niveau 1 contient des IP locales, je n'ai donc pas pris de risque...) cela fait un petit complément d'adresses "à risque" intégrées dans la liste des blocages! #!/bin/sh
#
#How to Use: AutoblockScriptSynology
#
# Téléchargez le script. Copiez le dans un dossier de votre choix.
# Pour DSM, Créez une tache planifiée avec root comme utilisateur.
# sh /dossier_où_ a_été copié_le_script/autoblocksynology.sh
# Pour SRM, Créez une ligne dans crontab avec root comme utilisateur.
# sh /dossier_où_ a_été copié_le_script/autoblocksynology.sh
#
###############################################################################
###############################################################################
# Script du tutoriel de nas-forum.com par Superthx
###############################################################################
# Ce script accepte un paramètre: "raz"
# S'il est présent:
# le script débute par la suppression des IP non bloquées définitivement
###############################################################################
### PARAMETRAGE ###
###################
# Fréquence de lancement de ce script par la tache planifiée (en heures)
#(exemples: 1 si chaque heure, 24 si journalier)
Freq="1"
# Adresses des sites source séparées par un espace
Liste_Url="https://lists.blocklist.de/lists/ \
https://feodotracker.abuse.ch/downloads/ https://rules.emergingthreats.net/blockrules/ https://raw.githubusercontent.com/firehol/blocklist-ipsets/master/"
# Pour la liste de www.blocklist.de
# Liste de choix: {all} {ssh} {mail} {apache} {imap} {ftp} {sip} {bots}
# {strongips} {ircbot} {bruteforcelogin}
#Choix séparés par un espace, exemple "ssh apache bruteforcelogin"
Choix_Blocklist_de="all"
# Pour la liste de www.feodotracker.abuse.ch
# Liste de choix par ordre de sévérité:
# {ipblocklist_recommended} {ipblocklist} {ipblocklist_aggressive}
Choix_Feodotracker="ipblocklist_aggressive"
Choix_Emergingthreats="compromised-ips"
Choix_Firehol_lev2="firehol_level2.netset"
Choix_Firehol_lev3="firehol_level3.netset"
#Fichier personnel facultatif listant des IP (1 par ligne) à bloquer
Filtre_Perso="filtreperso.txt"
# Pour trace facultative des IP non conformes au format IP v4 ou v6
#Choix: {0}: sans trace, {1}: dans fichier log, {2}: dans fichier spécifique
Trace_Ano=1
File_Ano="anoip.txt" # à renseigner si option2 (sinon ne pas supprimer)
# Pour afficher des info plus détaillées en fin de fichier log
#info: {0}: sans info, {1}: avec info
info=1
###############################################################################
###############################################################################
### CONSTANTES ###
##################
Version="v0.1.0"
db="/etc/synoautoblock.db"
dirtmp="/tmp/autoblock_synology"
marge=60
###############################################################################
### FONCTIONS ###
##################################
raz_ip_bloquees(){
sqlite3 $db <<EOL
delete from AutoBlockIP where DENY = 1 and ExpireTime > 0;
EOL
echo "Le blocage des IP non bloquées définitivement a été supprimé"
}
###############################################################################
tests_initiaux(){
echo -e "\nDemarrage du script `basename $0` $Version: $(date)"
if [ -f "/bin/bash" ]; then
TypeShell="bash"
elif [ -f "/bin/sh" ]; then
TypeShell="sh"
else
echo -e "Erreur dans le script\nAbandon du script"
exit 1
fi
if [[ $# -gt 0 ]]; then
if [[ "$1" == "raz" ]]; then
raz_ip_bloquees
else
echo -e "Parametre $1 incorrect!\nSeul parametre autorisé: 'raz'"
echo "Abandon du script"
exit 1
fi
fi
if [ ! -d "/tmp" ]; then # par sécurité
echo -e "Le dossier tmp n'existe pas\nAbandon du script" # par sécurité
exit 1 # par sécurité
elif [ ! -d $dirtmp ]; then
mkdir $dirtmp
chmod 755 $dirtmp
fi
}
###############################################################################
raz_fil_ano(){
if [ -f $File_Ano ]; then
rm $File_Ano
fi
if [[ $Trace_Ano == 2 ]]; then
echo -e "\nDemarrage du script $Version: $(date)" > $File_Ano
fi
}
###############################################################################
info_nb_ip(){
nb_new=$(wc -l $tmp1 | cut -d' ' -f1)
nb=$((nb_new-nb_old))
if [[ $nb -gt 0 ]];then
if [[ $info == 1 ]];then
echo "$nb IP téléchargées sur $fil"
fi
else
if [[ $fil != $Filtre_Perso ]];then
echo "Echec chargement IP depuis le site $fil"
else
echo "$nb IP téléchargée sur $fil"
fi
fi
nb_old=$nb_new
}
###############################################################################
acquisition_ip(){
tmp1="$dirtmp/temp1"
nb_old=0
if [ -f $Filtre_Perso ];then
fil=$Filtre_Perso
cat "$fil" > $tmp1
info_nb_ip
else
touch $Filtre_Perso
fi
for url in $Liste_Url; do
host=`echo $url | sed -n "s/^https\?:\/\/\([^/]\+\).*$/\1/p"`
case $host in
lists.blocklist.de)
for chx in $Choix_Blocklist_de; do
fil="$url$chx.txt"
curl -s $fil >> $tmp1
echo "" >> $tmp1 # ajout fin ligne absent
info_nb_ip
done
;;
feodotracker.abuse.ch)
for chx in $Choix_Feodotracker; do
fil="$url$chx.txt"
curl -s $fil | sed "/#/d" >> $tmp1
info_nb_ip
done
;;
# ajouté le 06/04/2026, mettre le racine du site, le script vient jusqu'à la liste d'IP
rules.emergingthreats.net)
for chx in $Choix_Emergingthreats; do
fil="$url$chx.txt"
curl -s $fil >> $tmp1
echo "" >> $tmp1 # ajout fin ligne absent
info_nb_ip
done
;;
# ajouté le 06/04/2026, mettre le racine du site, le script vient jusqu'à la liste d'IP
raw.githubusercontent.com)
for chx in $Choix_Firehol_lev2; do
fil=$url$chx
curl -s $fil | sed "/#/d" >> $tmp1
info_nb_ip
done
for chx in $Choix_Firehol_lev3; do
fil=$url$chx
curl -s $fil | sed "/#/d" >> $tmp1
info_nb_ip
done
;;
# pour des sites comportant une seule liste contenant exclusivement des IP
host3|host4|host5)
fil=$url
curl -s $url >> $tmp1
info_nb_ip
;;
*)
echo "Le traitement pour $url n'est pas implanté"
;;
esac
done
sort -ufdo $tmp1 $tmp1
nb_ip=$(wc -l $tmp1 | cut -d' ' -f1)
}
###############################################################################
maj_ip_connues(){
sqlite3 $db <<EOL
drop table if exists Var;
create table Var (name text primary key, value text);
EOL
`sqlite3 $db "insert into Var values ('stop', $block_off)"
`sqlite3 $db <<EOL
drop table if exists Tmp;
create table Tmp (IP varchar(50) primary key);
.mode csv
.import /tmp/autoblock_synology/temp1 Tmp
alter table Tmp add column ExpireTime date;
alter table Tmp add column Old boolean;
update Tmp set ExpireTime = (select value from Var where name = 'stop');
update Tmp set Old = (
select 1 from AutoBlockIP where Tmp.IP = AutoBlockIP.IP);
update AutoBlockIP set ExpireTime=(
select ExpireTime from Tmp where AutoBlockIP.IP = Tmp.IP and Tmp.Old = 1)
where exists (
select ExpireTime from Tmp where AutoBlockIP.IP = Tmp.IP and Tmp.Old = 1);
delete from Tmp where Old = 1;
drop table Var;
EOL
rm $tmp1
}
###############################################################################
tracer_ip_incorrecte(){
case $Trace_Ano in
1) echo "$nb_invalide:IP non traitée (format IP incorrect): $ip"
;;
2) echo "$nb_invalide : $ip" >> $File_Ano
;;
*) ;;
esac
}
###############################################################################
hex_en_dec(){
if [ "$1" != "" ];then
printf "%d" "$(( 0x$1 ))"
fi
}
###############################################################################
maj_ipstd(){
ipstd=''
if [[ $ip != '' ]]; then
if expr "$ip" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' > \
/dev/null; then
ipstd=$(printf "0000:0000:0000:0000:0000:FFFF:%02X%02X:%02X%02X" \
${ip//./' '})
elif [[ $ip != "${1#*:[0-9a-fA-F]}" ]]; then
ip6=$ip
echo $ip6 | grep -qs "^:" && $ip6="0${ip6}"
if echo $ip6 | grep -qs "::"; then
sep=$(echo $ip6 | sed 's/[^:]//g')
absent=$(echo ":::::::::" | sed "s/$sep//")
rempl=$(echo $absent | sed 's/:/:0/g')
ip6=$(echo $ip6 | sed "s/::/$rempl/")
fi
blocks=$(echo $ip6 | grep -o "[0-9a-f]\+")
set $blocks
ipstd=$(printf "%04X:%04X:%04X:%04X:%04X:%04X:%04X:%04X" \
$(hex_en_dec $1) $(hex_en_dec $2) $(hex_en_dec $3) $(hex_en_dec $4) \
$(hex_en_dec $5) $(hex_en_dec $6) $(hex_en_dec $7) $(hex_en_dec $8))
else
tracer_ip_incorrecte
fi
if [[ $ipstd != '' ]]; then
printf '%s,%s,%s,%s\n' "$ip" "$start" "$block_off" "$ipstd" >> $tmp1
fi
fi
}
###############################################################################
import_nouvelles_ip(){
sqlite3 $db <<EOL
drop table Tmp;
create table Tmp (IP varchar(50) primary key, RecordTime date,
ExpireTime date, IPStd varchar(50));
.mode csv
.import /tmp/autoblock_synology/temp1 Tmp
EOL
}
###############################################################################
insertion_nouvelles_ip_nas(){
sqlite3 $db <<EOL
insert into AutoBlockIP
select IP, RecordTime, ExpireTime, 1, IPStd, NULL, NULL
from Tmp where IPStd is not NULL;
drop table Tmp;
EOL
}
###############################################################################
insertion_nouvelles_ip_routeur(){
sqlite3 $db <<EOL
insert into AutoBlockIP
select IP, RecordTime, ExpireTime, 1, IPStd
from Tmp where IPStd is not NULL;
drop table Tmp;
EOL
}
###############################################################################
insertion_nouvelles_ip(){
newip=`sqlite3 $db "select IP from Tmp where IP <>''"`
for ip in $newip; do
maj_ipstd
done
if [ -f $tmp1 ]; then
import_nouvelles_ip
if [[ $TypeShell == "bash" ]];then
insertion_nouvelles_ip_nas
elif [[ $TypeShell == "sh" ]];then
insertion_nouvelles_ip_routeur
fi
rm $tmp1
fi
}
###############################################################################
informations_finales(){
maj=`sqlite3 $db "select count(*) from AutoBlockIP
where RecordTime < $start and ExpireTime = $block_off"`
ajt=`sqlite3 $db "select count(*) from AutoBlockIP where RecordTime = $start"`
block=`sqlite3 $db "select count(*) from AutoBlockIP
where DENY = 1 and
(ExpireTime = 0 or ExpireTime > $(($start+$marge+30)))"`
echo "$nb_ip IP ont été traitées"
echo "$maj IP ont vu leur blocage prolongé"
echo "$ajt nouvelles IP ont été ajoutées"
echo "$block IP seront bloquées suite à cette mise à jour"
duree=$((`date +%s`- $start))
if [[ $TypeShell == "bash" ]];then
echo -e "Fin du script exécuté en $(($duree/60))mn $(($duree%60))s"
elif [[ $TypeShell == "sh" ]];then
echo "Fin du script exécuté en $duree secondes"
fi
}
###############################################################################
### SCRIPT ###
##############
start=`date +%s`
block_off=$((start+Freq*2*3600+$marge))
pwd_init=`pwd`
cd `dirname $0`
tests_initiaux $1
raz_fil_ano
acquisition_ip
maj_ip_connues
insertion_nouvelles_ip
if [[ $info == 1 ]];then
informations_finales
else
echo "Script terminé"
fi
cd $pwd_init
exit 0
###############################################################################