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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
| 练习:INPUT和OUTPUT默认策略为DROP;
1、限制本地主机的web服务器在周一不允许访问;新请求的速率不能超过100个每秒;web服务器包含了admin字符串的页面不允许访问;web服务器仅允许响应报文离开本机; iptables -P OUTPUT DORP iptables -P OUTPUT DROP iptables -P FORWARD DROP iptables -A INPUT -d 172.16.36.61 -p tcp --dport 80 -m time --weekdays 2,3,4,5,6,7 -m limit --limit 100/second -j ACCEPT iptables -A OUTPUT -s 172.16.36.61 -p tcp --sport 80 -m string --algo bm --string "admin" -j DROP (string的关键字过滤,一定要做在output链上, 在回应报文中才应该会有内容) iptables -A OUTPUT -m state --state ESTABLISHED -j ACCEPT [root@Centos6 netfilter]# iptables -L -n Chain INPUT (policy DROP) target prot opt source destination ACCEPT tcp -- 172.16.0.0/16 172.16.36.61 tcp dpt:22 ACCEPT tcp -- 0.0.0.0/0 172.16.36.61 tcp dpt:80 TIME on Tue,Wed,Thu,Fri,Sat,Sun limit: avg 100/sec burst 5
Chain FORWARD (policy DROP) target prot opt source destination
Chain OUTPUT (policy DROP) target prot opt source destination DROP tcp -- 172.16.36.61 0.0.0.0/0 tcp spt:80 STRING match "admin" ALGO name bm TO 65535 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state ESTABLISHED
Chain in_icmp (0 references) target prot opt source destination 2、在工作时间,即周一到周五的8:30-18:00,开放本机的ftp服务给172.16.0.0网络中的主机访问;数据下载请求的次数每分钟不得超过5个; ~]# modprobe nf_conntrack_ftp (需要使用related的连接状态,需要挂载此模块) iptables -P OUTPUT DORP iptables -P OUTPUT DROP iptables -P FORWARD DROP ~]# iptables -A INPUT -s 172.16.0.0/16 -p tcp --dport 21 -m time --weekdays 1,2,3,4,5 --timestart 8:30 --timestop 18:00 -m limit --limit 5/minute -m state --state NEW -j ACCEPT ~]# iptables -A INPUT -s 172.16.0.0/16 -p tcp -m state --state RELATED -j ACCEPT ~]# iptables -A OUTPUT -m state --state ESTABLISHED -j ACCEPT 3、开放本机的ssh服务给172.16.x.1-172.16.x.100中的主机,x为你的学号,新请求建立的速率一分钟不得超过2个;仅允许响应报文通过其服务端口离开本机; iptables -P OUTPUT DORP iptables -P OUTPUT DROP iptables -P FORWARD DROP ~]# iptables -A INPUT -m state --state ESTABLISHED,NEW -j ACCEPT ~]# iptables -A INPUT -d 172.16.36.61 -p tcp --dport 22 -m iprange --src-range 172.16.36.1-172.16.36.100 -m connlimit --connlimit-above 2/minute -j DROP ~]# iptables -A OUTPUT -m state --state ESTABLISHED -j ACCEPT 4、拒绝TCP标志位全部为1及全部为0的报文访问本机; iptables -P OUTPUT DORP iptables -P OUTPUT DROP iptables -P FORWARD DROP iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP 5、允许本机ping别的主机;但不开放别的主机ping本机; ~]# iptables -A OUTPUT -p icmp --icmp-type 8 -j ACCEPT ~]# iptables -A INPUT -p icmp --icmp-type 0 -j ACCEPT (如果INPUT上放行了ESTABLISHED,就不需要再放行代码为0的) 练习:判断下述规则的意义: # iptables -N clean_in # iptables -A clean_in -d 255.255.255.255 -p icmp -j DROP # iptables -A clean_in -d 172.16.255.255 -p icmp -j DROP
# iptables -A clean_in -p tcp ! --syn -m state --state NEW -j DROP # iptables -A clean_in -p tcp --tcp-flags ALL ALL -j DROP # iptables -A clean_in -p tcp --tcp-flags ALL NONE -j DROP # iptables -A clean_in -d 172.16.100.7 -j RETURN
# iptables -A INPUT -d 172.16.100.7 -j clean_in
# iptables -A INPUT -i lo -j ACCEPT # iptables -A OUTPUT -o lo -j ACCEPT(如果在OUTPUT上允许了state为ESTABLISHED的连接, 就不需要再放行,直接可以通过)
等同 # iptables -A INPUT -d 127.0.0.1 -j ACCEPT # iptables -A OUTPUT -s 127.0.0.1 -j ACCEPT (如果在OUTPUT上允许了state为ESTABLISHED的连接, 就不需要再放行,直接可以通过)
# iptables -A INPUT -i eth0 -m multiport -p tcp --dports 53,113,135,137,139,445 -j DROP # iptables -A INPUT -i eth0 -m multiport -p udp --dports 53,113,135,137,139,445 -j DROP # iptables -A INPUT -i eth0 -p udp --dport 1026 -j DROP # iptables -A INPUT -i eth0 -m multiport -p tcp --dports 1433,4899 -j DROP
# iptables -A INPUT -p icmp -m limit --limit 10/second -j ACCEPT iptables FORWARD实验:
环境说明: Centos6_1 eth0 IP : 172.16.36.60/16 gateway:172.16.36.61 Centos6_2 eth0 IP : 172.16.36.61/16 gateway: 172.16.0.1/16(可以不使用网关) eth1 IP: 192.168.0.1/24 Centos6_3 eth0 IP: 192.168.0.2/24 GATE: 192.168.0.1
说明: 172.16.36.60与61使用桥接网络,处于教室实验网络, 192.168.0.1与0.2处于vmnet2虚拟网络
实验一: 在172.16.36.60服务器安装vsftpd服务, 要求放行ftp服务 Centos 6_2 1 在36.60服务器上安装vsftpd服务, 并确认vsftpd服务监听在21端口 2 设置FORWARD链的默认重力为DROP iptables -P FORWARD DROP 3 装载模块 ~]# modprobe nf_conntrack_ftp 4 FORWARD链策略设置 ~]# iptables -A FORWARD -m state --state ESTABLISHED -j ACCEPT #开放两个方向的所有ESTABLISHED连接,放到第一条(因为匹配到的机率最多) ~]# iptables -A FORWARD -d 172.16.36.60 -p tcp --dport 21 -m state --state NEW -j ACCEPT #开放在目标服务器的tcp 21号端口的服务,并且状态为NEW的才放行 ~]# iptables -A FORWARD -d 172.16.36.60 -m state --state RELATED -j ACCEPT #将FTP的RELATED连接也放行, 这样客户端才能连接数据端口 5 使用tcpdump工具查看数据包(172.16.36.60端查看) ~]# tcpdump -i eth0 -nn tcp and host 192.168.0.2
6 开放80端口,给192.168./24网络的主机访问: ~]# iptables -R FORWARD 2 -d 172.16.36.60 -p tcp -m multiport --dport 21,80 -m state --state NEW -j ACCEPT 7 测试80端口开放情况(192.168.0.2主机测试) ~]# curl http://172.16.36.60 8 iptbles的查看 [root@Centos6 ~]# iptables -L -n Chain INPUT (policy ACCEPT) target prot opt source destination
Chain FORWARD (policy DROP) target prot opt source destination ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state ESTABLISHED ACCEPT tcp -- 0.0.0.0/0 172.16.36.60 multiport dports 21,80 state NEW ACCEPT all -- 0.0.0.0/0 172.16.36.60 state RELATED
Chain OUTPUT (policy ACCEPT) target prot opt source destination
实验二: 在172.16.36.60运行apache服务, 要求内网(192.168.0)网络的主机,访问时, 使用172.16.36.61为源IP地址(SNAT) 1 确认Centos6_2的主机所有规则清空, 默认规则为允许, 以防止干扰规则判断 [root@Centos6 ~]# iptables -L -n Chain INPUT (policy ACCEPT) target prot opt source destination
Chain FORWARD (policy ACCEPT) target prot opt source destination
Chain OUTPUT (policy ACCEPT) target prot opt source destination
root@Centos6 ~]# iptables -t nat -L -n Chain PREROUTING (policy ACCEPT) target prot opt source destination
Chain POSTROUTING (policy ACCEPT) target prot opt source destination
Chain OUTPUT (policy ACCEPT) target prot opt source destination 2 在网络防火墙主机上(172.16.36.61)设置POSTROUTING链的SANT规则 ~]# iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j SNAT --to-source 172.16.36.61 3 验证是否源主机为192.168.0.0/24网络的主机访问172.16.36.60主机的web, 源地址更改为172.16.36.61的IP地址 ~]# tailf /var/log/httpd/access_log 172.16.36.61 - - [26/Jan/2016:16:27:44 +0800] "GET / HTTP/1.1" 200 25 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.19.1 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
实验三: 在192.168.0.2的主机安装web服务, 要求外网(172.16.36.60)主机访问时,完成DNAT转换 1 确认FORWARD/POSTROUTING/PREROUTING的链都没有设置规则 , 并且默认规则为ACCEPT 2 在192.168.0.2主机安装web服务,并启动相关服务, 确认并且能正常访问192.168.0.2的服务 3 在网络防火墙主机上(172.16.36.61)设置PREROUTING链的DNAT规则 ~]# iptables -t nat -A PREROUTING -d 172.16.36.61 -p tcp --dport 80 -j DNAT --to-destination 192.168.0.2:80 4 在172.16.36.60主机上尝试访问, 并查看192.168.0.2web的访问日志 172.16.36.60 - - [26/Jan/2016:16:27:44 +0800] "GET / HTTP/1.1" 200 25 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.19.1 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2" 实现三: 在192.168.0.2的主机安装web服务, 要求外网(172.16.36.60)主机访问, 完成PNAT转换 1 确认FORWARD/POSTROUTING/PREROUTING的链都没有设置规则 , 并且默认规则为ACCEPT 2 在192.168.0.2主机安装web服务,并启动相关服务, 确认并且能正常访问192.168.0.2的服务 3 在网络防火墙主机上(172.16.36.61)设置PREROUTING链的DNAT规则 iptables -t nat -A PREROUTING -d 172.16.36.61 -p tcp --dport 80 -j DNAT --to-destination 192.168.0.2:8080
|