Last active
December 1, 2016 17:01
-
-
Save Juma7C9/6720641 to your computer and use it in GitHub Desktop.
Script for loading and reloading iptables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Script to configure Linux' iptables and ip6tables | |
# setting the specified ports open and closing the rest. | |
# Optionally enables Fail2Ban on IPv4 (as the latest | |
# stables version [0.9.5] does not support it yet). | |
# The MIT License (MIT) | |
# Copyright (c) 2013-2016 Juma7C9 | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software | |
# and associated documentation files (the "Software"), to deal in the Software without restriction, | |
# including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
# and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, | |
# subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included | |
# in all copiesor substantial portions of the Software. | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | |
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE | |
# FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
IPv6_Enabled="True" | |
SSH_on_IPv4="True" | |
Fail2Ban_Enabled="True" | |
tcpPorts=( ) | |
udpPorts=( ) | |
bannedIPs=( ) | |
if [ "${IPv6_Enabled}" == "True" ] | |
then | |
tables=( iptables ip6tables ) | |
else | |
tables=( iptables ) | |
fi | |
# Stop Fail2Ban | |
service fail2ban stop | |
for iptables in "${tables[@]}" | |
do | |
"${iptables}" -P INPUT ACCEPT | |
"${iptables}" -F | |
"${iptables}" -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT | |
"${iptables}" -A INPUT -i lo -j ACCEPT | |
for IP in "${bannedIPs[@]}" | |
do | |
"${iptables}" -A INPUT -s $IP -j DROP | |
done | |
for i in "${tcpPorts[@]}" | |
do | |
"${iptables}" -A INPUT -p tcp --dport $i -j ACCEPT | |
done | |
for k in "${udpPorts[@]}" | |
do | |
"${iptables}" -A INPUT -p udp --dport $k -j ACCEPT | |
done | |
"${iptables}" -P INPUT DROP | |
"${iptables}" -P FORWARD DROP | |
"${iptables}" -P OUTPUT ACCEPT | |
done | |
# Configure ICMP rules separately for v4 and v6 | |
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT | |
if [ "${IPv6_Enabled}" == "True" ] | |
then | |
ip6tables -A INPUT -p ipv6-icmp -j ACCEPT | |
fi | |
if [ "${SSH_on_IPv4}" == "True" ] | |
then | |
# Configure port 22 (ssh) separately as Fail2Ban does not support IPv6 | |
iptables -A INPUT -p tcp --dport 22 -j ACCEPT | |
if [ "${Fail2Ban_Enabled}" == "True" ] | |
then | |
# Restart Fail2Ban | |
service fail2ban start | |
fi | |
fi | |
# Show result | |
sleep 1 | |
for iptables in "${tables[@]}" | |
do | |
"${iptables}" -L -v | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment