Chinaunix首页 | 论坛 | 博客
  • 博客访问: 8466
  • 博文数量: 5
  • 博客积分: 126
  • 博客等级: 入伍新兵
  • 技术积分: 70
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-12 15:27
文章分类

全部博文(5)

文章存档

2013年(2)

2012年(3)

我的朋友
最近访客

分类: LINUX

2012-12-17 13:45:26

Iptables防火墙功能设计文档

一、iptables基本管理命令 1.1iptables服务的管理

命令:

#查看Iptables服务状态

service iptables Options

Options

1. status 状态

2. start 开启

3. stop 关闭

4. retart 重启

返回值:

1

#查看iptables状态

service iptables status

Table: filter

Chain INPUT (policy ACCEPT)

target prot opt source destination

RH-Firewall-1-INPUT  all  --  0.0.0.0/0            0.0.0.0/0

Chain FORWARD (policy ACCEPT)

target     prot opt source               destination

RH-Firewall-1-INPUT  all  --  0.0.0.0/0            0.0.0.0/0

Chain OUTPUT (policy ACCEPT)

target     prot opt source               destination

Chain RH-Firewall-1-INPUT (2 references)

target     prot opt source               destination

ACCEPT all  --  0.0.0.0/0            0.0.0.0/0

ACCEPT icmp --  0.0.0.0/0            0.0.0.0/0           icmp type 255

ACCEPT     esp  --  0.0.0.0/0            0.0.0.0/0

ACCEPT     ah   --  0.0.0.0/0            0.0.0.0/0

ACCEPT     udp  --  0.0.0.0/0            224.0.0.251         udp dpt:5353

ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:631

ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED

REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited

2)

Firewall is not configured. 原因:防火墙禁用

#即时启动iptables服务

service  iptables  start

返回值:

1)

Applying iptables firewall rules:                             [  OK  ]

2)

Unloading iptables modules:                                [  OK  ]

原因:防火墙禁用

#关闭iptables服务

service  iptables  stop

返回值:

Flushing firewall rules:                                      [  OK  ]

Setting chains to policy ACCEPT: filter                       [  OK  ]

Unloading iptables modules:                                   [  OK  ]

#重启iptables服务

Service iptables  restart

返回值:

Flushing firewall rules:                                      [  OK  ]

Setting chains to policy ACCEPT: filter                       [  OK  ]

Unloading iptables modules:                                   [  OK  ]

Applying iptables firewall rules:                             [  OK  ]

#设置iptables开机启动

chkconfig iptables on 

返回值:

#设置iptables开机关闭

chkconfig iptables off

返回值:

1.2规则查询

#iptables Options

-L   --list  [chain]

List  all  rules  in  the selected chain.  If no chain is selected, all chains are listed. 

OTHER OPTIONS

-v, --verbose(详细)

-n, --numeric(数字输出)

-x, --exact(精确)

 --line-numbers(当列表显示规则时,在每个规则的前面加上行号,与该规则在链中的位置相对应)

 --modprobe=command

                             

返回值:

Chain INPUT (policy ACCEPT)

target     prot opt source               destination

RH-Firewall-1-INPUT  all  --  anywhere             anywhere

Chain FORWARD (policy ACCEPT)

target     prot opt source               destination

RH-Firewall-1-INPUT  all  --  anywhere             anywhere

Chain OUTPUT (policy ACCEPT)

target     prot opt source               destination

Chain RH-Firewall-1-INPUT (2 references)

target     prot opt source               destination

ACCEPT     all  --  anywhere             anywhere

ACCEPT     icmp --  anywhere             anywhere            icmp any

ACCEPT     ipv6-crypt--  anywhere             anywhere

ACCEPT     ipv6-auth--  anywhere             anywhere

ACCEPT     udp  --  anywhere             224.0.0.251         udp dpt:5353

ACCEPT     udp  --  anywhere             anywhere            udp dpt:ipp

ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED

REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited

Example

#查看防火墙所有策略

iptables  -t  filter  -L

#查看INPUT 策略

iptables  -t  filter  -L  INPUT  -v 

#查看策略编号

iptables  -t  filter  -L  INPUT  --line-number

1.3清空规则

iptables  【Options

-F   清除iptables内置规则

-X   清除用户自定义规则

返回值:无

Explanation

Flush the selected chain (all the chains in the table if none is given). This  is  equivalent

to deleting all the rules one by one.

1.4规则的保存

#规则保存

service  iptables  save

返回值

Saving firewall rules to /etc/sysconfig/iptables:          [  OK  ]

Explanation

重启后依然生效

二、iptables的相关配置文件 2.1配置文件

位置

/etc/sysconfig/iptables 

Explanation

执行 service iptables save 命令保存规则到 /etc/sysconfig/iptables

Example

#vi /etc/sysconfig/iptables

# Firewall configuration written by system-config-securitylevel

# Manual customization of this file is not recommended. 

*filter

:INPUT ACCEPT [0:0]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [0:0]

:RH-Firewall-1-INPUT - [0:0]

-A INPUT -j RH-Firewall-1-INPUT

-A FORWARD -j RH-Firewall-1-INPUT

-A RH-Firewall-1-INPUT -i lo -j ACCEPT

-A RH-Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT

-A RH-Firewall-1-INPUT -p 50 -j ACCEPT

-A RH-Firewall-1-INPUT -p 51 -j ACCEPT

-A RH-Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT

-A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT

-A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited

COMMIT

Explanation

可以把iptables规则直接写该配置文档里面:

开头的两行是注释

*filter 表示使用filter

一共有四种链:INPUTOUTPUTFORWARD 是内建的链,还有一个是RH-Firewall-1的链。

三、IP包过滤 3.1命令格式

基本格式:

            

iptables  -t   table     -Operation  chain     -j  target  match(es)

             filter        -A      INPUT           ACCEPT

             nat         -D      OUTPUT         DROP

             mangle      -I       FORWARD       REJECT

                         -R   

                                                                                                                                   

Explanation

1一个iptables命令基本上包含如下五部分:

    希望工作在哪个表上

    希望使用该表哪个链

    进行的操作(插入,添加,删除,修改)

    对特定规则的目标动作

    匹配数据报条件

2) filternatmangleiptables内建的三个表。

3filter是默认的表,表中有三个链:INPUT处理进入的包OUTPUT处理本地生成的包)和 FORWAR(处理通过的包),默认的链策略是ACCEPT.

4nat,这个表被查询时表示遇到了产生新的连接的包,由三个内建的链构成:PREROUTING (修改到来的包)、OUTPUT(修改之前本地的包)、POSTROUTING(修改准备出去的包)。

5mangle 这个表用来对指定的包进行修改。它有两个内建规则:PREROUTING(修改之前进入的包)和OUTPUT(修改路由之前本地的包)。

6)可以自定义链名

filter   INPUT          nat   PREROUTING        mangle  PREROUTING

       OUTPUT              OUTPUT                    OUTPUT 

       FORWARD            POSTROUTING

Options : 

              

-t 可以省略不写

-A ,--append chain rule-specification 

Append one or more rules to the end of the selected chain.

在所选择的链末添加一条或更多规则。

-D, --delete chain rulenum  

Delete one or more rules from the selected chain.

从所选链中删除一条或更多规则。

-I, --insert chain [rulenum] rule-specification 

Insert one or more rules in the selected chain as the given rule number.

根据给出的规则序号向所选链中插入一条或更多规则。

-R, --replace chain rulenum rule-specification

Replace a rule in the selected chain. 

从选中的链中取代一条规则

-N, --new-chain chain

Create a new user-defined chain by the given name. There must be no target of that name

Already.

根据给出的名称建立一个新的用户定义链。这必须保证没有同名的链存在

-X, --delete-chain [chain]

Delete the optional user-defined chain specified.

删除指定的用户自定义的非内建的链。

匹配条件:

-p  --protocol    规则或者包检查(待检查包)的协议。

-s  --source      指定源地址,可以是、网络名和清楚的IP地址。可以匹配单个ipip段(a network IP address (with /mask))、ip网段(192.168.100.1-192.168.100.199

-d  --destination  指定目标地址。可以匹配单个ipip段、ip网段。

-j  --jump target   指定规则的目标;也就是说,如果包匹配应当做什么。

-i  --in-interface   这是包经由该接口接收的可选的入口名称,包通过该接口接收(在链             INPUTFORWORDPREROUTING中进入的包)。

-o  --out-interface 这是包经由该接口送出的可选的出口名称,包通过该口输出(在链FORWARDOUTPUTPOSTROUTING中送出的包)

增加一条规则

Options: -A 

Example

1

iptables  -A INPUT  -s 192.168.100.199  -p icmp  -j  DROP

Explanation:禁止192.168.100.199主机 ping我的主机

2

iptables  -A  INPUT  -p  tcp  -s  0.0.0.0/0 -d  192.168.100.254 --dport 80 -j DROP

iptables  -A  INPUT  -p  tcp  -s  0.0.0.0/0  -d  192.168.100.254  --dport 22  -j  DROP

iptables  -I  INPUT  -p  tcp  -s 192.168.100.199  -d 192.168.100.254 --dport 80 -j ACCEPT

iptables  -I  NPUT  -p  tcp  -s 192.168.100.199  -d 192.168.100.254 --dport 22 -j ACCEPT

Explanation:允许指定的一个主机192.168.100.25480端口和22端口,其它主机不行

插入一条规则

Options-I,--insert

Example

1

iptables -A INPUT -p tcp -s 192.168.100.254 --sport 80 -d 192.168.100.199 -i eth0 -j ACCEPT

Explanation:允许192.168.100.254访问 192.168.100.19980端口,单ip的访问规则

2

iptables -A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT

Explanation本规则允许所有来自外部的SSH连接请求

3

iptables -A OUTPUT -o eth0  -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

Explanation允许从本地发起的SSH连接

4

# iptables -I INPUT -p tcp -s 192.1.68.1.0/24 -d 192.168.100.254 --dport 22 -j DROP

Explanation:不允许外部任何主机链接我的ssh端口

iptables -L

Chain INPUT (policy ACCEPT)

target     prot opt source               destination

DROP       tcp  --  anywhere             192.168.100.254     tcp dpt:ssh

删除# iptables -D INPUT -p tcp -s 192.1.68.1.0/24 -d 192.168.100.254 --dport 22 -j DROP

修改规则

Options-R

Example

iptables -R A INPUT -p tcp -s 192.168.100.254 --sport 80 -d 192.168.100.199 -i eth0 -j DROP

删除一条规则

Options-D --delete

Example

iptables -D INPUT -s 192.168.100.199 -p icmp -j DROP

Explanation:有俩种方式删除规则,Example中是规则不太复杂,比较清楚规则时使用;还有一种是通过规则编号

iptables -D INPUT 3

3.2 扩展iptables规则 #iptables有很好的扩展性

-m state 
      --state {NEW,ESTATBLISHED,INVALID,RELATED}  指定检测那种状态
-m string 按字符串限定
        --string "STRING" 指定字符串本身

-m multiport 指定多端口号
      --sport
      --dport
      --ports
-m iprange 指定IP
      --src-range ip-ip
      --dst-range ip-ip


-connlimit 连接限定
      --comlimit-above # 限定大连接个数


-m limit 现在连接速率,也就是限定匹配数据包的个数

Example

1

iptables -A  INPUT  -m  state  --state  NEW  -j  DROP 

拒绝进站的连接请求(外网无法访问本机)

2

#iptables -A  INPUT  -m  state  --state  ESTABLISHED,RELATED  -j  ACCEPT  

允许外网数据对本机的回应信息

3
#iptables  -p  OUTPUT  ACCEPT   

允许访问外网

4
iptables -A INPUT -s 0.0.0.0/0.0.0.0 -d 192.168.80.139 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT

5

# iptables -A INPUT -d 192.168.80.140 -p tcp --dport 80 -m state --state  NEW -m limit --limit 1/second --limit-burst 3 -j ACCEPT

6
iptables -A FORWARD -m iprange --src-range 192.168.1.5-192.168.1.124 -j ACCEPT  

指定了ip范围


内容过滤 :

Options -m string  --string  内容

Example

1

iptables -I FORWARD -m string --algo bm --string "qq" -j DROP

  

2

iptables -I FORWARD -s 192.168.3.159 -m string  --algo bm  --string" -j DROP

 

3  

iptables -I FORWARD -d 192.168.3.0/24 -m string  --algo  bm  --string "宽频影院" -j DROP

4   

iptables -I FORWARD -s 192.168.3.0/24 -m string  ---algo  bm -string "色情" -j DROP 

5  

iptables -I FORWARD -p tcp --sport 80 -m string --algo  bm  --string "广告" -j DROP 

Explanation

BM算法Boyer Moore Exact Pattern Matching Algorithms 精确字符串匹配算法algo algorithm 算法的意思

四、MAC过滤

匹配mac地址

Options-m mac  --mac--source 

Explanation:匹配。必须是XX:XX:XX:XX:XX这样的格式。注意它只对来自以太设备并进入PREROUTING、FORWORD和INPUT链的包有效。 

Example#iptables -I INPUT -m mac --mac-source 00:60:08:91:CC:B7 -j ACCEPT/DROP/REJECT

拒绝MAC地址为00:60:08:91:CC:B7的数据包进入

五、NAT 5.1 NAT原理

1)在使用NAT的时候,我们所使用的表不再是"filter",而是"nat"表,所以我们必须使用"-t nat"选项来显式地指明这一点。因为系统缺省的表是"filter",所以在使用filter功能时,我们没有必要显式的指明"-t filter"
    2)同filter表一样,nat表也有三条缺省的""(chains),这三条链也是规则的容器,它们分别是:
    3)PREROUTING:可以在这里定义进行目的NAT的规则,因为路由器进行路由时只检查数据包的目的ip地址,所以为了使数据包得以正确路由,我们必须在路由之前就进行目的NAT;
    4POSTROUTING:可以在这里定义进行源NAT的规则,系统在决定了数据包的路由以后在执行该链中的规则。
    5OUTPUT:定义对本地产生的数据包的目的NAT规则。

5.2 操作语法

1. 对规则的操作

加入(append) 一个新规则到一个链 (-A)的最后。 
在链内某个位置插入(insert) 一个新规则(-I),通常是插在最前面。 
在链内某个位置替换(replace) 一条规则 (-R)
在链内某个位置删除(delete) 一条规则 (-D)。 
删除(delete) 链内第一条规则 (-D)

2. 指定源地址和目的地址

    1通过--source/--src/-s来指定源地址(这里的/表示或者的意思),通过--destination/--dst/-d 来指定目的地址。
    2使用完整的域名,如“www.linuxaid.com.cn”
    3使用ip地址,如“192.168.1.1”
    4x.x.x.x/x.x.x.x指定一个网络地址,如“192.168.1.0/255.255.255.0”;
    5x.x.x.x/x指定一个网络地址,如“192.168.1.0/24”这里的24表明了子网掩码的有效 位数,这是UNIX环境中通常使用的表示方法。缺省的子网掩码数是32,也就是说指定192.168.1.1等效于192.168.1.1/32

3. 指定网络接口

    可以使用--in-interface/-i--out-interface/-o来指定网络接口。从NAT的原理可以看出,对于PREROUTING链,我们只能用-i指定进来的网络接口;而对于POSTROUTINGOUTPUT我们只能用-o指定出去的网络接口。

4. 指定协议及端口

    可以通过--protocol/-p选项来指定协议,如果是udptcp协议,还可--source-port/--sport和 --destination-port/--dport来指明端口。

Example

1iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j SNAT --to 1.2.3.4

Explanation更改所有来自192.168.1.0/24的数据包的源ip地址为1.2.3.4

这里需要注意的是,系统先进行路由及过滤等处理,直到数据包要被送出时才进行SNAT

2)iptables -t nat -A PREROUTING -s 192.168.1.0/24 -i eth1 -j DNAT --to 1.2.3.4

Explanation更改所有来自192.168.1.0/24的数据包的目的ip地址为1.2.3.4

六、端口映射

七、IptablesDDOS攻击

7.1  syn攻击的防御

检测syn攻击:

命令:netstat -n -p -t

返回值:

Active Internet connections (w/o servers)

Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   

tcp        0    196 192.168.100.251:22          192.168.100.223:50856       ESTABLISHED 27846/sshd         

防御syn攻击:

1、修改等待数:

# sysctl -w net.ipv4.tcp_max_syn_backlog=2048

Options默认1024,可调整至2048

Explanation

/etc/sysctl.conf 文件里有如下配置:

net.ipv4.ip_forward = 0

net.ipv4.conf.default.rp_filter = 1

net.ipv4.conf.default.accept_source_route = 0

kernel.sysrq = 0

kernel.core_uses_pid = 1

Explanation

tcp_max_syn_backlogSYN队列的长度

tcp_syncookies是一个开关,是否打开SYN Cookie 功能该功能可以防止部分SYN攻击    

tcp_synack_retriestcp_syn_retries定义SYN 的重试次数

     

2启用syncookies

Options1表示开启syncookies  0 表示关闭

sysctl -w net.ipv4.tcp_syncookies=1

3、修改重试次数

#sysctl -w net.ipv4.tcp_syn_retries = 0

Options 重传次数设置为0,只要收不到客户端的响应,立即丢弃该连接,默认设置为5

4、使用iptables限制单个地址的并发连接数量:

Options:  -m connlimit --conlimit 

Example:

iptables -t filter -A INPUT -p tcp --dport 80 --tcp-flags FIN,SYN,RST,ACK SYN -m connlimit --connlimit-above 10 --connlimit-mask 32 -j REJECT

5、使用iptables限制单个c类子网的并发链接数量:

Options: --connlimit-mask

Example:

iptables -t filter -A INPUT -p tcp --dport 80 --tcp-flags FIN,SYN,RST,ACK SYN -m connlimit --connlimit-above 10 --connlimit-mask 24 -j REJECT

6、限制单位时间内的连接数:

Example:

iptables -t filter -A INPUT -p tcp --dport 80 -m  state --state NEW -m recent --set --name access --resource
iptables -t filter -A INPUT -p tcp --dport 80 -m  state --state NEW -m recent --update --seconds 60 --hitcount 30 --name access -j DROP

iptables -t filter -A INPUT -p tcp --dport 80 -m --state --syn -m recent --set
iptables -t filter -A INPUT -p tcp --dport 80 -m --state --syn -m recent --update --seconds 60 --hitcount 30 -j DROP

7、修改/etc/modprobe.conf

Options: 1000地址数 60包数

Example:

 ipt_recent ip_list_tot=1000  ip_pkt_list_tot=60 

记录10000个地址,每个地址60个包 ip_list_tot最大为8100,超过这个数值会导致iptables错误

8、限制单个地址最大连接数:

Options: -m connlimit --connlimit-above 

Example:

iptables -I INPUT -p tcp --dport 80 -m connlimit --connlimit-above 50 -j DROP

9、缩短SYN- Timeout时间:

Options: --syn -m limit --limit 1/s 

Example:

iptables -A FORWARD -p tcp --syn -m limit --limit 1/s -j ACCEPT

iptables -A INPUT -i eth0 -m limit --limit 1/sec --limit-burst 5 -j ACCEPT

10、Ping洪水攻击(Ping of Death

Options: --icmp-type echo-request -m limit --limit 1/s

Example:

iptables -A FORWARD -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT

7.2 防范CC攻击

概念和原理:

攻击者借助生成指向受害主机的合法请求,实现DOS,和伪装就叫:cc(ChallengeCollapsar)CC攻击是DDOS(分布式拒绝服务)的一种。CC攻击的原理就是攻击者控制某些不停地发大量给对方服务器造成服务器资源耗尽,一直到宕机崩溃。CC主要是用来攻击页面的,CC就是模拟多个用户(多少线程就是多少用户)不停地进行访问那些需要大量数据操作(就是需要大量CPU时间)的页面,造成服务器资源的浪费,CPU长时间处于100%,永远都有处理不完的连接直至就网络拥塞,正常的访问被中止。

防御方法:

控制单个IP的最大并发连接数

用到模块 connlimit 

connlimit 作用:连接限制   

--connlimit-above  n  限定并发连接的最大数目
--connlimit-mask   x 这组主机的掩码,默认是connlimit-mask 32,即每ip. 限定某个网络的最大并发连接数,x32时,代表具体的主机,为默认值。x的值一般是代表A,B,C三类网络的8,16,24

Example

1)

只允许每个ip同时580端口转发,超过的丢弃:   

iptables -I FORWARD -p tcp --syn --dport 80 -m connlimit --connlimit-above 5 -j DROP  

2)

只允许每组ip同时1080端口转发:   

iptables -I FORWARD -p tcp --syn --dport 80 -m connlimit --connlimit-above 10 --connlimit-mask 24 -j DROP 

3)

控制单个iP在一定的时间(比如60秒)内允许新建立的连接数

iptables -A INPUT -p tcp --dport 80 -m recent --name nihao --update --seconds 20 --hitcount 20 -j REJECT 

iptables -A INPUT -p tcp --dport 80 -m recent --name BAD_HTTP_ACCESS --set -j ACCEPT
单个IP60秒内只允许最多新建30个连接

4)

限制 ssh 猜密码,对每个 IP 允许三分钟内允许有 次 TCP 的 NEW 请求

iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --name ROUTER-SSH --update --seconds 180 --hitcount 5  -j REJECT --reject-with tcp-reset

iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --name ROUTER-SSH --set -j ACCEPT

7.3 应对 ACK攻击

ACK 攻击是针对syn-cookies而发产生的,通过发送大量的ACK数据报,使目标服务器忙于计算,达到拒绝服务的目的,使用iptables对发起 ACK攻击的地址进行限制

iptables -I INPUT -p tcp --dport 80 -m connlimit --connlimit-above 50 -j DROP

限制并发连接数不大于50

iptables -t filter -A INPUT -p tcp --dport 80 --tcp-flags FIN,SYN,RST,ACK ACK -m connlimit --connlimit-above 10 --connlimit-mask 32 -j REJECT 限制并发ACK不大于50


iptables -t filter -A INPUT -p tcp --dport 80 --tcp-flags FIN,SYN,RST,ACK ACK -m recent --set --name drop

iptables -t filter -A INPUT -p tcp --dport 80 --tcp-flags FIN,SYN,RST,ACK ACK -m recent --update --seconds 60 --hitcount 30 -j DROP 一分钟内大于30次的连接全部丢弃。

阅读(625) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~