yaks-apt-firewall.sh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env bash
  2. ################################################################################
  3. # FILE : yaks-apt-firewall.sh
  4. # DESCRIPTION: Firewall script for debian-ubuntu.
  5. # AUTHOR : SVAKSHA, http://svaksha.com/pages/Bio
  6. # SOURCE : http://svaksha.github.io/yaksha
  7. # COPYRIGHT© : 2005-Now SVAKSHA, All Rights Reserved.
  8. # LICENSE : GNU AGPLv3 and subject to meeting all the terms in the LICENSE
  9. # file: https://github.com/svaksha/yaksha/blob/master/LICENSE.md
  10. # DATES : Created:2015nov03 - Updated:2016jan14
  11. ################################################################################
  12. #
  13. # Credits: The amazing Carla Schroder's IPtables script posted on the techtalk list.
  14. # An IPtables firewall script for sharing a cable/DSL Internet connection, with no public services.
  15. #define variables
  16. ipt="/sbin/iptables"
  17. mod="/sbin/modprobe"
  18. LAN_IFACE="eth0"
  19. WAN_IFACE="wlan0"
  20. #load kernel modules
  21. $mod ip_tables
  22. $mod iptable_filter
  23. $mod iptable_nat
  24. $mod ip_conntrack
  25. $mod ipt_LOG
  26. $mod ipt_limit
  27. $mod ipt_state
  28. $mod iptable_mangle
  29. $mod ipt_MASQUERADE
  30. # Flush all active rules and delete all custom chains
  31. $ipt -F
  32. $ipt -t nat -F
  33. $ipt -t mangle -F
  34. $ipt -X
  35. $ipt -t nat -X
  36. $ipt -t mangle -X
  37. #Set default policies
  38. $ipt -P INPUT DROP
  39. $ipt -P FORWARD DROP
  40. $ipt -P OUTPUT ACCEPT
  41. $ipt -t nat -P OUTPUT ACCEPT
  42. $ipt -t nat -P PREROUTING ACCEPT
  43. $ipt -t nat -P POSTROUTING ACCEPT
  44. $ipt -t mangle -P PREROUTING ACCEPT
  45. $ipt -t mangle -P POSTROUTING ACCEPT
  46. #this line is necessary for the loopback interface
  47. #and internal socket-based services to work correctly
  48. $ipt -A INPUT -i lo -j ACCEPT
  49. #Enable IP masquerading
  50. $ipt -t nat -A POSTROUTING -o $WAN_IFACE -j MASQUERADE
  51. #Enable unrestricted outgoing traffic, incoming
  52. #is restricted to locally-initiated sessions only
  53. $ipt -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  54. $ipt -A FORWARD -i $WAN_IFACE -o $LAN_IFACE -m state --state
  55. ESTABLISHED,RELATED -j ACCEPT $ipt -A FORWARD -i $LAN_IFACE -o
  56. $WAN_IFACE -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
  57. # Accept ICMP echo-request and time-exceeded
  58. $ipt -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
  59. $ipt -A INPUT -p icmp --icmp-type time-exceeded -j ACCEPT
  60. $ipt -A INPUT -p icmp --icmp-type destination-unreachable -j ACCEPT
  61. #Reject connection attempts not initiated from inside the LAN
  62. $ipt -A INPUT -p tcp --syn -j DROP