嘿嘿!
全部博文(140)
分类: LINUX
2012-01-10 16:49:50
Previously we posted a little script for quickly checking your asterisk log for failed peer registrations. Building on that script, and with the use of iptables and cron, you can easily (and automatically) block flooding traffic from your system. Iptables, a linux command line program to filter IP traffic, provides high level packet filtering before the traffic can be used to corrupt a program. Cron, the linux time scheduler, enables you to automatically run commands at scheduled time periods.
Set up IP TablesWe will not be discussing the intricacies of iptables in this post. There are excellent tutorials on iptables, and with most things linux, help is only a google away. To help identify the traffic blocked as asterisk related, a new chain will be created appropriately called… asterisk.
Here’s how to add the new chain:
1 2 3 | iptables -N asterisk iptables -A INPUT -j asterisk iptables -A FORWARD -j asterisk |
This will help identify hosts blocked for failed registrations.
Asterisk’s Log for Failed RegistrationsIn most cases of a sip flood attack, the host attempts registration to Asterisk. These hosts are identified in the Asterisk log (/var/log/messages) as “No matching peer found.” The following perl script scans /var/log/messages for these patterns, strips the IP address, and puts the IP address into an array.
After the file has been read, the IP addresses are counted (each count is a failed attempt), compared against the existing blocked hosts, and new occurrences are blocked. With this script we are blocking any host after the 4th failed attempt.
Here’s the script (last updated 05 SEP 2010):
The final step is to schedule your script to run every X minutes in cron. We’ve chosen to run our script every 2 minutes, but you can change this to 1 minute or any other time period you choose. Just remember… you can receive thousands of attempts within 2 minutes.
If you have named your script check-failed-regs.pl and placed it in your /usr/local/bin directory, your cron statement would look like this:
1 | */2 * * * * perl /usr/local/bin/check-failed-regs.pl &> /dev/null |