Voor de configuratie heb ik gebruik gemaakt van WebMin. In WebMin kun je in het menu Networking->Linux Firewall een “Reset Firewall” doen.

Daarna kun je kiezen voor: “Block all except SSH, IDENT, ping and high ports on interface” en klik op “Setup Firewall”.

Nu hebben we een mooie template. Hieronder staat de gegenereerde configuratie, de vet gemarkeerde gedeelten heb ik zelf toegevoegd voor “Force Attack prevention”. Ook heb ik extra poorten opengezet. Verwijder van de vette regels wat je niet gebruikt.

$ vi /etc/iptables.up.rules

# Generated by iptables-save v1.4.1.1 on Tue Sep 29 20:11:09 2009  
*nat  
:PREROUTING ACCEPT [0:0]  
:POSTROUTING ACCEPT [0:0]  
:OUTPUT ACCEPT [0:0]  
COMMIT  

# Completed on Tue Sep 29 20:11:09 2009  
# Generated by iptables-save v1.4.1.1 on Tue Sep 29 20:11:09 2009  
*mangle  
:PREROUTING ACCEPT [0:0]  
:INPUT ACCEPT [0:0]  
:FORWARD ACCEPT [0:0]  
:OUTPUT ACCEPT [0:0]  
:POSTROUTING ACCEPT [0:0]  
COMMIT  

# Completed on Tue Sep 29 20:11:09 2009  
# Generated by iptables-save v1.4.1.1 on Tue Sep 29 20:11:09 2009  
*filter  
:FORWARD ACCEPT [0:0]  
:INPUT DROP [0:0]  
:OUTPUT ACCEPT [0:0]  

# Accept traffic from internal interfaces  
-A INPUT ! -i eth0 -j ACCEPT  

# Accept traffic with the ACK flag set  
-A INPUT -p tcp -m tcp --tcp-flags ACK ACK -j ACCEPT  

# Allow incoming data that is part of a connection we established  
-A INPUT -m state --state ESTABLISHED -j ACCEPT  

# Allow data that is related to existing connections  
-A INPUT -m state --state RELATED -j ACCEPT  

# Accept responses to DNS queries  
-A INPUT -p udp -m udp --dport 1024:65535 --sport 53 -j ACCEPT  

# Accept responses to our pings  
-A INPUT -p icmp -m icmp --icmp-type echo-reply -j ACCEPT  

# Accept notifications of unreachable hosts  
-A INPUT -p icmp -m icmp --icmp-type destination-unreachable -j ACCEPT  

# Accept notifications to reduce sending speed  
-A INPUT -p icmp -m icmp --icmp-type source-quench -j ACCEPT  

# Accept notifications of lost packets  
-A INPUT -p icmp -m icmp --icmp-type time-exceeded -j ACCEPT  

# Accept notifications of protocol problems  
-A INPUT -p icmp -m icmp --icmp-type parameter-problem -j ACCEPT  

# SSH Dictionary attack prevention
-N SSH_CHECK  
-A INPUT -p tcp --dport 22 -m state --state NEW -j SSH_CHECK  
-A SSH_CHECK -m recent --set --name SSH  
-A SSH_CHECK -m recent --update --seconds 300 --hitcount 5 --name SSH -j DROP  

# Allow connections to our SSH server  
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT  

# FTP Dictionary attack prevention  
-N FTP_CHECK  
-A INPUT -p tcp --dport 20:21 -m state --state NEW -j FTP_CHECK  
-A FTP_CHECK -m recent --set --name FTP  
-A FTP_CHECK -m recent --update --seconds 300 --hitcount 5 --name FTP -j DROP  

# FTP  
-A INPUT -p tcp -m tcp --dport 20:21 -j ACCEPT  

# HTTP  
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT  

# HTTPS  
-A INPUT -p tcp -m tcp --dport 443 -j ACCEPT  

# Rsync  
-A INPUT -p tcp -m tcp -s 192.168.1.0/24 --dport 873 -j ACCEPT

# Allow connections to our IDENT server  
-A INPUT -p tcp -m tcp --dport auth -j ACCEPT  

# Respond to pings  
-A INPUT -p icmp -m icmp --icmp-type echo-request -j ACCEPT  

# Protect our NFS server  
-A INPUT -p tcp -m tcp --dport 2049:2050 -j DROP  

# Protect our X11 display server  
-A INPUT -p tcp -m tcp --dport 6000:6063 -j DROP  

# Protect our X font server  
-A INPUT -p tcp -m tcp --dport 7000:7010 -j DROP  

# Allow connections to unprivileged ports  
-A INPUT -p tcp -m tcp --dport 1024:65535 -j ACCEPT  

COMMIT  
# Completed on Tue Sep 29 20:11:09 2009  

Samba connecties vanaf het interne netwerk

Eventueel kun je de volgende regels toevoegen voor machines vanaf het interne netwerk om bij de samba service te komen.

Voeg het volgende aan het einde van de iptables.up.rules file, vlak voor de COMMIT

# Allow samba from internal network  
-A INPUT -p tcp -m tcp -s 192.168.1.0/24 --dport 135 -j ACCEPT  
-A INPUT -p udp -m udp -s 192.168.1.0/24 --dport 137 -j ACCEPT  
-A INPUT -p udp -m udp -s 192.168.1.0/24 --dport 138 -j ACCEPT  
-A INPUT -m state --state NEW -m tcp -p tcp -s 192.168.1.0/24 --dport 139 -j ACCEPT  
-A INPUT -m state --state NEW -m tcp -p tcp -s 192.168.1.0/24 --dport 445 -j ACCEPT

Automatisch starten van IPTables (zonder Webmin)

In het bovenstaande voorbeeld wordt IPTables gestart door Webmin. Als je Webmin niet (meer) hebt geinstalleerd, dan kun je de firewall starten door in /etc/network/interfaces onderaan de volgende regel toe te voegen:

$ vi /etc/network/interfaces
...  
pre-up iptables-restore < /etc/iptables.up.rules

Stoppen en handmatig starten van de firewall

Het volgende script is het commando iptables-stop om de firewall te disablen.

Deze kan als volgt gemaakt worden:

$ cd /usr/local/sbin
$ vi iptables-stop
echo -n "Disabling firewall ...."  
iptables -F  
iptables -X  
iptables -t nat -F  
iptables -t nat -X  
iptables -t mangle -F  
iptables -t mangle -X  
iptables -P INPUT ACCEPT  
iptables -P FORWARD ACCEPT  
iptables -P OUTPUT ACCEPT  
echo " ready, all ports are unprotected now"

Hetzelfde voor het start script:

$ vi iptables-start
echo "Enabling firewall"  
iptables-restore < /etc/iptables.up.rules

En maak de scripts uitvoerbaar:

$ chmod x iptables-stop
$ chmod x iptables-start

Nu hebben we het commando iptables-start en iptables-stop gemaakt.

IPTables op de commandline

In WebMin kan eenvoudigweg op de “Apply Configuration” gedrukt worden, maar de wijzigingen kunnen later ook op de commandline bekeken/geladen worden, met de volgende commando’s: iptables-save en iptables-restore.

Om de IPTables te bekijken gebruik je:

$ iptables-save

Om de IPTables in een file op te slaan:

$ iptables-save > /etc/iptables.up.rules

Om de opgeslagen /etc/iptables.conf terug te schrijven:

$ iptables-restore < /etc/iptables.up.rules