Answers to: iptables configuration script - need help with ithttp://linuxexchange.org/questions/1206/iptables-configuration-script-need-help-with-it<p>This section is part of a larger install-script I'm working on, and my knowledge of iptables stinks (still learning it), so what I need is for this to be in the right order, with the right options enabled.</p> <p>Basically I want: - to allow in what need be, to drop (not reject - what I don't want) - to have separation of the info into the appropriate custom log files in /var/log - to be as safe as possible and secure against probes, attacks etc, yet still usable - to allow NAT port forwarding (I have a router like many people do) (I use SSH and VNC in/out)</p> <p>This is intended to be used on an Ubuntu desktop machine (Lucid Lynx or later)</p> <p>Again... I know this stinks, but I need some serious help with it. Some options are in there, just not enabled, and that's fine.</p> <p>I've updated the script on 08/13/10.</p> <pre> #! /bin/bash # # ####################################################################################################################### # iptables setup and configuration # ####################################################################################################################### # # The following is a script for setting up and configuring iptables on a desktop computer running Ubuntu. # iptables -F # # ####################################################################################################################### # Logging # ####################################################################################################################### # iptables -A OUTPUT -j LOG iptables -A INPUT -j LOG iptables -A FORWARD -j LOG # # # ####################################################################################################################### # iptables-rules-input-policies # ####################################################################################################################### # iptables -P INPUT ACCEPT iptables -P FORWARD DROP iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -A INPUT -i lo -j ACCEPT iptables -A OUTPUT -o lo -j ACCEPT # # ####################################################################################################################### # Allow unlimited outbound traffic # ####################################################################################################################### # iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT iptables -A OUTPUT -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT # # ####################################################################################################################### # icmp, INPUT and ports setup # ####################################################################################################################### # iptables -A INPUT -p icmp -m icmp --icmp-type 3 -j ACCEPT iptables -A INPUT -p icmp -m icmp --icmp-type 11 -j ACCEPT iptables -A INPUT -p icmp -m icmp --icmp-type 4 -j ACCEPT iptables -A INPUT -p icmp -m icmp --icmp-type 12 -j ACCEPT # iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 20 -j ACCEPT # iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT # iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 25 -j ACCEPT # iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT # iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 110 -j ACCEPT iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 113 -j ACCEPT iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 5500 -j ACCEPT iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 5800 -j ACCEPT iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 5900 -j ACCEPT # # ####################################################################################################################### # Securing INPUT # ####################################################################################################################### # iptables -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j DROP iptables -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -j DROP iptables -A INPUT -p tcp -m tcp --tcp-flags SYN,RST SYN,RST -j DROP iptables -A INPUT -p tcp -m tcp --tcp-flags FIN,RST FIN,RST -j DROP iptables -A INPUT -p tcp -m tcp --tcp-flags FIN,ACK FIN -j DROP iptables -A INPUT -p tcp -m tcp --tcp-flags ACK,URG URG -j DROP iptables -A INPUT -p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -m state --state NEW -j DROP # # ####################################################################################################################### # Allow freenode to talk to gidentd # ####################################################################################################################### # iptables -A INPUT -s chat.freenode.net -p tcp --dport 113 -j ACCEPT # # ####################################################################################################################### # Enable IP Forwarding # ####################################################################################################################### # echo 1 > /proc/sys/net/ipv4/ip_forward # # ####################################################################################################################### # IP Masquerading # ####################################################################################################################### # # (not needed if intranet is not using private ip-addresses) iptables -t nat -A POSTROUTING -o ppp+ -j MASQUERADE # # ####################################################################################################################### # iptables-anti-attack-measures-policies # ####################################################################################################################### # # In the following section set it 1 to enable the feature or 0 to disable the feature # # TCP SYN cookie protection from SYN floods echo 1 > /proc/sys/net/ipv4/tcp_syncookies # # 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 # # 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 # # ####################################################################################################################### # Lockdown INPUT # ####################################################################################################################### # iptables -A INPUT -j DROP iptables -P INPUT DROP # # ####################################################################################################################### # iptables-save workaround # ####################################################################################################################### # iptables-save -c > /etc/iptables.rules iptables-restore </pre>enWed, 20 Apr 2011 14:08:26 -0400Comment by rfelsburg on Ron's questionhttp://linuxexchange.org/questions/1206/iptables-configuration-script-need-help-with-it#2446<p>Please accept an answer so the question/answer can be finished. Or provide more details so we can help.</p>rfelsburgWed, 20 Apr 2011 14:08:26 -0400http://linuxexchange.org/questions/1206/iptables-configuration-script-need-help-with-it#2446