Code :
- #!/bin/sh
- ## Variables ##
- IPTABLES="/sbin/iptables"
- LOOPBACK="lo" ## Loopback Interface
- EXT="ppp0" ## External Interface
- INT="eth0" ## Internal Interface
- INT_NET="192.168.0.0/24" ## Network address for the internal network
- INT_IP="192.168.0.100" ## IP Address of Internal Interface
- #LOG_LEVEL="notice" ## Default log level: kern.notice
- ## Attempt to Flush All Rules in Filter Table
- $IPTABLES -F
- ## Flush Built-in Rules
- $IPTABLES -F INPUT
- $IPTABLES -F OUTPUT
- $IPTABLES -F FORWARD
- ## Delete all user-defined chains, reduces dumb warnings if you run
- ## this script more than once.
- $IPTABLES -X
- ## Set Default Policies
- $IPTABLES -P INPUT DROP ## Highly Recommended Default Policy
- $IPTABLES -P OUTPUT DROP
- $IPTABLES -P FORWARD ACCEPT
- ###############################################################################
- ## Special chain KEEP_STATE to handle incoming, outgoing, and
- ## established connections.
- $IPTABLES -N KEEP_STATE
- $IPTABLES -F KEEP_STATE
- ## DROP packets associated with an "INVALID" connection.
- $IPTABLES -A KEEP_STATE -m state --state INVALID -j DROP
- ## ACCEPT packets which are related to an established connection.
- $IPTABLES -A KEEP_STATE -m state --state RELATED,ESTABLISHED -j ACCEPT
- ###############################################################################
- ## Special chain CHECK_FLAGS that will DROP and log TCP packets with certain
- ## TCP flags set.
- $IPTABLES -N CHECK_FLAGS
- $IPTABLES -F CHECK_FLAGS
- ## Possible NULL scan.
- $IPTABLES -A CHECK_FLAGS -p tcp --tcp-flags ALL NONE \
- -m limit --limit 5/minute -j LOG \
- --log-prefix "NULL SCAN: " --log-tcp-options --log-ip-options
- $IPTABLES -A CHECK_FLAGS -p tcp --tcp-flags ALL NONE -j DROP
- ## NMAP FIN/URG/PSH
- $IPTABLES -A CHECK_FLAGS -p tcp --tcp-flags ALL FIN,URG,PSH -m limit \
- --limit 5/minute -j LOG --log-prefix "NMAP-XMAS: "
- $IPTABLES -A CHECK_FLAGS -p tcp --tcp-flags ALL FIN,URG,PSH -j DROP
- ## SYN/RST
- $IPTABLES -A CHECK_FLAGS -p tcp --tcp-flags SYN,RST SYN,RST -m limit \
- --limit 5/minute -j LOG --log-prefix "SYN/RST: "
- $IPTABLES -A CHECK_FLAGS -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
- ## SYN/FIN -- Scan(probably)
- $IPTABLES -A CHECK_FLAGS -p tcp --tcp-flags SYN,FIN SYN,FIN -m limit \
- --limit 5/minute -j LOG --log-prefix "SYN/FIN: "
- $IPTABLES -A CHECK_FLAGS -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
- #echo 'check flags'
- ###############################################################################
- ## Special Chain DENY_PORTS
- ## This chain will DROP/LOG packets based on port number.
- $IPTABLES -N DENY_PORTS
- $IPTABLES -F DENY_PORTS
- ## DROP TCP packets based on port number.
- ## See DOCUMENTATION for explanation of these example port numbers.
- DENIED_PORTS_TCP="137:139 2049 6000:6063 \
- 20034 12345:12346 27374 27665 \
- 27444 31335 10498 12754"
- for PORT in $DENIED_PORTS_TCP; do
- $IPTABLES -A DENY_PORTS -p tcp --dport $PORT -m limit --limit 5/minute \
- -j LOG --log-prefix "DENIED PORT:"
- $IPTABLES -A DENY_PORTS -p tcp --sport $PORT -m limit --limit 5/minute \
- -j LOG --log-prefix "DENIED PORT:"
- $IPTABLES -A DENY_PORTS -p tcp --dport $PORT -j DROP
- $IPTABLES -A DENY_PORTS -p tcp --sport $PORT -j DROP
- done
- ## DROP UDP packets based on port number.
- ## See DOCUMENTATION for explanation of these example port numbers.
- DENIED_PORTS_UDP="2049 31337 27444 31335 10498"
- for PORT in $DENIED_PORTS_UDP; do
- $IPTABLES -A DENY_PORTS -p udp --dport $PORT -m limit --limit 5/minute \
- -j LOG --log-prefix "DENIED PORT:"
- $IPTABLES -A DENY_PORTS -p udp --sport $PORT -m limit --limit 5/minute \
- -j LOG --log-prefix "DENIED PORT:"
- $IPTABLES -A DENY_PORTS -p udp --dport $PORT -j DROP
- $IPTABLES -A DENY_PORTS -p udp --sport $PORT -j DROP
- done
- #echo 'deny port'
- ###############################################################################
- ## Special Chain ALLOW_PORTS
- ## Rules to allow packets based on port number. This sort of thing is generally
- ## required only if you're running services on(!!!) the firewall or if you have a
- ## FORWARD policy of DROP(which we don't right now).
- $IPTABLES -N ALLOW_PORTS
- $IPTABLES -F ALLOW_PORTS
- ## ACCEPT TCP traffic based on port number. (Examples)
- # TCP_PORTS="ssh domain"
- TCP_PORTS="20 21 22 25 53 80 443 110 993"
- for PORT in $TCP_PORTS; do
- $IPTABLES -A ALLOW_PORTS -m state --state NEW -p tcp \
- --dport $PORT -j ACCEPT
- done
- ## ACCEPT UDP traffic based on port number.
- # UDP_PORTS="domain"
- UDP_PORTS="53"
- for PORT in $UDP_PORTS; do
- $IPTABLES -A ALLOW_PORTS -m state --state NEW -p udp \
- --dport $PORT -j ACCEPT
- done
- ## REJECT port 113 ident requests.
- # $IPTABLES -A ALLOW_PORTS -p tcp --dport 113 -j DROP
- # $IPTABLES -A ALLOW_PORTS -p tcp --dport 113 -j REJECT \
- # --reject-with tcp-reset
- #echo 'accept port'
- ###############################################################################
- ## Special Chain ALLOW_ICMP
- ## This chain contains rules to allow/drop specific types of ICMP datagrams.
- $IPTABLES -N ALLOW_ICMP
- $IPTABLES -F ALLOW_ICMP
- ## Echo Reply (pong)
- $IPTABLES -A ALLOW_ICMP -p icmp --icmp-type echo-reply -j ACCEPT
- ## Destination Unreachable
- $IPTABLES -A ALLOW_ICMP -p icmp --icmp-type destination-unreachable \
- -j ACCEPT
- ## Echo Request (ping) -- Several Options:
- ## Accept Pings ##
- $IPTABLES -A ALLOW_ICMP -p icmp --icmp-type echo-request -j ACCEPT
- ## Accept Pings at the rate of one per second ##
- $IPTABLES -A ALLOW_ICMP -p icmp --icmp-type echo-request \
- -m limit --limit 1/second -j ACCEPT
- ## LOG all pings ##
- # $IPTABLES -A ALLOW_ICMP -p icmp --icmp-type echo-request \
- # -m limit --limit 5/minute -j LOG --log-level $LOG_LEVEL \
- # --log-prefix "PING:"
- ## TTL Exceeded (traceroute)
- $IPTABLES -A ALLOW_ICMP -p icmp --icmp-type time-exceeded -j ACCEPT
- #echo 'allow icmp'
- ###############################################################################
- ## Firewall Input Chains
- ###############################################################################
- ## New chain for input to the external interface
- $IPTABLES -N EXT_INPUT
- $IPTABLES -F EXT_INPUT
- ## Hated Hosts -- Block hosts/subnets(Example)
- # $IPTABLES -A EXT_INPUT -i $EXT -s 123.123.123.0/24 -j DROP
- ## Check TCP packets coming in on the external interface for wierd flags
- $IPTABLES -A EXT_INPUT -i $EXT -p tcp -j CHECK_FLAGS
- ## Filter incoming packets based on port number.
- $IPTABLES -A EXT_INPUT -i $EXT -p ! icmp -j DENY_PORTS
- #----------------------------------------------------------------------------#
- ## New chain for input to the internal interface
- $IPTABLES -N INT_INPUT
- $IPTABLES -F INT_INPUT
- ## DROP anything not coming from the internal network.
- # $IPTABLES -A INT_INPUT -i $INT -s ! $INT_NET -j DROP
- ## Check TCP packets coming in on the external interface for wierd flags.
- $IPTABLES -A INT_INPUT -i $INT -p tcp -j CHECK_FLAGS
- ## DROP/LOG packets based on port number.
- $IPTABLES -A INT_INPUT -i $INT -p ! icmp -j DENY_PORTS
- #----------------------------------------------------------------------------#
- ## New chain for input to the loopback interface
- $IPTABLES -N LO_INPUT
- $IPTABLES -F LO_INPUT
- ## Accept packets to the loopback interface.
- $IPTABLES -A LO_INPUT -i $LOOPBACK -j ACCEPT
- #echo 'input chain'
- ###############################################################################
- ## Firewall Output Chains
- ###############################################################################
- ## New chain for output from the external interface
- $IPTABLES -N EXT_OUTPUT
- $IPTABLES -F EXT_OUTPUT
- ## Check TCP packets coming in on the external interface for wierd flags.
- $IPTABLES -A EXT_OUTPUT -o $EXT -p tcp -j CHECK_FLAGS
- ## Filter outgoing packets based on port number.
- $IPTABLES -A EXT_OUTPUT -o $EXT -p ! icmp -j DENY_PORTS
- #----------------------------------------------------------------------------#
- ## New chain for output across the internal interface
- $IPTABLES -N INT_OUTPUT
- $IPTABLES -F INT_OUTPUT
- ## DROP packets not destined for the internal network.
- # $IPTABLES -A INT_OUTPUT -o $INT -d ! $INT_NET -j DROP
- ## Filter outgoing packets based on port number.
- $IPTABLES -A INT_OUTPUT -o $INT -p ! icmp -j DENY_PORTS
- ## Check TCP packets going out on the internal interface for wierd flags.
- $IPTABLES -A INT_OUTPUT -o $INT -p tcp -j CHECK_FLAGS
- #----------------------------------------------------------------------------#
- ## New chain for output across the loopback device
- $IPTABLES -N LO_OUTPUT
- $IPTABLES -F LO_OUTPUT
- ## ACCEPT all traffic across loopback device
- $IPTABLES -A LO_OUTPUT -o $LOOPBACK -j ACCEPT
- #echo 'output chain'
- ###############################################################################
- ## Main Stuff
- ###############################################################################
- ## This is where we get to jump to our user-defined chains from the built-in
- ## chains.
- ## Jump to our INPUT chains.
- ## INPUT to our loopback interface.
- ## Jump to our LO_INPUT Chain.
- $IPTABLES -A INPUT -i $LOOPBACK -j LO_INPUT
- ## INPUT to our internal interface.
- ## DROP packets not destined for the internal IP address of the
- ## firewall.
- # $IPTABLES -A INPUT -i $INT -d ! $INT_IP -j DROP
- ## Jump to our INT_INPUT Chain.
- $IPTABLES -A INPUT -i $INT -j INT_INPUT
- ## DROP/ACCEPT packets based on the state of the connection.
- $IPTABLES -A INPUT -i $INT -j KEEP_STATE
- ## ACCEPT packets based on port number.
- $IPTABLES -A INPUT -i $INT -s $INT_NET -d $INT_IP \
- -p ! icmp -j ALLOW_PORTS
- ## Jump to ALLOW_ICMP for general rules relating to the ICMP protocol.
- $IPTABLES -A INPUT -i $INT -p icmp -j ALLOW_ICMP
- ## INPUT to the external Interface
- ## Jump to our EXT_INPUT Chain.
- $IPTABLES -A INPUT -i $EXT -j EXT_INPUT
- ## DROP/ACCEPT packets based on the state of the connection.
- $IPTABLES -A INPUT -i $EXT -j KEEP_STATE
- ## Allow Packets On Certain External Ports.
- $IPTABLES -A INPUT -i $EXT -p ! icmp -j ALLOW_PORTS
- ## Jump to ALLOW_ICMP for general rules relating to the ICMP protocol.
- $IPTABLES -A INPUT -i $EXT -p icmp -j ALLOW_ICMP
- #echo "input end"
- ## End INPUT Chain Rules ##
- ## Jump to our OUTPUT chains.
- ## OUTPUT on the loopback interface.
- ## Jump to our LO_OUTPUT Chain.
- $IPTABLES -A OUTPUT -o $LOOPBACK -j LO_OUTPUT
- ## OUTPUT on the internal interface.
- ## Jump to our INT_OUTPUT Chain.
- $IPTABLES -A OUTPUT -o $INT -j INT_OUTPUT
- ## DROP anything not coming from the firewall.
- # $IPTABLES -A OUTPUT -o $INT -s ! $INT_IP -j DROP
- ## Jump to the KEEP_STATE chain for generic state-based packet filtering.
- $IPTABLES -A OUTPUT -o $INT -j KEEP_STATE
- ## ACCEPT NEW connections from the firewall to the internal network.
- $IPTABLES -A OUTPUT -o $INT -s $INT_IP \
- -d $INT_NET -m state --state NEW -j ACCEPT
- ## OUTPUT on the external interface
- ## Jump to our EXT_OUTPUT Chain.
- $IPTABLES -A OUTPUT -o $EXT -j EXT_OUTPUT
- ## Jump to the KEEP_STATE chain for generic state-based packet filtering.
- $IPTABLES -A OUTPUT -o $EXT -j KEEP_STATE
- ## Accept outgoing packets establishing a NEW connection.
- $IPTABLES -A OUTPUT -o $EXT -m state --state NEW -j ACCEPT
- #echo "output end"
- ## End OUTPUT Chain Rules ##
- ## Jump to our FORWARD chains.
- ## Jump to our (INTERFACE)_INPUT/OUTPUT Chains.
- $IPTABLES -A FORWARD -i $EXT -j EXT_INPUT
- $IPTABLES -A FORWARD -i $INT -j INT_INPUT
- $IPTABLES -A FORWARD -o $EXT -j EXT_OUTPUT
- $IPTABLES -A FORWARD -o $INT -j INT_OUTPUT
- ## More rules to DROP stuff.
- ## DROP any attempted NEW connections to the internal network.
- $IPTABLES -A FORWARD -i $EXT -d $INT_NET -m state \
- --state NEW -j DROP
- # à forwarder sinon vers la DMZ !
- ## DROP any outbound traffic to the internal network that is trying to
- ## establish a NEW connection.
- $IPTABLES -A FORWARD -o $INT -d $INT_NET \
- -m state --state NEW -j DROP
- ## DROP anything not headed for the internal network.
- # $IPTABLES -A FORWARD -i $EXT -d ! $INT_NET -j DROP
- ## Basic State Based Rules.
- $IPTABLES -A FORWARD -j KEEP_STATE
- ## Accept outgoing packets establishing a NEW connection.
- $IPTABLES -A FORWARD -o $EXT -m state --state NEW -j ACCEPT
- ## Jump to ALLOW_ICMP for general rules relating to the ICMP protocol.
- $IPTABLES -A FORWARD -p icmp -j ALLOW_ICMP
- ## End FORWARD Chain Rules ##
- ### END FIREWALL RULES ###
|