Najosnovnija podešavanja iptablesa sa otvorenim portom 80 za dolazni saobraćaj (korisno za webserver) i pristup nekim osnovnim portovima sa unutrašnje mreže. Napraviti skriptu pa je okinuti kao root.

#!/bin/bash
 
#####################################################
# ------          PROMENLJIVE           ----------- #
# ------------------------------------------------- #
# IP adrese/opsezi IP adresa sa kojih je dozvoljen pun pristup masini. 
FULL_ACCESS="10.0.99.0/24"

# Portovi koje treba da vidi ceo svet po protokolu. Format PORT:PROTOKOL
PUBLIC_PORTS="80:tcp 443:tcp 22:tcp"

# Portovi dozvoljeni po IP adresi/opsegu i protokolu. Format: IP:PORT:PROTOKOL
ALLOWED_IP_PORT="192.168.0.0/16:1521:tcp 123.123.123.123:8081:udp"

# IP adrese ili opsezi sa kojih je dozvoljen ping ka masini
PING_ALLOWED="10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 234.234.234.234"
# --------------------------------------------------------------------------#
#############################################################################


# Brisanje trenutnih pravila ------------------------------------------------------
service iptables stop 1>/dev/null
iptables -F 1>/dev/null
iptables -X 1>/dev/null
service iptables save 1>/dev/null

# PORTOVI OTVORENI ZA SVE ---------------------------------------------------------
if [ ! -z "${PUBLIC_PORTS}" ] ; then
for i in ${PUBLIC_PORTS}
do 
	PORT_PROTOKOL=$(echo $i | tr ':' ' ')
	PORT=$(echo $PORT_PROTOKOL | awk '{print $1}')
	PROTOKOL=$(echo $PORT_PROTOKOL | awk '{print $2}')
	iptables -A INPUT -p ${PROTOKOL} --dport ${PORT} -j ACCEPT
done
fi

# PUN PRISTUP ---------------------------------------------------------------------
if [ ! -z "${FULL_ACCESS}" ] ; then
for i in ${FULL_ACCESS}
do 
	iptables -A INPUT -s ${i} -j ACCEPT
done
fi

# PORTOVI OTVORENI ZA DOZVOLJENE ADRESE --------------------------------------------
if [ ! -z "${ALLOWED_IP_PORT}" ] ; then
for i in ${ALLOWED_IP_PORT}
do
	IP_PORT=$(echo $i | tr ':' ' ')
	IP=$(echo $IP_PORT | awk '{print $1}')
	PORT=$(echo $IP_PORT | awk '{print $2}')
	PROTOKOL=$(echo $IP_PORT | awk '{print $3}')
	iptables -A INPUT -p ${PROTOKOL} -s ${IP} --dport ${PORT} -j ACCEPT
done
fi

# PING DOZVOLJEN SAMO SA DOZVOLJENE ADRESE ------------------------------------------
if [ ! -z "${PING_ALLOWED}" ] ; then
	for i in ${PING_ALLOWED}
	do
	iptables -A INPUT -s ${i} -p ICMP --icmp-type 8 -j ACCEPT
	done
fi

# OSTALO ----------------------------------------------------------------------------
iptables -I INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT # Prvo pravilo
iptables -A INPUT -s 127.0.0.1 -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

# DROPS ------------------------------------------------------------------------------
# Zabrani DoS, Port skeniranje, neke tipove DDoS-a, neispravne pakete i slično
iptables -A INPUT -m conntrack --ctstate INVALID -j DROP
iptables -t mangle -A PREROUTING -p tcp ! --syn -m conntrack --ctstate NEW -j DROP
iptables -t mangle -A PREROUTING -p tcp -m conntrack --ctstate NEW -m tcpmss ! --mss 536:65535 -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags FIN,SYN FIN,SYN -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags FIN,RST FIN,RST -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags FIN,ACK FIN -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags ACK,URG URG -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags ACK,FIN FIN -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags ACK,PSH PSH -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags ALL ALL -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags ALL NONE -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags ALL FIN,PSH,URG -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags ALL SYN,FIN,PSH,URG -j DROP
iptables -t mangle -A PREROUTING -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP
iptables -A INPUT -p tcp -m connlimit --connlimit-above 150 -j REJECT --reject-with tcp-reset
iptables -t mangle -A PREROUTING -f -j DROP
iptables -A INPUT -p tcp --tcp-flags RST RST -m limit --limit 2/s --limit-burst 2 -j ACCEPT
iptables -A INPUT -p tcp --tcp-flags RST RST -j DROP
iptables -D INPUT -m conntrack --ctstate INVALID -j DROP
iptables -N port-scanning
iptables -A port-scanning -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s --limit-burst 2 -j RETURN
iptables -A port-scanning -j DROP
 
 
################################################################################
############          DROP SVEGA STO NIJE DEFINISANO               #############
################################################################################
                         iptables -A INPUT -j DROP
################################################################################
 
#--------------------------------
service iptables save
service iptables restart

# listing pravila ---------------
iptables -L -n --line-numbers

exit 0

Više i iptables: Linux kao firewall (iptables – opis i pravila)