Asterisk : Protégez-vous !

Depuis quelques semaines, avec plusieurs administrateurs de serveurs Asterisk nous constatons une évolution de tentatives d’attaques sont tentées chaque jours sur nos infrastructures. Ce sont des attaques de type ‘bruteforce’. Surement du à l’euphorie des vacances scolaires, nous avons répertorié une augmentation de 400% du nombre d’attaques/jours, soit 15-20 adresses IP bannies/jours et quelques mégas de traffic plombé 🙂

1. Détecter les attaques

Si votre serveur est visible sur internet (votre port SIP ouvert) vous pouvez détecter simplement si votre serveur subit des attaques, jetez un œil au fichier /var/asterisk/messages.log avec un pipe et un grep, comme ceci :

cat /var/log/asterisk/messages | grep failed

Si vous obtenez plusieurs lignes comme ceci :

[2010-09-02 11:54:16] NOTICE[16750] chan_sip.c: Registration from '"104"' failed for '67.228.xxx.xx' - No matching peer found
[2010-09-02 11:54:17] NOTICE[16750] chan_sip.c: Registration from '"105"' failed for '67.228.xxx.xx' - No matching peer found
[2010-09-02 11:54:17] NOTICE[16750] chan_sip.c: Registration from '"106"' failed for '67.228.xxx.xx' - No matching peer found
[2010-09-02 11:54:17] NOTICE[16750] chan_sip.c: Registration from '"107"' failed for '67.228.xxx.xx' - No matching peer found
[2010-09-02 11:54:17] NOTICE[16750] chan_sip.c: Registration from '"108"' failed for '67.228.xxx.xx' - No matching peer found
[2010-09-02 11:54:17] NOTICE[16750] chan_sip.c: Registration from '"109"' failed for '67.228.xxx.xx' - No matching peer found
[2010-09-02 11:54:17] NOTICE[16750] chan_sip.c: Registration from '"111"' failed for '67.228.xxx.xx' - No matching peer found
[2010-09-02 11:54:17] NOTICE[16750] chan_sip.c: Registration from '"112"' failed for '67.228.xxx.xx' - No matching peer found
[2010-09-02 11:54:17] NOTICE[16750] chan_sip.c: Registration from '"113"' failed for '67.228.xxx.xx' - No matching peer found
[2010-09-02 11:54:17] NOTICE[16750] chan_sip.c: Registration from '"114"' failed for '67.228.xxx.xx' - No matching peer found
[2010-09-02 11:54:17] NOTICE[16750] chan_sip.c: Registration from '"115"' failed for '67.228.xxx.xx' - No matching peer found
[2010-09-02 11:54:17] NOTICE[16750] chan_sip.c: Registration from '"116"' failed for '67.228.xxx.xx' - No matching peer found
[2010-09-02 11:54:17] NOTICE[16750] chan_sip.c: Registration from '"117"' failed for '67.228.xxx.xx' - No matching peer found
[2010-09-02 11:54:17] NOTICE[16750] chan_sip.c: Registration from '"118"' failed for '67.228.xxx.xx' - No matching peer found
[2010-09-02 11:54:17] NOTICE[16750] chan_sip.c: Registration from '"119"' failed for '67.228.xxx.xx' - No matching peer found
[2010-09-02 11:54:17] NOTICE[16750] chan_sip.c: Registration from '"1069446905"' failed for '67.228.xxx.xx' - No matching peer found
[2010-09-02 11:54:17] NOTICE[16750] chan_sip.c: Registration from '"120"' failed for '67.228.xxx.xx' - No matching peer found
[2010-09-02 11:54:17] NOTICE[16750] chan_sip.c: Registration from '"121"' failed for '67.228.xxx.xx' - No matching peer found
[2010-09-02 11:54:18] NOTICE[16750] chan_sip.c: Registration from '"122"' failed for '67.228.xxx.xx' - No matching peer found
[2010-09-02 11:54:18] NOTICE[16750] chan_sip.c: Registration from '"123"' failed for '67.228.xxx.xx' - No matching peer found
[2010-09-02 11:54:18] NOTICE[16750] chan_sip.c: Registration from '"124"' failed for '67.228.xxx.xx' - No matching peer found
[2010-09-02 16:53:53] NOTICE[16750] chan_sip.c: Registration from '"2614337962"' failed for '70.32.xxx.xxx' - No matching peer found
[2010-09-02 16:53:53] NOTICE[16750] chan_sip.c: Registration from '"123"' failed for '70.32.xxx.xxx' - No matching peer found
[2010-09-02 16:53:53] NOTICE[16750] chan_sip.c: Registration from '"1234"' failed for '70.32.xxx.xxx' - No matching peer found
[2010-09-02 16:53:53] NOTICE[16750] chan_sip.c: Registration from '"12345"' failed for '70.32.xxx.xxx' - No matching peer found
[2010-09-02 16:53:53] NOTICE[16750] chan_sip.c: Registration from '"123456"' failed for '70.32.xxx.xxx' - No matching peer found
[2010-09-02 16:53:53] NOTICE[16750] chan_sip.c: Registration from '"test"' failed for '70.32.xxx.xxx' - No matching peer found
[2010-09-02 16:53:53] NOTICE[16750] chan_sip.c: Registration from '"sip"' failed for '70.32.xxx.xxx' - No matching peer found
[2010-09-02 16:53:53] NOTICE[16750] chan_sip.c: Registration from '"user"' failed for '70.32.xxx.xxx' - No matching peer found
[2010-09-02 16:53:53] NOTICE[16750] chan_sip.c: Registration from '"admin"' failed for '70.32.xxx.xxx' - No matching peer found

C’est que vous êtes attaqué(e), pas de panique nous allons remédier à tout ca 🙂

2. Se préparer aux attaques

Vous vous doutez bien que dès qu’un serveur est ouvert sur la toile il sera évidement scanné… Si vous ouvrez le port 5060 (ou celui de votre choix sur lequel votre asterisk écoute) c’est que vous en avez une bonne raison (pas de VPN et des clients SIP mobiles).
Nous sommes dans une époque ou quelqu’un peut se procurer très facilement de la grosse artillerie en terme de bande passante sont capables de surcharger une petite connexion internet et bouffer du temps CPU sur des petits systèmes embarqués.

3. Faire face aux attaques

Je vais vous indiquer plusieurs possibilités pour protéger un serveur simple (après si vous avez du matos lourd pour détecter les bruteforce faites ^^), je vais parler au nom des petites infrastructures déployées ici et là et personnelles.

3.1 Changez votre port d’écoute

Le port d’écoute de l’asterisk par défault 5060, vous devriez me modifier si vous l’ouvrez sur internet 😉

La modification se fait dans le fichier /etc/asterisk/sip.conf :

[general]
context=in-da-box
bindport=5060
srvlookup=yes

Remplacer 5060 par un port d’écoute comme 38000 (qui est normalement libre), vérifiez quand même si aucun service écoute dessus à l’aide de la commande suivante :

netstat -nlapute | grep 38000

si aucune ligne n’en ressort, c’est qu’il est pas écouté par un autre process.

3.2 Utilisation de fail2ban

fail2ban est une application qui inspecte les fichier de logs et grâce à des expression régulières de bannir les adresses qui attaque les services de vos machines. tr-s simple à mettre en place :
Installation d’un serveur fail2ban => Article sur fail2ban
Création d’un filtre pour asterisk => Article sur la protection asterisk avec fail2ban

3.3 Restreindre le nombre de nouvelles connexions en un temps donné :

Entrez les commandes suivantes dans un shell : (Modifiez 38000 par votre port d’écoute et l’interface eth1 par la votre)

iptables -A INPUT -p udp --dport 38000 -i eth1 -m state --state NEW -m recent --set
iptables -A INPUT -p udp --dport 38000 -i eth1 -m state --state NEW -m recent --rcheck --seconds 3600 --hitcount 100 -j DROP
iptables -A INPUT -p udp --dport 38000 -i eth1 -m state --state NEW -m recent --rcheck --seconds 600 --hitcount 20 -j DROP
iptables -A INPUT -p udp --dport 38000 -i eth1 -m state --state NEW -m recent --rcheck --seconds 300 --hitcount 10 -j DROP
iptables -A INPUT -p udp --dport 38000 -i eth1 -m state --state NEW -m recent --rcheck --seconds 180 --hitcount 5 -j DROP
iptables -A INPUT -p udp --dport 38000 -i eth1 -m state --state NEW -m recent --rcheck --seconds 60 --hitcount 3 -j DROP

3.4 Bloquer les scanners les plus connus :

Entez les commandes suivantes dans un shell :

iptables -A RH-Firewall-1-INPUT -p udp -m string --to 300 --algo bm --string "friendly-scanner" -m udp --dport 5060 -j LOGDROP
iptables -A RH-Firewall-1-INPUT -p udp -m string --to 300 --algo bm --string "sip-scan" -m udp --dport 5060 -j LOGDROP
iptables -A RH-Firewall-1-INPUT -p udp -m string --to 300 --algo bm --string "iWar" -m udp --dport 5060 -j LOGDROP
iptables -A RH-Firewall-1-INPUT -p udp -m string --to 300 --algo bm --string "sipsak" -m udp --dport 5060 -j LOGDROP

N’hésitez pas à ajouter un commentaire si vous avez d’autres solutions pour protéger un asterisk 😉