iptables 构建防火墙应用
IPtables 不是真正的防火墙,netfilter 才是防火墙真正的安全框架。iptables 其实是一个命令行工具,位于用户空间,能用这个工具操作真正的框架。
iptables 中有5张表
filter表:iptables 中使用最广泛的表,作用是进行过滤主机的数据包,也是由filter 表来决定一个数据包是否继续发往目的地地址或者被拒绝。
nat表:用于网络地址转换,可以改变数据包的源地址或者目的地址。
input——forward——output
output,负责发出去的数据包
perrouting 在数据包达到防火墙时进行判断,改变目的地址
postrouting 在数据包离开防火墙时,进行路由判断之后的判断的规则,改变源地址
nat表:用于望楼地址转换,可以改变数据包的源地址或者目标地址
mangle表:用于修改IP的头部信息,如修改ttl
raw表:为iptables提供了一种不经过状态追踪的机制,在大流量对外业务的服务器上使用这个表,可以避免状态追踪带来的性能问题。
security表:提供在数据包中加入SELinux特性的功能。在实际应用中,security一般不常用。
在centos7下安装防火墙
首先查看防火墙是否存在
输入 services iptables status
如果找不到得自己下载配置
1、停止 firewalld 服务
systemctl stop filrewalld
禁止 firewalld 服务
systemctl mask filrewalld
2、安装 iptables
yum install -y iptables
升级 iptables
yum update iptables
3、检查是否安装完成
systemctl status iptables
ok fine
iptables 常用规则
iptables -L -n :查看现有的规则
iptables -P INPUT ACCEPT:允许所有(在配置时,先允许)
iptables -F:清空所有默认规则
iptables -X:清空所有自定义规则
iptables -Z:所有计数器归0
iptables -A INPUT -i lo -j ACCEPT:允许来自于lo接口的数据包(本地访问)
iptables -A INPUT -p tcp --dport 22 -j ACCEPT:开放22端口(远程链接linux端口)
iptables -A INPUT -p tcp --dport 21 -j ACCEPT:开放21端口(FTP端口)
iptables -A INPUT -p tcp --dport 80 -j ACCEPT:开放80端口(HTTP端口)
iptables -A INPUT -p tcp --dport 443 -j ACCEPT:开放443端口(HTTPS端口)
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT:允许ping
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT:允许接受本机请求之后的返回数据related,是为ftp设置的
iptables -P INPUT DROP:其他入站一律丢弃
iptables -P OUTPUT ACCEPT:所有出站都允许
iptables -P FORWARD DROP:所有转发一律丢弃
iptables -A INPUT -p tcp -s 45.96.174.68 -j ACCEPT:添加内网ip信息(接受所有TCP)请求
iptables -P INPUT DROP:过滤所有非以上规则的请求
iptables -I INPUT -s .***.***. -j ACCEPT:封停该ip
iptables -D INPUT -s .***.***. -j DROP :解封该ip
实际生产中,iptables常用的命令配置
iptables -F:清除所有规则
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1-j ACCEPT:允许某些调用localhost应用访问。
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT:允许其他地方ping
iptables -A INPUT -p cmp --icmp-type fragmentation-needed -j ACCEPT :允许从其他主机/网络设备发送MTU调整报文
iptables -A INPUT -p tcp --dport 80 -j ACCEPT:开放80端口(HTTP端口)
iptables -A INPUT -p tcp --dport 443 -j ACCEPT:开放443端口(HTTPS端口)
iptables -A INPUT -p tcp -s 10.0.1.17 --dport 22 -j ACCEPT:仅允许内网10.0.1.17,通过tcp访问22端口。
iptables -A INPUT -p udp -s 10.0.1.17 --dport 161 -j ACCEPT:仅允许内网10.0.1.17,通过UDP访问161端口。
iptables -A INPUT -j DROP:禁止非开放白名单流量进入。
iptables -A OUTPUT -m state --state ESTABLISHED -j ACCEPT:允许本机响应规则为01-08的数据包发出
iptables -A OUTPUT -j DROP:禁止本机主动发出外部链接,可以有效的防止类似“反弹shell”的攻击
iptables -A FORWARD -j DROP:禁止本机转发数据包
重启服务
当我们配置完成后,需要保存命令并重启iptables服务,启动服务通过如下命令:
service iptables save :保存命令
systemctl enable iptables.service:注册iptables服务
systenctl start iptables.service:开启服务
systemctl status iptables .service:查看状态
systemctl restart iptables.service:重启防火墙
网络地址转换
在实际工作中,iptables还经常用于网络地址转换(NAT),通过网络地址转换,可以有效的减少直接部署公网IP地址的服务器数量,增强网络环境的完全性。
源地址转换:
1.在Server B上指定其网络默认网关是Server A的内网地址(10.128.70.112)
2.在Server A上启动路由功能。
sysctl -w net.ipv4.ip_forward=1 改servicesB的网关
3.在Server A上设置iptables规则。
iptables -t filter -A FORWARD -j ACCEPT 过滤数据
iptables -t nat -A POSTROUTING -o enth 0 -j SNAT -to 220.217.143.73 桥接servicesA里的网关
通过上述三步操作,Server B将会通过Server A访问互联网,此时在互联网上看到的源地址是Server A的外网IP。
目标地址转换:
目标地址转换用于外部用户直接访问无外网IP的服务器,例如:外部用户希望访问Server B上的MySQL数据库,端口3306,那么在Server A上可以进行如下配置:
iptables -t nat -A PREROUTING -d 220.217.143.73 -p tcp -m tcp --dport 3306 -j DNAT --to-destination 10.128.70.111:3306
DNAT 目的网络地址,重写目的IP地址。从外网进来转发到B上去。
iptables -t nat -A POSTROUTING -d 10.128.70.111 -p tcp -m tcp --dport 3306 -j SANT --to-source 10.128.70.112
SNAT 让本地IP地址伪装其他IP地址。 从B上出来,转发到A上去。
常见参数说明
这里,我们先对iptables的常用参数进行说明:
需要注意的是,Linux是个严格区分大小写的系统,所以所有命令,也需要严格遵循大小写。
tcp wrappers 构建应用访问控制列表
通过ldd命令来查看是否调用了libwrap 库
ldd /usr/sbin/sshd |grep libwarp
cd /usr/sbin
无连接时配置、etc/hosts.allow 限制SSHD 的访问
利用DenyHosts 防止暴力破解
通过DenyHosts 下载安装
wegt ‘https://sourceforge.net/projects/denyhosts/files/denyhosts/2.6/DenyHosts-2.6.tar.gz/download’ -O DenyHosts-2.6.tar.gz
下载完解压
tar zxvf DenyHosts-2.6.tar.gz
解压后进入安装目录,通过Python对服务进行安装
cd DenyHosts-2.6
python setup.py install
安装完后对目录进行配置
cd /usr/share/denyhosts
列出文件后复制一下
cp daemon-control-dist daemon-control
cp danyhosts.cfg-dist denyhosts.cfg
创建连接并启动
ln daemon-control /etc/init.d
/etc/init.d/daemon-control start