Created
February 9, 2012 21:14
-
-
Save yrosen/1783272 to your computer and use it in GitHub Desktop.
Basic iptables firewall
This file contains hidden or 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/sh | |
# Drop ICMP echo-request messages sent to broadcast or multicast addresses | |
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts | |
# Drop source routed packets | |
echo 0 > /proc/sys/net/ipv4/conf/all/accept_source_route | |
# Enable TCP SYN cookie protection from SYN floods | |
echo 1 > /proc/sys/net/ipv4/tcp_syncookies | |
# Don't accept ICMP redirect messages | |
echo 0 > /proc/sys/net/ipv4/conf/all/accept_redirects | |
# Don't send ICMP redirect messages | |
echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects | |
# Enable source address spoofing protection | |
echo 1 > /proc/sys/net/ipv4/conf/all/rp_filter | |
# Log packets with impossible source addresses | |
echo 1 > /proc/sys/net/ipv4/conf/all/log_martians | |
# Flush all chains | |
/sbin/iptables --flush | |
# Allow unlimited traffic on the loopback interface | |
/sbin/iptables -A INPUT -i lo -j ACCEPT | |
/sbin/iptables -A OUTPUT -o lo -j ACCEPT | |
# Set default policies | |
/sbin/iptables --policy INPUT DROP | |
/sbin/iptables --policy OUTPUT DROP | |
/sbin/iptables --policy FORWARD DROP | |
# Previously initiated and accepted exchanges bypass rule checking | |
# Allow unlimited outbound traffic | |
/sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT | |
/sbin/iptables -A OUTPUT -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT | |
# Allow incoming TCP - SSH & HTTP/HTTPS | |
/sbin/iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j ACCEPT | |
/sbin/iptables -A INPUT -p tcp --dport 80 -j ACCEPT | |
/sbin/iptables -A INPUT -p tcp --dport 443 -j ACCEPT | |
# Drop all other traffic | |
/sbin/iptables -A INPUT -j DROP | |
# Have these rules take effect when iptables is started | |
#/sbin/service iptables save |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment