Chinaunix.net 技术沙龙


    2004 年 3 月 27 日
Linux 防火墙的原理和实现

            netloafer
Email:netloafer@anti-spam.org.cn
提纲
   防火墙的基本概念
   Linux 防火墙的原理
   构建 Linux 防火墙
   Netfilter 配置工具 iptables
   防火墙的常见策略
   Linux 防火墙的应用
   FAQ
                www.chinaunix.net
防火墙的基本概念
什么是防火墙
 防火墙是一种保护网络安全的方法
 防火墙是在两个网络间实现访问控制的一个
  或一组软件或硬件系统。
 防火墙的主要功能就是依照所定义的访问控
  制策略对数据通讯进行屏蔽和允许通信。




       www.chinaunix.net
防火墙的类型
   包过滤防火墙
   基于状态检测的包过滤防火墙
   应用代理(网关)防火墙
   侧重包过滤的混合式防火墙
   侧重应用代理的混合式防火墙



          www.chinaunix.net
包过滤防火墙
 一般工作在 OSI 的三层和三层以下
 主要控制报文的源地址、报文的目标地址、
  服务类型、以及第二层数据链路层可控的
  MAC 地址等。
 有些也包括部分 OSI 第四层的内容,如报文
  的源端口和目的端口
 常见设备:路由器,可以通过访问控制列表
  ( ACL )来对路由器端口的数据包进行控制

        www.chinaunix.net
包过滤防火墙
 优点
   速度快、对于客户端透明
 缺点
   不能进行内容检查、所工作的层次较低、
  端口必须静态开放、 ACL 的配置在复杂网络
  下容易出错


        www.chinaunix.net
带状态检测的包过滤防火墙
   工作在 IP 层
   动态开放端口
   动态的状态列表
   可以对应用层过滤
   速度和效率都不错



          www.chinaunix.net
应用代理(网关)防火墙
 包含了网络中的第七层应用
 可以提供复杂的访问控制
    认证机制
    内容过滤
    详细日志
 速度慢,系统开销大
 网络协议和应用都需要代理

            www.chinaunix.net
防火墙的其它功能
   DMZ
   IP 地址转换和伪装
   VPN
   IDS 联动
   病毒过滤
   流量和计费控制
   自我保护
   GUI 管理
            www.chinaunix.net
Linux 防火墙的原理
Linux 防火墙的历史
 内核防火墙的发展
 2.0.X 内核 ipfwadm
 2.2.X 内核 ipchains
 2.4.X 内核 netfilter/iptables




              www.chinaunix.net
ipchains
 ipchains 访问控制规则被称为“链”
 ipchains 共有三条链
   • INPUT
   • OUTPUT
   • FORWARD
 可实现的功能 : 包过滤 , 伪装 , 路由 ,TOS
 扩展性不好

               www.chinaunix.net
ipchains 的处理流程图




   www.chinaunix.net
netfilter/iptables
   全新的 netfilter 框架
   优秀的包过滤子系统
   支持 IPv4 、 IPv6 、 IPX 等协议
   支持 MAC 过滤
   功能模块化



              www.chinaunix.net
netfilter/iptables 处理流程图




       www.chinaunix.net
构建 Linux 防火墙
知识准备
 编译和升级 Linux 内核
 Linux 下软件的安装
 Linux 网络的配置




      ( 我们这里的安装以 netfilter/iptables 为例)


             www.chinaunix.net
Linux 内核的编译
   使用 wget 从网站获取最新的内核源码包
   cp linux-2.4.18.tar.gz /usr/src
   cd /usr/src
   tar zxvf linux-2.4.18.tar.gz
   cd /usr/src/linux
   make menuconfig
   make dep
   make clean
   make bzImage
   make modules
   make modules_install

                  www.chinaunix.net
Linux 内核 netfilter 的配置
   [*] Network packet filtering (replaces ipchains)
   <M> Connection tracking (required for masq/NAT)
   <M> FTP protocol support
   <M> IRC protocol support
   <M> Userspace queueing via NETLINK (EXPERIMENTAL)
   <M> IP tables support (required for filtering/masq/NAT)
   <M> limit match support
   <M> MAC address match support
   <M> netfilter MARK match support
   <M> Multiple port match support
   <M> TOS match support
   <M> tcpmss match support



                        www.chinaunix.net
Linux 内核 netfilter 的配置
   <M> Connection state match support
   <M> Packet filtering
   <M> REJECT target support
   <M> Full NAT
   <M> MASQUERADE target support
   <M> REDIRECT target support
   <M> Packet mangling
   <M> TOS target support
   <M> MARK target support
   <M> LOG target support
   <M> TCPMSS target support
   <M> ARP tables support
   <M> ARP packet filtering


                       www.chinaunix.net
安装 netfilter
 下载 netfilter (http://www.netfilter.org)
 RPM 方式:
   • rpm –ivh netfiter.0.1.18.i386.rpm
 源码安装:
   •   tar zxvf netfilter-0.1.18.tar.gz
   •   cd netfilter
   •   ./configure
   •   make
   •   make install
                        www.chinaunix.net
安装 iptables
 下载 iptables (http://www.netfilter.org)
 RPM 方式:
  • rpm –ivh iptables-1.2.7a-1.i386.rpm
 源码安装:
  •   tar zcvf iptables-1.2.7a.tar.gz
  •   cd iptables-1.2.7a
  •   ./configure
  •   make
  •   make install
                      www.chinaunix.net
Linux 防火墙管理工具

  ipchains iptables
ipchains
ipchains 的动作
   ACCEPT
   DENY
   REJECT
   MASQ
   REDIRECT
   RETURN


               www.chinaunix.net
ipchains 指令
 -A : --append 在所选择的链上
                               -X chain : --delete-chain 删除
  加一条规则到末尾
                                一个用户自定义的链
 -D : --delete 从所选链上删除
                               –P chain target : --policy 把一
  一条或多条规则
                                个规链上策略(规则的动作)改
 -R : --replace 在所选链上替换        变为目标
  一条规则                         –M : --masquerade 列出当前
 -I : --insert 以给出的规则号在        伪装包的所有链接
  所选链上插入一条或多条规则                -M -S tcp/tcpfin/udp : --set
 -L : --list 列出指定链的规则          设置伪装的超时
 -F : --flush 清除一个链的所有
  规则
 -Z : --zero 计数器清零
 -N : --new 创建一个新的用户
  链
                 www.chinaunix.net
ipchains 使用方法
 ipchains -[ADC] chain rule-specification [options]
 ipchains -[RI] chain rulenum rule-specification
  [options]
 ipchains -D chain rulenum [options]
 ipchains -[LFZNX] [chain] [options]
 ipchains -P chain target [options]
 ipchains -M [ -L | -S ] [options]
 ipchains -h [icmp]

                  www.chinaunix.net
netfilter 配置工具 iptables
iptables 动作
   ACCEPT
   DROP
   REJECT
   MASQUERADE
   REDIRECT
   RETURN
   SNAT
   DNAT
             www.chinaunix.net
iptables 指令
 –A chain --append 添加到规则           –C chain --check 测试规则链
  链中                                –N chain --new 创建一个用户自
 –D chain --delete 从规则链中删           定义的新链
  除匹配的规则                            -X chain --delete –chain 删除用
 –D chain rulenum --delete 从规       户定义的新链
  则中删除对应的规则号                        -P chain target --policy 把一个
 –I chain [rulenum] --insert 将规     规则链上的策略改变为目标
  则插入对应的规则号                         -E old-chain-name new-chain-
 –R chain rulenum --replace 在       name --rename chain 改变规则
  规则链中取代指定的规则号                       链名
 –L[chain] --list 列出在一条链或
  所有链上的规则                                 • iptables 内置有三个表
 –F [chain] --flush 清除一条链或                 filter 、 nat 、 mangle ,缺省为
  所有链上的规则                                   filter 表,可使用 -t 参数来选择操
                                            作的表,用户可自定义表
 –Z [chain] --zero 计数器清零

                      www.chinaunix.net
iptables 使用方法
 iptables -[ADC] chain rule-specification [options]
 iptables -[RI] chain rulenum rule-specification
  [options]
 iptables -D chain rulenum [options]
 iptables -[LFZ] [chain] [options]
 iptables -[NX] chain
 iptables -E old-chain-name new-chain-name
 iptables -P chain target [options]
 iptables -h
                  www.chinaunix.net
防火墙的常见策略
防火墙的常见策略
 设置默认拒绝规则
 • iptables -P INPUT -j DROP
 • iptables -P OUTPUT -j DROP
 • iptables -P FORWARD -j DROP
 禁止所有分片包
 • iptables -A INPUT -f -i eth0 -j DROP



                www.chinaunix.net
防火墙的常见策略
 允许对外的 FTP 访问
 • iptables -A OUTPUT -i eth0 -p tcp -s
   192.168.0.0/24 --sport 1024:65535 -d any/0
   --dport 21 -j ACCEPT
 • iptables -A INPUT -i eth0 -p tcp -s any/0 --sport
   21 -d 192.168.0.0/24 --dport 1024:65535 -j
   ACCEPT



                www.chinaunix.net
防火墙的常见策略
 SNAT
 • iptables -t nat -A POSTROUTING -o eth0 -j
   SNAT --to 1.2.3.4
 • 伪装
    iptables -t nat -A POSTROUTING -o ppp0 -j
     MASQUERADE
 DNAT
 • iptables -t nat -A PREROUTING -i eth0 -j DNAT
   --to 1.2.4.4

                www.chinaunix.net
防火墙的常见策略
 端口映像
 • iptables -t nat -A PREROUTING -i eth0 -p tcp
   --dport 80 -j REDIRECT --to-port 3128
 DNAT 的重定向
 • iptables -t nat -A PREROUTING -i eth0 -p tcp
   --dport 80 -j DNAT --to 1.2.4.4:8080



               www.chinaunix.net
Linux 防火墙的应用
Linux 防火墙的应用
   各类安全产品的低层模块
   netfilter+iprouter2+squid 的透明代理
   netfilter+iprouter2+tc 的 QoS 管理
   网络的接入
   网络的安全管理



               www.chinaunix.net
FAQ
谢谢

欢迎大家继续参加 chinaunix.net 技术沙龙

Linux

  • 1.
    Chinaunix.net 技术沙龙 2004 年 3 月 27 日
  • 2.
    Linux 防火墙的原理和实现 netloafer Email:netloafer@anti-spam.org.cn
  • 3.
    提纲  防火墙的基本概念  Linux 防火墙的原理  构建 Linux 防火墙  Netfilter 配置工具 iptables  防火墙的常见策略  Linux 防火墙的应用  FAQ www.chinaunix.net
  • 4.
  • 5.
    什么是防火墙  防火墙是一种保护网络安全的方法  防火墙是在两个网络间实现访问控制的一个 或一组软件或硬件系统。  防火墙的主要功能就是依照所定义的访问控 制策略对数据通讯进行屏蔽和允许通信。 www.chinaunix.net
  • 6.
    防火墙的类型  包过滤防火墙  基于状态检测的包过滤防火墙  应用代理(网关)防火墙  侧重包过滤的混合式防火墙  侧重应用代理的混合式防火墙 www.chinaunix.net
  • 7.
    包过滤防火墙  一般工作在 OSI的三层和三层以下  主要控制报文的源地址、报文的目标地址、 服务类型、以及第二层数据链路层可控的 MAC 地址等。  有些也包括部分 OSI 第四层的内容,如报文 的源端口和目的端口  常见设备:路由器,可以通过访问控制列表 ( ACL )来对路由器端口的数据包进行控制 www.chinaunix.net
  • 8.
    包过滤防火墙  优点 速度快、对于客户端透明  缺点 不能进行内容检查、所工作的层次较低、 端口必须静态开放、 ACL 的配置在复杂网络 下容易出错 www.chinaunix.net
  • 9.
    带状态检测的包过滤防火墙  工作在 IP 层  动态开放端口  动态的状态列表  可以对应用层过滤  速度和效率都不错 www.chinaunix.net
  • 10.
    应用代理(网关)防火墙  包含了网络中的第七层应用  可以提供复杂的访问控制  认证机制  内容过滤  详细日志  速度慢,系统开销大  网络协议和应用都需要代理 www.chinaunix.net
  • 11.
    防火墙的其它功能  DMZ  IP 地址转换和伪装  VPN  IDS 联动  病毒过滤  流量和计费控制  自我保护  GUI 管理 www.chinaunix.net
  • 12.
  • 13.
    Linux 防火墙的历史  内核防火墙的发展 2.0.X 内核 ipfwadm 2.2.X 内核 ipchains 2.4.X 内核 netfilter/iptables www.chinaunix.net
  • 14.
    ipchains  ipchains 访问控制规则被称为“链” ipchains 共有三条链 • INPUT • OUTPUT • FORWARD  可实现的功能 : 包过滤 , 伪装 , 路由 ,TOS  扩展性不好 www.chinaunix.net
  • 15.
  • 16.
    netfilter/iptables  全新的 netfilter 框架  优秀的包过滤子系统  支持 IPv4 、 IPv6 、 IPX 等协议  支持 MAC 过滤  功能模块化 www.chinaunix.net
  • 17.
  • 18.
  • 19.
    知识准备  编译和升级 Linux内核  Linux 下软件的安装  Linux 网络的配置 ( 我们这里的安装以 netfilter/iptables 为例) www.chinaunix.net
  • 20.
    Linux 内核的编译  使用 wget 从网站获取最新的内核源码包  cp linux-2.4.18.tar.gz /usr/src  cd /usr/src  tar zxvf linux-2.4.18.tar.gz  cd /usr/src/linux  make menuconfig  make dep  make clean  make bzImage  make modules  make modules_install www.chinaunix.net
  • 21.
    Linux 内核 netfilter的配置  [*] Network packet filtering (replaces ipchains)  <M> Connection tracking (required for masq/NAT)  <M> FTP protocol support  <M> IRC protocol support  <M> Userspace queueing via NETLINK (EXPERIMENTAL)  <M> IP tables support (required for filtering/masq/NAT)  <M> limit match support  <M> MAC address match support  <M> netfilter MARK match support  <M> Multiple port match support  <M> TOS match support  <M> tcpmss match support www.chinaunix.net
  • 22.
    Linux 内核 netfilter的配置  <M> Connection state match support  <M> Packet filtering  <M> REJECT target support  <M> Full NAT  <M> MASQUERADE target support  <M> REDIRECT target support  <M> Packet mangling  <M> TOS target support  <M> MARK target support  <M> LOG target support  <M> TCPMSS target support  <M> ARP tables support  <M> ARP packet filtering www.chinaunix.net
  • 23.
    安装 netfilter  下载netfilter (http://www.netfilter.org)  RPM 方式: • rpm –ivh netfiter.0.1.18.i386.rpm  源码安装: • tar zxvf netfilter-0.1.18.tar.gz • cd netfilter • ./configure • make • make install www.chinaunix.net
  • 24.
    安装 iptables  下载iptables (http://www.netfilter.org)  RPM 方式: • rpm –ivh iptables-1.2.7a-1.i386.rpm  源码安装: • tar zcvf iptables-1.2.7a.tar.gz • cd iptables-1.2.7a • ./configure • make • make install www.chinaunix.net
  • 25.
  • 26.
  • 27.
    ipchains 的动作  ACCEPT  DENY  REJECT  MASQ  REDIRECT  RETURN www.chinaunix.net
  • 28.
    ipchains 指令  -A: --append 在所选择的链上  -X chain : --delete-chain 删除 加一条规则到末尾 一个用户自定义的链  -D : --delete 从所选链上删除  –P chain target : --policy 把一 一条或多条规则 个规链上策略(规则的动作)改  -R : --replace 在所选链上替换 变为目标 一条规则  –M : --masquerade 列出当前  -I : --insert 以给出的规则号在 伪装包的所有链接 所选链上插入一条或多条规则  -M -S tcp/tcpfin/udp : --set  -L : --list 列出指定链的规则 设置伪装的超时  -F : --flush 清除一个链的所有 规则  -Z : --zero 计数器清零  -N : --new 创建一个新的用户 链 www.chinaunix.net
  • 29.
    ipchains 使用方法  ipchains-[ADC] chain rule-specification [options]  ipchains -[RI] chain rulenum rule-specification [options]  ipchains -D chain rulenum [options]  ipchains -[LFZNX] [chain] [options]  ipchains -P chain target [options]  ipchains -M [ -L | -S ] [options]  ipchains -h [icmp] www.chinaunix.net
  • 30.
  • 31.
    iptables 动作  ACCEPT  DROP  REJECT  MASQUERADE  REDIRECT  RETURN  SNAT  DNAT www.chinaunix.net
  • 32.
    iptables 指令  –Achain --append 添加到规则  –C chain --check 测试规则链 链中  –N chain --new 创建一个用户自  –D chain --delete 从规则链中删 定义的新链 除匹配的规则  -X chain --delete –chain 删除用  –D chain rulenum --delete 从规 户定义的新链 则中删除对应的规则号  -P chain target --policy 把一个  –I chain [rulenum] --insert 将规 规则链上的策略改变为目标 则插入对应的规则号  -E old-chain-name new-chain-  –R chain rulenum --replace 在 name --rename chain 改变规则 规则链中取代指定的规则号 链名  –L[chain] --list 列出在一条链或 所有链上的规则 • iptables 内置有三个表  –F [chain] --flush 清除一条链或 filter 、 nat 、 mangle ,缺省为 所有链上的规则 filter 表,可使用 -t 参数来选择操 作的表,用户可自定义表  –Z [chain] --zero 计数器清零 www.chinaunix.net
  • 33.
    iptables 使用方法  iptables-[ADC] chain rule-specification [options]  iptables -[RI] chain rulenum rule-specification [options]  iptables -D chain rulenum [options]  iptables -[LFZ] [chain] [options]  iptables -[NX] chain  iptables -E old-chain-name new-chain-name  iptables -P chain target [options]  iptables -h www.chinaunix.net
  • 34.
  • 35.
    防火墙的常见策略  设置默认拒绝规则 •iptables -P INPUT -j DROP • iptables -P OUTPUT -j DROP • iptables -P FORWARD -j DROP  禁止所有分片包 • iptables -A INPUT -f -i eth0 -j DROP www.chinaunix.net
  • 36.
    防火墙的常见策略  允许对外的 FTP访问 • iptables -A OUTPUT -i eth0 -p tcp -s 192.168.0.0/24 --sport 1024:65535 -d any/0 --dport 21 -j ACCEPT • iptables -A INPUT -i eth0 -p tcp -s any/0 --sport 21 -d 192.168.0.0/24 --dport 1024:65535 -j ACCEPT www.chinaunix.net
  • 37.
    防火墙的常见策略  SNAT •iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to 1.2.3.4 • 伪装  iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE  DNAT • iptables -t nat -A PREROUTING -i eth0 -j DNAT --to 1.2.4.4 www.chinaunix.net
  • 38.
    防火墙的常见策略  端口映像 •iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3128  DNAT 的重定向 • iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to 1.2.4.4:8080 www.chinaunix.net
  • 39.
  • 40.
    Linux 防火墙的应用  各类安全产品的低层模块  netfilter+iprouter2+squid 的透明代理  netfilter+iprouter2+tc 的 QoS 管理  网络的接入  网络的安全管理 www.chinaunix.net
  • 41.
  • 42.