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