iptables作为路由器实现上网和访问控制

凉白开 Linux219,4379字数 5991阅读19分58秒阅读模式

作为公司上网的路由器需要实现的功能有nat地址转换、dhcp、dns缓存、流量控制、应用程序控制,nat地址转换通过iptables可以直 接实现,dhcp服务需要安装dhcpd,dns缓存功能需要使用bind,流量控制可以使用tc,应用程序控制:例如对qq的封锁可以使用 netfilter-layer7-v2.22+17-protocols-2009-05-28.tar.gz来实现

1、网络规划

iptables文章源自运维生存时间-https://www.ttlsa.com/linux/iptables-router-acl/

操作系统是centos5.8文章源自运维生存时间-https://www.ttlsa.com/linux/iptables-router-acl/

 文章源自运维生存时间-https://www.ttlsa.com/linux/iptables-router-acl/

2、安装dhcpd

 文章源自运维生存时间-https://www.ttlsa.com/linux/iptables-router-acl/

yum install dhcp-3.0.5-31.el5
vim /etc/dhcp/dhcpd.conf
ddns-update-style interim;
ignore client-updates;
subnet 10.0.0.0 netmask 255.255.255.0 {
 option routers 10.0.0.1;
 option subnet-mask 255.255.255.0;
 option domain-name-servers 10.0.0.1;
 range dynamic-bootp 10.0.0.100 10.0.0.200;
 default-lease-time 21600;
 max-lease-time 43200;
}

iptables文章源自运维生存时间-https://www.ttlsa.com/linux/iptables-router-acl/

3、安装bind,实现dns缓存

yum install bind97.i386  bind97-libs.i386 bind97-utils.i386
vim /etc/named.conf
options {
 directory "/var/named";
 allow-recursion { 10.0.0.0/24; };
 recursion yes;
 forward first;                     #将所有请求都进行转发
 forwarders { 114.114.114.114; };   #定义转发服务器地址
};
zone "." IN {
 type hint;
 file "named.ca";
};
zone "localhost" IN {
 type master;
 file "named.localhost";
 allow-transfer { none; };
};
zone "0.0.127.in-addr.arpa" IN {
 type master;
 file "named.loopback";
 allow-transfer { none; };
};

创建根域文件,默认有文章源自运维生存时间-https://www.ttlsa.com/linux/iptables-router-acl/

dig -t NS . > /var/named/named.ca
chown :named /var/named/named.ca

创建本地正向解析文件,默认有文章源自运维生存时间-https://www.ttlsa.com/linux/iptables-router-acl/

vim /var/named/named.localhost
$TTL 1D
@ IN SOA @ rname.invalid. (
                    0 ; serial
                   1D ; refresh
                   1H ; retry
                    1W ; expire
                   3H ) ; minimum
 NS     @
 A      127.0.0.1
chown :named /var/named/named.localhost

创建本地反向解析文件,默认有文章源自运维生存时间-https://www.ttlsa.com/linux/iptables-router-acl/

vim /var/named/named.loopback
 $TTL 1D
@ IN SOA @ rname.invalid. (
                  0 ; serial
                  1D ; refresh
                  1H ; retry
                  1W ; expire
                  3H ) ; minimum
 NS     @
 A       127.0.0.1
 PTR   localhost.
chown :named /var/named/named.loopback

检查主配置文件
named-checkconf
检查根区域配置文件
named-checkzone “.” /var/named/named.ca
检查区域文件
named-checkzone “localhost” /var/named/named.localhost
启动服务
service named start文章源自运维生存时间-https://www.ttlsa.com/linux/iptables-router-acl/

 文章源自运维生存时间-https://www.ttlsa.com/linux/iptables-router-acl/

4、重新编译编译内核和iptables以支持应用层过滤

由于实行防火墙功能的是netfilter内核模块,所以需要重新编译内核,需要下载新的内核源码,并使用netfilter-layer7-v2.22作为内核的补丁一起编译到内核中。而控制netfiler的是iptables工具,因此iptables也必须重新编译安装,最后再安装应用程序过滤特征码库17-protocols-2009-05028.tar.gz文章源自运维生存时间-https://www.ttlsa.com/linux/iptables-router-acl/

1、给内核打补丁,并重新编译内核
2、给iptables源码打补丁,并重新编译iptables
3、安装17proto文章源自运维生存时间-https://www.ttlsa.com/linux/iptables-router-acl/

备份iptables脚本和配置文件文章源自运维生存时间-https://www.ttlsa.com/linux/iptables-router-acl/

cp /etc/rc.d/init.d/iptables /root/iptables.sysv
cp /etc/sysconfig/iptables-config /root/iptables-config

2.6内核下载地址文章源自运维生存时间-https://www.ttlsa.com/linux/iptables-router-acl/

https://www.kernel.org/pub/linux/kernel/v2.6/文章源自运维生存时间-https://www.ttlsa.com/linux/iptables-router-acl/

netfilter下载地址文章源自运维生存时间-https://www.ttlsa.com/linux/iptables-router-acl/

http://download.clearfoundation.com/l7-filter/文章源自运维生存时间-https://www.ttlsa.com/linux/iptables-router-acl/

iptables源码下载地址文章源自运维生存时间-https://www.ttlsa.com/linux/iptables-router-acl/

http://www.netfilter.org/projects/iptables/downloads.html文章源自运维生存时间-https://www.ttlsa.com/linux/iptables-router-acl/

应用程序特征码库下载地址文章源自运维生存时间-https://www.ttlsa.com/linux/iptables-router-acl/

http://download.clearfoundation.com/l7-filter/

xz -d linux-2.6.28.10.tar.xz
tar -xvf linux-2.6.28.10.tar.gz -C /usr/src #新的内核源码,用于重新编译
tar -zxvf netfilter-layer7-v2.22.tar.gz -C /usr/src #内核补丁和iptables补丁 ,只支持到2.6.28

 

#进入解压目录并创建软连接

cd /usr/src
ln -sv linux-2.6.28.10 linux

#进入内核目录

cd /usr/src/linux

#为当前内核打补丁

patch -p1 < ../netfilter-layer7-v2.22/kernel-2.6.25-2.6.28-layer7-2.22.path

#为了方便编译内核将系统上的内核配置文件复制过来

cp /boot/config-2.6.18-164.el5 /usr/src/linux/.config

编译内核

make menuconfig
Networking support -> Networking Options -> Network packet filtering framework -> Core Netfilter Configuration
<M> Netfilter connection tracking support
<M> "lawyer7" match support
<M> "string" match support
<M> "time" match support
<M> "iprange" match support
<M> "connlimit" match support
<M> "state" match support
<M> "conntrack" connection match support
<M> "mac" address match support
<M> "multiport" Multiple port match support
Networking support -> Networign options -> Network packet filtering framework -> IP:Netfiltr Configuration
<M> IPv4 connection tracking support (required for NAT)
<M> Full NAT
<M> MASQUERADE target support
<M> NETMAP target support
<M> REDIRECT target support

在Networking support中选择 Networking options

查找Network packet filtering framework(Netfilter)–>Core Netfiler Configrationg–>Netfilter connection tracking support(NEW),”layer7″ match support(NEW),”time” match support(NEW),”iprange”

查找IP:Netfilter Configuration–>IPv4 connection tracking support,Full NAT(NEW)

make
make modules_install
make install

重启操作系统选择新内核登录

卸载旧的iptables

rpm -e iptables-1.3.5-9.1.el5 iptables-ipv6-1.3.5-9.1.el5 iptstate-1.4-2.el5 --nodeps

安装新的iptables,以支持新的netfiler模块

tar -jsvf iptables-1.4.6.tar.bz2 -C /usr/src
cd /usr/src/iptables-1.4.6
cd iptables-1.4.3forward-for-kernel-2.6.20forward
cp * /usr/src/iptables-1.4.6/extensions/
cd /usr/src/iptables-1.4.6/
./configure --prefix=/usr --with-ksource=/usr/src/linux
make
make install

查看安装后的iptables的文件

ls /usr/sbin |grep iptables
ls /usr/libexec/xtables

复制之前备份的配置文件和脚本

cp /root/iptables-config /etc/sysconfig/
cp /root/iptables.sysv /etc/rc.d/init.d/iptables

修改脚本中iptables的路径

vim /etc/rc.d/init.d/iptables
:.,$s@/sbin/$IPTABLES@/usr/sbin/$IPTABLES@g

让iptables服务开机自动启动

chkconfig --add iptables

修改iptables 配置文件
将/etc/sysconfig/iptables-config中的
IPTABLES_MODULES=”ip_conntrack_netbios_ns”    注释掉

安装协议特征码

tar xvf 17-protocols-2009-05028.tar.gz
make install

完成后在/etc/l7-protocols会生成文件
支持的协议/etc/l7-protocols/protocols

添加iptables策略,运行内部网络上网,禁止qq和视频

iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -j SNAT --to-soure 192.168.6.67
iptables -A FORWARD -m layer7 --l7proto qq -j DROP
iptables -A FORWARD -m layer7 --l7proto httpvideo -j DROP
iptables -A FORWARD -m layer7 --l7proto httpaudio -j DROP

指定8点到12点无法上网

iptables -A FORWARD -m time --timestart 08:00 --timestop 12:00 -j DROP

5、使用tc控制带宽

例如公司出口带宽是10Mbps,个用户A分配500KB的最大下载带宽,给用户B 分配分配的最大下载带宽是200KB
A用户ip:10.0.0.100
B用户ip:10.0.0.101

#在eth0网卡上创建一个根队列规则,队列规则的算法使用htb,default 2表示指定一个默认类别编号,默认的流量控制策略,如果ip没有在后面的filter中被匹配到就都是有这个策略

tc qdisc add dev eth0 root handle 1:0 htb default 2

#在eth0网卡上定义一个类,prant 1:0中的1对应根队列规则中的handle 1:0,classid 1:2表示当前这个类的标识,用于应用在后面的得到filter中,rate 200kbsp表示带宽为200KB/s,ceil 200kbps表示最大带宽也为200KB/s,prio 2是优先级

tc class add dev eth0 parent 1:0 classid 1:2 htb rate 200kbps ceil 200kbps prio 2
tc class add dev eth0 parent 1:0 classid 1:3 htb rate 500kbps ceil 500kbps prio 2

#将两个类的默认的fifq队列规则改为sfq

tc qdisc add dev eth0 parent 1:2 handle 20 sfq
tc qdisc add dev eth0 parent 1:3 handle 30 sfq

#在网卡eth0上的1:0节点(对应qdisc中的handle 1:0)添加一个u32过滤规则,优先级为1,凡是目标地址是10.0.0.100的数据包都使用1:2类(对应classid为1:2的类)

tc filter add dev eth0 parent 1:0 protocol ip prio 1 u32 match ip dst 10.0.0.100 flowid 1:2
tc filter add dev eth0 parent 1:0 protocol ip prio 1 u32 match ip dst 10.0.0.101 flowid 1:3

如果还有其他用户例如用户C和D的ip是102、103,要求的下载带宽也要求500那么在加入

tc filter add dev eth0 parent 1:0 protocol ip prio 1 u32 match ip dst 10.0.0.102 flowid 1:3
tc filter add dev eth0 parent 1:0 protocol ip prio 1 u32 match ip dst 10.0.0.103 flowid 1:3

清除eth0上的规则

tc qdisc del dev eth1 root> /dev/null

 

转自:http://www.zhengdazhi.com/archives/1241

weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
凉白开
  • 本文由 发表于 06/03/2015 01:00:05
  • 转载请务必保留本文链接:https://www.ttlsa.com/linux/iptables-router-acl/
评论  2  访客  2
    • 运维生存时间网友
      运维生存时间网友 9

      非技术宅用Panabit更方便

      • 匿名
        匿名 9

        非技术宅用Panabit更方便

      评论已关闭!