一、防火墙
1、防火墙的基础
- 分类:
- 从逻辑上分类
网络防火墙、主机防火墙 - 从物理上分类
硬件防火墙,软件防火墙 - 网络安全的知名企业:深信服,思科,天融信
- 硬件防火墙:
- 思科(AIR-CT5508-300-K9)
- 华为(USG6670)
- iptables并不是真正意义上的防火墙、我们可以理解为一个客户端工具
- netfilter才是防火墙真正的安全框架,netfilter位于内核空间
- iptables/netfilter(简称为iptables)组成Linux平台下的包过滤防火墙,它可以代替昂贵的商业防火墙解决方案,完成封装包过滤、封包重定向和网络地址转换等功能。
2、四表、五链
- 四表:
- filter (INPUT、FORWARD、OUTPUT)
负责过滤功能 - nat (PREROUTING、POSTROUTING、OUTPUT)
网络地址转换功能 - raw (PREOUTING、OUTPUT)
关闭net表上启用的连接追踪机制 - mangle (五条)
拆解报文、做出修改,并重新封装的功能 - 五链:
- INPUT(mangle表、filter表(centos7有nat表,centos6里边没有))
- OUTPUT(raw表、mangle表、nat表、filter表)
- FORWARD(mangle表、filter表)
- PREROUTING(raw表、mangle表、nat表)
- POSTROUTING(mangle表、nat表)
- 四表在同一个链中有顺序:
raw>mangel>nat>filter
3、防火墙配置
3.1、iptables使用规则
- iptables -L 查看所有规则
- iptables -F 清空规则
- iptables -D 清除某一条规则
- iptables -t title -L 查看指定表的规则
- [root@localhost ~]# iptables -nvL --line-number
- -n 转化成数字
- -v 显示详细信息
3.2、–line-number 显示链下规则的序号
3.3、target:
- ACCEPT 容许、接受
- DROP 丢弃
- REJECT 拒绝
3.4、增
- [root@localhost ~]# iptables -t filter -I INPUT -s 192.168.42.102 -j DROP
- [root@localhost ~]# iptables -t filter -I INPUT -s 192.168.42.102 -j REJECT
- [root@localhost ~]# iptables -t filter -I OUTPUT -d 192.168.42.102 -j DROP
- [root@localhost ~]# iptables -t filter -A INPUT -s 192.168.42.102 -j DROP
- [root@localhost ~]# iptables -t filter -I INPUT 2 -s 192.168.42.102 -j ACCEPT #“具体添加到哪一行”
- [root@localhost ~]# iptables -t filter -I INPUT -s 192.168.42.102 -p ICMP -j DROP #禁ping
- [root@localhost ~]# iptables -t filter -I INPUT -s 192.168.42.145 -p tcp --dport 80 -j REJECT
- [root@localhost ~]# iptables -t filter -I INPUT -p ICMP -s 192.168.42.145,192.168.42.102 -j ACCEPT
- [root@localhost ~]# iptables -t filter -I INPUT -p ICMP -s 192.168.42.0/24 -j ACCEPT
- [root@localhost ~]# iptables -t filter -I INPUT -p ICMP ! -s 192.168.42.145 -j ACCEPT #在192.168.42.0/24 屏蔽了 192.168.42.145
!排除 - [root@localhost ~]# iptables -t filter -I INPUT -p tcp --dport 22 -j ACCEPT
- [root@localhost ~]# iptables -t filter -I INPUT -p tcp --sport 22 -j ACCEPT
- [root@localhost ~]# iptables -t filter -A INPUT -j REJECT #拒绝所有。
- -s 源地址
[root@localhost ~]# iptables -t filter -I INPUT 2 -s 192.168.42.102 -j ACCEPT 指定行数添加 - -d 目的地址
[root@localhost ~]# iptables -t filter -I OUTPUT -d 192.168.108.5 -j DROP - -I 添加到第一条
- -p 指定协议
- –dport 目标端口
- –sport 源端口
- -A 追加到最后一条
[root@localhost ~]# iptables -t filter -A OUTPUT -d 192.168.108.5 -j DROP
封目的端口自己出不去,封源端口别人进不来
3.5、删
- [root@localhost ~]# iptables -F
- [root@localhost ~]# iptables -t filter -D INPUT 2
- [root@localhost ~]# iptables -t filter -D INPUT -p tcp --sport 22 -j ACCEPT
3.6、改
- [root@localhost ~]# iptables -t filter -R INPUT 2 -p tcp --dport 80 -s 192.168.42.102 -j ACCEPT
3.7、查
- [root@localhost ~]# iptables -nvL --line-number
- -n
- -v
- -L
3.8、保存规则:
修改后的所有规则都是临时的,当重启服务器或者重启机器规则就会消失,所以我们需要把规则保存到文件中
- [root@localhost ~]# service iptables save
保存防火墙规则到文件,/etc/sysconfig/iptables - [root@localhost ~]# iptables-save 输出规则到屏幕
- 对比文件,生成校验码
- md5sum filename
[root@localhost ~]# md5sum /etc/sysconfig/iptables
两个文件校验码相同则文件内容相同,否则不相同