Server Firewall mit Iptables (presistent)

Aktuelle Rules löschen

Um die aktuellen Regeln zu löschen gibt es hier ein ganz einfaches script.

#!/bin/sh
echo "Stopping firewall and allowing everyone..."
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
Das Script muss nur einmal ausgeführt werden und schon sind die jetzigen Regeln erstmal entfernt.

Neue Rules hinzufügen

Wenn wir die alten Rules gelöscht haben, können wir neue Regeln hinzufügen.
Hier mal die Regeln für ein Tomcat Server, die Regeln können beliebig angepasst werden.
#!/bin/sh
#Drop all Input Connections
iptables -P INPUT DROP
iptables -P FORWARD DROP
#Allow Local Traffic
iptables -v -A INPUT -i lo -j ACCEPT;
#Allow All Incoming SSH
iptables -A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
#Allow Incomming HTTP
iptables -A INPUT -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT
#Allow Incomming HTTPS
iptables -A INPUT -i eth0 -p tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 443 -m state --state ESTABLISHED -j ACCEPT
#Allow Incomming ICMP (Ping from Outside -> Inside)
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT
#Allow DNS
iptables -I INPUT -p udp --sport 53 -m state --state ESTABLISHED -j ACCEPT
ip6tables -I INPUT -p udp --sport 53 -m state --state ESTABLISHED -j ACCEPT
#Allow All Incomming MOSH Connections(MobileSSH)
iptables -A INPUT -i eth0 -p udp --dport 6001 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p udp --sport 6001 -m state --state ESTABLISHED -j ACCEPT
##################
#     IPv6       #
##################
#Drop all Incomming Connections
ip6tables -P INPUT DROP
ip6tables -P FORWARD DROP
#Allow All Outgoing Connections
ip6tables -P OUTPUT ACCEPT
#Allow All Incoming SSH
ip6tables -A INPUT  -i eth0 -p tcp -m tcp --dport 22 -j ACCEPT
#Allow Incomming HTTP
ip6tables -A INPUT -i etho -p tcp -m tcp --dport 80 -j ACCEPT
#Allow Incomming HTTPS
ip6tables -A INPUT -i eth0 -p tcp -m tcp --dport 443 -j ACCEPT
#Allow Incomming ICMP
ip6tables  -A INPUT  -p ipv6-icmp -j ACCEPT
ip6tables  -A OUTPUT -p ipv6-icmp -j ACCEPT

Regeln beim Startup laden mit persistent

Voraussetzung ist hier iptables-persistent, das kann ganz einfach nachinstalliert werden.
sudo apt-get install iptables-persistent

Wenn wir Rules eingerichtet haben können wir diese ganz einfach mit einem Befehl speichern, und sie werden beim nächsten Startup geladen.
iptables-save > /etc/iptables/rules.v4

ip6tables-save > /etc/iptables/rules.v6

Previous
Next Post »
0 Komentar