asterisk_security [post by JPRANGI on didforsale.com] This week one of our customer was attacked by more than 10000 Unique IPs. These hackers try to register on your system using some random username and easy to crack password. Few week back I wrote few tips on securing your asterisk servers.
Even if you took all the steps to secure your asterisk, still you dont want these attackers to flood your system with dummy registration requests. Use iptables and easy to implement shell script to block these attackers. You can easily block flooding traffic to your system. Iptables, can be used to filter IP traffic, provides high level packet filtering. Use the shell script below and setup a cron and have a good night sleep. The script will automatically block the IPs flooding your Asterisk system with failed registration requests.
Monitor Asterisk’s Log for Failed Registrations
In most cases of a sip flood attack, the host tries to register on your Asterisk. All the failed attempts from these hosts are identified in the Asterisk log (/var/log/messages or /var/log/full if you are using freepbx or Trixbox) as “No matching peer found.” The following script scans /var/log/full for these patterns, strips the IP address of attacker, and block it.
Script reads the log file and use IPTABLES to block any further attempts. While reading the log file it always set a check in and check out flag. So that next time it can start from last check out position.
Copy the code and save in /usr/local/bin/check_sip_attack
chomod 755 /usr/local/bin/check_sip_attack
#########Start from Next line ##########
#!/bin/bash
# Script Donated by www.didforsale.com
#crontab -l
# make an entry in Crontab
#01-59/2 * * * * /usr/local/bin/check_sip_attack
# echo “BINDIR= “ ${BINDIR}
cd /var/log/asterisk
log=”full”
if [ ! -r ${log} ]; then
printf “could not read error file (${log})\n”
else
start=”`grep -n -e CRON\:\ start ${log} | tail -n -1 | sed s/\:/\ /g | awk ‘{print $1}’`”
stop=”`grep -n -e CRON\:\ stop ${log} | tail -n -1 | sed s/\:/\ /g | awk ‘{print $1}’`”
if [ "$start" = "" ]; then start=0; fi
if [ "$stop" = "" ]; then stop=0; fi
if [ "$start" -le "$stop" ]; then
error=”`tail -n +${stop} ${log} | grep -i Registration | grep -i Failed | tail -n +1`”
if [ \! \( "x$error" = "x" \) ]; then
printf “\n\nCRON: start — sending info — `date`\n\n” >> ${log}
ccc=`printf “%s” “$error” | wc -l | awk ‘{print $1}’`
if [ $ccc -gt 0 ]; then
printf “EXCERPT FROM ASTERISK LOG FILE ${log}:\n\n%s\n\nDONE.\n\n” “$error”
printf “%s” “$error” > /tmp/sipappatck.tmp
for ip in `cat /tmp/sipappatck.tmp | awk ‘{print $11}’ | sort | uniq | sed s/\’//g` ; do
echo “iptables -I INPUT -s $ip -j DROP”
/sbin/iptables -I INPUT -s $ip -j DROP
done
fi
printf “\n\nCRON: stop — info sent — `date`\n\n” >> ${log}
fi
fi
fi
exit 0
# end
#########Stop here ##########
Final step is to schedule the script with cron. Add a line in cron.
01-59/2 * * * * /usr/local/bin/check_sip_attack
This will run the scrip for every two minutes (Of course you can change the timings) and have a good night sleep.
Last Updated ( Thursday, 31 March 2011 11:30 )