观察iptable规则添加的方法,删除和查询的方法。本案例并不是为了体验策略效果。
iptables -t filter -A INPUT -p tcp -j ACCEPT #最后一行 iptables -I INPUT -p udp -j ACCEPT #第一行 iptables -I INPUT 4 -p icmp -j ACCEPT #(插入到第4行)#第4行 iptables -L #看一看 iptables -D INPUT 3 # 删除第三行 iptables -F #全清空---------(改不了默认) service iptables save #保存 systemctl restart iptables #重启 注意:如果不保存重启之后规则就不在了。1、通用匹配(协议),可以独立使用
协议:-p (小p) tcp ---用的最多 udp icmp ---ping的时候用的协议 #使用协议的时候可以不指定端口,使用端口的时候必须指定协议。 案例: 禁止自己被ping,在filter表的INPUT链插入一个丢弃icmp的规则。 # iptables -I INPUT -p icmp -j REJECT ----拒绝 验证: [root@iptables-test ~]# ping 192.168.246.200 PING 192.168.246.200 (192.168.246.200) 56(84) bytes of data. From 192.168.246.200 icmp_seq=1 Destination Port Unreachable2、通过端口规则匹配:
端口: --sport ---源端口 --dport --目标端口 案例: 拒绝192.168.246.201这台机器通过ssh连接到这台服务器 # iptables -I INPUT -s 192.168.246.201 -p tcp --dport 22 -j REJECT 例子:端口的范围: 拒绝22端口到80端口的访问。(22-80),包括22和80端口在内 # iptables -I INPUT -s 192.168.246.201 -p tcp --dport 22:80 -j REJECT ======================================================== 验证: # curl -I http://192.168.246.200 curl: (7) Failed connect to 192.168.246.200:80; Connection refused # ssh root@192.168.246.200 ssh: connect to host 192.168.246.200 port 22: Connection refused3、通过ip地址
1.#禁止源246.201主机进来。(换个主机ping一下,就可以通信) [root@iptables-server ~]# iptables -I INPUT -s 192.168.246.201 -p icmp -j REJECT -s: 源ip地址 在源ip机器验证: [root@iptables-test ~]# ping 192.168.246.200 PING 192.168.246.200 (192.168.246.200) 56(84) bytes of data. From 192.168.246.200 icmp_seq=1 Destination Port Unreachable =========================================================================== 2.拒绝多个ip地址:后面跟ip地址可以更多个ip地址用逗号隔开 # iptables -t filter -I INPUT -s 192.168.246.201,192.168.246.133 -p icmp -j REJECT # iptables -t filter -I INPUT -s 192.168.246.201,192.168.246.133 -p tcp --dport 22:80 -j REJECT 验证:在源ip地址通过curl访问。在246.133和246.201机器分别验证 # curl -I http://192.168.246.200 curl: (7) Failed connect to 192.168.246.200:80; Connection refused # ssh root@192.168.246.200 ssh: connect to host 192.168.246.200 port 22: Connection refused ============================================================ 3.举例::#限制源10网段的数据包。 # iptables -I INPUT -s 192.168.10.0/24 -j DROP4、修改规则:
# iptables -L target prot opt source destination REJECT tcp -- 192.168.246.133 anywhere tcp dpts:ssh:http reject-wi REJECT tcp -- 192.168.246.201 anywhere tcp dpts:ssh:http reject-wi REJECT icmp -- 192.168.246.201 anywhere reject-with icmp-port-unreachable 将修改第二条规则访问80端口: # iptables -R INPUT 2 -p tcp --dport 80 -s 192.168.246.201 -j ACCEPT # iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination REJECT tcp -- 192.168.246.133 anywhere tcp dpts:ssh:http reject-with icmp-port-unreachable ACCEPT tcp -- 192.168.246.201 anywhere tcp dpt:http REJECT icmp -- 192.168.246.201 anywhere reject-with icmp-port-unreachable 验证在修改为允许访问的源ip机器上: # curl -I http://192.168.246.200 HTTP/1.1 200 OK5、icmp类型匹配
禁止ping策略原则 iptables服务器是ping命令发起者或是接受者 -i --in-interface:在INPUT链配置规则中,指定从哪一个网卡接口进入的流量(只能配置在INPUT链上) -o --out-interface:在OUTPUT链配置规则中,指定从哪一个网接口出去的流量(只能配置在OUTPUT链上) ==================================================== icmp的类型: 0: Echo Reply——回显应答(Ping应答) 8: Echo request——回显请求(Ping请求) ===================================================== iptables服务器-----发起者:ping 别的机器 input链: 禁止icmp-type 0 1.自己不能ping别人,但是别人可以ping自己,自己也可以ping自己: [root@iptables-server ~]# iptables -A INPUT -i ens33 -p icmp --icmp-type 0 -j DROP #将ping的回显答应给禁止掉了 [root@iptables-server ~]# iptables -A OUTPUT -o ens33 -p icmp --icmp-type 8 -j DROP #ping发出的请求禁止掉了 验证: [root@iptables-server ~]# ping 192.168.246.133 #ping不通。 PING 192.168.246.133 (192.168.246.133) 56(84) bytes of data. ping: sendmsg: Operation not permitted [root@jenkins-server ~]# ping 192.168.246.200 #可以ping通 PING 192.168.246.200 (192.168.246.200) 56(84) bytes of data. 64 bytes from 192.168.246.200: icmp_seq=1 ttl=64 time=0.280 ms =========================================================================================== iptables服务器作为接受者。也就是别人ping自己: 本机可以ping自己也可以ping其他机器。其他机器不能ping通本机: [root@iptables-server ~]# iptables -A INPUT -i ens33 -p icmp --icmp-type 8 -j DROP #将发送进来的ping请求给禁止掉了 [root@iptables-server ~]# iptables -A OUTPUT -o ens33 -p icmp --icmp-type 0 -j DROP #将输出的回显答应给禁止掉了 验证: [root@iptables-server ~]# ping 192.168.246.201 #ping其他机器通 PING 192.168.246.201 (192.168.246.201) 56(84) bytes of data. 64 bytes from 192.168.246.201: icmp_seq=1 ttl=64 time=0.491 ms [root@iptables-test ~]# ping 192.168.246.200 #其他机器ping不同 PING 192.168.246.200 (192.168.246.200) 56(84) bytes of data. =========================================================================================显示匹配:如端口匹配,IP范围,MAC地址,等特殊匹配
#iptables -m iprange --help 1.指定ip范围: 语法: -m iprange --src-range # iptables -I INPUT -p tcp --dport 80 -m iprange --src-range 192.168.246.199-192.168.246.206 -j REJECT 2.指定多端口范围:一次拒绝多个指定端口 语法: -m multiport --sports #源端口 -m multiport --dports #目的端口 # iptables -A INPUT -p tcp -m multiport --dports 22,80 -s 192.168.246.133 -j REJECT 验证:在246.133机器上 # ssh root@192.168.246.200 #不通 ssh: connect to host 192.168.246.200 port 22: Connection refused 3.MAC地址匹配 拒绝MAC地址的匹配:只能匹配源MAC地址 语法: -m mac --mac-source # iptables -I INPUT -m mac --mac-source 00:0C:29:64:E3:8D -j REJECT #将指定的MAC地址服务请求全部禁止了通过网卡接口:
# iptables -I INPUT -i ens33-j DROP #谁也连不上了.保存和删除规则
删除: # iptables -D INPUT 3 #通过查看行号,指定行号删除; # iptables -D INPUT -p icmp -j REJECT #方式二 ======================================================================================= 保存: [root@iptables-server ~]# iptables-save > /etc/sysconfig/iptables #保存到文件里面,方式一 [root@iptables-server ~]# service iptables save #第二种方式,推荐 iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ] 最后写完规则后记得保存!firewalld 管理防火墙规则的模式(动态):任何规则的变更都不需要对整个防火墙规则列表进行重新加载
rhel 7:firewall-cmd工具,firewalld服务
firewalld将网卡对应到不同的区域(zone)
trusted :允许所有流量通过 home/internal:仅允许ssh数据通过 work:仅允许ssh,ipp-client,dhcpv6-client数据通过 public:默认区域,仅允许ssh,dhcpv6-client数据通过 external:仅允许ssh数据通过,通过该区域的数据将会伪装(SNAT/DNAT) dmz:仅允许ssh数据通过 block:任何传入的网络数据包都将被阻止。拒绝所有流量 drop:拒绝所有流量查看默认区域:
[root@iptables-server ~]# firewall-cmd --get-default-zone public 验证: 在192.168.246.201机器上访问192.168.246.200 [root@iptables-test ~]# curl -I http://192.168.246.200 #不通 curl: (7) Failed connect to 192.168.246.200:80; No route to host [root@iptables-test ~]# ssh root@192.168.246.200 #ssh 可以 root@192.168.246.200's password:2、更改默认区域
[root@iptables-server ~]# firewall-cmd --set-default-zone=trusted success [root@iptables-server ~]# firewall-cmd --reload success [root@iptables-server ~]# firewall-cmd --get-default-zone trusted 验证: 在192.168.246.201机器上访问192.168.246.200 [root@iptables-test ~]# curl -I http://192.168.246.200 #访问成功 HTTP/1.1 200 OK ================================================ 修改回默认区域: [root@iptables-server ~]# firewall-cmd --set-default-zone=public success [root@iptables-server ~]# firewall-cmd --reload success3.向public区域添加服务
[root@iptables-server ~]# firewall-cmd --permanent --add-service=http --zone=public success [root@iptables-server ~]# firewall-cmd --reload #重新加载配置文件 success 验证: 在192.168.246.201机器上访问192.168.246.200 [root@iptables-test ~]# curl -I http://192.168.246.200 HTTP/1.1 200 OK4.指定IP地址为192.168.246.201/24的客户端进入drop区域
[root@iptables-server ~]# firewall-cmd --permanent --add-source=192.168.246.201/24 --zone=drop success [root@iptables-server ~]# firewall-cmd --reload success 验证: 在192.168.246.201的机器上访问246.200 [root@iptables-test ~]# curl -I http://192.168.246.200 #访问不通5.将192.168.246.201/24移除drop区域
[root@iptables-server ~]# firewall-cmd --permanent --remove-source=192.168.246.201/24 --zone=drop success [root@iptables-server ~]# firewall-cmd --reload success 验证: 在192.168.246.201的机器上面访问246.200 [root@iptables-test ~]# curl -I http://192.168.246.200 #访问成功 HTTP/1.1 200 OK6.向pubic区域添加服务,以添加端口的方式
[root@iptables-server ~]# firewall-cmd --permanent --add-port=80/tcp --zone=public success [root@iptables-server ~]# firewall-cmd --reload success 验证: 用192.168.246.201访问192.168.246.200机器 [root@iptables-test ~]# curl -I http://192.168.246.200 HTTP/1.1 200 OK7.删除服务、端口
[root@iptables-server ~]# firewall-cmd --permanent --remove-service=http --zone=public success [root@iptables-server ~]# firewall-cmd --reload success 验证: 用192.168.246.201访问192.168.246.200机器 [root@iptables-test ~]# curl -I http://192.168.246.200 #访问通 HTTP/1.1 200 OK ==================================================================================== [root@iptables-server ~]# firewall-cmd --permanent --remove-port=80/tcp --zone=public success [root@iptables-server ~]# firewall-cmd --reload success 验证: 在192.168.246.201访问192.168.246.200机器 [root@iptables-test ~]# curl -I http://192.168.246.200 #访问失败 curl: (7) Failed connect to 192.168.246.200:80; No route to host