Linux防火墙基础试题(3)

iptables练习题
1.INPUT和OUTPUT默认策略为DROP;

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

2.限制本地主机的web服务器在周二、周五不允许访问;新请求的速率不能超过150个每秒;web服务器包含了demo字符串的页面不允许访问;web服务器仅允许响应报文离开本机;

[root@localhost ~]# iptables -I INPUT 1 -d 192.168.88.84 -p tcp --dport 80 -m limit --limit 150/second -m time --weekdays Tue,Fri -j REJECT 
[root@localhost ~]# iptables -I OUTPUT 1 -d 192.168.88.84 -p tcp --dport 80 -m string --string "demo" --algo kmp -j REJECT
[root@localhost ~]# iptables -I OUTPUT 2 -m state --state RELATED,ESTABLISHED -j ACCEPT
[root@localhost ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
REJECT     tcp  --  0.0.0.0/0            192.168.88.84        tcp dpt:80 limit: avg 151/sec burst 5 TIME on Tue,Fri UTC reject-with icmp-port-unreachable
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
INPUT_direct  all  --  0.0.0.0/0            0.0.0.0/0           
INPUT_ZONES_SOURCE  all  --  0.0.0.0/0            0.0.0.0/0           
INPUT_ZONES  all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
REJECT     tcp  --  0.0.0.0/0            192.168.88.84        tcp dpt:80 STRING match  "demo" ALGO name kmp TO 65535 reject-with icmp-port-unreachable
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
OUTPUT_direct  all  --  0.0.0.0/0            0.0.0.0/0       
    

3.在工作时间,即周一到周五的8:30-18:00,开放本机的samba服务共享的目录给192.168.100.20网络中的主机访问;数据访问次数每分钟不得超过20个;

[root@localhost ~]# iptables -A INPUT -p tcp --dport 445 -s 192.168.100.20 -m time --weekdays 1,2,3,4,5 --timestart 8:30 --timestop 18:00 -mconnlimit --connlimit-above 20  -j ACCEPT                
[root@localhost ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
INPUT_direct  all  --  0.0.0.0/0            0.0.0.0/0           
INPUT_ZONES_SOURCE  all  --  0.0.0.0/0            0.0.0.0/0           
INPUT_ZONES  all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
ACCEPT     tcp  --  192.168.100.20       0.0.0.0/0            tcp dpt:445 TIME from 08:30:00 to 18:00:00 on Mon,Tue,Wed,Thu,Fri UTC #conn src/32 > 20

4.开放本机的ssh服务给192.168.100.9-192.168.100.155中的主机,新请求建立的速率一分钟不得超过3个;仅允许响应报文通过其服务端口离开本机;

[root@localhost ~]# iptables -A INPUT -m iprange --src-range 192.168.100.9-192.168.100.155 -m limit --limit 3/m --limit-burst 3 -j ACCEPT
[root@localhost ~]# iptables -A OUTPUT -p tcp --dport 22 -mstate --state ESTABLISHED -j ACCEPT
[root@localhost ~]# iptables -nL             
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
INPUT_direct  all  --  0.0.0.0/0            0.0.0.0/0           
INPUT_ZONES_SOURCE  all  --  0.0.0.0/0            0.0.0.0/0           
INPUT_ZONES  all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            source IP range 192.168.100.9-192.168.100.155 limit: avg 3/min burst 3
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
OUTPUT_direct  all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22 state ESTABLISHED

5.定制源地址访问策略:1)接收来自192.168.100.30的IP访问;2)拒绝来自192.168.200.0/24网段的访问

[root@localhost ~]# iptables -A INPUT -s 192.168.100.30 -j ACCEPT
[root@localhost ~]# iptables -A INPUT -s 192.168.200.0/24 -j DROP           
[root@localhost ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
INPUT_direct  all  --  0.0.0.0/0            0.0.0.0/0           
INPUT_ZONES_SOURCE  all  --  0.0.0.0/0            0.0.0.0/0           
INPUT_ZONES  all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
ACCEPT     all  --  192.168.100.30       0.0.0.0/0           
DROP       all  --  192.168.200.0/24     0.0.0.0/0           

6.目标地址192.168.100.20的访问给予记录,并查看/var/log/message

[root@localhost ~]# iptables -A INPUT -s 192.168.100.20 -j LOG
[root@localhost ~]# iptables -nL                              
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
INPUT_direct  all  --  0.0.0.0/0            0.0.0.0/0           
INPUT_ZONES_SOURCE  all  --  0.0.0.0/0            0.0.0.0/0           
INPUT_ZONES  all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
LOG        all  --  192.168.100.20       0.0.0.0/0            LOG flags 0 level 4

7.定制端口访问策略:1)拒绝任何地址访问本机的8081端口;2)拒绝192.168.200.0/24网段的1024-65534的源端口访问SSH

[root@localhost ~]# iptables -A INPUT -p tcp --dport 8081 -j DROP
[root@localhost ~]# iptables -A INPUT -p tcp -s 192.168.200.0/24 --sport 1024:65534 --dport ssh -j DROP 
[root@localhost ~]# iptables -nL                                                                       
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
INPUT_direct  all  --  0.0.0.0/0            0.0.0.0/0           
INPUT_ZONES_SOURCE  all  --  0.0.0.0/0            0.0.0.0/0           
INPUT_ZONES  all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
DROP       tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:8081
DROP       tcp  --  192.168.200.0/24     0.0.0.0/0            tcp spts:1024:65534 dpt:22

8.定制防火墙的MAC地址访问策略:1)清除所以已经存的规则;2)将INPUT设为DROP;3)将目标计算机192.168.100.30的MAC设为ACCEPT

[root@localhost ~]# iptables -F
[root@localhost ~]# iptables -X
[root@localhost ~]# iptables -Z
[root@localhost ~]# iptables -P INPUT DROP
[root@localhost ~]# iptables -A INPUT -m mac --mac-source 00:0c:29:ef:43:36  -j ACCEPT     
[root@localhost ~]# iptables -nL                      
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
INPUT_direct  all  --  0.0.0.0/0            0.0.0.0/0           
INPUT_ZONES_SOURCE  all  --  0.0.0.0/0            0.0.0.0/0           
INPUT_ZONES  all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            MAC 00:0C:29:EF:43:36

9.定制防火墙的NAT访问策略:1)清除所有NAT策略;2)重置ip_forward为1;3)通过SNAT设定来源于192.168.100.20网段通过ens33转发出去;4)用iptables观察转发的数据包。

[root@localhost ~]# iptables -F -t nat
[root@localhost ~]# iptables -X -t nat 
[root@localhost ~]# iptables -Z -t nat 
[root@localhost ~]# echo "1" > /proc/sys/net/ipv4/ip_forward
[root@localhost ~]# iptables -t nat -A POSTROUTING -o eno16777736 -j SNAT --to 192.168.100.20
[root@localhost ~]# iptables -nvL -t nat
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
0     0 SNAT       all  --  *      eno16777736  0.0.0.0/0            0.0.0.0/0            to:192.168.100.20

10.端口转发访问策略:1)清除所有NAT策略;2)重置ip_forward为1;3)通过DNAT设定为所有访问192.168.100.30的22端口,都访问到192.168.100.20的22端口;4)设定所有到192.168.100.20的22端口的数据包都通过FORWARD转发;5)设定回应数据包,即通过NAT的POSTROUTING设定,使通讯正常。

[root@localhost ~]# iptables -F -t nat
[root@localhost ~]# iptables -X -t nat 
[root@localhost ~]# iptables -Z -t nat 
[root@localhost ~]# echo "1" > /proc/sys/net/ipv4/ip_forward                                 
[root@localhost ~]# iptables -t nat -A PREROUTING -d 192.168.100.30 -p tcp --dport 22 -j DNAT  --to-destination 192.168.100.20:20
[root@localhost ~]# iptables -A FORWARD -p tcp -d 192.168.100.20 --dport 22 -j ACCEPT
[root@localhost ~]# iptables -t nat -I POSTROUTING -p tcp --dport 22 -j MASQUERADE
[root@localhost ~]# iptables -nvL -t nat
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DNAT       tcp  --  *      *       0.0.0.0/0            192.168.100.30       tcp dpt:22 to:192.168.100.20:20 
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 MASQUERADE  tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22

Firewalld练习题
1.配置防火墙规则允许192.168.100.0/24 网段的用户对controller和compute进行ssh访问;禁止192.168.200.0/24 网段的用户对controller和compute进行ssh访问.

[root@localhost ~]# firewall-cmd  --zone=public --add-rich-rule="rule family="ipv4" source address="192.168.100.0/24" service name="ssh" accept"                 
success
[root@localhost ~]# firewall-cmd  --zone=public --add-rich-rule="rule family="ipv4" source address="192.168.200.0/24" service name="ssh" reject" 
success
[root@localhost ~]# firewall-cmd --list-all
public (default, active)
  interfaces: eno16777736
  sources: 
  services: dhcpv6-client ssh
  ports: 
  masquerade: no
  forward-ports: 
  icmp-blocks: 
  rich rules: 
        rule family="ipv4" source address="192.168.100.0/24" service name="ssh" accept
        rule family="ipv4" source address="192.168.200.0/24" service name="ssh" reject

2.配置端口转发在controller和compute配置端口转发,要求:在192.168.200.0/24网段中的主机,访问server的本地端口5321将被转发到80端口此设置必须永久有效、设置开机启动防火墙.

[root@localhost ~]# firewall-cmd --permanent --zone=public --add-rich-rule 'rule family="ipv4" source address="192.168.200.0/24" forward-port port="5321" protocol="tcp"  to-port="80"' 
success 
[root@localhost ~]# firewall-cmd --reload  
success
[root@localhost ~]# firewall-cmd --list-all
public (default, active)
  interfaces: eno16777736
  sources: 
  services: dhcpv6-client ssh
  ports: 
  masquerade: no
  forward-ports: 
  icmp-blocks: 
  rich rules: 
        rule family="ipv4" source address="192.168.200.0/24" forward-port port="5321" protocol="tcp" to-port="80"

3.开启80端口,查看80端口状态;

[root@localhost ~]# firewall-cmd --zone=public --add-port=80/tcp
success
[root@localhost ~]# firewall-cmd --list-all
public (default, active)
  interfaces: eno16777736
  sources: 
  services: dhcpv6-client ssh
  ports: 80/tcp
  masquerade: no
  forward-ports: 
  icmp-blocks: 
  rich rules:

4.允许来自主机 192.168.100.20 的80 端口的 IPv4 的 TCP 流量,并将流量转发到 6532 端口上;

[root@localhost ~]# firewall-cmd --zone=public --add-rich-rule 'rule family="ipv4" source address="192.168.100.20" forward-port port="80" protocol="tcp"  to-port="6532"'                  
success
[root@localhost ~]# firewall-cmd --list-all
public (default, active)
  interfaces: eno16777736
  sources: 
  services: dhcpv6-client ssh
  ports: 
  masquerade: no
  forward-ports: 
  icmp-blocks: 
  rich rules: 
        rule family="ipv4" source address="192.168.100.20" forward-port port="80" protocol="tcp" to-port="6532"

5.每分钟允许2个新连接访问ftp服务;

[root@localhost ~]# firewall-cmd --add-rich-rule='rule service name=ftp limit value=2/m accept'
success
[root@localhost ~]# firewall-cmd --list-all
public (default, active)
  interfaces: eno16777736
  sources: 
  services: dhcpv6-client ssh
  ports: 
  masquerade: no
  forward-ports: 
  icmp-blocks: 
  rich rules: 
        rule service name="ftp" accept limit value="2/m"

6.修改默认区域为home,并添加接口ens33到当前默认区域下,查询出结果;

[root@localhost ~]# firewall-cmd --set-default-zone=home
success
[root@localhost ~]# firewall-cmd --zone=home  --change-interface=ens33
success
[root@localhost ~]# firewall-cmd --get-default-zone
home
[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens33
home

7.放开ssh服务,并限制只有192.168.100.30可以访问3306端口;

[root@localhost ~]# firewall-cmd --add-rich-rule="rule family="ipv4" source address="192.168.100.30" port protocol="tcp" port="3306" accept"
success
[root@localhost ~]# firewall-cmd --list-all
public (default, active)
  interfaces: eno16777736
  sources: 
  services: dhcpv6-client ssh
  ports: 
  masquerade: no
  forward-ports: 
  icmp-blocks: 
  rich rules: 
        rule family="ipv4" source address="192.168.100.30" port port="3306" protocol="tcp" accept

8.增加666端口到public域上,并查询结果;

[root@localhost ~]# firewall-cmd --zone=public --add-port=666/tcp
success
[root@localhost ~]# firewall-cmd --list-all
public (default, active)
  interfaces: eno16777736
  sources: 
  services: dhcpv6-client ssh
  ports: 666/tcp
  masquerade: no
  forward-ports: 
  icmp-blocks: 
  rich rules:

9.将tcp协议1000-8888端口,添加到home区域下;并添加192.168.100.0/24网段到home区域;

[root@localhost ~]# firewall-cmd --zone=home --add-port=1000-8888/tcp
success
[root@localhost ~]# firewall-cmd --zone=home --add-source=192.168.100.0/24
success
[root@localhost ~]# firewall-cmd --list-all --zone=home
home (active)
  interfaces: ens33
  sources: 192.168.100.0/24
  services: dhcpv6-client ipp-client mdns samba-client ssh
  ports: 1000-8888/tcp
  masquerade: no
  forward-ports: 
  icmp-blocks: 
  rich rules:

10.修改home区域绑定的网卡接口,将它绑定到internal区域,并查询接口所在区域结果;

[root@localhost ~]# firewall-cmd --zone=internal --change-interface=ens33
success
[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens33
internal

你可能感兴趣的:(Linux)