iptables详解--转
22 ?1660 ? ?ACCEPT ? ? all ?-- ?* ? ? ?* ? ? ? 10.0.10.0/24 ? ? ? ? ? ? ? ?10.0.10.0/24?Chain FORWARD (policy ACCEPT 0 packets,0 bytes)pkts bytes target ? ? prot opt in ? ?out ? ? ? source ? ? ? ? ? ? ? ? ? destination?Chain OUTPUT (policy ACCEPT 16 packets,1536 bytes)pkts bytes target ? ? prot opt in ? ?out ? ? ? source ? ? ? ? ? ? ? ? ?destination ? ? pkts ? ??被本机报文所匹配的个数 bytes ??报文所有大小记起来之和 opt ? ??额外的选项,--表示没有 target ??处理机制 prot ? ??放行哪种协议 source ?源地址 destination ?目标地址 对于严谨的规则,一般默认规则都是拒绝未知,允许已知 如下所示: 只放行信任IP地址段,其他全部禁止 iptables-P INPUT DORP iptables-A INPUT -s ? 10.0.10.0/24 ? -d ?10.0.10.0/24 -j ACCEPT iptables-P OUTPUT DORP iptables-A OUTPUT -d ? 10.0.10.0/24 ?-s ? ?10.0.10.0/24-j ACCEPT 保存规则 [root@test3~]# /etc/init.d/iptables save iptables:Saving firewall rules to /etc/sysconfig/iptables:[ ?OK ?] 保存规则至其他文件 [root@test3~]# iptables-save > /tmp/iptables ? 加载iptables文件规则 [root@test3~]# iptables-resotre < /tmp/iptables ? 1.2.规则的替换 首先来查看规则 [root@test3 ~]# iptables -L -n --line-number ChainINPUT (policy ACCEPT) num ?target ? ?prot opt source ? ? ? ? ? ? ?destination ? ? ? ? 1 ? ?ACCEPT ? ?all ?-- ?10.0.10.0/24 ? ? ? ? 10.0.10.0/24 ? ? ? ? ChainFORWARD (policy DROP) num ?target ? ?prot opt source ? ? ? ? ? ? ?destination ? ? ? ? ChainOUTPUT (policy ACCEPT) num ?target ? ?prot opt source ? ? ? ? ? ? ?destination 替换规则:将规则1替换为 eth0只能够通过某个网段进来 [root@test3~]# iptables -R ?INPUT 1 -s 10.0.10.0/24-d 10.0.10.62 -i eth0 -j ACCEPT [root@test3~]# iptables -L -n --line-number ChainINPUT (policy ACCEPT) num ?target ? ?prot opt source ? ? ? ? ? ? ?destination ? ? ? ? 1 ? ?ACCEPT ? ?all ?-- ?10.0.10.0/24 ? ? ? ? 10.0.10.62 ? ? 2.扩展匹配 #所有的扩展匹配表示要使用-m来指定扩展的名称来引用,而每个扩展模块一般都会有自己特有的专用选项,在这些选项中,有些是必备的: 2.1隐含扩展 如下所示: #端口之间必须是连续的 -p tcp--sport|--dport 21-80 #取反,非21-80的端口 -p tcp--sport|--dport !21-80 #检测报文中的标志位 --tcp-flagsSYN,ACK,RST,FIN,SYN ALL ? ? ? ? ? ? ? ? ? #表示为所有标志位 NONE ? ? ? ? ? ? ? ? ? ?#表示没有任何一个标志位 #--tcp-flags ALL NONE ? #表示所有标志位都检测,但是其中多有都为0 #--tcp-flage ALL SYN,FIN #表示SYN,FIN都为1(即握手又断开) #生成环境下tcp-flags 用的非常多,意义非常重要 例:放行本机对web的访问 [root@test3~]# iptables -A INPUT -d 10.0.10.62 ?-ptcp --dport 80 -j ACCEPT [root@test3~]# iptables -L -n ChainINPUT (policy DROP) target ? ? prot opt source ? ? ? ? ? ? ? destination ? ? ? ? ACCEPT ? ? all -- ?10.0.10.0/24 ? ? ? ? 10.0.10.62 ? ? ? ? ? ACCEPT ? ? tcp -- ?0.0.0.0/0 ? ? ? ? ? ?10.0.10.62 ? ? ? ? ?tcp dpt:80 放行出去的报文,源端口为80 [root@test3~]# iptables -A OUTPUT -s 10.0.10.62 -p tcp --sport 80 -j ACCEPT 查看匹配规则 [root@test3 ~]# iptables -L -n --line-number ChainINPUT (policy DROP) num ?target ? ?prot opt source ? ? ? ? ? ? ?destination ? ? ? ? 1 ? ?ACCEPT ? ?all ?-- ?10.0.10.0/24 ? ? ? ? 10.0.10.62 ? ? ? ? ? 2 ? ?ACCEPT ? ?tcp ?-- ?0.0.0.0/0 ? ? ? ? ? ?10.0.10.62 ? ? ? ? ?tcp dpt:80 ChainFORWARD (policy DROP) num ?target ? ?prot opt source ? ? ? ? ? ? ?destination ? ? ? ? ChainOUTPUT (policy DROP) num ?target ? ?prot opt source ? ? ? ? ? ? ?destination ? ? ? ? 1 ? ?ACCEPT ? ?all ?-- ?10.0.10.0/24 ? ? ? ? 10.0.10.0/24 ? ? ? ? 2 ? ?ACCEPT ? ?tcp ?-- ?10.0.10.62 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? tcp spt:80 考虑要点: (1)规则为放行出去的响应报文 (2)考虑源IP地址为本机,目标为访问的时候拆开报文才可以获知,而写规则的时候是面向所有主机,所以这里不用写 (3)源端口:80 ,因为用户访问的时候一定会访问其80端口,无可非议的 (4)目标端口:请求到来的时候事先无法断定对方的端口是多少,所以不用写 2.2协议匹配 通常对协议做匹配则使用 -p 参数 来指定协议即可 匹配UDP:UDP只有端口的匹配,没有任何可用扩展,格式如下 -p udp--sport | --dport 匹配ICMP格式如下 -picmp --icmp-[number] icmp常见类型:请求为8(echo-request),响应为0(echo-reply) 例:默认规则input output 都为DROP,使其本机能ping(响应的报文)的报文出去 通过此机器去ping网关10.0.10.1 , 可结果却提示not permitted,使其能通10.0.10.0/24网段中的所有主机 [root@test3~]#iptables -A OUTPUT -s 10.0.10.62 -d 10.0.10.0/24 -p icmp --icmp-type8 -j ACCEPT 可看到无法响应:0表示响应进来的报文规则,并没有放行自己作为服务端的的角色规则 [root@test3~]# iptables -A INPUT -s 10.0.10.0/24 -d 10.0.10.62 -p icmp --icmp-type0 -j ACCEPT #ping 10.0.10.x 允许类型为0(响应报文)出去 [root@test3~]# iptables -A OUTPUT -s 10.0.10.62 -d ?10.0.10.0/24 -picmp --icmp-type 0 -j ACCEPT 例2:本机DNS服务器,要为本地客户端做递归查询;iptables的input output默认为drop 本机地址是10.0.10.62 [root@test3~]# iptables -A INPUT -d 10.0.10.62 -p udp --dprot 53 -j ACCEPT [root@test3~]# iptables -A OUTPUT -S 10.0.10.62 -p udp --sprot 53 -j ACCEPT 客户端请求可以进来,响应也可以出去,但是自己作为客户端请求别人是没有办法出去的,所以: [root@test3~]# iptables -A OUTPUT -s 10.0.10.62 -p udp --dport 53 -j ACCEPT [root@test3~]# iptables -A INPUT -d 10.0.10.62 -p udp --sprot 53 -j ACCEPT 如果为tcp 则将以上udp改为tcp即可 2.3 TCP协议的报文走向 TCP连接的建立 双方主机为了实现tcp的通信,所以首先三次握手 客户端主动发出了SYN,服务器端处于监听状态,随时等待客户端的请求信息; 服务器端接收到了SYN请求,从而回应用户的请求,发送SYN_ACK ,从而转换为SYN_REVIN 客户端在发出了请求,从发出的那一刻close状态转换为SYN_SENT状态 客户端在SYN_SENT状态中一旦收到了服务端发来的SYN_ACK 之后,转换为ESTABLISHED状态,这时便可以开始传送数据了,无论怎么传都是ESTABLISHED状态 而服务器端收到了对方的ACK,同样处于ESTABLISHED状态 数据传输结束之后 客户端从ESTABLEISHED状态,发起四次断开请求 客户端发起FIN请求,从而进入等待状态 服务端收到断开请求之后,便发起ACK请求 客户端收到服务端发来的ACK确认信息后,从而又发起FIN_2 请求 等待服务端发来的FIN请求之后,便确认 服务器端收到FIN并发送ACK之后,服务器端便处于CLOSE_WAIT便自己发送FIN,从而进入LAST ACK模式 , 确认完后不能立刻断开,还需要等待一定的时间(大约240秒),确认报文是否传递给对方 于是转换为CLOSED iptables中有一个扩张参数--status 此扩展可以追踪tcp udp icmp等各种状态 其能够使用某种内核数据结构保持此前曾经建立的连接状态时间的功能,称为连接追踪 内核参数文件路径为: [root@test3~]# ls /proc/sys/net/netfilter/ [root@test3~]# cat /proc/sys/net/netfilter/nf_conntrack_udp_timeout30 以此为例,在其30秒钟内,曾经建立过的udp连接,这些连接都可以被追踪到的,可以明确知道在这期间哪个客户端曾经访问过,只要基于请求的序列,能跟此前保持会话信息,即可查询 2.4显式扩展 (编辑:PHP编程网 - 黄冈站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |