Azuria

BASH - ipTables firewall for DMZ

Jul 14th, 2012
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.69 KB | None | 0 0
  1. Firewalling your Linux box.
  2. This firewall script is intended to work an a firewall with LAN and DMZ zones.
  3. Everything is blocked and then allowed as nessesary.
  4. Firewall Script
  5.  
  6. FW=/sbin/iptables
  7. echo 0 > /proc/sys/net/ipv4/ip_forward
  8. OFF_IP=192.168.2.110 # Firewalls exterior IP.
  9. INET_IF=eth0 # Internet interface
  10. DMZ_IF=eth1 # DMZ Interface
  11. LAN_IF=eth2 # LAN interface
  12. SRV1=172.16.1.2 # DMZ Server
  13.  
  14. echo "Remove all prior rules including NAT"
  15. $FW -F ; $FW -X ; $FW -Z ; $FW -F -t nat
  16. echo "Set policy"
  17. $FW -P INPUT DROP
  18. $FW -P FORWARD DROP
  19. $FW -P OUTPUT ACCEPT
  20. echo "Allow connections established from LAN or DMZ."
  21. $FW -A INPUT -j ACCEPT -m state --state ESTABLISHED,RELATED
  22. $FW -A FORWARD -j ACCEPT -m state --state ESTABLISHED,RELATED
  23. $FW -t nat -A POSTROUTING -o $INET_IF -j SNAT --to-source $OFF_IP
  24. echo "INPUT rules"
  25. # Accept trafic from DMZ and LAN
  26. $FW -A INPUT -i lo -j ACCEPT # Server can connect locally
  27. $FW -A INPUT -i $DMZ_IF -j ACCEPT
  28. $FW -A INPUT -i $LAN_IF -j ACCEPT
  29. # Route trafic from DMZ, LO2, LO3
  30. $FW -A FORWARD -i $DMZ_IF -j ACCEPT
  31. $FW -A FORWARD -i $LAN_IF -j ACCEPT
  32. $FW -A FORWARD -i lo -j ACCEPT
  33. echo "Allow SSH from the Internet."
  34. $FW -A INPUT -j ACCEPT -p tcp --dport 22
  35.  
  36. echo "Allow ICMP packages (Ping)"
  37. $FW -A FORWARD -p icmp --icmp-type echo-request -j ACCEPT
  38. $FW -A FORWARD -p icmp --icmp-type echo-reply -j ACCEPT
  39. $FW -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
  40. $FW -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT
  41.  
  42. echo "Ports that will be forwarded for DMZ server, insert as best fitted."
  43. FORWARDPORTS="25 80 443"
  44. for port in $FORWARDPORTS
  45. do
  46. echo $port
  47. $FW -A FORWARD -j ACCEPT -p tcp --dport $port
  48. $FW -A FORWARD -j ACCEPT -p udp --dport $port
  49. $FW -t nat -A PREROUTING -p tcp -d $OFF_IP --dport $port -j DNAT --to $SRV1
  50. $FW -t nat -A PREROUTING -p udp -d $OFF_IP --dport $port -j DNAT --to $SRV1
  51. done
  52. # start routing
  53. echo 1 > /proc/sys/net/ipv4/ip_forward
  54. Installation
  55.  
  56. # mkdir /etc/firewall
  57. # nano /etc/firewall/firewall.sh <- Copy'n'Paste the script, save and exit.
  58. # chmod +x /etc/firewall/firewall.sh
  59. # nano /etc/init.d/firewall <- Copy'n'Paste the init script, save and exit.
  60. # update-rc.d firewall defaults <- To install the init script.
  61. # update-rc.d -f firewall remove <- To remove the init script again
  62. Init script
  63.  
  64. #! /bin/sh
  65. # /etc/init.d/blah
  66. #
  67.  
  68. # Some things that run always
  69. touch /var/lock/blah
  70.  
  71. # Carry out specific functions when asked to by the system
  72. case "$1" in
  73.   start)
  74.     echo "Starting script blah "
  75.     echo "Could do more here"
  76.     ;;
  77.   stop)
  78.     echo "Stopping script blah"
  79.     echo "Could do more here"
  80.     ;;
  81.   *)
  82.     echo "Usage: /etc/init.d/blah {start|stop}"
  83.     exit 1
  84.     ;;
  85. esac
  86.  
  87. exit 0
Advertisement
Add Comment
Please, Sign In to add comment