|NO.Z.00048|——————————|Applications|——|防火墙.V2|——|3台server|

一、实验专题:添加规则,常规操作。
### --- 添加规则,常规操作。
### --- 开启防火墙并设置开机自启动

[root@server11 ~]# service iptables start
iptables: Applying firewall rules:                         [  OK  ]
[root@server11 ~]# chkconfig iptables on
### --- 查看当前防火墙的规则
 
[root@server11 ~]# iptables -L                                              // 默认查看的就是filter表
[root@server11 ~]# iptables -t filter -L                                    // 上面命令完整写法
Chain INPUT     (policy ACCEPT)                                             // 默认规则有两种:ACCEPT和DROP
Chain FORWARD   (policy ACCEPT)
Chain OUTPUT    (policy ACCEPT)
### --- 清空当前表的所有规则
[root@server11 ~]# iptables -F  
 
### --- 添加一条规则
[root@server11 ~]#  iptables -t filter -A INPUT -p tcp -j ACCEPT 
[root@server11 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere   

[root@server11 ~]# iptables -L -n                                           // -n以端口号的方式去展示
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0                          // 0.0.0.0/0表示所有地址

[root@server11 ~]# iptables -L -n -v
Chain INPUT (policy ACCEPT 3 packets, 234 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   36  2396 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0  
~~~     36:这条规则过滤了多个数据包了  2396:过滤的字节大小 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0  
~~~     加入-v就知道那条规则过滤的报文多了,过滤的报文多的规则可以把它放在前面。
~~~     在书写规则的时候,把匹配较多的规则放在最上面。
### --- 添加一条-I选项的规则

[root@server11 ~]#  iptables -t filter -I INPUT -p udp -j ACCEPT 
[root@server11 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     udp  --  anywhere             anywhere                           // -I 默认是添加到规则的第一行      
ACCEPT     tcp  --  anywhere             anywhere 
[root@server11 ~]#  iptables -t filter -A INPUT -p ICMP -j ACCEPT 
 
[root@server11 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     udp  --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             anywhere            
ACCEPT     icmp --  anywhere             anywhere                           // -A默认添加到规则的最后一行
[root@server11 ~]# iptables -I INPUT 2 -p ICMP -j ACCEPT                    // -I可以指定规则第二列添加的列的,2表示规则的

[root@server11 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     udp  --  anywhere             anywhere            
ACCEPT     icmp --  anywhere             anywhere                           // -I INPUT 2 :INPUT链的第二列           
ACCEPT     tcp  --  anywhere             anywhere            
ACCEPT     icmp --  anywhere             anywhere
### --- 显示规则列的序号

[root@server11 ~]# iptables -L --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     udp  --  anywhere             anywhere            
2    ACCEPT     icmp --  anywhere             anywhere            
3    ACCEPT     tcp  --  anywhere             anywhere            
4    ACCEPT     icmp --  anywhere             anywhere
### --- 删除一条固定的规则

[root@server11 ~]# iptables -t filter -D INPUT 4                            // 删除第四条的ICMP规则
[root@server11 ~]# iptables -L --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     udp  --  anywhere             anywhere            
2    ACCEPT     icmp --  anywhere             anywhere            
3    ACCEPT     tcp  --  anywhere             anywhere  
### --- 删除所有规则

[root@server11 ~]# iptables  -F
[root@server11 ~]# iptables -t filter -F                                    // 上面命令的完整写入,实现结果一样
### --- 放行Apache服务

[root@server11 ~]# service httpd start
[root@server11 ~]# echo "this is iptables output port 80 22" >>/var/www/html/index.html
[root@server11 ~]# curl localhost
this is iptables output port 80 22
[root@server11 ~]# iptables -t filter -A INPUT -p tcp --dport 22 -j ACCEPT  // 放行2端口
[root@server11 ~]# iptables -L 
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh 

[root@server11 ~]# iptables -t filter -I INPUT -p tcp --dport 80 -j ACCEPT  // 放心80端口,80比22端口的访问量大,卸载最前面。加入-I
[root@server11 ~]# iptables -L 
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:http 
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh 
[root@server11 ~]# iptables -P INPUT DROP                                   // 默认规则设置为拒绝
[root@server11 ~]# iptables -L 
Chain INPUT (policy DROP)                                                   // 其它全部拒绝
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:http   // 放行80
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh    // 放行22

### --- 验证:
~~~     通过Chrome访问http://10.10.10.11/可以正常访问
~~~     通过CMD窗口ping 10.10.10.11ping不同的,ICMP不在默认规则里,所以拒绝。
二、防火墙类型
### --- 匹配类型(它们三者难度是依次增加的)

~~~     通用匹配
~~~     可直接使用,不依赖于其他条件或扩展
~~~     包括网络协议、IP地址、网络接口等条件
~~~     隐含匹配
~~~     要求以特定的协议匹配作为前提
~~~     包括端口、TCP标记、ICMP类型等条件
~~~     显示匹配
~~~     要求以“-m 扩展模块”的形式明确指出类型
~~~     包括多端口、MAC地址、IP范围、数据包状态等条件
### --- 通用匹配

~~~     常见的通用匹配条件
~~~     协议匹配:-p  协议名
~~~     地址匹配:-s 源地址、-d    目的地址
~~~     接口匹配:-i  入站网卡、-o  出站网卡
### --- 通用匹配

iptables -A FORWARD -s 192.168.1.11 -j REJECT                        // 添加一条规则到FORWARD表的末尾源地址192.168.1.11的值;filter表做的过滤,FORWARD链做的是转发,在源地址是192.168.1.11的时候在我这台机器做转发请求的时候REJECT拒绝。
iptables -I INPUT -s  10.20.30.0/24  -j  DROP                        // 在filter表的INPUT链的最开头添加一条规则,当入站源地址是10.20.30.0/24位的时候拒绝
iptables -I  INPUT -p  icmp -j  DROP                                 // 当入站请求时ICMP协议的时候拒绝
iptables -A  FORWARD -p ! icmp -j ACCEPT                            // 感叹号表示非:非ICMP协议。非ICMP拒绝,其他的都放行。
iptables -A  INPUT -i  eth1  -s  172.16.0.0/12 -j  DROP              // 当你的入站网卡是eth1并且源地址是172.16.0.0/12位的时候丢弃。

三、实验专题:丢弃和拒绝的区别
### --- 实验专题:丢弃和拒绝的区别

~~~     HA-server1:10.10.10.11:web服务器
~~~     HA-server2:10.10.10.12:client
### --- HA-server1下启动iptables

[root@server11 ~]# service iptables start
[root@server11 ~]# chkconfig iptables on
[root@server11 ~]# iptables -P INPUT ACCEPT                                 // 默认规则还原
[root@server11 ~]# iptables -L              
Chain INPUT (policy ACCEPT)                                                 // 默认规则放行           
[root@server11 ~]# iptables -F                                              // 清空防火墙
[root@server11 ~]# iptables -L
### --- 添加一条丢弃规则

[root@server11 ~]# iptables -A INPUT -p tcp --dport 80 -j DROP      
[root@server11 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
DROP       tcp  --  anywhere             anywhere            tcp dpt:http   // DROP丢弃
### --- 验证丢弃:client HA-srver2向HA-server1服务端发起请求

[root@server11 ~]# service httpd start
[root@server11 ~]# echo "this is the HA-serve1 IPtablwes DROP" >>/var/www/html/index.html
[root@server11 ~]# curl localhost
this is the HA-serve1 IPtablwes DROP

[root@server12 ~]# curl 10.10.10.11                                         // 执行不下去,client发起数据报文,它认为服务端已经接受到数据报文了,等待服务端回应,但服务端把数据包丢弃了,所以没有回应,所以client等待到响应超时。
 curl: (7) couldn't connect to host
[root@server11 ~]# iptables -F                                              // 把规则清空
[root@server12 ~]# curl 10.10.10.11
this is the HA-serve1 IPtablwes DROP                                        // 可以正常访问了
### --- 验证拒绝:

[root@server11 ~]# iptables -A INPUT -p tcp --dport 80 -j REJECT
[root@server11 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
REJECT     tcp  --  anywhere             anywhere            tcp dpt:http reject-with icmp-port-unreachable
[root@server12 ~]# curl 10.10.10.11                                         //立马响应,不会等到响应超时才中断,直接决绝,立马反馈响应。
curl: (7) couldn't connect to host

### --- 问题一:什么时候用拒绝,什么时候用丢弃呢?
~~~     答案:REJECT需要消耗一定的资源,比如web维护这种需要响应给客户的数据使用拒绝,
~~~     而数据库的3306,这是一个非法入侵,直接丢弃。不需要回应,DROP不需要消耗CPU资源。

四、实验专题:添加一条规则,ICMP的所有类型全部拒绝
### --- 实验专题:添加一条规则,ICMP的所有类型全部拒绝
### --- 添加一条规则,ICMP的所有类型全部拒绝。

[root@server11 ~]# iptables -F
[root@server11 ~]# iptables -A INPUT -p icmp -j DROP                        // 把入站ICMP的ping放行,出站ACCEPT放行。
[root@server11 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
DROP       icmp --  anywhere             anywhere 
[root@server11 ~]# ping 10.10.10.12                                         // ping不通,数据报文从10.10.10.11发起请求,可以正常出去,默认策略是ACCEPT放行,并且10.10.10.12也会给回应,但会的时候回应的这个数据报文要进行入站,入站的时候发现ICMP是DROP是丢弃的,所以ping是不通的。
### --- 添加一条规则,我可以ping 其它主机,其它主机不可以ping我

[root@server11 ~]# iptables -F
[root@server11 ~]# iptables -A INPUT -p icmp -j DROP
[root@server11 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
DROP       icmp --  anywhere             anywhere                           // 这条规则表示,我去ping其它主机,还是其它主机ping该主机都是ping不通的。
[root@server11 ~]# iptables -I INPUT -p icmp --icmp-type 0 -j ACCEPT
[root@server11 ~]# iptables -I INPUT -p icmp --icmp-type 3 -j ACCEPT
[root@server11 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     icmp --  anywhere             anywhere            icmp destination-unreachable       //3级别,主机不可达destination-unreachable
ACCEPT     icmp --  anywhere             anywhere            icmp echo-reply                    //0级别,回显,echo-reply 

[root@server11 ~]# ping 10.10.10.12                                         // 本机可以ping其它主机
PING 10.10.10.12 (10.10.10.12) 56(84) bytes of data.
64 bytes from 10.10.10.12: icmp_seq=1 ttl=64 time=0.237 ms
[root@server12 ~]# ping 10.10.10.11                                         // 其它主机不可以ping本主机
PING 10.10.10.11 (10.10.10.11) 56(84) bytes of data.
五、显示匹配
### --- 显示匹配
### --- 常用的显示匹配条件
### --- 多端口匹配:-m multiport --sport 源端口列表
~~~                -m multiport --dport 目的端口列表
~~~     IP范围匹配:-m iprange  -src-range  IP范围
~~~     MAC地址匹配:-m mac  --mac1-source MAC地址
~~~     状态匹配:-m state --state 连接状态
### --- 显示匹配

~~~     iptables    -A    INPUT    -p    tcp    -m    multiport    --dport    25.80.110.143 -j    ACCEPT        
~~~     iptables    -A    FORWARD    -p    tcp    -m    iprange    --src-range    192.168.4.21-192.168.4.28 -j  ACCEPT           // --src-range:添加ip的范围
~~~     iptables    -A    INPUT    -m    mac    --mac-source    00:0c:29:c0:55:3f:    -j    DROP                    //-mac-source:添加Mac地址的方式添加对应的处理
~~~     iptables    -P    INPUT DROP
~~~     iptables    -I    INPUT    -p    tcp    -m    multiport    --dport    80-82,85    -j    ACCEPT    
~~~     iptables     -I    INPUT    -p    tcp    -m    state    --state    NEW,ESTABLISHED,RELATED    -j    ACCEPT            // -m state:表示当前的状态模块,--state NEW:指定当前的状态是NEW新建连接,ESTABLISHED:有数据传输,RELATED:有相关连,
### --- 建议生产环境中使用。
### --- 案例一:搭建一个ftp服务供视频下载,待定时间是2点钟。但是2点钟的时候有一个用户下载到99%,还有1%未下载完成,
### --- 方案一:若是直接中断,导致该用户下载不完成,体验不行。
### --- 方案二:若是直接供下载完成,那么这个时间段会有新的连接进来,也达不到我们的目的
### --- 解决方案:新建一条规则:让这个用户下载完成,拒绝其他用户的连接。
iptables     -I    INPUT    -p    tcp    -m    --dport 21 state    --state    NEW   -j DROP // 添加一条规则在最开头,当原地址并且这个端口是21号端口的时候,并且这个连接时新建连接的时候拒绝。这样旧的连接不会断,新连接会断。

### --- 案例二:80端口 443端口 22端口;之前有链接的都放行
iptables     -I    INPUT    -p    tcp     state    --state   ESTABLISHED    -j ACCEPT 
iptables     -I    INPUT    -p    tcp     state    --state   ERLATED    -j ACCEPT           // 之前和80/443//22之前有连接的都放行。
### --- 显示匹配

~~~     SANT策略的典型应用环境
~~~     局域网主机共享单个公网IP地址介入INternet
~~~     SNAT策略的原理
~~~     源地址转换:Source Network Address Translation
~~~     修改数据包的源地址

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(20)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示