1.策略与规则链

  iptables 服务把用于处理或过滤流量的策略条目称之为规则,多条规则可以组成一个规则链,而规则链则依据数据包处理位置的不同进行分类,具体如下:

    在进行路由选择前处理数据包(PREROUTING);
    处理流入的数据包(INPUT);
    处理流出的数据包(OUTPUT);
    处理转发的数据包(FORWARD);
    在进行路由选择后处理数据包(POSTROUTING)。

  一般来说,从内网向外网发送的流量一般都是可控且良性的,因此我们使用最多的就是INPUT 规则链,该规则链可以增大黑客人员从外网入侵内网的难度。

  处理匹配流量的动作:

    ACCEPT(允许流量通过)、REJECT(拒绝流量通过)、LOG(记录日志信息)、DROP(拒绝流量通过)。

    DROP:直接将流量丢弃而且不响应;

    REJECT :在拒绝流量后再回复一条“您的信息已经收到,但是被扔掉了”信息,从而让流量发送方清晰地看到数据被拒绝的响应信息。

    当把 Linux 系统中的防火墙策略修改成DROP 拒绝动作后,流量发送方会看到响应超时的提醒。但是流量发送方无法判断流量是被拒绝,还是接收方主机当前不在线:

    当把 Linux 系统中的防火墙策略设置为REJECT 拒绝动作后,流量发送方会看到端口不可达的响应;

    举例: reject

[root@localhost ~]# ping -c 4 192.168.10.10
PING 192.168.10.10 (192.168.10.10) 56(84) bytes of data.
From 192.168.10.10 icmp_seq=1 Destination Port Unreachable
From 192.168.10.10 icmp_seq=2 Destination Port Unreachable
From 192.168.10.10 icmp_seq=3 Destination Port Unreachable
From 192.168.10.10 icmp_seq=4 Destination Port Unreachable
--- 192.168.10.10 ping statistics ---
4 packets transmitted, 0 received, +4 errors, 100% packet loss, time 3002ms

    drop    

[root@localhost ~]# ping -c 4 192.168.10.10
PING 192.168.10.10 (192.168.10.10) 56(84) bytes of data.
--- 192.168.10.10 ping statistics ---
4 packets transmitted, 0 received, 100% packet loss, time 3000ms

2.iptables中的基本命令参数

    

  1)把INPUT规则链的默认规则设置为拒绝:  

[root@localhost ~]# iptables -P INPUT  DROP

  此时ssh远程连接会断开。

  2)向INPUT规则链中添加允许ICMP 和SSH流量进入的策略规则: 

root@localhost ~]# iptables -I INPUT -p icmp -j ACCEPT

  此时从远程设备ping 通测试机

3)向INPUT规则链中添加允许10.10.65.65远程SSH登录的策略规则:

[root@localhost ~]# iptables -I INPUT -s 10.10.65.65/32 -p tcp --dport 22 -j ACCEPT

  4)查看规则链  

[root@localhost ~]# iptables -L
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  10.10.65.65          anywhere             tcp dpt:ssh
ACCEPT     icmp --  anywhere             anywhere            

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

  5)清除所有的防火墙规则链,默认规则除外  

[root@localhost ~]# iptables -F
[root@localhost ~]# iptables -L
Chain INPUT (policy DROP)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination      

  此时ssh远程连接会断开。

  6)向INPUT 规则链中添加拒绝所有人访问本机12345 端口的策略规则:  

[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
REJECT     udp  --  anywhere             anywhere             udp dpt:italk reject-with icmp-port-unreachable
REJECT     tcp  --  anywhere             anywhere             tcp dpt:italk reject-with icmp-port-unreachable

  7)向INPUT 规则链中添加拒绝所有主机访问本机1000~1024 端口的策略规则:  

[root@localhost ~]# iptables -A INPUT -p tcp --dport 1000:1024 -j REJECT
[root@localhost ~]# iptables -A INPUT -p udp --dport 1000:1024 -j REJECT

  注:使用iptables 命令配置的防火墙规则默认会在系统下一次重启时失效,如果想让配置的防火墙策略永久生效,还要执行保存命令: 

[root@localhost ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables: [ OK ]

3.centos 7 中没有iptables 和service iptables save 指令使用失败问题解决方案

  如果执行service iptables save 命令来保存iptables时失败,报出:  

The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl.

  解决方法:

  systemctl stop firewalld 关闭防火墙

  yum install iptables-services 安装或更新服务

  再使用systemctl enable iptables 启动iptables

  最后 systemctl start iptables 打开iptables

  再执行service iptables save

  重启iptables服务:

  service iptables restart

  执行完毕之后/etc/syscofig/iptables文件就有了

 

posted on 2020-08-25 14:36  星痕1216  阅读(161)  评论(0编辑  收藏  举报