Linux 版 (精华区)

发信人: tcpip (俺的昵称改了), 信区: Linux
标  题: 杂项--5
发信站: 哈工大紫丁香 (Fri Sep  3 16:27:05 1999), 转信

5. 杂项

此节包含在前面无法找到合适位置的所有信息和 FAQ.

5.1 如何组织你的防火墙规则. 

你需要想一想. 你可以尝试组织它们来优化速度(最少的规则检查用于大部分普通包)或

增强管理性.

假如你有一个间断的连接, 就说 PPP 连接吧, 在启动时, 你也许想在进入链(input)中

设置第一个规则为 '-i ppp0 -j DENY', 那么在你的 ip-up 脚本中加入:

       # Re-create the `ppp-in' chain.

       ipchains-restore -f < ppp-in.firewall

       # Replace DENY rule with jump to ppp-handling chain.

       ipchains -R input 1 -i ppp0 -j ppp-in

你的 ip-down 脚本将象这:

       ipchains -R input 1 -i ppp0 -j DENY

5.2 什么不能过滤掉. 

在你开始设置过滤器前, 需要知道一些事, 以免把不该过滤掉的东西过滤掉. 

ICMP packets.

除其它作用外, ICMP 包被用来为其它协议(TCP, UDP)指示错误. 如: 'destination-u

nreachable' 包. 阻挡这些包意味这你将不会收到 'Host unreachable' or 'No rout

e tohost' 等报错信息; 所有的连接将等待一个永不会来的回复. 这样做虽然不好, 但

还不致命.

一个更糟的问题是 ICMP 包在 'MTU 发现'中担任角色. 为良好的完成 TCP 连接(包括

 Linux), 会使用 MTU 发现去算出不被分解成片段就可以到达目的地的最大包. MTU 发

现是这样工作的, 发送带 'Don't Fragment'(不分解成片段)位的包, 如果收到 'Frag

mentation needed but DF set'('包需要分解成片段')包, 那么就发送较小的包. 返回

的是典型的 'destination-unreachable' 包, 假如它没有被收到, 本地主机将不会减

小 MTU, 测试将永远进行或没有意义.

连接到 DNS 的 TCP.

假如你想阻止外出的 TCP 连接, 记注 DNS 并不总是用 UDP; 假如服务器的回复超过5

12字节, 客户机使用 TCP 连接得到数据(仍从53端口).

假如你不允许这样的 TCP 传输, 大部分时间 DNS 会正常工作, 因此这可能是一个陷阱

; 假如你这样做, 你也许经历过奇怪的长延时和其它偶尔的 DNS 错误.

If your DNS queries are always directed at the same external source (either

 directly by using the nameserver line in /etc/resolv.conf or by using a ca

ching nameserver in forward mode), then you need only allow TCP connections

 to portdomain on that nameserver from the local domain port (if using a ca

ching nameserver) or from a high port (> 1023) if using /etc/resolv.conf. 

假如你的 DNS 请求总是指向相同的外部服务器(用 /etc/resolv.conf 文件中的 name

server 指向或使用工作于中转方式的缓存命名服务器来指向),那么你可以仅需允许 T

CP 连接从本地域(domain)端口(假如使用缓存命名服务器)或从一个高端口(>1023, 假

如使用 /etc/resolv.conf 文件)连向 DNS服务器的 域(domain) 端口.

噩梦般的FTP.

典型的包过滤问题是 FTP. FTP 有两种模式; 传统的叫主动模式(active), 新的叫被动

模式(passive). WEB 浏览器通常使用被动模式, 但命令行 FTP 常使用默认的主动模式

.

在主动模式中, 当远程端想发送一个文件时(或 ls or dir 命令的结果), 它将试图建

立一个到本地端的 TCP 连接. 这意味着你不破坏主动 FTP的话就不能过滤掉这些 TCP

 连接.

假如你有机会选择使用被动模式, 那么好; 被动模式从客户端到服务器端建立数据连接

, 甚至包括进入的数据. 否则, 建议你只允许1024以上端口并且不包括6000-6010(600

0由 X-windows 使用)的 TCP 连接.

5.3 过滤掉 Ping of Death. 

Linux 对于著名的 Ping of Death 有免役能力, 它通过传送一个大的不合法的 ICMP 

包, 造成接收者的 TCP 栈溢出并造成破坏.

假如你正在保护的机器容易受伤害, 你简单的阻止 ICMP 片段包即可. 通常 ICMP 包不

会大到需要分成片段的地步, 除了大的 pings 外你不会破坏什么. 我曾经听说(没有确

认)仅需大的 ICMP 包的最后一个片段就会对一些系统造成破坏, 所以不建议仅阻止第

一个片段.

虽然我看到过的优秀程序(用于 ping of death 攻击)都使用 ICMP, 但TCP 或 UCP (或

其它未知协议)的片段也可以用于攻击, 所以仅阻止 ICMP 片段是临时的解决方案.

5.4 过滤掉 Teardrop and Bonk. 

Teardrop and Bonk (主要针对微软的 NT)是依靠重叠片段的两种攻击. 用你的 Linux

 路由器做重整片段处理(defragmentation), 或不允许所有的片段发往容易受攻击的机

器.

5.5 过滤掉 Fragment Bombs. 

一些不可靠的 TCP 栈处理大量的片段时, 如果不能收到所有的片段, 它就会出问题. 

Linux 没有此问题. 你可以过滤掉片段(这样会影响合法的用户)或编译内核时把 'IP:

 always defragment' 设为 'Y'(仅用于你的 Linux 机器是这些包的唯一路由).

5.6 改变防火墙规则. 

在改变防火规则时有一些复杂的时间安排问题. 假如你不注意, 当你改到一半时也许会

让包进来. 一个简单的好方法是:

       # ipchains -I input 1 -j DENY

       # ipchains -I output 1 -j DENY

       # ipchains -I forward 1 -j DENY

       ... make changes ...

       # ipchains -D input 1

       # ipchains -D output 1

       # ipchains -D forward 1

       # 

这样在改动时会丢弃所有的包.

假如你的改动仅限于单个链, 你可以建立一个包含新规则的新链, 然后把指向旧链的规

则改为指向新链中的规则('-R'): 然后你可以删除旧链. 替换会自动进行.

5.7 我如何建立 IP 欺骗防护? 

IP 欺骗是一个技巧: 包从一个主机发出, 包却自称是从另外的主机发出的. 由于包过

滤器是基于包源地址作出决定的, IP 欺骗被用来对付愚蠢的包过滤器. 它也被用来隐

藏使用 SYN 攻击, Teardrop, Ping of Death 和其它类似攻击者的身份.(如果你不知

道他们是谁, 他们就不用担心).

免受 IP 欺骗的最好办法是用源地址确认, 这是路由代码来做的, 而不是防火墙. 找一

个叫 /proc/sys/net/ipv4/conf/all/rp_filter 的文件. 如果存在, 那么每次启动时

打开源地址确认是个好方法. 这样做, 把下面的几行插入到你的启动脚本中初始化任何

网络接口的前面.

       # This is the best method: turn on Source Address Verification and g

et       # spoof protection on all current and future interfaces.

       if [ -e /proc/sys/net/ipv4/conf/all/rp_filter ]; then

         echo -n "Setting up IP spoofing protection..."

         for f in /proc/sys/net/ipv4/conf/*/rp_filter; do

             echo 1 > $f

         done

         echo "done."

       else

         echo PROBLEMS SETTING UP IP SPOOFING PROTECTION.  BE WORRIED.

         echo "CONTROL-D will exit from this shell and continue system star

tup."         echo

         # Start a single user shell on the console

         /sbin/sulogin $CONSOLE

       fi

假如你不能这样做, 你可以手工插入规则以保护所有接口. 这需要有每个接口的知识.

 2.1 核心自动拒绝自称来自 127.* 地址的包.

例如: 有三个接口, eth0, eth1 and ppp0. 我们可以使用 ifconfig 知道这些接口的

地址和掩码. eth0 用 255.255.255.0 的掩码, 隶属于 192.168.0.0 网, eth1 用 25

5.0.0.0的掩码, 隶属于 10.0.0.0 网, ppp0 连接到因特网(除了批准保留的私用地址

, 可以是任何地址), 我们将插入下列规则:

       # ipchains -A input -i eth0 -s ! 192.168.1.0/255.255.255.0 -j DENY

       # ipchains -A input -i ! eth0 -s 192.168.1.0/255.255.255.0 -j DENY

       # ipchains -A input -i eth1 -s ! 10.0.0.0/255.0.0.0 -j DENY

       # ipchains -A input -i ! eth1 -s 10.0.0.0/255.0.0.0 -j DENY

       # 

此方法不如源地址确认好, 因为, 假如你的网络改变了, 你不得不改变防火规则来适应

.

假如你运行 2.0 核心, 你也须想保护本地环路接口, 使用下面的:

       # ipchains -A input -i ! lo -s 127.0.0.0/255.0.0.0 -j DENY

       #

5.8 高级项目 

There is a userspace library I have written which is included with the sour

ce distribution called `libfw'. It uses the ability of IP Chains 1.3 and ab

ove to copy a packet to userspace (using the IP_FIREWALL_NETLINK config opt

ion). 

Things such as stateful inspection (I prefer the term dynamic firewalling) 

can be implemented in userspace using this library. Other nifty ideas inclu

de controlling packets on a per-user basis by doing a lookup in a userspace

 daemon. Thisshould be pretty easy. 

The `mark' capability of the firewalls is underutilized: it could easily be

 used to represent a priority for the Quality of Service code, which would 

make it simple to control packet priorities. 

5.9 未来 

Firewalling and NAT are being redesigned for 2.3. Plans and discussions are

 available on the netdev archive. These enhancements should clear up many o

utstanding usability issues (really, firewalling and masquerading shouldn't

 be this hard),and allow growth for far more flexible firewalling. 


--
☆ 来源:.哈工大紫丁香 bbs.hit.edu.cn.[FROM: bin@mtlab.hit.edu.cn]
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:3.098毫秒