graham.myers
29-03-2008, 8:21 AM
After spending some time locking down my linux firewall, I thought I'd share my iptables list. Its probably not complete and probably has a few errors in it, so feedback welcome.
I basically allow anything on my network to use the services, but only the services I specify. I only allow my work's IP address access via SSH and everything else (bit torrent client, vnc etc) tunnel across ssh. This means the connection from my work desktop to my home server is across a encrypted line. VNC normally does not encrypt the connection. I use puTTY on my work's desktop to set up the ssh link. Another guy at work uses Hamachi, but I couldn't get that working properly.
I have the bittorrent port and port 22 for SSH open on the router/NAT and they are port forwarded to the linux machine.
I have a file which is commented so I know why I did things and which ports relate to which service.
I then use
# iptables-restore < iptables.base
This loads in the changes
cat iptables.base
*filter
:INPUT ACCEPT [19422:4819133]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [10064:2038008]
# Only the following sources are allowed to connect to the server
#-s 213.xxx.xxx.xxx # Work's External IP
#-s 192.168.1.0/255.255.255.0 # internal network
#
# Accept local connect traffic through 127.0.0.1
-A INPUT -i lo -j ACCEPT
# Allow established and related connections through
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
# Brute force prevention - prevents more than two SSH connections per minute to slow down SSH scans
-A INPUT -p tcp --dport 22 -m recent --update --hitcount 2 --seconds 60 --name SSHIN -j REJECT
-A INPUT -p tcp --dport 22 -m recent --set --name SSHIN -j ACCEPT
# Allow ftp access from local hosts only
#-A INPUT -s 192.168.1.0/255.255.255.0 -p tcp -m multiport --dports 20,21 -j ACCEPT
#-A OUTPUT -s 192.168.1.0/255.255.255.0 -p tcp -m multiport --dports 20,21 -j ACCEPT
# Allow samba access for the allowed hosts
-A INPUT -s 192.168.1.0/255.255.255.0 -p udp -m multiport --dports 137,138 -j ACCEPT
-A INPUT -s 192.168.1.0/255.255.255.0 -p tcp -m multiport --dports 139,145 -j ACCEPT
# Allow ssh from allowed hosts only
-A INPUT -s 213.xxx.xxx.xxx -p tcp --dport 22 -j ACCEPT
-A INPUT -s 192.168.1.0/255.255.255.0 -p tcp --dport 22 -j ACCEPT
# Allow itunes (bonjour) for local traffic only
-A INPUT -s 192.168.1.0/255.255.255.0 -p tcp --dport 3689 -j ACCEPT
-A OUTPUT -s 192.168.1.0/255.255.255.0 -p tcp --dport 3689 -j ACCEPT
# Allow twonky for local traffic only
-A INPUT -s 192.168.1.0/255.255.255.0 -p tcp --dport 9000 -j ACCEPT
-A INPUT -s 192.168.1.0/255.255.255.0 -p udp -m multiport --dports 1030,1900,9080 -j ACCEPT
-A OUTPUT -s 192.168.1.0/255.255.255.0 -p tcp --dport 9000 -j ACCEPT
-A OUTPUT -s 192.168.1.0/255.255.255.0 -p udp -m multiport --dports 1030,1900,9080 -j ACCEPT
# Allow VNC from selected hosts
-A INPUT -s 5.0.0.0/255.0.0.0 -p tcp -m multiport --dports 5900,5901 -i ham0 -j ACCEPT
-A INPUT -s 192.168.1.0/255.255.255.0 -p tcp -m multiport --dports 5900,5901 -j ACCEPT
# Allow DNS lookups out
-A OUTPUT -p udp --dport 53 -j ACCEPT
# Allow web browsing from this server
-A INPUT -p tcp -m multiport --dports 80,443 -j ACCEPT
# Allow MLdonkey connections from a GUI from allowed hosts only
-A INPUT -s 192.168.1.0/255.255.255.0 -p tcp --dport 4001 -j ACCEPT
# for now dont allow the http connection on 4080
# open the bitTorent incoming port
-A INPUT -p tcp -m multiport --dports 6881,6882 -j ACCEPT
# Allow outbound SMTP
-A OUTPUT -p tcp --dport 25 -j ACCEPT
# Allow outbound NTP
-A OUTPUT -p tcp --dport 123 -j ACCEPT
#if we get here reject the packet
-A INPUT -j REJECT
COMMIT
I basically allow anything on my network to use the services, but only the services I specify. I only allow my work's IP address access via SSH and everything else (bit torrent client, vnc etc) tunnel across ssh. This means the connection from my work desktop to my home server is across a encrypted line. VNC normally does not encrypt the connection. I use puTTY on my work's desktop to set up the ssh link. Another guy at work uses Hamachi, but I couldn't get that working properly.
I have the bittorrent port and port 22 for SSH open on the router/NAT and they are port forwarded to the linux machine.
I have a file which is commented so I know why I did things and which ports relate to which service.
I then use
# iptables-restore < iptables.base
This loads in the changes
cat iptables.base
*filter
:INPUT ACCEPT [19422:4819133]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [10064:2038008]
# Only the following sources are allowed to connect to the server
#-s 213.xxx.xxx.xxx # Work's External IP
#-s 192.168.1.0/255.255.255.0 # internal network
#
# Accept local connect traffic through 127.0.0.1
-A INPUT -i lo -j ACCEPT
# Allow established and related connections through
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
# Brute force prevention - prevents more than two SSH connections per minute to slow down SSH scans
-A INPUT -p tcp --dport 22 -m recent --update --hitcount 2 --seconds 60 --name SSHIN -j REJECT
-A INPUT -p tcp --dport 22 -m recent --set --name SSHIN -j ACCEPT
# Allow ftp access from local hosts only
#-A INPUT -s 192.168.1.0/255.255.255.0 -p tcp -m multiport --dports 20,21 -j ACCEPT
#-A OUTPUT -s 192.168.1.0/255.255.255.0 -p tcp -m multiport --dports 20,21 -j ACCEPT
# Allow samba access for the allowed hosts
-A INPUT -s 192.168.1.0/255.255.255.0 -p udp -m multiport --dports 137,138 -j ACCEPT
-A INPUT -s 192.168.1.0/255.255.255.0 -p tcp -m multiport --dports 139,145 -j ACCEPT
# Allow ssh from allowed hosts only
-A INPUT -s 213.xxx.xxx.xxx -p tcp --dport 22 -j ACCEPT
-A INPUT -s 192.168.1.0/255.255.255.0 -p tcp --dport 22 -j ACCEPT
# Allow itunes (bonjour) for local traffic only
-A INPUT -s 192.168.1.0/255.255.255.0 -p tcp --dport 3689 -j ACCEPT
-A OUTPUT -s 192.168.1.0/255.255.255.0 -p tcp --dport 3689 -j ACCEPT
# Allow twonky for local traffic only
-A INPUT -s 192.168.1.0/255.255.255.0 -p tcp --dport 9000 -j ACCEPT
-A INPUT -s 192.168.1.0/255.255.255.0 -p udp -m multiport --dports 1030,1900,9080 -j ACCEPT
-A OUTPUT -s 192.168.1.0/255.255.255.0 -p tcp --dport 9000 -j ACCEPT
-A OUTPUT -s 192.168.1.0/255.255.255.0 -p udp -m multiport --dports 1030,1900,9080 -j ACCEPT
# Allow VNC from selected hosts
-A INPUT -s 5.0.0.0/255.0.0.0 -p tcp -m multiport --dports 5900,5901 -i ham0 -j ACCEPT
-A INPUT -s 192.168.1.0/255.255.255.0 -p tcp -m multiport --dports 5900,5901 -j ACCEPT
# Allow DNS lookups out
-A OUTPUT -p udp --dport 53 -j ACCEPT
# Allow web browsing from this server
-A INPUT -p tcp -m multiport --dports 80,443 -j ACCEPT
# Allow MLdonkey connections from a GUI from allowed hosts only
-A INPUT -s 192.168.1.0/255.255.255.0 -p tcp --dport 4001 -j ACCEPT
# for now dont allow the http connection on 4080
# open the bitTorent incoming port
-A INPUT -p tcp -m multiport --dports 6881,6882 -j ACCEPT
# Allow outbound SMTP
-A OUTPUT -p tcp --dport 25 -j ACCEPT
# Allow outbound NTP
-A OUTPUT -p tcp --dport 123 -j ACCEPT
#if we get here reject the packet
-A INPUT -j REJECT
COMMIT