Previous |
Home | Next |
Many of the newer
broadband modems provide built-in firewall
features that allow for stateful packet inspection and detailed network
address translations, which are capable of providing a high security
level for your internal network. |
[bash]# echo 1 >
/proc/sys/net/ipv4/ip_forward |
[bash]# echo 0 >
/proc/sys/net/ipv4/ip_forward |
[bash]# vi
/etc/sysctl.conf |
net.ipv4.ip_forward = 1 |
It is common practice
for users to have manual control over packet
forwarding, and to active or disable the function within their firewall
control scripts. However setting packet forwarding to start
automatically will be more suitable for a dedicated server. |
Table
Name |
Chain
Name |
Chain
Details |
filter |
INPUT |
For any packet coming
into the system |
FORWARD |
For any packet that is
being routed through the system |
|
OUTPUT |
For any packet that is
leaving the system |
[bash]# iptables -t
filter -nvL |
[bash]# iptables -nvL |
Chain INPUT (policy
ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination |
[bash]# /etc/init.d/iptables
stop |
01- [bash]# iptables -P
INPUT DROP 02- [bash]# iptables -P FORWARD DROP 03- [bash]# iptables -P OUTPUT DROP 04- [bash]# iptables -A INPUT -i lo -j ACCEPT 05- [bash]# iptables -A OUTPUT -o lo -j ACCEPT 06- [bash]# iptables -A INPUT -i ppp0 -p tcp --sport 80 -j ACCEPT 07- [bash]# iptables -A OUTPUT -o ppp0 -p tcp --dport 80 -j ACCEPT 08- [bash]# iptables -A INPUT -i eth1 -s 192.168.1.0/24 -p tcp --dport 3128 -j ACCEPT 09- [bash]# iptables -A OUTPUT -o eth1 -d 192.168.1.0/24 -p tcp --sport 3128 -j ACCEPT [bash]# iptables -nvL |
01- 04- 06- 08- 02- 03- 05- 07- 09- |
Chain INPUT (policy DROP
0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT tcp -- ppp0 * 0.0.0.0/0 0.0.0.0/0 tcp spt:80 0 0 ACCEPT tcp -- eth1 * 192.168.1.0/24 0.0.0.0/0 tcp dpt:3128 Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT all -- * lo 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT tcp -- * ppp0 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 0 0 ACCEPT tcp -- * eth1 0.0.0.0/0 192.168.1.0/24 tcp spt:3128 |
[bash]# iptables -I
INPUT 2 -i ppp0 -p tcp --dport ftp
-j ACCEPT [bash]# iptables -I OUTPUT 2 -o ppp0 -p tcp --sport ftp -j ACCEPT [bash]# iptables -D INPUT -i ppp0 -p tcp --sport 80 -j ACCEPT [bash]# iptables -D OUTPUT -o ppp0 -p tcp --dport 80 -j ACCEPT [bash]# iptables -A INPUT -p icmp --icmp-type any -j ACCEPT [bash]# iptables -A OUTPUT -p icmp --icmp-type any -j ACCEPT [bash]# iptables -I INPUT -m state --state INVALID -j LOG --log-prefix "INVALID Input: " [bash]# iptables -I INPUT -m state --state INVALID -j DROP [bash]# iptables -F [bash]# /etc/init.d/iptables restart |
Table
Name |
Chain
Name |
Chain
Details |
nat |
PREROUTING |
For altering packets as
they are entering the system (before filter INPUT) |
POSTROUTING |
For altering packets as
they are exiting the system (after filter OUTPUT) |
|
OUTPUT |
For altering
packets before leaving the local system (before the routing table) |
[bash]# iptables -t nat -nvL |
01- [bash]# iptables -P
INPUT ACCEPT 02- [bash]# iptables -P FORWARD DROP 03- [bash]# iptables -P OUTPUT ACCEPT 04- [bash]# iptables -A FORWARD -i eth1 -o ppp0 -s 192.168.1.0/24 -p tcp --dport 80 -j ACCEPT 05- [bash]# iptables -A FORWARD -i ppp0 -o eth1 -d 192.168.1.0/24 -p tcp --sport 80 -j ACCEPT 06- [bash]# iptables -t nat -A POSTROUTING -o ppp0 -s 192.168.1.0/24 -j SNAT --to-source 123.123.123.2 07- [bash]# echo 1 > /proc/sys/net/ipv4/ip_forward [bash]# iptables -nvL ; iptables -t nat -nvL |
01- 02- 04- 05- 03- 06- |
Chain INPUT (policy
ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT tcp -- eth1 ppp0 192.168.1.0/24 0.0.0.0/0 tcp dpt:80 0 0 ACCEPT tcp -- ppp0 eth1 0.0.0.0/0 192.168.1.0/24 tcp spt:80 Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 SNAT all -- * ppp0 192.168.1.0/24 0.0.0.0/0 to:123.123.123.2 Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination |
01- [bash]# iptables -P
INPUT ACCEPT 02- [bash]# iptables -P FORWARD DROP 03- [bash]# iptables -P OUTPUT ACCEPT 04- [bash]# iptables -A FORWARD -i ppp0 -m state --state ESTABLISHED,RELATED -j ACCEPT 05- [bash]# iptables -A FORWARD -i eth1 -o ppp0 -s 192.168.1.0/24 -j ACCEPT 06- [bash]# iptables -t nat -A POSTROUTING -o ppp0 -s 192.168.1.0/24 -j MASQUERADE 07- [bash]# echo 1 > /proc/sys/net/ipv4/ip_forward [bash]# iptables -nvL ; iptables -t nat -nvL |
01- 02- 04- 05- 03- 06- |
Chain INPUT (policy
ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT all -- ppp0 * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 0 0 ACCEPT tcp -- eth1 ppp0 192.168.1.0/24 0.0.0.0/0 Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 MASQUERADE all -- * ppp0 192.168.1.0/24 0.0.0.0/0 Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination |
[bash]# iptables -t nat -A POSTROUTING -j MASQUERADE |
WARNING: The above rule
is dangerous, it allows masquerading
in both directions. |
01- [bash]# iptables -P
INPUT ACCEPT 02- [bash]# iptables -P FORWARD DROP 03- [bash]# iptables -P OUTPUT ACCEPT 04- [bash]# iptables -A FORWARD -i eth1 -o ppp0 -s 192.168.1.0/24 -j ACCEPT 05- [bash]# iptables -A FORWARD -i ppp0 -o eth1 -p tcp --dport 80 -j ACCEPT 06- [bash]# iptables -t nat -A PREROUTING -i ppp0 -p tcp --dport 80 -j DNAT --to-destination 192.168.1.2:80 07- [bash]# echo 1 > /proc/sys/net/ipv4/ip_forward [bash]# iptables -nvL ; iptables -t nat -nvL |
01- 02- 04- 05- 03- 06- |
Chain INPUT (policy
ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT all -- eth1 ppp0 192.168.1.0/24 0.0.0.0/0 0 0 ACCEPT tcp -- ppp0 eth1 0.0.0.0/0 192.168.1.0/24 tcp dpt:80 Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 DNAT tcp -- ppp0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 to:192.168.1.2:80 Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination |
[bash]# vi /root/firewall.sh |
#!/bin/sh # # Example Firewall Script ############################################################### ### Define interfaces here EXT_DEV=ppp0 INT_DEV=eth1 INT_NET=192.168.1.0/24 ### Loading firewall modules modprobe ip_conntrack modprobe ip_conntrack_ftp ############################################################### ### Enable Packet Forwarding echo 1 > /proc/sys/net/ipv4/ip_forward ### Remove all previous rules, and delete any user defined chains iptables -F iptables -X iptables -t nat -F iptables -t nat -X ### Set the default policies to drop iptables -P INPUT DROP iptables -P OUTPUT DROP iptables -P FORWARD DROP ### Loopback device OK iptables -A INPUT -i lo -s 127.0.0.0/8 -d 127.0.0.0/8 -j ACCEPT iptables -A OUTPUT -o lo -s 127.0.0.0/8 -d 127.0.0.0/8 -j ACCEPT ### Allow all ICMP Traffic (optional) - IN, OUT and THROUGH. iptables -A INPUT -p icmp --icmp-type any -j ACCEPT iptables -A OUTPUT -p icmp --icmp-type any -j ACCEPT iptables -A FORWARD -p icmp --icmp-type any -j ACCEPT ### Allow all Internal traffic to Server iptables -A INPUT -i $INT_DEV -s $INT_NET -d $INT_NET -j ACCEPT iptables -A OUTPUT -o $INT_DEV -s $INT_NET -d $INT_NET -j ACCEPT ############################################################### ### OUTBOUND Rule: Allow ALL packets out the external device iptables -A OUTPUT -o $EXT_DEV -j ACCEPT iptables -A FORWARD -i $INT_DEV -o $EXT_DEV -j ACCEPT ############################################################### ### MASQUERADING: All packets from the internal network will ### appear as if they had originated from the firewall. iptables -t nat -A POSTROUTING -o $EXT_DEV -s $INT_NET -j MASQUERADE ############################################################### ### INBOUND Rule: Allow ALL EXT packets if a connection already exists (See "NEW" Inbound Rules) iptables -A INPUT -i $EXT_DEV -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -A FORWARD -i $EXT_DEV -m state --state RELATED,ESTABLISHED -j ACCEPT # ### INBOUND Rules: Allow ONLY NEW packets on these ports. # # New INBOUND Connection: FTP (with TLS) iptables -A INPUT -i $EXT_DEV -m state --state NEW -m tcp -p tcp --syn --dport 20 -j ACCEPT iptables -A INPUT -i $EXT_DEV -m state --state NEW -m tcp -p tcp --syn --dport 21 -j ACCEPT # New INBOUND Connection: Secure Shell iptables -A INPUT -i $EXT_DEV -m state --state NEW -m tcp -p tcp --syn --dport 22 -j ACCEPT # New INBOUND Connection: SMTP and SMTPS (over TLS/SSL) iptables -A INPUT -i $EXT_DEV -m state --state NEW -m tcp -p tcp --syn --dport 25 -j ACCEPT iptables -A INPUT -i $EXT_DEV -m state --state NEW -m tcp -p tcp --syn --dport 465 -j ACCEPT # New INBOUND Connection: HTTP (Plain and SSL) iptables -A INPUT -i $EXT_DEV -m state --state NEW -m tcp -p tcp --syn --dport 80 -j ACCEPT iptables -A INPUT -i $EXT_DEV -m state --state NEW -m tcp -p tcp --syn --dport 443 -j ACCEPT # New INBOUND Connection: LDAPS Server (over SSL) iptables -A INPUT -i $EXT_DEV -m state --state NEW -m tcp -p tcp --syn --dport 636 -j ACCEPT # New INBOUND Connection: IMAPS Email Clients (over SSL) iptables -A INPUT -i $EXT_DEV -m state --state NEW -m tcp -p tcp --syn --dport 993 -j ACCEPT ### # Squid Transparent Proxy: Enable rule for transparent proxy redirection # Redirect all WWW (port 80) OUTBOUNT packets to the Squid Server on port 3128 #iptables -t nat -A PREROUTING -i $INT_DEV -s $INT_NET -p tcp --dport 80 -j REDIRECT --to-port 3128 # ### INBOUND DNAT (redirection) Rules: Allow ONLY NEW packets on these ports and redirect to internal services. # ### INBOUND Rule: Redirect ALL packets to the INTERNAL workstation - HTTP #iptables -t nat -A PREROUTING -i $EXT_DEV -p tcp --dport 80 -j DNAT --to-destination wkstn1.example.com:80 #iptables -A FORWARD -i $EXT_DEV -o $INT_DEV -p tcp --dport 80 -j ACCEPT ### INBOUND Rule: Redirect ALL packets to the INTERNAL workstation - HTTPS #iptables -t nat -A PREROUTING -i $EXT_DEV -p tcp --dport 443 -j DNAT --to-destination wkstn1.example.com:443 #iptables -A FORWARD -i $EXT_DEV -o $INT_DEV -p tcp --dport 443 -j ACCEPT |
[bash]# sh /root/firewall.sh [bash]# /etc/init.d/iptables save |
[bash]# /etc/init.d/iptables
restart [bash]# iptables -nvL ; iptables -t nat -nvL |
[bash]# vi
/etc/init.d/iptables |
IPTABLES_MODULES="ip_conntrack
ip_conntrack_ftp" |
[bash]# lsmod |
Previous |
Home | Next |