112 lines
4.3 KiB
Bash
112 lines
4.3 KiB
Bash
#!/bin/bash
|
|||
|
|
|
||
|
|
echo Flush existing rules
|
||
|
|
iptables -F
|
||
|
|
iptables -F -t nat
|
||
|
|
iptables -F -t mangle
|
||
|
|
iptables -F -t filter
|
||
|
|
echo Delete all
|
||
|
|
iptables -X
|
||
|
|
|
||
|
|
|
||
|
|
if [ "$1" == "-d" ]; then
|
||
|
|
echo Input -d open all traffic
|
||
|
|
iptables -P INPUT ACCEPT
|
||
|
|
iptables -P FORWARD ACCEPT
|
||
|
|
exit 0
|
||
|
|
else
|
||
|
|
echo "Standard Firewall, -d to open all"
|
||
|
|
fi
|
||
|
|
ifx=( $(/sbin/ifconfig -s -a | tail -n +2 | cut -f1 -d' ' | grep -v lo) )
|
||
|
|
for i in ${!ifx[@]}; do
|
||
|
|
ipx[$i]="$(/sbin/ifconfig ${ifx[$i]} | grep -m1 -o -P '(?<=inet ).*(?= netmask)')"
|
||
|
|
if [ "${ipx[$i]}" == "" ]; then
|
||
|
|
ipx[$i]="----------"
|
||
|
|
fi
|
||
|
|
printf "%s:\t%s\n" "${ifx[$i]}" "${ipx[$i]}"
|
||
|
|
done
|
||
|
|
|
||
|
|
echo Set Variables
|
||
|
|
HOST=$(hostname)
|
||
|
|
SSHPORT=$(cat /etc/ssh/sshd_config | grep Port | grep -v "#" | cut -f2 -d' ')
|
||
|
|
printf "Local IP: $LOCALIP\nInterface: $NETIF\n"
|
||
|
|
|
||
|
|
echo Make Rules
|
||
|
|
|
||
|
|
|
||
|
|
echo Allen verkehr blockieren
|
||
|
|
iptables -P INPUT DROP
|
||
|
|
iptables -P FORWARD DROP
|
||
|
|
iptables -P OUTPUT ACCEPT
|
||
|
|
|
||
|
|
for i in ${!ifx[@]}; do
|
||
|
|
LOCALIP="${ipx[$i]}"
|
||
|
|
NETIF="${ifx[$i]}"
|
||
|
|
echo "Established Connections for $NETIF"
|
||
|
|
iptables -A INPUT -i $NETIF -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
|
||
|
|
iptables -A OUTPUT -o $NETIF -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
|
||
|
|
echo "DHCP for $NETIF"
|
||
|
|
iptables -A OUTPUT -o $NETIF -p udp --dport 67 --sport 68 -m state --state NEW,ESTABLISHED -j ACCEPT
|
||
|
|
iptables -A INPUT -i $NETIF -p udp --sport 67 --dport 68 -m state --state RELATED,ESTABLISHED -j ACCEPT
|
||
|
|
echo "reject connections from predator IP going over $NETIF"
|
||
|
|
iptables -A OUTPUT ! --src $LOCALIP -o $NETIF -j REJECT
|
||
|
|
if [ "${HOST}" == "serverhost" ]; then
|
||
|
|
echo Incoming https
|
||
|
|
iptables -A INPUT -i $NETIF -p tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT
|
||
|
|
iptables -A OUTPUT -o $NETIF -p tcp --sport 443 -m state --state ESTABLISHED -j ACCEPT
|
||
|
|
fi
|
||
|
|
echo "Incoming SSH P3032 for $NETIF"
|
||
|
|
iptables -A INPUT -i $NETIF -p tcp -m state --state NEW -m tcp --dport $SSHPORT -j ACCEPT
|
||
|
|
iptables -A OUTPUT -o $NETIF -p tcp --sport $SSHPORT -m state --state ESTABLISHED -j ACCEPT
|
||
|
|
echo "Ping Reply for $NETIF"
|
||
|
|
iptables -A INPUT -i $NETIF -p icmp --icmp-type echo-request -j ACCEPT
|
||
|
|
iptables -A OUTPUT -o $NETIF -p icmp --icmp-type echo-reply -j ACCEPT
|
||
|
|
done
|
||
|
|
echo loopback
|
||
|
|
iptables -A INPUT -i lo -j ACCEPT
|
||
|
|
iptables -A OUTPUT -o lo -j ACCEPT
|
||
|
|
|
||
|
|
echo Logging
|
||
|
|
iptables -N LOGGING
|
||
|
|
iptables -A INPUT -j LOGGING
|
||
|
|
iptables -A LOGGING -m limit --limit 2/min -j LOG --log-prefix "IPTables Packet Dropped: " --log-level 7
|
||
|
|
iptables -A LOGGING -j DROP
|
||
|
|
|
||
|
|
#echo Outgoing https
|
||
|
|
#iptables -A OUTPUT -o $NETIF -p tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT
|
||
|
|
#iptables -A INPUT -i $NETIF -p tcp --sport 443 -m state --state ESTABLISHED -j ACCEPT
|
||
|
|
|
||
|
|
#echo torrents
|
||
|
|
#iptables -A OUTPUT -o $NETIF -p udp -m state --state NEW,ESTABLISHED -j ACCEPT
|
||
|
|
#iptables -A INPUT -i $NETIF -p tcp --sport 6890:6999 -m state --state NEW,ESTABLISHED -j ACCEPT
|
||
|
|
#iptables -A INPUT -i $NETIF -p udp --sport 6890:6999 -m state --state NEW,ESTABLISHED -j ACCEPT
|
||
|
|
|
||
|
|
#echo Outgoing SSH p1160
|
||
|
|
#iptables -A INPUT -i $NETIF -p tcp --sport 1160 -m state --state ESTABLISHED -j ACCEPT
|
||
|
|
#iptables -A OUTPUT -o $NETIF -p tcp --dport 1160 -m state --state NEW,ESTABLISHED -j ACCEPT
|
||
|
|
|
||
|
|
#echo Ping Request
|
||
|
|
#iptables -A OUTPUT -o $NETIF -p icmp --icmp-type echo-request -j ACCEPT
|
||
|
|
#iptables -A INPUT -i $NETIF -p icmp --icmp-type echo-reply -j ACCEPT
|
||
|
|
|
||
|
|
#echo Incoming SSH p22
|
||
|
|
#iptables -A INPUT -i $NETIF -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
|
||
|
|
#iptables -A OUTPUT -o $NETIF -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
|
||
|
|
|
||
|
|
#echo Outgoing SSH p22
|
||
|
|
#iptables -A INPUT -i $NETIF -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
|
||
|
|
#iptables -A OUTPUT -o $NETIF -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
|
||
|
|
|
||
|
|
#echo outgouing http
|
||
|
|
#iptables -A OUTPUT -o $NETIF -p tcp --dport 80 -m state --state NEW -j ACCEPT
|
||
|
|
#iptables -A INPUT -i $NETIF -p tcp --sport 80 -m state --state RELATED,ESTABLISHED -j ACCEPT
|
||
|
|
|
||
|
|
#echo incoming http
|
||
|
|
#iptables -A INPUT -i $NETIF -p tcp --dport 80 -m state --state RELATED,ESTABLISHED -j DROP
|
||
|
|
#iptables -A OUTPUT -o $NETIF -p tcp --sport 80 -m state --state NEW -j DROP
|
||
|
|
|
||
|
|
#echo DNS
|
||
|
|
#iptables -A OUTPUT -o $NETIF -p tcp --dport 53 -m state --state NEW -j ACCEPT
|
||
|
|
#iptables -A OUTPUT -o $NETIF -p udp --dport 53 -m state --state NEW -j ACCEPT
|
||
|
|
#iptables -A INPUT -i $NETIF -p tcp --sport 53 -m state --state NEW -j ACCEPT
|
||
|
|
#iptables -A INPUT -i $NETIF -p udp --sport 53 -m state --state NEW -j ACCEPT
|