Advertisement
Guest User

Firewall

a guest
Jul 20th, 2013
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 21.62 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # Generated iptables firewall script for the Linux 2.4 kernel
  4. # Script generated by Easy Firewall Generator for IPTables 1.15
  5. # copyright 2002 Timothy Scott Morizot
  6. #
  7. # Redhat chkconfig comments - firewall applied early,
  8. #                             removed late
  9. # chkconfig: 2345 08 92
  10. # description: This script applies or removes iptables firewall rules
  11. #
  12. # This generator is primarily designed for RedHat installations,
  13. # although it should be adaptable for others.
  14. #
  15. # It can be executed with the typical start and stop arguments.
  16. # If used with stop, it will stop after flushing the firewall.
  17. # The save and restore arguments will save or restore the rules
  18. # from the /etc/sysconfig/iptables file.  The save and restore
  19. # arguments are included to preserve compatibility with
  20. # Redhat's or Fedora's init.d script if you prefer to use it.
  21.  
  22. # Redhat/Fedora installation instructions
  23. #
  24. # 1. Have the system link the iptables init.d startup script into run states
  25. #    2, 3, and 5.
  26. #    chkconfig --level 235 iptables on
  27. #
  28. # 2. Save this script and execute it to load the ruleset from this file.
  29. #    You may need to run the dos2unix command on it to remove carraige returns.
  30. #
  31. # 3. To have it applied at startup, copy this script to
  32. #    /etc/init.d/iptables.  It accepts stop, start, save, and restore
  33. #    arguments.  (You may wish to save the existing one first.)
  34. #    Alternatively, if you issue the 'service iptables save' command
  35. #    the init.d script should save the rules and reload them at runtime.
  36. #
  37. # 4. For non-Redhat systems (or Redhat systems if you have a problem), you
  38. #    may want to append the command to execute this script to rc.local.
  39. #    rc.local is typically located in /etc and /etc/rc.d and is usually
  40. #    the last thing executed on startup.  Simply add /path/to/script/script_name
  41. #    on its own line in the rc.local file.
  42.  
  43. ###############################################################################
  44. #
  45. # Local Settings
  46. #
  47.  
  48. # sysctl location.  If set, it will use sysctl to adjust the kernel parameters.
  49. # If this is set to the empty string (or is unset), the use of sysctl
  50. # is disabled.
  51.  
  52. SYSCTL="/sbin/sysctl -w"
  53.  
  54. # To echo the value directly to the /proc file instead
  55. # SYSCTL=""
  56.  
  57. # IPTables Location - adjust if needed
  58.  
  59. IPT="/sbin/iptables"
  60. IPTS="/sbin/iptables-save"
  61. IPTR="/sbin/iptables-restore"
  62.  
  63. # Internet Interface
  64. INET_IFACE="eth0"
  65. INET_ADDRESS="XX.XX.XX.XX"
  66.  
  67. # Localhost Interface
  68.  
  69. LO_IFACE="lo"
  70. LO_IP="127.0.0.1"
  71.  
  72. # Save and Restore arguments handled here
  73. if [ "$1" = "save" ]
  74. then
  75.     echo -n "Saving firewall to /etc/sysconfig/iptables ... "
  76.     $IPTS > /etc/sysconfig/iptables
  77.     echo "done"
  78.     exit 0
  79. elif [ "$1" = "restore" ]
  80. then
  81.     echo -n "Restoring firewall from /etc/sysconfig/iptables ... "
  82.     $IPTR < /etc/sysconfig/iptables
  83.     echo "done"
  84.     exit 0
  85. fi
  86.  
  87. ###############################################################################
  88. #
  89. # Load Modules
  90. #
  91.  
  92. echo "Loading kernel modules ..."
  93.  
  94. # You should uncomment the line below and run it the first time just to
  95. # ensure all kernel module dependencies are OK.  There is no need to run
  96. # every time, however.
  97.  
  98. # /sbin/depmod -a
  99.  
  100. # Unless you have kernel module auto-loading disabled, you should not
  101. # need to manually load each of these modules.  Other than ip_tables,
  102. # ip_conntrack, and some of the optional modules, I've left these
  103. # commented by default.  Uncomment if you have any problems or if
  104. # you have disabled module autoload.  Note that some modules must
  105. # be loaded by another kernel module.
  106.  
  107. # core netfilter module
  108. /sbin/modprobe ip_tables
  109.  
  110. # the stateful connection tracking module
  111. /sbin/modprobe ip_conntrack
  112.  
  113. # filter table module
  114. # /sbin/modprobe iptable_filter
  115.  
  116. # mangle table module
  117. # /sbin/modprobe iptable_mangle
  118.  
  119. # nat table module
  120. # /sbin/modprobe iptable_nat
  121.  
  122. # LOG target module
  123. # /sbin/modprobe ipt_LOG
  124.  
  125. # This is used to limit the number of packets per sec/min/hr
  126. # /sbin/modprobe ipt_limit
  127.  
  128. # masquerade target module
  129. # /sbin/modprobe ipt_MASQUERADE
  130.  
  131. # filter using owner as part of the match
  132. # /sbin/modprobe ipt_owner
  133.  
  134. # REJECT target drops the packet and returns an ICMP response.
  135. # The response is configurable.  By default, connection refused.
  136. # /sbin/modprobe ipt_REJECT
  137.  
  138. # This target allows packets to be marked in the mangle table
  139. # /sbin/modprobe ipt_mark
  140.  
  141. # This target affects the TCP MSS
  142. # /sbin/modprobe ipt_tcpmss
  143.  
  144. # This match allows multiple ports instead of a single port or range
  145. # /sbin/modprobe multiport
  146.  
  147. # This match checks against the TCP flags
  148. # /sbin/modprobe ipt_state
  149.  
  150. # This match catches packets with invalid flags
  151. # /sbin/modprobe ipt_unclean
  152.  
  153. # The ftp nat module is required for non-PASV ftp support
  154. /sbin/modprobe ip_nat_ftp
  155.  
  156. # the module for full ftp connection tracking
  157. /sbin/modprobe ip_conntrack_ftp
  158.  
  159. # the module for full irc connection tracking
  160. /sbin/modprobe ip_conntrack_irc
  161.  
  162.  
  163. ###############################################################################
  164. #
  165. # Kernel Parameter Configuration
  166. #
  167. # See http://ipsysctl-tutorial.frozentux.net/chunkyhtml/index.html
  168. # for a detailed tutorial on sysctl and the various settings
  169. # available.
  170.  
  171. # Required to enable IPv4 forwarding.
  172. # Redhat users can try setting FORWARD_IPV4 in /etc/sysconfig/network to true
  173. # Alternatively, it can be set in /etc/sysctl.conf
  174. if [ "$SYSCTL" = "" ]
  175. then
  176.     echo "1" > /proc/sys/net/ipv4/ip_forward
  177. else
  178.     $SYSCTL net.ipv4.ip_forward="1"
  179. fi
  180.  
  181. # This enables dynamic address hacking.
  182. # This may help if you have a dynamic IP address \(e.g. slip, ppp, dhcp\).
  183. #if [ "$SYSCTL" = "" ]
  184. #then
  185. #    echo "1" > /proc/sys/net/ipv4/ip_dynaddr
  186. #else
  187. #    $SYSCTL net.ipv4.ip_dynaddr="1"
  188. #fi
  189.  
  190. # This enables SYN flood protection.
  191. # The SYN cookies activation allows your system to accept an unlimited
  192. # number of TCP connections while still trying to give reasonable
  193. # service during a denial of service attack.
  194. if [ "$SYSCTL" = "" ]
  195. then
  196.     echo "1" > /proc/sys/net/ipv4/tcp_syncookies
  197. else
  198.     $SYSCTL net.ipv4.tcp_syncookies="1"
  199. fi
  200.  
  201. # This enables source validation by reversed path according to RFC1812.
  202. # In other words, did the response packet originate from the same interface
  203. # through which the source packet was sent?  It's recommended for single-homed
  204. # systems and routers on stub networks.  Since those are the configurations
  205. # this firewall is designed to support, I turn it on by default.
  206. # Turn it off if you use multiple NICs connected to the same network.
  207. if [ "$SYSCTL" = "" ]
  208. then
  209.     echo "1" > /proc/sys/net/ipv4/conf/all/rp_filter
  210. else
  211.     $SYSCTL net.ipv4.conf.all.rp_filter="1"
  212. fi
  213.  
  214. # This option allows a subnet to be firewalled with a single IP address.
  215. # It's used to build a DMZ.  Since that's not a focus of this firewall
  216. # script, it's not enabled by default, but is included for reference.
  217. # See: http://www.sjdjweis.com/linux/proxyarp/
  218. #if [ "$SYSCTL" = "" ]
  219. #then
  220. #    echo "1" > /proc/sys/net/ipv4/conf/all/proxy_arp
  221. #else
  222. #    $SYSCTL net.ipv4.conf.all.proxy_arp="1"
  223. #fi
  224.  
  225. # The following kernel settings were suggested by Alex Weeks. Thanks!
  226.  
  227. # This kernel parameter instructs the kernel to ignore all ICMP
  228. # echo requests sent to the broadcast address.  This prevents
  229. # a number of smurfs and similar DoS nasty attacks.
  230. if [ "$SYSCTL" = "" ]
  231. then
  232.     echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
  233. else
  234.     $SYSCTL net.ipv4.icmp_echo_ignore_broadcasts="1"
  235. fi
  236.  
  237. # This option can be used to accept or refuse source routed
  238. # packets.  It is usually on by default, but is generally
  239. # considered a security risk.  This option turns it off.
  240. if [ "$SYSCTL" = "" ]
  241. then
  242.     echo "0" > /proc/sys/net/ipv4/conf/all/accept_source_route
  243. else
  244.     $SYSCTL net.ipv4.conf.all.accept_source_route="0"
  245. fi
  246.  
  247. # This option can disable ICMP redirects.  ICMP redirects
  248. # are generally considered a security risk and shouldn't be
  249. # needed by most systems using this generator.
  250. #if [ "$SYSCTL" = "" ]
  251. #then
  252. #    echo "0" > /proc/sys/net/ipv4/conf/all/accept_redirects
  253. #else
  254. #    $SYSCTL net.ipv4.conf.all.accept_redirects="0"
  255. #fi
  256.  
  257. # However, we'll ensure the secure_redirects option is on instead.
  258. # This option accepts only from gateways in the default gateways list.
  259. if [ "$SYSCTL" = "" ]
  260. then
  261.     echo "1" > /proc/sys/net/ipv4/conf/all/secure_redirects
  262. else
  263.     $SYSCTL net.ipv4.conf.all.secure_redirects="1"
  264. fi
  265.  
  266. # This option logs packets from impossible addresses.
  267. if [ "$SYSCTL" = "" ]
  268. then
  269.     echo "1" > /proc/sys/net/ipv4/conf/all/log_martians
  270. else
  271.     $SYSCTL net.ipv4.conf.all.log_martians="1"
  272. fi
  273.  
  274.  
  275. ###############################################################################
  276. #
  277. # Flush Any Existing Rules or Chains
  278. #
  279.  
  280. echo "Flushing Tables ..."
  281.  
  282. # Reset Default Policies
  283. $IPT -P INPUT ACCEPT
  284. $IPT -P FORWARD ACCEPT
  285. $IPT -P OUTPUT ACCEPT
  286. $IPT -t nat -P PREROUTING ACCEPT
  287. $IPT -t nat -P POSTROUTING ACCEPT
  288. $IPT -t nat -P OUTPUT ACCEPT
  289. $IPT -t mangle -P PREROUTING ACCEPT
  290. $IPT -t mangle -P OUTPUT ACCEPT
  291.  
  292. # Flush all rules
  293. $IPT -F
  294. $IPT -t nat -F
  295. $IPT -t mangle -F
  296.  
  297. # Erase all non-default chains
  298. $IPT -X
  299. $IPT -t nat -X
  300. $IPT -t mangle -X
  301.  
  302. if [ "$1" = "stop" ]
  303. then
  304.     echo "Firewall completely flushed!  Now running with no firewall."
  305.     exit 0
  306. fi
  307.  
  308. ###############################################################################
  309. #
  310. # Rules Configuration
  311. #
  312.  
  313. ###############################################################################
  314. #
  315. # Filter Table
  316. #
  317. ###############################################################################
  318.  
  319. # Set Policies
  320.  
  321. $IPT -P INPUT DROP
  322. $IPT -P OUTPUT DROP
  323. $IPT -P FORWARD DROP
  324.  
  325. ###############################################################################
  326. #
  327. # User-Specified Chains
  328. #
  329. # Create user chains to reduce the number of rules each packet
  330. # must traverse.
  331.  
  332. echo "Create and populate custom rule chains ..."
  333.  
  334. # Create a chain to filter INVALID packets
  335.  
  336. $IPT -N bad_packets
  337.  
  338. # Create another chain to filter bad tcp packets
  339.  
  340. $IPT -N bad_tcp_packets
  341.  
  342. # Create separate chains for icmp, tcp (incoming and outgoing),
  343. # and incoming udp packets.
  344.  
  345. $IPT -N icmp_packets
  346.  
  347. # Used for UDP packets inbound from the Internet
  348. $IPT -N udp_inbound
  349.  
  350. # Used to block outbound UDP services from internal network
  351. # Default to allow all
  352. $IPT -N udp_outbound
  353.  
  354. # Used to allow inbound services if desired
  355. # Default fail except for established sessions
  356. $IPT -N tcp_inbound
  357.  
  358. # Used to block outbound services from internal network
  359. # Default to allow all
  360. $IPT -N tcp_outbound
  361.  
  362. ###############################################################################
  363. #
  364. # Populate User Chains
  365. #
  366.  
  367. # bad_packets chain
  368. #
  369.  
  370. # Drop INVALID packets immediately
  371. $IPT -A bad_packets -p ALL -m state --state INVALID -j LOG \
  372.     --log-prefix "Invalid packet: "
  373.  
  374. $IPT -A bad_packets -p ALL -m state --state INVALID -j DROP
  375.  
  376. # Then check the tcp packets for additional problems
  377. $IPT -A bad_packets -p tcp -j bad_tcp_packets
  378.  
  379. # All good, so return
  380. $IPT -A bad_packets -p ALL -j RETURN
  381.  
  382. # bad_tcp_packets chain
  383. #
  384. # All tcp packets will traverse this chain.
  385. # Every new connection attempt should begin with
  386. # a syn packet.  If it doesn't, it is likely a
  387. # port scan.  This drops packets in state
  388. # NEW that are not flagged as syn packets.
  389.  
  390.  
  391. $IPT -A bad_tcp_packets -p tcp ! --syn -m state --state NEW -j LOG \
  392.     --log-prefix "New not syn: "
  393. $IPT -A bad_tcp_packets -p tcp ! --syn -m state --state NEW -j DROP
  394.  
  395. $IPT -A bad_tcp_packets -p tcp --tcp-flags ALL NONE -j LOG \
  396.     --log-prefix "Stealth scan: "
  397. $IPT -A bad_tcp_packets -p tcp --tcp-flags ALL NONE -j DROP
  398.  
  399. $IPT -A bad_tcp_packets -p tcp --tcp-flags ALL ALL -j LOG \
  400.     --log-prefix "Stealth scan: "
  401. $IPT -A bad_tcp_packets -p tcp --tcp-flags ALL ALL -j DROP
  402.  
  403. $IPT -A bad_tcp_packets -p tcp --tcp-flags ALL FIN,URG,PSH -j LOG \
  404.     --log-prefix "Stealth scan: "
  405. $IPT -A bad_tcp_packets -p tcp --tcp-flags ALL FIN,URG,PSH -j DROP
  406.  
  407. $IPT -A bad_tcp_packets -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j LOG \
  408.     --log-prefix "Stealth scan: "
  409. $IPT -A bad_tcp_packets -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP
  410.  
  411. $IPT -A bad_tcp_packets -p tcp --tcp-flags SYN,RST SYN,RST -j LOG \
  412.     --log-prefix "Stealth scan: "
  413. $IPT -A bad_tcp_packets -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
  414.  
  415. $IPT -A bad_tcp_packets -p tcp --tcp-flags SYN,FIN SYN,FIN -j LOG \
  416.     --log-prefix "Stealth scan: "
  417. $IPT -A bad_tcp_packets -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
  418.  
  419. # All good, so return
  420. $IPT -A bad_tcp_packets -p tcp -j RETURN
  421.  
  422. # icmp_packets chain
  423. #
  424. # This chain is for inbound (from the Internet) icmp packets only.
  425. # Type 8 (Echo Request) is not accepted by default
  426. # Enable it if you want remote hosts to be able to reach you.
  427. # 11 (Time Exceeded) is the only one accepted
  428. # that would not already be covered by the established
  429. # connection rule.  Applied to INPUT on the external interface.
  430. #
  431. # See: http://www.ee.siue.edu/~rwalden/networking/icmp.html
  432. # for more info on ICMP types.
  433. #
  434. # Note that the stateful settings allow replies to ICMP packets.
  435. # These rules allow new packets of the specified types.
  436.  
  437. # ICMP packets should fit in a Layer 2 frame, thus they should
  438. # never be fragmented.  Fragmented ICMP packets are a typical sign
  439. # of a denial of service attack.
  440. $IPT -A icmp_packets --fragment -p ICMP -j LOG \
  441.     --log-prefix "ICMP Fragment: "
  442. $IPT -A icmp_packets --fragment -p ICMP -j DROP
  443.  
  444. # Echo - uncomment to allow your system to be pinged.
  445. # Uncomment the LOG command if you also want to log PING attempts
  446. #
  447. # $IPT -A icmp_packets -p ICMP -s 0/0 --icmp-type 8 -j LOG \
  448. #    --log-prefix "Ping detected: "
  449. # $IPT -A icmp_packets -p ICMP -s 0/0 --icmp-type 8 -j ACCEPT
  450.  
  451. # By default, however, drop pings without logging. Blaster
  452. # and other worms have infected systems blasting pings.
  453. # Comment the line below if you want pings logged, but it
  454. # will likely fill your logs.
  455. $IPT -A icmp_packets -p ICMP -s 0/0 --icmp-type 8 -j DROP
  456.  
  457. # Time Exceeded
  458. $IPT -A icmp_packets -p ICMP -s 0/0 --icmp-type 11 -j ACCEPT
  459.  
  460. # Not matched, so return so it will be logged
  461. $IPT -A icmp_packets -p ICMP -j RETURN
  462.  
  463. # TCP & UDP
  464. # Identify ports at:
  465. #    http://www.chebucto.ns.ca/~rakerman/port-table.html
  466. #    http://www.iana.org/assignments/port-numbers
  467.  
  468. # udp_inbound chain
  469. #
  470. # This chain describes the inbound UDP packets it will accept.
  471. # It's applied to INPUT on the external or Internet interface.
  472. # Note that the stateful settings allow replies.
  473. # These rules are for new requests.
  474. # It drops netbios packets (windows) immediately without logging.
  475.  
  476. # Drop netbios calls
  477. # Please note that these rules do not really change the way the firewall
  478. # treats netbios connections.  Connections from the localhost and
  479. # internal interface (if one exists) are accepted by default.
  480. # Responses from the Internet to requests initiated by or through
  481. # the firewall are also accepted by default.  To get here, the
  482. # packets would have to be part of a new request received by the
  483. # Internet interface.  You would have to manually add rules to
  484. # accept these.  I added these rules because some network connections,
  485. # such as those via cable modems, tend to be filled with noise from
  486. # unprotected Windows machines.  These rules drop those packets
  487. # quickly and without logging them.  This prevents them from traversing
  488. # the whole chain and keeps the log from getting cluttered with
  489. # chatter from Windows systems.
  490. $IPT -A udp_inbound -p UDP -s 0/0 --destination-port 137 -j DROP
  491. $IPT -A udp_inbound -p UDP -s 0/0 --destination-port 138 -j DROP
  492.  
  493. # User specified allowed UDP protocol
  494. $IPT -A udp_inbound -p UDP -s 0/0 --destination-port 1194 -j ACCEPT
  495.  
  496.  
  497. # Not matched, so return for logging
  498. $IPT -A udp_inbound -p UDP -j RETURN
  499.  
  500. # udp_outbound chain
  501. #
  502. # This chain is used with a private network to prevent forwarding for
  503. # UDP requests on specific protocols.  Applied to the FORWARD rule from
  504. # the internal network.  Ends with an ACCEPT
  505.  
  506. # No match, so ACCEPT
  507. $IPT -A udp_outbound -p UDP -s 0/0 -j ACCEPT
  508.  
  509. # tcp_inbound chain
  510. #
  511. # This chain is used to allow inbound connections to the
  512. # system/gateway.  Use with care.  It defaults to none.
  513. # It's applied on INPUT from the external or Internet interface.
  514.  
  515. # sshd
  516. $IPT -A tcp_inbound -p TCP -s 0/0 --destination-port 22 -j ACCEPT
  517.  
  518. # User specified allowed UDP protocol
  519. $IPT -A tcp_inbound -p TCP -s 0/0 --destination-port 1194 -j ACCEPT
  520. $IPT -A tcp_inbound -p TCP -s 0/0 --destination-port 10000 -j ACCEPT
  521.  
  522.  
  523. # Not matched, so return so it will be logged
  524. $IPT -A tcp_inbound -p TCP -j RETURN
  525.  
  526. # tcp_outbound chain
  527. #
  528. # This chain is used with a private network to prevent forwarding for
  529. # requests on specific protocols.  Applied to the FORWARD rule from
  530. # the internal network.  Ends with an ACCEPT
  531.  
  532.  
  533. # No match, so ACCEPT
  534. $IPT -A tcp_outbound -p TCP -s 0/0 -j ACCEPT
  535.  
  536. ###############################################################################
  537. #
  538. # INPUT Chain
  539. #
  540.  
  541. echo "Process INPUT chain ..."
  542.  
  543. # Allow all on localhost interface
  544. $IPT -A INPUT -p ALL -i $LO_IFACE -j ACCEPT
  545.  
  546. # Drop bad packets
  547. $IPT -A INPUT -p ALL -j bad_packets
  548.  
  549. # DOCSIS compliant cable modems
  550. # Some DOCSIS compliant cable modems send IGMP multicasts to find
  551. # connected PCs.  The multicast packets have the destination address
  552. # 224.0.0.1.  You can accept them.  If you choose to do so,
  553. # Uncomment the rule to ACCEPT them and comment the rule to DROP
  554. # them  The firewall will drop them here by default to avoid
  555. # cluttering the log.  The firewall will drop all multicasts
  556. # to the entire subnet (224.0.0.1) by default.  To only affect
  557. # IGMP multicasts, change '-p ALL' to '-p 2'.  Of course,
  558. # if they aren't accepted elsewhere, it will only ensure that
  559. # multicasts on other protocols are logged.
  560. # Drop them without logging.
  561. $IPT -A INPUT -p ALL -d 224.0.0.1 -j DROP
  562. # The rule to accept the packets.
  563. # $IPT -A INPUT -p ALL -d 224.0.0.1 -j ACCEPT
  564.  
  565.  
  566. # Inbound Internet Packet Rules
  567.  
  568. # Accept Established Connections
  569. $IPT -A INPUT -p ALL -i $INET_IFACE -m state --state ESTABLISHED,RELATED \
  570.      -j ACCEPT
  571.  
  572. # Route the rest to the appropriate user chain
  573. $IPT -A INPUT -p TCP -i $INET_IFACE -j tcp_inbound
  574. $IPT -A INPUT -p UDP -i $INET_IFACE -j udp_inbound
  575. $IPT -A INPUT -p ICMP -i $INET_IFACE -j icmp_packets
  576.  
  577. # Drop without logging broadcasts that get this far.
  578. # Cuts down on log clutter.
  579. # Comment this line if testing new rules that impact
  580. # broadcast protocols.
  581. $IPT -A INPUT -m pkttype --pkt-type broadcast -j DROP
  582.  
  583. # Log packets that still don't match
  584. $IPT -A INPUT -m limit --limit 3/minute --limit-burst 3 -j LOG \
  585.     --log-prefix "INPUT packet died: "
  586.  
  587. ###############################################################################
  588. #
  589. # FORWARD Chain
  590. #
  591.  
  592. echo "Process FORWARD chain ..."
  593.  
  594. # Used if forwarding for a private network
  595.  
  596. # Use the 'state' module to ACCEPT RELATED or ESTABLISHED FORWARDed
  597. # connections
  598. iptables -A FORWARD -i eth0 -o tun0 -m state --state ESTABLISHED,RELATED -j ACCEPT
  599.  
  600. # ACCEPT 10.8.0.0-10.8.0.255 FORWARDed connections
  601. iptables -A FORWARD -s 10.8.0.0/8 -o eth0 -j ACCEPT
  602.  
  603. # REJECT all other FORWARDed connections
  604. iptables -A FORWARD -j REJECT
  605.  
  606. ###############################################################################
  607. #
  608. # OUTPUT Chain
  609. #
  610.  
  611. echo "Process OUTPUT chain ..."
  612.  
  613. # Generally trust the firewall on output
  614.  
  615. # However, invalid icmp packets need to be dropped
  616. # to prevent a possible exploit.
  617. $IPT -A OUTPUT -m state -p icmp --state INVALID -j DROP
  618.  
  619. # Localhost
  620. $IPT -A OUTPUT -p ALL -s $LO_IP -j ACCEPT
  621. $IPT -A OUTPUT -p ALL -o $LO_IFACE -j ACCEPT
  622.  
  623. # To internet
  624. $IPT -A OUTPUT -p ALL -o $INET_IFACE -j ACCEPT
  625.  
  626. # Log packets that still don't match
  627. $IPT -A OUTPUT -m limit --limit 3/minute --limit-burst 3 -j LOG \
  628.     --log-prefix "OUTPUT packet died: "
  629.  
  630. ###############################################################################
  631. #
  632. # nat table
  633. #
  634. ###############################################################################
  635.  
  636. # The nat table is where network address translation occurs if there
  637. # is a private network.  If the gateway is connected to the Internet
  638. # with a static IP, snat is used.  If the gateway has a dynamic address,
  639. # masquerade must be used instead.  There is more overhead associated
  640. # with masquerade, so snat is better when it can be used.
  641. # The nat table has a builtin chain, PREROUTING, for dnat and redirects.
  642. # Another, POSTROUTING, handles snat and masquerade.
  643.  
  644. echo "Load rules for nat table ..."
  645.  
  646. # MASQUERADE any VPN addresses going out
  647. iptables -t nat -A POSTROUTING -s 10.8.0.0/8 ! -d 10.8.0.0/8 -o eth0 -j MASQUERADE
  648.  
  649. ###############################################################################
  650. #
  651. # PREROUTING chain
  652. #
  653.  
  654.  
  655. ###############################################################################
  656. #
  657. # POSTROUTING chain
  658. #
  659.  
  660.  
  661. ###############################################################################
  662. #
  663. # mangle table
  664. #
  665. ###############################################################################
  666.  
  667. # The mangle table is used to alter packets.  It can alter or mangle them in
  668. # several ways.  For the purposes of this generator, we only use its ability
  669. # to alter the TTL in packets.  However, it can be used to set netfilter
  670. # mark values on specific packets.  Those marks could then be used in another
  671. # table like filter, to limit activities associated with a specific host, for
  672. # instance.  The TOS target can be used to set the Type of Service field in
  673. # the IP header.  Note that the TTL target might not be included in the
  674. # distribution on your system.  If it is not and you require it, you will
  675. # have to add it.  That may require that you build from source.
  676.  
  677. echo "Load rules for mangle table ..."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement