个人工具

“UbuntuHelp:Router/Firewall”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
(创建新页面为 '{{From|https://help.ubuntu.com/community/Router/Firewall}} {{Languages|UbuntuHelp:Router/Firewall}} == Basic == Install the Uncomplicated Firewall, package name is '''ufw'''. Th...')
 
 
(未显示同一用户的1个中间版本)
第2行: 第2行:
 
{{Languages|UbuntuHelp:Router/Firewall}}
 
{{Languages|UbuntuHelp:Router/Firewall}}
 
== Basic ==
 
== Basic ==
Install the Uncomplicated Firewall, package name is '''ufw'''.
+
Install the Uncomplicated Firewall, package name is '''ufw'''. Uncomplicated firewall just sets up
There is a graphical interface to this programme, called '''gufw'''
+
iptables using a simple syntax, or an extended syntax based on OpenBSD's PF. To use ```ufw``` for
 +
routing, you must know iptables and should edit the files in /etc/ufw/*.rules.
 
== Advanced ==
 
== Advanced ==
Uncomplicated firewall just sets up iptables using plain English.
+
The following is a specific example of a firewall script using only iptables.
The following is a specific example of a firewall script.
+
 
<pre><nowiki>
 
<pre><nowiki>
 
#!/bin/sh
 
#!/bin/sh
 
IPTABLES=/sbin/iptables
 
AWK=/usr/bin/awk
 
IFCONFIG=/sbin/ifconfig
 
 
  
 
# External (Internet-facing) interface
 
# External (Internet-facing) interface
第19行: 第14行:
  
 
# External IP address (automatically detected)
 
# External IP address (automatically detected)
EXTIP="`$IFCONFIG $EXTIF | $AWK /$EXTIF/'{next}//{split($0,a,":");split(a[2],a," ");print a[1];exit}'`"
+
EXTIP=$(/sbin/ip addr show dev "$EXTIF" | perl -lne 'if(/inet (\S+)/){print$1;last}');
 
   
 
   
 
# Internal interface
 
# Internal interface
第44行: 第39行:
  
  
# Clear any existing rules and set the default policy to DROP
+
/sbin/iptables-restore <<-EOF;
$IPTABLES -P INPUT DROP
+
$IPTABLES -F INPUT
+
$IPTABLES -P OUTPUT DROP
+
$IPTABLES -F OUTPUT
+
$IPTABLES -P FORWARD DROP
+
$IPTABLES -F FORWARD
+
$IPTABLES -F -t nat
+
  
# Delete all User-specified chains
+
*filter
$IPTABLES -X
+
:INPUT DROP [0:0]
 
+
:FORWARD DROP [0:0]
# Reset all IPTABLES counters
+
:OUTPUT DROP [0:0]
$IPTABLES -Z
+
  
 
# INPUT: Incoming traffic from various interfaces #
 
# INPUT: Incoming traffic from various interfaces #
  
 
# Loopback interface is valid
 
# Loopback interface is valid
$IPTABLES -A INPUT -i lo -s $UNIVERSE -d $UNIVERSE -j ACCEPT
+
-A INPUT -i lo -s $UNIVERSE -d $UNIVERSE -j ACCEPT
  
  
 
# Local interface, local machines, going anywhere is valid
 
# Local interface, local machines, going anywhere is valid
$IPTABLES -A INPUT -i $INTIF -s $INTNET -d $UNIVERSE -j ACCEPT
+
-A INPUT -i $INTIF -s $INTNET -d $UNIVERSE -j ACCEPT
  
  
 
# Remote interface, claiming to be local machines, IP spoofing, get lost
 
# Remote interface, claiming to be local machines, IP spoofing, get lost
$IPTABLES -A INPUT -i $EXTIF -s $INTNET -d $UNIVERSE -j REJECT
+
-A INPUT -i $EXTIF -s $INTNET -d $UNIVERSE -j REJECT
  
  
 
# External interface, from any source, for ICMP traffic is valid
 
# External interface, from any source, for ICMP traffic is valid
$IPTABLES -A INPUT -i $EXTIF -p ICMP -s $UNIVERSE -d $EXTIP -j ACCEPT
+
-A INPUT -i $EXTIF -p ICMP -s $UNIVERSE -d $EXTIP -j ACCEPT
  
  
 
# Allow any related traffic coming back to the MASQ server in.
 
# Allow any related traffic coming back to the MASQ server in.
$IPTABLES -A INPUT -i $EXTIF -s $UNIVERSE -d $EXTIP -m state --state ESTABLISHED,RELATED -j ACCEPT
+
-A INPUT -i $EXTIF -s $UNIVERSE -d $EXTIP -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
  
  
 
# Internal interface, DHCP traffic accepted
 
# Internal interface, DHCP traffic accepted
$IPTABLES -A INPUT -i $INTIF -p tcp --sport 68 --dport 67 -j ACCEPT
+
-A INPUT -i $INTIF -p tcp --sport 68 --dport 67 -j ACCEPT
$IPTABLES -A INPUT -i $INTIF -p udp --sport 68 --dport 67 -j ACCEPT
+
-A INPUT -i $INTIF -p udp --sport 68 --dport 67 -j ACCEPT
  
  
 
# External interface, HTTP/HTTPS traffic allowed
 
# External interface, HTTP/HTTPS traffic allowed
$IPTABLES -A INPUT -i $EXTIF -m state --state NEW,ESTABLISHED,RELATED -p tcp -s $UNIVERSE -d $EXTIP --dport 80 -j ACCEPT
+
-A INPUT -i $EXTIF -m conntrack --ctstate NEW,ESTABLISHED,RELATED -p tcp -s $UNIVERSE -d $EXTIP --dport 80 -j ACCEPT
$IPTABLES -A INPUT -i $EXTIF -m state --state NEW,ESTABLISHED,RELATED -p tcp -s $UNIVERSE -d $EXTIP --dport 443 -j ACCEPT
+
-A INPUT -i $EXTIF -m conntrack --ctstate NEW,ESTABLISHED,RELATED -p tcp -s $UNIVERSE -d $EXTIP --dport 443 -j ACCEPT
  
 
# External interface, SSH traffic allowed
 
# External interface, SSH traffic allowed
$IPTABLES -A INPUT -i $EXTIF -m state --state NEW,ESTABLISHED,RELATED -p tcp -s $UNIVERSE -d $EXTIP --dport 22 -j ACCEPT
+
-A INPUT -i $EXTIF -m conntrack --ctstate NEW,ESTABLISHED,RELATED -p tcp -s $UNIVERSE -d $EXTIP --dport 22 -j ACCEPT
  
 +
# Accept port 1234 to be forwarded (this rule needs to correspond with PREROUTING rules in NAT table)
 +
#-A FORWARD -i $EXTIF -o $INTIF -p tcp --dport 1234 -m conntrack --ctstate NEW,ESTABLISHED,RELATED -j ACCEPT
  
 
# Catch-all rule, reject anything else
 
# Catch-all rule, reject anything else
$IPTABLES -A INPUT -s $UNIVERSE -d $UNIVERSE -j REJECT
+
-A INPUT -s $UNIVERSE -d $UNIVERSE -j REJECT
  
  
第101行: 第90行:
  
 
# Workaround bug in netfilter
 
# Workaround bug in netfilter
$IPTABLES -A OUTPUT -m state -p icmp --state INVALID -j DROP
+
-A OUTPUT -m conntrack -p icmp --ctstate INVALID -j DROP
  
 
# Loopback interface is valid.
 
# Loopback interface is valid.
$IPTABLES -A OUTPUT -o lo -s $UNIVERSE -d $UNIVERSE -j ACCEPT
+
-A OUTPUT -o lo -s $UNIVERSE -d $UNIVERSE -j ACCEPT
  
  
 
# Local interfaces, any source going to local net is valid
 
# Local interfaces, any source going to local net is valid
$IPTABLES -A OUTPUT -o $INTIF -s $EXTIP -d $INTNET -j ACCEPT
+
-A OUTPUT -o $INTIF -s $EXTIP -d $INTNET -j ACCEPT
  
  
 
# local interface, MASQ server source going to the local net is valid
 
# local interface, MASQ server source going to the local net is valid
$IPTABLES -A OUTPUT -o $INTIF -s $INTIP -d $INTNET -j ACCEPT
+
-A OUTPUT -o $INTIF -s $INTIP -d $INTNET -j ACCEPT
  
  
 
# outgoing to local net on remote interface, stuffed routing, deny
 
# outgoing to local net on remote interface, stuffed routing, deny
$IPTABLES -A OUTPUT -o $EXTIF -s $UNIVERSE -d $INTNET -j REJECT
+
-A OUTPUT -o $EXTIF -s $UNIVERSE -d $INTNET -j REJECT
  
  
 
# anything else outgoing on remote interface is valid
 
# anything else outgoing on remote interface is valid
$IPTABLES -A OUTPUT -o $EXTIF -s $EXTIP -d $UNIVERSE -j ACCEPT
+
-A OUTPUT -o $EXTIF -s $EXTIP -d $UNIVERSE -j ACCEPT
  
  
 
# Internal interface, DHCP traffic accepted
 
# Internal interface, DHCP traffic accepted
$IPTABLES -A OUTPUT -o $INTIF -p tcp -s $INTIP --sport 67 -d 255.255.255.255 --dport 68 -j ACCEPT
+
-A OUTPUT -o $INTIF -p tcp -s $INTIP --sport 67 -d 255.255.255.255 --dport 68 -j ACCEPT
$IPTABLES -A OUTPUT -o $INTIF -p udp -s $INTIP --sport 67 -d 255.255.255.255 --dport 68 -j ACCEPT
+
-A OUTPUT -o $INTIF -p udp -s $INTIP --sport 67 -d 255.255.255.255 --dport 68 -j ACCEPT
  
  
 
# Catch all rule, all other outgoing is denied and logged.  
 
# Catch all rule, all other outgoing is denied and logged.  
$IPTABLES -A OUTPUT -s $UNIVERSE -d $UNIVERSE -j REJECT
+
-A OUTPUT -s $UNIVERSE -d $UNIVERSE -j REJECT
  
 +
# Accept solicited tcp packets
 +
-A FORWARD -i $EXTIF -o $INTIF -m conntrack --ctstate ESTABLISHED,RELATED  -j ACCEPT
  
# Packet Forwarding / NAT #
+
# Allow packets across the internal interface
 +
-A FORWARD -i $INTIF -o $INTIF -j ACCEPT
  
# ----- Begin OPTIONAL FORWARD Section -----
+
# Forward packets from the internal network to the Internet
 +
-A FORWARD -i $INTIF -o $EXTIF -j ACCEPT
  
#Optionally forward incoming tcp connections on port 1234 to 192.168.0.100
+
# Catch-all REJECT rule
#$IPTABLES -A FORWARD -i $EXTIF -o $INTIF -p tcp --dport 1234 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
+
-A FORWARD -j REJECT
#$IPTABLES -A PREROUTING -t nat -p tcp -d $EXTIP --dport 1234 -m state --state NEW,ESTABLISHED,RELATED -j DNAT --to 192.168.0.100:1234
+
  
# ----- End OPTIONAL FORWARD Section -----
+
COMMIT
  
  
# Accept solicited tcp packets
+
# Address translations (only; there is no actual forwarding done here)
$IPTABLES -A FORWARD -i $EXTIF -o $INTIF -m state --state ESTABLISHED,RELATED  -j ACCEPT
+
*nat
 +
:PREROUTING ACCEPT [0:0]
 +
:POSTROUTING ACCEPT [0:0]
 +
:OUTPUT ACCEPT [0:0]
  
# Allow packets across the internal interface
+
# ----- Begin OPTIONAL FORWARD Section -----
$IPTABLES -A FORWARD -i $INTIF -o $INTIF -j ACCEPT
+
  
# Forward packets from the internal network to the Internet
+
#Optionally forward incoming tcp connections on port 1234 to 192.168.0.100
$IPTABLES -A FORWARD -i $INTIF -o $EXTIF -j ACCEPT
+
#-A PREROUTING -p tcp -d $EXTIP --dport 1234 -m conntrack --ctstate NEW,ESTABLISHED,RELATED -j DNAT --to 192.168.0.100:1234
  
# Catch-all REJECT rule
+
# ----- End OPTIONAL FORWARD Section -----
$IPTABLES -A FORWARD -j REJECT
+
  
 
# IP-Masquerade
 
# IP-Masquerade
$IPTABLES -t nat -A POSTROUTING -o $EXTIF -j SNAT --to $EXTIP
+
-A POSTROUTING -o $EXTIF -j MASQUERADE
  
 +
COMMIT
 +
EOF
  
 
echo " done."
 
echo " done."
 
</nowiki></pre>
 
</nowiki></pre>
 +
Or use '''[http://firehol.sourceforge.net/ fireHOL]'''
  
 
[[category:UbuntuHelp]]
 
[[category:UbuntuHelp]]

2010年5月20日 (四) 00:06的最新版本

Basic

Install the Uncomplicated Firewall, package name is ufw. Uncomplicated firewall just sets up iptables using a simple syntax, or an extended syntax based on OpenBSD's PF. To use ```ufw``` for routing, you must know iptables and should edit the files in /etc/ufw/*.rules.

Advanced

The following is a specific example of a firewall script using only iptables.

#!/bin/sh

# External (Internet-facing) interface
EXTIF="eth0"

# External IP address (automatically detected)
EXTIP=$(/sbin/ip addr show dev "$EXTIF" | perl -lne 'if(/inet (\S+)/){print$1;last}');
 
# Internal interface
INTIF="br0"

# Internal IP address (in CIDR notation)
INTIP="192.168.0.1/32"

# Internal network address (in CIDR notation)
INTNET="192.168.0.0/24"

# The address of anything/everything (in CIDR notation)
UNIVERSE="0.0.0.0/0"


echo "External: [Interface=$EXTIF] [IP=$EXTIP]"
echo "Internal: [Interface=$INTIF] [IP=$INTIP] [Network:$INTNET]"

echo
echo -n "Loading rules..."

# Enabling IP forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward


/sbin/iptables-restore <<-EOF;

*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT DROP [0:0]

# INPUT: Incoming traffic from various interfaces #

# Loopback interface is valid
-A INPUT -i lo -s $UNIVERSE -d $UNIVERSE -j ACCEPT


# Local interface, local machines, going anywhere is valid
-A INPUT -i $INTIF -s $INTNET -d $UNIVERSE -j ACCEPT


# Remote interface, claiming to be local machines, IP spoofing, get lost
-A INPUT -i $EXTIF -s $INTNET -d $UNIVERSE -j REJECT


# External interface, from any source, for ICMP traffic is valid
-A INPUT -i $EXTIF -p ICMP -s $UNIVERSE -d $EXTIP -j ACCEPT


# Allow any related traffic coming back to the MASQ server in.
-A INPUT -i $EXTIF -s $UNIVERSE -d $EXTIP -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT


# Internal interface, DHCP traffic accepted
-A INPUT -i $INTIF -p tcp --sport 68 --dport 67 -j ACCEPT
-A INPUT -i $INTIF -p udp --sport 68 --dport 67 -j ACCEPT


# External interface, HTTP/HTTPS traffic allowed
-A INPUT -i $EXTIF -m conntrack --ctstate NEW,ESTABLISHED,RELATED -p tcp -s $UNIVERSE -d $EXTIP --dport 80 -j ACCEPT
-A INPUT -i $EXTIF -m conntrack --ctstate NEW,ESTABLISHED,RELATED -p tcp -s $UNIVERSE -d $EXTIP --dport 443 -j ACCEPT

# External interface, SSH traffic allowed
-A INPUT -i $EXTIF -m conntrack --ctstate NEW,ESTABLISHED,RELATED -p tcp -s $UNIVERSE -d $EXTIP --dport 22 -j ACCEPT

# Accept port 1234 to be forwarded (this rule needs to correspond with PREROUTING rules in NAT table)
#-A FORWARD -i $EXTIF -o $INTIF -p tcp --dport 1234 -m conntrack --ctstate NEW,ESTABLISHED,RELATED -j ACCEPT

# Catch-all rule, reject anything else
-A INPUT -s $UNIVERSE -d $UNIVERSE -j REJECT


# OUTPUT: Outgoing traffic from various interfaces #

# Workaround bug in netfilter
-A OUTPUT -m conntrack -p icmp --ctstate INVALID -j DROP

# Loopback interface is valid.
-A OUTPUT -o lo -s $UNIVERSE -d $UNIVERSE -j ACCEPT


# Local interfaces, any source going to local net is valid
-A OUTPUT -o $INTIF -s $EXTIP -d $INTNET -j ACCEPT


# local interface, MASQ server source going to the local net is valid
-A OUTPUT -o $INTIF -s $INTIP -d $INTNET -j ACCEPT


# outgoing to local net on remote interface, stuffed routing, deny
-A OUTPUT -o $EXTIF -s $UNIVERSE -d $INTNET -j REJECT


# anything else outgoing on remote interface is valid
-A OUTPUT -o $EXTIF -s $EXTIP -d $UNIVERSE -j ACCEPT


# Internal interface, DHCP traffic accepted
-A OUTPUT -o $INTIF -p tcp -s $INTIP --sport 67 -d 255.255.255.255 --dport 68 -j ACCEPT
-A OUTPUT -o $INTIF -p udp -s $INTIP --sport 67 -d 255.255.255.255 --dport 68 -j ACCEPT


# Catch all rule, all other outgoing is denied and logged. 
-A OUTPUT -s $UNIVERSE -d $UNIVERSE -j REJECT

# Accept solicited tcp packets
-A FORWARD -i $EXTIF -o $INTIF -m conntrack --ctstate ESTABLISHED,RELATED  -j ACCEPT

# Allow packets across the internal interface
-A FORWARD -i $INTIF -o $INTIF -j ACCEPT

# Forward packets from the internal network to the Internet
-A FORWARD -i $INTIF -o $EXTIF -j ACCEPT

# Catch-all REJECT rule
-A FORWARD -j REJECT

COMMIT


# Address translations (only; there is no actual forwarding done here)
*nat
:PREROUTING ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]

# ----- Begin OPTIONAL FORWARD Section -----

#Optionally forward incoming tcp connections on port 1234 to 192.168.0.100
#-A PREROUTING -p tcp -d $EXTIP --dport 1234 -m conntrack --ctstate NEW,ESTABLISHED,RELATED -j DNAT --to 192.168.0.100:1234

# ----- End OPTIONAL FORWARD Section -----

# IP-Masquerade
-A POSTROUTING -o $EXTIF -j MASQUERADE

COMMIT
EOF

echo " done."

Or use fireHOL