iptables:firewall_script
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
iptables:firewall_script [2016/11/22 16:47] – created peter | iptables:firewall_script [2019/11/29 17:33] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== IPTables - Firewall Script ====== | ||
- | ===== Turning on native Kernel IPv4 protection ===== | ||
- | |||
- | The Linux kernel provides some basic protections against manipulated IP packets. A configuration could be: | ||
- | |||
- | <code bash> | ||
- | echo 1 > / | ||
- | echo 0 > / | ||
- | echo 1 > / | ||
- | echo 1 > / | ||
- | echo 1 > / | ||
- | echo 1 > / | ||
- | echo 0 > / | ||
- | echo 0 > / | ||
- | </ | ||
- | |||
- | Further reading: | ||
- | |||
- | * The file Documentation/ | ||
- | * [[http:// | ||
- | |||
- | |||
- | ===== Firewall ===== | ||
- | |||
- | |||
- | <file bash firewall.sh> | ||
- | #!/bin/sh | ||
- | # set -e | ||
- | |||
- | ### BEGIN INIT INFO | ||
- | # Provides: | ||
- | # Required-Start: | ||
- | # Required-Stop: | ||
- | # Default-Start: | ||
- | # Default-Stop: | ||
- | # Short-Description: | ||
- | # Description: | ||
- | ### END INIT INFO | ||
- | |||
- | # Version: | ||
- | # | ||
- | # | ||
- | |||
- | PATH=/ | ||
- | NAME=firewall | ||
- | |||
- | |||
- | |||
- | iptables4=/ | ||
- | iptables6=/ | ||
- | pub_if=eth0 | ||
- | pub_ipv4=70.85.16.194 | ||
- | pub_ipv6=2600: | ||
- | |||
- | |||
- | test -x $iptables4 || exit 0 | ||
- | test -x $iptables6 || exit 0 | ||
- | |||
- | firewall_flush() | ||
- | { | ||
- | # flush rules | ||
- | $iptables4 -F | ||
- | $iptables6 -F | ||
- | $iptables4 -F -t mangle | ||
- | $iptables6 -F -t mangle | ||
- | $iptables4 -X -t mangle | ||
- | $iptables6 -X -t mangle | ||
- | $iptables4 -F -t nat | ||
- | $iptables4 -X -t nat | ||
- | $iptables4 -X | ||
- | $iptables6 -X | ||
- | } | ||
- | |||
- | |||
- | firewall_default() | ||
- | { | ||
- | default=$1 | ||
- | |||
- | # apply default rules | ||
- | $iptables4 -P INPUT | ||
- | $iptables6 -P INPUT | ||
- | $iptables4 -P FORWARD $default | ||
- | $iptables6 -P FORWARD $default | ||
- | $iptables4 -P OUTPUT | ||
- | $iptables6 -P OUTPUT | ||
- | } | ||
- | |||
- | |||
- | firewall_start() | ||
- | { | ||
- | # default policy | ||
- | firewall_default DROP | ||
- | |||
- | # accept everything from loopback | ||
- | $iptables4 -A INPUT -i lo -j ACCEPT | ||
- | $iptables6 -A INPUT -i lo -j ACCEPT | ||
- | $iptables4 -A OUTPUT -o lo -j ACCEPT | ||
- | $iptables6 -A OUTPUT -o lo -j ACCEPT | ||
- | |||
- | # Allow Link-Local addresses | ||
- | $iptables6 -A INPUT -s fe80::/10 -j ACCEPT | ||
- | $iptables6 -A OUTPUT -s fe80::/10 -j ACCEPT | ||
- | |||
- | # drop Bad Guys | ||
- | $iptables4 -A INPUT -m recent --update --seconds 60 -j DROP | ||
- | $iptables6 -A INPUT -m recent --update --seconds 60 -j DROP | ||
- | |||
- | # Filter all packets that have RH0 headers: | ||
- | $iptables6 -A INPUT -m rt --rt-type 0 -j DROP | ||
- | $iptables6 -A FORWARD -m rt --rt-type 0 -j DROP | ||
- | $iptables6 -A OUTPUT -m rt --rt-type 0 -j DROP | ||
- | |||
- | # drop spoofed packets (i.e. packets with local source addresses coming from outside etc.), mark as Bad Guy | ||
- | $iptables4 -A INPUT -i $pub_if -s $pub_ipv4 -m recent --set -j DROP | ||
- | $iptables6 -A INPUT -i $pub_if -s $pub_ipv6 -m recent --set -j DROP | ||
- | |||
- | # accept ICMP packets (ping et.al.) | ||
- | $iptables4 -A INPUT -p icmp -j ACCEPT | ||
- | $iptables6 -A INPUT -p icmpv6 -j ACCEPT | ||
- | $iptables6 -A OUTPUT -p icmpv6 -j ACCEPT | ||
- | $iptables6 -A FORWARD -p icmpv6 -j ACCEPT | ||
- | |||
- | # internet (established and out) | ||
- | $iptables4 -A OUTPUT -o $pub_if -j ACCEPT | ||
- | $iptables6 -A OUTPUT -o $pub_if -j ACCEPT | ||
- | $iptables4 -A INPUT -i $pub_if -m state --state ESTABLISHED, | ||
- | $iptables6 -A INPUT -i $pub_if -m state --state ESTABLISHED, | ||
- | |||
- | # allow public services | ||
- | $iptables4 -A INPUT -i $pub_if -p tcp -d $pub_ipv4 -m multiport --dports 25, | ||
- | $iptables6 -A INPUT -i $pub_if -p tcp -d $pub_ipv6 -m multiport --dports 25, | ||
- | |||
- | # accept ssh connections (max 2/minute from the same IP address) | ||
- | $iptables4 -A INPUT -p tcp --dport 22 -m recent --update --seconds 300 --hitcount 10 --name SSH -j DROP | ||
- | $iptables6 -A INPUT -p tcp --dport 22 -m recent --update --seconds 300 --hitcount 10 --name SSH -j DROP | ||
- | $iptables4 -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --set --name SSH -j ACCEPT | ||
- | $iptables6 -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --set --name SSH -j ACCEPT | ||
- | |||
- | # reject everything else in INPUT | ||
- | $iptables4 -A INPUT -j REJECT | ||
- | $iptables6 -A INPUT -j REJECT | ||
- | } | ||
- | |||
- | firewall_stop() | ||
- | { | ||
- | # flush rules | ||
- | firewall_flush | ||
- | |||
- | # default policy | ||
- | firewall_default ACCEPT | ||
- | } | ||
- | |||
- | case " | ||
- | start) | ||
- | echo -n " | ||
- | firewall_start | ||
- | echo " | ||
- | ;; | ||
- | stop) | ||
- | echo -n " | ||
- | firewall_stop | ||
- | echo " | ||
- | ;; | ||
- | restart|reload) | ||
- | echo -n " | ||
- | firewall_stop | ||
- | sleep 1 | ||
- | firewall_start | ||
- | echo " | ||
- | ;; | ||
- | *) | ||
- | echo " | ||
- | exit 1 | ||
- | ;; | ||
- | esac | ||
- | |||
- | exit 0 | ||
- | </ |
iptables/firewall_script.1479833265.txt.gz · Last modified: 2020/07/15 09:30 (external edit)