Guest User

Slackware IPv6 Firewall Script (ip6tables)

a guest
Oct 10th, 2022
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.17 KB | Cybersecurity | 0 0
  1. #!/bin/sh
  2. #
  3. # Standard IPv6 Firewall Script
  4. # Designed for Slackware Linux 15.0
  5. # Reddit u/cyranix, r/slackware
  6. # Released without any license for use/modification by the general public
  7. #
  8. # It can be executed with the typical start and stop arguments.
  9. # If used with stop, it will stop after flushing the firewall.
  10. # The save and restore arguments will save or restore the rules
  11. # from the /etc/sysconfig/iptables file.  The save and restore
  12. # arguments are included to preserve compatibility with
  13. # Redhat's or Fedora's init.d script if you prefer to use it.
  14.  
  15. # You may want to save this script as /etc/rc.d/rc.firewall6
  16. # and make it executable, like this:
  17. #   chmod +x /etc/rc.d/rc.firewall6
  18. # Slackware Linux will then automatically run this script
  19. # with the "start" parameter.
  20.  
  21. ###############################################################################
  22. #
  23. # Local Settings
  24. #
  25.  
  26. # IPTables Location - adjust if needed
  27.  
  28. IP6T="/usr/sbin/ip6tables"
  29. IP6TS="/usr/sbin/ip6tables-save"
  30. IP6TR="/usr/sbin/ip6tables-restore"
  31.  
  32. # Save and Restore arguments handled here
  33. if [ "$1" = "save" ]
  34. then
  35.     echo -n "Saving firewall to /etc/sysconfig/iptables ... "
  36.     $IP6TS > /etc/sysconfig/ip6tables
  37.     echo "done"
  38.     exit 0
  39. elif [ "$1" = "restore" ]
  40. then
  41.     echo -n "Restoring firewall from /etc/sysconfig/iptables ... "
  42.     $IP6TR < /etc/sysconfig/ip6tables
  43.     echo "done"
  44.     exit 0
  45. fi
  46.  
  47. ###############################################################################
  48. #
  49. # Load Modules
  50. #
  51.  
  52. echo "Loading kernel modules ..."
  53.  
  54. # You should uncomment the line below and run it the first time just to
  55. # ensure all kernel module dependencies are OK.  There is no need to run
  56. # every time, however.
  57.  
  58. # /sbin/depmod -a
  59.  
  60. # Unless you have kernel module auto-loading disabled, you should not
  61. # need to manually load each of these modules.  Other than ip_tables,
  62. # ip_conntrack, and some of the optional modules, I've left these
  63. # commented by default.  Uncomment if you have any problems or if
  64. # you have disabled module autoload.  Note that some modules must
  65. # be loaded by another kernel module.
  66.  
  67. # core netfilter module
  68. /sbin/modprobe ip6_tables
  69.  
  70. ###############################################################################
  71. #
  72. # Flush Any Existing Rules or Chains
  73. #
  74.  
  75. echo "Flushing Tables ..."
  76.  
  77. $IP6T -F
  78.  
  79. # Set default chain policies
  80. $IP6T -P INPUT ACCEPT
  81. $IP6T -P FORWARD ACCEPT
  82. $IP6T -P OUTPUT ACCEPT
  83.  
  84. if [ "$1" = "stop" ]
  85. then
  86.         echo "Firewall completely flushed!  Now running with no firewall."
  87.         exit 0
  88. fi
  89.  
  90. # This accepts ongoing traffic for any existing connections that we've already accepted through other rule:.
  91. $IP6T -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  92.  
  93. # Accept all ICMP packets. Unlike with IPv4, it's not a good idea to block ICMPv6 traffic as IPv6 is much more heavily dependent on it:
  94. $IP6T -A INPUT -p ipv6-icmp -j ACCEPT
  95.  
  96. # Feedback for problems
  97. $IP6T -A INPUT -p icmpv6 --icmpv6-type 1 -j ACCEPT
  98. $IP6T -A INPUT -p icmpv6 --icmpv6-type 2 -j ACCEPT
  99. $IP6T -A INPUT -p icmpv6 --icmpv6-type 3 -j ACCEPT
  100. $IP6T -A INPUT -p icmpv6 --icmpv6-type 4 -j ACCEPT
  101.  
  102. # Router and neighbor discovery incoming and outgoing
  103. $IP6T -A INPUT -p icmpv6 --icmpv6-type 133 -j ACCEPT
  104. $IP6T -A INPUT -p icmpv6 --icmpv6-type 134 -j ACCEPT
  105. $IP6T -A INPUT -p icmpv6 --icmpv6-type 135 -j ACCEPT
  106. $IP6T -A INPUT -p icmpv6 --icmpv6-type 136 -j ACCEPT
  107.  
  108. $IP6T -A OUTPUT -p icmpv6 --icmpv6-type 133 -j ACCEPT
  109. $IP6T -A OUTPUT -p icmpv6 --icmpv6-type 134 -j ACCEPT
  110. $IP6T -A OUTPUT -p icmpv6 --icmpv6-type 135 -j ACCEPT
  111. $IP6T -A OUTPUT -p icmpv6 --icmpv6-type 136 -j ACCEPT
  112.  
  113. # Ping request from firewall
  114. $IP6T -A OUTPUT -p icmpv6 --icmpv6-type 128 -j ACCEPT
  115.  
  116. # Accept all traffic from/to the local interface:
  117. $IP6T -A INPUT -i lo -j ACCEPT
  118. $IP6T -A OUTPUT -o lo -j ACCEPT
  119.  
  120. # Enable stateful inspection
  121. $IP6T -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  122. $IP6T -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  123.  
  124. # Anti-spoofing
  125. $IP6T -A INPUT ! -i lo -s ::1/128 -j DROP
  126. $IP6T -A INPUT -i $INET_IFACE -s fc00::/7 -j DROP
  127.  
  128. # Block tunnel prefixes
  129. $IP6T -A INPUT -s 2002::/16 -j DROP
  130. $IP6T -A INPUT -s 2001:0::/32 -j DROP
  131.  
  132. # ------------------------------- Add all other rules starting here ---------------------------
  133.  
  134. # Accept DHCPv6 traffic. If you use stateless autoconfiguration, or statically configure your machines, this is not necessary:
  135. $IP6T -A INPUT -d fe80::/64 -p UDP -m state --state NEW -m udp --dport 546 -j ACCEPT
  136.  
  137. # HTTP
  138. $IP6T -A INPUT -p TCP -m state --state NEW -m tcp --dport 80 -j ACCEPT
  139.  
  140. # HTTPS (Secure Web Server)
  141. $IP6T -A INPUT -p TCP -m state --state NEW -m tcp --dport 443 -j ACCEPT
  142.  
  143. # Email Server (SMTP)
  144. $IP6T -A INPUT -p TCP -m state --state NEW -m tcp --dport 25 -j ACCEPT
  145.  
  146. # Secure Email Server (SMTPS)
  147. $IP6T -A INPUT -p TCP -m state --state NEW -m tcp --dport 465 -j ACCEPT
  148.  
  149. # Submission
  150. $IP6T -A INPUT -p TCP -m state --state NEW -m tcp --dport 587 -j ACCEPT
  151.  
  152. # Email Server (POP3)
  153. $IP6T -A INPUT -p TCP -m state --state NEW -m tcp --dport 110 -j ACCEPT
  154.  
  155. # Email Server (IMAP4)
  156. $IP6T -A INPUT -p TCP -m state --state NEW -m tcp --dport 143 -j ACCEPT
  157.  
  158. # SSL Email Server (POP3)
  159. $IP6T -A INPUT -p TCP -m state --state NEW -m tcp --dport 995 -j ACCEPT
  160.  
  161. # SSL Email Server (IMAP4)
  162. $IP6T -A INPUT -p TCP -m state --state NEW -m tcp --dport 993 -j ACCEPT
  163.  
  164. # sshd
  165. $IP6T -A INPUT -p TCP -m state --state NEW -m TCP --dport 22 -j ACCEPT
  166.  
  167. # BIND/named
  168. $IP6T -A INPUT -p TCP -m state --state NEW -m tcp --dport 53 -j ACCEPT
  169. $IP6T -A INPUT -p UDP -m udp --dport 53 -j ACCEPT
  170. $IP6T -A INPUT -p TCP -m state --state NEW -m tcp --dport 953 -j ACCEPT
  171. $IP6T -A INPUT -p UDP -m udp --dport 953 -j ACCEPT
  172.  
  173. # At the end of our rules, we reject all traffic that didn't match a rule, using "port unreachable".
  174. # This results in the standard "Connection refused" message at the other end, and effectively hides the fact that we have a firewall.
  175. # Tools such as nmap will report that all our ports are "closed" rather than "filtered"
  176. # and have a much more difficult time determining that we even have a firewall.
  177. $IP6T -A INPUT -j REJECT --reject-with icmp6-adm-prohibited
  178. $IP6T -A FORWARD -j REJECT --reject-with icmp6-adm-prohibited
  179.  
Advertisement
Add Comment
Please, Sign In to add comment