login | register
Sat 04 of Jul, 2009 [06:18 UTC]

voip-info.org

Discuss [9] History

Asterisk firewall rules

Created by: oej,Last modification on Tue 15 of Apr, 2008 [18:55 UTC] by rsertori

Sample Asterisk Firewall Rules

IPTables

This is an example on how to configure a Linux IPTables firewall for Asterisk:


# SIP on UDP port 5060. Other SIP servers may need TCP port 5060 as well
iptables -A INPUT -p udp -m udp --dport 5060 -j ACCEPT

# IAX2- the IAX protocol
iptables -A INPUT -p udp -m udp --dport 4569 -j ACCEPT

# IAX - most have switched to IAX v2, or ought to
iptables -A INPUT -p udp -m udp --dport 5036 -j ACCEPT

# RTP - the media stream
iptables -A INPUT -p udp -m udp --dport 10000:20000 -j ACCEPT

# MGCP - if you use media gateway control protocol in your configuration
iptables -A INPUT -p udp -m udp --dport 2727 -j ACCEPT



More security:
BEWARE: these rules opened the firewall completely for me! Test them.

# iptables -A INPUT -p tcp --syn -m limit --limit 1/s -j ACCEPT
# iptables -A INPUT -p tcp --syn -j DROP



Bleeding edge and even more security:
(link is dead)

Get this one http://www.netfilter.org/patch-o-matic/pom-extra.html#pom-extra-sip-conntrack-nat and follow the docs :)




PF (Packet Filter)

This is an example on how to configure a OpenBSD/FreeBSD 5 PF firewall for Asterisk:

pf.conf

# Your inet interface
ext = rl0

# SIP (TCP)
voip_tcp = "5060"
# SIP, IAX2, IAX, RTP, MGCP (UDP)
voip_udp = "{5060, 4569, 5036, 9999 >< 20001, 2727}"

pf pass in on $ext inet proto tcp from any to any port $voip_tcp flags S/SA keep state
pf pass out on $ext inet proto tcp all flags S/SA keep state
pf pass in on $ext inet proto udp from any to any port $voip_udp keep state
pf pass out on $ext proto udp all keep state


pf.conf on gateway router/asterisk box with QoS

#### macros ####
ext_if="xl0" # 172.16.0.2
int_if="xl1" # 10.0.0.1
lan_net = "10.0.0.0/24"
table <blocked> persist
table <routed> persist
##machines
ext_ip = "172.16.0.2"
siphost = "172.16.0.3"
voip = "10.0.0.4"
#### options ####
set skip on lo0
set optimization conservative
set block-policy drop
set loginterface $ext_if
scrub in all
#### QoS stuff #######
altq on $ext_if priq bandwidth 520Kb queue { q_pri, q_def, q_bulk, q_crap }
queue q_pri priority 7
queue q_def priority 5 priq(default)
queue q_bulk priority 1
queue q_crap priority 0
#####    NAT   ####
nat on $ext_if from <routed> -> $ext_ip

#####  rules  ####
block drop out quick on $ext_if proto { udp, icmp, tcp } from any to <blocked>
block drop in quick on $ext_if proto { udp, icmp, tcp } from <blocked> to any
block drop in on $ext_if from any to any
pass in on $ext_if from $lan_net to any

## basic
pass out on $ext_if proto tcp all modulate state flags S/SA
pass out on $ext_if proto { udp, icmp } all keep state
pass in on $int_if proto icmp all keep state

## asterisk
pass in from any to $siphost
pass in quick proto udp from any to any port 4569 \
                keep state queue (q_pri)
pass out quick proto udp from any to any port 4569 \
                keep state queue (q_pri)
                                                                        
## default
pass out on $ext_if proto tcp from $ext_if to any flags S/SA \
       keep state queue (q_def, q_pri)
pass in on $ext_if proto tcp from any to $ext_if flags S/SA \
       keep state queue (q_def, q_pri)


IPFW

This is an example on how to configure a FreeBSD IPFW firewall for Asterisk:

rc.firewall

# Firewall comand
fwcmd="/sbin/ipfw -q"

# Interface setup
# Outside interface
oip="<your external ip address>"

# * pbx ip
pbxip="<your * internal ip>"

# VoIP Traffic - SIP & IAX
${fwcmd} add pass tcp from ${oip} to ${pbxip} 5060 keep-state in
${fwcmd} add pass tcp from ${pbxip} to any 5060 keep-state out
${fwcmd} add pass udp from ${oip} to ${pbxip} 5060 keep-state in
${fwcmd} add pass udp from ${oip} to ${pbxip} 4569 keep-state in
${fwcmd} add pass udp from ${oip} to ${pbxip} 2727 keep-state in
${fwcmd} add pass udp from ${oip} to ${pbxip} 9999-20001 keep-state in
${fwcmd} add pass udp from ${pbxip} to any keep-state out



rc.conf

# Your NAT & Firewall section should have this line
natd_flags="-redirect_address <your * internal ip> <your external ip address>"


ISA Server

To configure an ISA Server firewall for Windows, to permit Asterisk (win32 version) to run on the same box as the ISA Server:

SIPPF.VBS

Follow these steps:
  • Download the SPIPF.VBS script from www.generationd.com
  • Copy to any directory on the ISA Server.
  • Edit the file with any text editor - if you want to modify the log file parameters, etc.
  • Run the script by double clicking it
  • Wait and relax while the ports are opened. Be warned - it can take a while!



See also




Comments

Comments Filter
222

333VPN for VoIP Blocking

by jenniferhan, Wednesday 12 of December, 2007 [03:48:12 UTC]
Somebody use VPN to solve the VoIP Blocking issue. But it seems not a good way to solve the voip blocking issue. Because VPN will take more bandwidth and will take effection on the Voice Quality

Currently I am using the VGCP, a new solution to solve the VoIP Blocking issue. Following is theirs website:
http://www.speed-voip.com/index-36.html

If any of you have interested, you may try to use it to solve your VoIP Blocking problems. Thanks.

Andy
andywong-01@hotmail.com

222

333IPTables rule to make iaxcomm to work

by antonjan, Saturday 28 of January, 2006 [15:46:18 UTC]
the rule "-A RH-Firewall-1-INPUT -p udp -m udp --dport 6001 -j ACCEPT" fixed my problem on the asterisk server connecting from iaxcomm client
222

333IPFW example -- Warning!

by , Wednesday 19 of January, 2005 [19:29:59 UTC]
For the IPFW (& natd) example, please note that the given rc.conf specification (for natd) will cause all traffic arriving at the "external" NIC to be NATted, then sent to the specific IP address ("pbxip") in question. In the event that you planned to use the connection for anything else, this may not be optimal. (And I have no clue what he stuff is with 4569/udp, 2727/udp, and 9999-20001/udp.) — dhw
222

333suggested changes for pf on free/openbsd

by , Sunday 02 of January, 2005 [09:34:19 UTC]
( damn wiki doesn't want to set cookies with my firefox for somereason ) ...

The example given shows what you would type in bash to create the ruleset. with pf, it's much easier to create a pf.conf file, and then enable pf in your rc.conf.

The same ruleset with native pf grammer is:

voip_tcp = "5060"
voip_udp = "{ 5060, 4569, 5036, 9999:20001, 2727 }"
pass in quick on $ext_if proto udp from any port $voip_udp to 64.81.53.18 keep state
pass in quick on $ext_if proto tcp from any port $voip_tcp to 64.81.53.18 flags S/SA keep state
222

333iptables typo

by , Friday 31 of December, 2004 [11:45:54 UTC]
The last line of the IPtables rules i think should be '-m udp' instead of '-m upp':

iptables -A INPUT -p udp -m udp --dport 2727 -j ACCEPT

(though i guess the whole '-m udp' bit could be left out completely as the udp match is loaded when -p udp is used):

iptables -A INPUT -p udp --dport 2727 -j ACCEPT

-kieren
222

333IPTables on Fedora

by , Thursday 21 of October, 2004 [06:44:11 UTC]
To get this to work with RH FC2 on IPTables make sure to include the rules as:

-A RH-Firewall-1-INPUT -p udp -m udp --dport 5060 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp --dport 4569 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp --dport 5036 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp --dport 10000:20000 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp --dport 5004 -j ACCEPT

then when your happy run:

service iptables save

Good luck and stay secure.
222

333Re: newer iptables

by kentec, Tuesday 14 of September, 2004 [17:23:57 UTC]
OK, I can completely strike the previous entry in this thread.
changing 'rtp.conf' to:
rtpstart=30000
rtpend=32000
as well as these entries in iptables:
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i eth0 -p udp -m udp --dport 5060 -j ACCEPT
-A INPUT -p udp -m udp --dport 30000:32000 -j ACCEPT
(making sure that 30000:32000 is outside of the ephemeral port range)
We've had 100% voice success rate since getting that figured out.
222

333newer iptables

by kentec, Monday 13 of September, 2004 [17:02:56 UTC]
connection tracking / related seems to work somewhat.
we don't allow the whole ephemeral range, rather have a rule:
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
also, curious we had to allow --sport 5004 udp from our subnets as
the related/established rule didn't seem to catch that. it gets IAX and
everything else so far, seemingly though.
222

333iaxComm requires ports above 60000

by , Thursday 02 of September, 2004 [21:29:27 UTC]
Hello all,

Using the above recipe, I still had problems connecting to asterisk with iaxComm. With the firewall off, I noticed that asterisk said that the client connected on port 62162. After opening udp on this port (in fact 60000-65000) on the firewall my problem disappeared. How come it uses this port? I haven't found it mentioned anywhere...

Kind regards and thanks for a great product,

Thomas :)