day4-Iptables防火墙

推荐去学朱双印博主写的iptables
http://www.zsythink.net/archives/category/%e8%bf%90%e7%bb%b4%e7%9b%b8%e5%85%b3/iptables/page/2/
http://www.zsythink.net/archives/1199 《iptables概念》
http://www.zsythink.net/archives/1493 《iptables实际操作之规则查询》

一、Iptables防火墙结构介绍

在这里插入图片描述

iptables进入主机进行过滤的流程图

防火墙规则的执行顺序默认为从前到后依次执行、遇到匹配的规则就不在继续向下检查,如果遇到不匹配的规则则会继续向下进行。
重点:匹配上了拒绝规则也是匹配,因此,不在继续向下进行,这一点需注意。

在这里插入图片描述

表与链的关系

注:这里没写raw表,oldboy说几乎没用过,不重点关注
在这里插入图片描述
在这里插入图片描述

filter表介绍

在这里插入图片描述

nat表介绍

在这里插入图片描述

mangle表介绍

在这里插入图片描述

二、Iptables命令规则及配置

iptables的表与链工作流程图

最左边是数据的流向,流程解释说明可参考朱双印的博客
在这里插入图片描述
在一般的运维工作中,很少用到mangle表,为了更好的理解掌握常用的iptalbes技术,简化为如下:
在这里插入图片描述

从上图我们看出,当数据包进入时,首先经过的是NAT表的PREROUTING的链,接着会分两条路走,
如果是进入主机就通过FILTER表的 INPUT链,然后到达主机内部(LOCALPROCESS),
最后通过NAT表与FILTER的OUTPUT 返回;如果是流经主机则会通过FILTER表的 FORWARD链,
最后出站时通过NAT表的POSTROUTING链。
注:以上流程图是一个形象的图,和实际流程略有出入,但不影响我们理解IPTABLES原理。

三、配置生产环境主机防火墙

主机防火墙工作流程图如下

http://www.netfilter.org/documentation/HOWTO//packet-filtering-HOWTO-6.html
在这里插入图片描述

永久保存防火墙配置

centos5、centos6

/etc/ini.d/iptables save
iptables-save >/etc/sysconfig/iptables
#其中 /etc/sysconfig/iptables为iptables的默认文件路径
#保存前备份 cp /etc/sysconfig/iptables /etc/sysconfig/iptables.bak

#15、保存IPtables规则
iptables-save > ~/iptables.rules
#16、还原IPtables规则
iptables-restore < ~/iptables.rules

在这里插入图片描述
还原的:或者将bak文件覆盖回去,然后重启防火墙即可
在这里插入图片描述

一份初始化及基本防火墙配置的脚本(scripts from老男孩老师)

https://www.cnblogs.com/blog-tim/p/10291337.html
脚本中包含很多例子

#!/bin/bash
#define variable Path 
IPT=/sbin/iptables

#remove any existing rules
$IPT -F
$IPT -X
$IPT -Z

#设置SSH的允许,已改默认SSH端口,变成52113
$IPT -A INPUT -s 202.81.17.0/24 -p tcp --dport 52113 -j ACCEPT
$IPT -A INPUT -s 202.81.18.0/24 -p tcp --dport 52113 -j ACCEPT
$IPT -A INPUT -s 192.168.1.0/24 -p tcp --dport 52113 -j ACCEPT
$IPT -A INPUT -p tcp --dport 22 -j ACCEPT

#允许放行的ip端、IPS
$IPT -A INPUT -s 202.81.17.0/24 -p all -j ACCEPT
$IPT -A INPUT -s 192.168.1.0/24 -p all -j ACCEPT
$IPT -A INPUT -s 10.0.0.0/24 -p all -j ACCEPT

#改变防火墙的默认policy
$IPT --policy OUTPUT ACCEPT
$IPT --policy FORWARD DROP
$IPT -P INPUT DROP

#允许放行环回网卡 loopback interface
$IPT -A INPUT -i lo -j ACCEPT
$IPT -A OUTPUT -o lo -j ACCEPT

#拦截IP欺骗(IP Address Spoofing)和bad addresses 
$IPT -A INPUT -i eth0 -s 172.16.0.0/12 -j DROP
$IPT -A INPUT -i eth0 -s 0.0.0.0/8 -j DROP
$IPT -A INPUT -i eth0 -s 169.254.0.0/16 -j DROP
$IPT -A INPUT -i eth0 -s 192.0.2.0/24 -j DROP

#拦截隐藏扫描stealth scans等扫描 下面的就cvcv了
# prevent all Stealth Scans and TCP State Flags
$IPT -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
# All of the bits are cleared
$IPT -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
$IPT -A INPUT -p tcp --tcp-flags ALL FIN,URG,PSH -j DROP
#SYN and RST are both set
$IPT -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
# SYN and FIN are both set
$IPT -A INPUT -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
# FIN and RST are both set
$IPT -A INPUT -p tcp --tcp-flags FIN,RST FIN,RST -j DROP
# FIN is the only bit set, without the expected accompanying ACK
$IPT -A INPUT -p tcp --tcp-flags ACK,FIN FIN -j DROP
# PSH is the only bit set, without the expected accompanying ACK
$IPT -A INPUT -p tcp --tcp-flags ACK,PSH PSH -j DROP
# URG is the only bit set, without the expected accompanying ACK
$IPT -A INPUT -p tcp --tcp-flags ACK,URG URG -j DROP


#允许nagios
$IPT -A INPUT -s 192.168.1.0/24 -p tcp --dport 5666 -j ACCEPT
$IPT -A INPUT -s 202.81.17.0/24 -p tcp --dport 5666 -j ACCEPT
$IPT -A INPUT -s 202.81.18.0/24 -p tcp --dport 5666 -j ACCEPT

#允许mysql oracle数据库
$IPT -A INPUT -s 192.168.1.0/24 -p tcp --dport 3306 -j ACCEPT
$IPT -A INPUT -s 192.168.1.0/24 -p tcp --dport 3307 -j ACCEPT
$IPT -A INPUT -s 192.168.1.0/24 -p tcp --dport 3308 -j ACCEPT
$IPT -A INPUT -s 192.168.1.0/24 -p tcp --dport 1521 -j ACCEPT

#允许ftp
$IPT -A INPUT -p tcp --dport 21 -j ACCEPT

#允许http
$IPT -A INPUT -p tcp --dport 80 -j ACCEPT
$IPT -A INPUT -s 192.168.1.0/24 -p tcp -m multiport --dport 8080,8081,8082,8888,8010,8020,8030,8150 -j ACCEPT
$IPT -A INPUT -s 202.81.17.0/24 -p tcp -m multiport --dport 8080,8081,8082,8888,8010,8020,8030,8150 -j ACCEPT
$IPT -A INPUT -s 124.43.62.96/27 -p tcp -m multiport --dport 8080,8081,8082,8888,8010,8020,8030,8150 -j ACCEPT

#允许snmp  (UDP协议)
$IPT -A INPUT -s 192.168.1.0/24 -p udp --dport 161 -j ACCEPT
$IPT -A INPUT -s 202.81.17.0/24 -p udp --dport 161 -j ACCEPT

#允许rsync 873  -m tcp:是要装载 tcp模块 。 -p tcp:使用tcp的协议
$IPT -A INPUT -s 192.168.1.0/24 -p tcp -m tcp --dport 873 -j ACCEPT
$IPT -A INPUT -s 124.43.62.96/27 -p tcp -m tcp --dport 873 -j ACCEPT
$IPT -A INPUT -s 202.81.17.0/24 -p tcp -m tcp --dport 873 -j ACCEPT

#允许nfs 2049,portmap 111
$IPT -A INPUT -s 192.168.1.0/24 -p udp -m multiport --dport 111,892,2049 -j ACCEPT
$IPT -A INPUT -s 192.168.1.0/24 -p tcp -m multiport --dport 111,892,2049 -j ACCEPT

#$IPT -A INPUT -p icmp -m icmp --icmp-type any -j ACCEPT
$IPT -A INPUT -s 124.43.62.96/27 -p icmp -m icmp --icmp-type any -j ACCEPT
$IPT -A INPUT -s 192.168.1.0/24 -p icmp -m icmp --icmp-type any -j ACCEPT

$IPT -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPT -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
#end
#bash common-ipt.sh

在这里插入图片描述

四、实战案例

猜你喜欢

转载自blog.csdn.net/Nightwish5/article/details/113629517