企业级iptalbes防火墙(2)

it2024-04-14  49

四 iptables操作

1、安装

centos(5/6) 启动防火墙:#/etc/init.d/iptables start centos7 启动防火墙 -----192.168.246.200服务器实验。 # yum install -y iptables iptables-services # systemctl stop firewalld # systemctl disable firewalld # systemctl start iptables 查看版本: [root@iptables-server ~]# iptables -V iptables v1.4.21 配置文件: /etc/sysconfig/iptables-config /etc/sysconfig/iptables #记录规则文件
2、参数解释
-L:列出一个链或所有链中的规则信息 -n:以数字形式显示地址、端口等信息 -v:以更详细的方式显示规则信息 --line-numbers:查看规则时,显示规则的序号(方便之处,通过需要删除规则-D INPUT 1 -F:清空所有的规则(-X是清理自定义的链,用的少;-Z清零规则序号) -D:删除链内指定序号(或内容)的一条规则 -P:为指定的链设置默认规则 -A:在链的末尾追加一条规则 -I:在链的开头(或指定序号)插入一条规则 -t: 指定表名 .... 更多参数可通过--help查看
3、参数使用
1.如果不写-t 默认使用filter表 指定表名查看规则 [root@iptables-server ~]# iptables -t nat -L 默认查看规则: # iptables -L 以数字的形式显示ip和端口与协议 # iptables -nL 显示规则行号 # iptables -nL --line 清空规则: #iptables -F #(改不了默认) 清空单独的某一个链里面的规则 #iptables -F 链名 保存规则: # service iptables save # iptables-svae > /etc/sysconfig/iptables
4、iptables语法
iptables -t 表名 动作 [链名] [匹配条件] [-j 控制类型] -j:控制类型, 通过前面匹配到之后是丢弃还是保留数据包的处理方式: ACCEPT允许,REJECT拒绝,DROP丢弃。 动作:添规则还是删除规则 -p:匹配条件:数据包特征ip,端口等 如果不写-t 默认使用filter表 ============================ 动作 修改默认规则: -P (大p) 删除规则:-D 修改规则:-R 追加规则: -A 默认追加到链的末尾 自定义链:-N 插入规则:-I (大i),在链的开头(或指定序号)插入一条规则
5、查看添加删除规则

观察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 #重启 注意:如果不保存重启之后规则就不在了。

2、规则匹配条件

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 Unreachable

2、通过端口规则匹配:

端口: --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 refused

3、通过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 DROP

4、修改规则:

# 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 OK

5、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. =========================================================================================

3、扩展匹配

显示匹配:如端口匹配,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

firewalld 管理防火墙规则的模式(动态):任何规则的变更都不需要对整个防火墙规则列表进行重新加载

rhel 7:firewall-cmd工具,firewalld服务

1、区域:

firewalld将网卡对应到不同的区域(zone)

trusted :允许所有流量通过 home/internal:仅允许ssh数据通过 work:仅允许ssh,ipp-client,dhcpv6-client数据通过 public:默认区域,仅允许ssh,dhcpv6-client数据通过 external:仅允许ssh数据通过,通过该区域的数据将会伪装(SNAT/DNAT) dmz:仅允许ssh数据通过 block:任何传入的网络数据包都将被阻止。拒绝所有流量 drop:拒绝所有流量
2、命令详解
firewall -cmd --permanent --permanent #永久生效的配置参数、资源、端口以及服务等信息 1、域zone相关的命令 --get-default-zone #查询默认的区域名称 --set-default-zone=<区域名称> #设置默认的区域 --get-active-zones #显示当前正在使用的区域与网卡名称 --get-zones #显示总共可用的区域 2、services管理的命令 --add-service=<服务名> --zone=<区域> #设置指定区域允许该服务的流量 --remove-service=<服务名> --zone=<区域> #设置指定区域不再允许该服务的流量 3、Port相关命令 --add-port=<端口号/协议> --zone=<区域> #设置指定区域允许该端口的流量 --remove-port=<端口号/协议> --zone=<区域> #设置指定区域不再允许该端口的流量 4、查看所有规则的命令 --list-all --zone=<区域> 显示指定区域的网卡配置参数、资源、端口以及服务等信息 --reload #让“永久生效”的配置规则立即生效,并覆盖当前的配置规则
3、firewalld配置使用

查看默认区域:

[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 success

3.向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 OK

4.指定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 OK

6.向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 OK

7.删除服务、端口

[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
最新回复(0)