Steve1961
June 1st, 2009, 10:15 AM
Just looking for any feedback about the simple script I've created below. I've not delved into iptables before but I want to start using the witopia openvpn service and ufw, firestarter, etc didn't seem to be flexible enough.
Basically I want to allow all outgoing traffic, allow all access from my home network (they're all my machines anyway), and allow a few services - ssh, bittorrent, from anywhere. I'm not sure that I need the specific cups and samba entries but hey...
Also, i believe there was a problem with network-manager when running iptables. Is this still the case?
Anyway, heres the script. Any feedback appreciated.
#!/bin/bash
################################################## ##########
# * Blocks all incoming connections, except those opened by
# me, or related to already open connections
# * Blocks all forward requests
# * Allows all outgoing connections
# * Allows all incoming traffic from 192.168.1.0/24 network
# * Allows printer sharing from home network
# * Allow samba from home network
# * Allows incoming traffic on ssh port 45500/tcp and 22/tcp
# * Allows incoming traffic on bittorrent 59400/tcp/udp
# * Allows incoming traffic openvpn 1194/udp
# * Allows all traffic on tun0 interface
###
################################################## ##########
#load some modules you may need
modprobe ip_tables
modprobe ip_nat_ftp
modprobe ip_nat_irc
modprobe iptable_filter
modprobe iptable_nat
modprobe ip_conntrack_irc
modprobe ip_conntrack_ftp
# Clearing all previous rules
iptables -F
iptables -X
# Setting Default Policies
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
# Allowing already-established and related-incoming connections
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
# Allow all traffic on localhost
iptables -A INPUT -i lo -j ACCEPT
# Allow home network - probably not necessary with other entries
iptables -A INPUT -s 192.168.1.0/24 -d 192.168.1.0/24 -j ACCEPT
iptables -A INPUT -s 192.168.1.0/24 -d 0.0.0.0/0 -j ACCEPT
iptables -A INPUT -s 0.0.0.0/0 -d 192.168.1.0/24 -j ACCEPT
# Samba access from home network only
iptables -A INPUT -p tcp -s 192.168.1.0/24 --dport 137 -j ACCEPT
iptables -A INPUT -p udp -s 192.168.1.0/24 --dport 137 -j ACCEPT
iptables -A INPUT -p tcp -s 192.168.1.0/24 --dport 138 -j ACCEPT
iptables -A INPUT -p udp -s 192.168.1.0/24 --dport 138 -j ACCEPT
iptables -A INPUT -p tcp -s 192.168.1.0/24 --dport 139 -j ACCEPT
iptables -A INPUT -p udp -s 192.168.1.0/24 --dport 139 -j ACCEPT
iptables -A INPUT -p tcp -s 192.168.1.0/24 --dport 445 -j ACCEPT
iptables -A INPUT -p udp -s 192.168.1.0/24 --dport 445 -j ACCEPT
# Cups IPP access from home network only
iptables -A INPUT -j ACCEPT -p tcp -s 192.168.1.0/24 --dport 631
iptables -A INPUT -j ACCEPT -p udp -s 192.168.1.0/24 --dport 631
# Allow SSH access on port 45500/tcp
iptables -A INPUT -p tcp --dport 45500 -j ACCEPT
# Keep port 22 open anyway
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# Bittorrent port
iptables -A INPUT -p tcp --dport 59400 -j ACCEPT
iptables -A INPUT -p udp --dport 59400 -j ACCEPT
# Openvpn port
iptables -A INPUT -p udp --dport 1194 -j ACCEPT
# Allow unrestricted traffic on tun0
iptables -A INPUT -i tun0 -j ACCEPT
iptables -A OUTPUT -o tun0 -j ACCEPT
iptables -A FORWARD -o tun0 -j ACCEPT
Basically I want to allow all outgoing traffic, allow all access from my home network (they're all my machines anyway), and allow a few services - ssh, bittorrent, from anywhere. I'm not sure that I need the specific cups and samba entries but hey...
Also, i believe there was a problem with network-manager when running iptables. Is this still the case?
Anyway, heres the script. Any feedback appreciated.
#!/bin/bash
################################################## ##########
# * Blocks all incoming connections, except those opened by
# me, or related to already open connections
# * Blocks all forward requests
# * Allows all outgoing connections
# * Allows all incoming traffic from 192.168.1.0/24 network
# * Allows printer sharing from home network
# * Allow samba from home network
# * Allows incoming traffic on ssh port 45500/tcp and 22/tcp
# * Allows incoming traffic on bittorrent 59400/tcp/udp
# * Allows incoming traffic openvpn 1194/udp
# * Allows all traffic on tun0 interface
###
################################################## ##########
#load some modules you may need
modprobe ip_tables
modprobe ip_nat_ftp
modprobe ip_nat_irc
modprobe iptable_filter
modprobe iptable_nat
modprobe ip_conntrack_irc
modprobe ip_conntrack_ftp
# Clearing all previous rules
iptables -F
iptables -X
# Setting Default Policies
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
# Allowing already-established and related-incoming connections
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
# Allow all traffic on localhost
iptables -A INPUT -i lo -j ACCEPT
# Allow home network - probably not necessary with other entries
iptables -A INPUT -s 192.168.1.0/24 -d 192.168.1.0/24 -j ACCEPT
iptables -A INPUT -s 192.168.1.0/24 -d 0.0.0.0/0 -j ACCEPT
iptables -A INPUT -s 0.0.0.0/0 -d 192.168.1.0/24 -j ACCEPT
# Samba access from home network only
iptables -A INPUT -p tcp -s 192.168.1.0/24 --dport 137 -j ACCEPT
iptables -A INPUT -p udp -s 192.168.1.0/24 --dport 137 -j ACCEPT
iptables -A INPUT -p tcp -s 192.168.1.0/24 --dport 138 -j ACCEPT
iptables -A INPUT -p udp -s 192.168.1.0/24 --dport 138 -j ACCEPT
iptables -A INPUT -p tcp -s 192.168.1.0/24 --dport 139 -j ACCEPT
iptables -A INPUT -p udp -s 192.168.1.0/24 --dport 139 -j ACCEPT
iptables -A INPUT -p tcp -s 192.168.1.0/24 --dport 445 -j ACCEPT
iptables -A INPUT -p udp -s 192.168.1.0/24 --dport 445 -j ACCEPT
# Cups IPP access from home network only
iptables -A INPUT -j ACCEPT -p tcp -s 192.168.1.0/24 --dport 631
iptables -A INPUT -j ACCEPT -p udp -s 192.168.1.0/24 --dport 631
# Allow SSH access on port 45500/tcp
iptables -A INPUT -p tcp --dport 45500 -j ACCEPT
# Keep port 22 open anyway
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# Bittorrent port
iptables -A INPUT -p tcp --dport 59400 -j ACCEPT
iptables -A INPUT -p udp --dport 59400 -j ACCEPT
# Openvpn port
iptables -A INPUT -p udp --dport 1194 -j ACCEPT
# Allow unrestricted traffic on tun0
iptables -A INPUT -i tun0 -j ACCEPT
iptables -A OUTPUT -o tun0 -j ACCEPT
iptables -A FORWARD -o tun0 -j ACCEPT