五分钟学会iptables 企业级的防火墙
2017-12-28 12:11
204 查看
通过防火墙可以有效的进行安全控制,下面就简单列举一下所涉及使用及生产脚本
2,开启内核转发
3,添加iptables模块
22端口开放 可以ssh连接
允许内网IP地址段访问
允许回环地址可以进出
4.1 修改默认规则 (看电影模式)
4.2 添加允许的服务及端口
允许访问http服务80 443
只允许内网ping
允许ftp请求
允许某端口映射
2) db数据服务
3) snmp 网络管理
4) rsync 实时同步
5) nfs 2049,rpc 111 (NFS网络文件共享、rpc)
6) 允许所有ping
7)允许内网172.16.1.0 网段ping
8) keepalived vrr
9) zabbix端口开放
iptables -A INPUT -p tcp --syn -m limit --limit 100/s --limit-burst 100 -j ACCEPT
2)没秒ping不超过10个
iptables -A FORWAD -p icmp --icmp-type echo-request -m limit --limit 1/s --limit-burst 10 -j ACCPET
3)将SYN及ACK SYN限制为每秒不超过200
iptables -A INPUT -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -m limit --limit 20/sec --limit-burst 200 -j ACCEPT
4)防范 SYN-Flood 碎片攻击
iptables -N syn-flood
iptables -A INPUT -syn -j syn-flood
iptables -A syn-flood -m limit -limit 5000/s -limit-burst 200 -j RETURN
iptables -A syn-flood -j DROP
prevent all Stealth Scans and TCP State Flags
iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
All of the bits are cleared
iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
iptables -A INPUT -p tcp --tcp-flags ALL FIN,URG,PSH -j DROP
SYN and RST are both set
iptables -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
SYN and FIN are both set
iptables -A INPUT -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
FIN and RST are both set
iptables -A INPUT -p tcp --tcp-flags FIN,RST FIN,RST -j DROP
FIN is the only bit set, without the expected accompanying ACK
iptables -A INPUT -p tcp --tcp-flags ACK,FIN FIN -j DROP
PSH is the only bit set, without the expected accompanying ACK
iptables -A INPUT -p tcp --tcp-flags ACK,PSH PSH -j DROP
URG is the only bit set, without the expected accompanying ACK
iptables -A INPUT -p tcp --tcp-flags ACK,URG URG -j DROP
1,iptables脚本
[root@ghzz ~]# vi /root/shell/iptables.sh #!/bin/bash #by qiuyuetao #contact :598759292 #-------------------------------------------------------------------------- IPTAB=/sbin/iptables #定义环境变量 $IPTAB -X #清空自定义链 $IPTAB -Z #清空计数器 $IPTAB -F #清空当前表的所有规则 $IPTAB -t nat -F #清空nat表的所有规则 $IPTAB -t mangle -F #清空mangel 表规则 $IPTAB -t raw -F #清空raw表规则 $IPTAB -X -t nat #清空自定义链 $IPTAB -X -t raw $IPTAB -X -t mangle #-------------------------------------------------------------------------- $IPTAB -A INPUT -i lo -j ACCEPT #允许回环地址访问 $IPTAB -A INPUT -s 127.0.0.1 -j ACCEPT #允许自己访问 $IPTAB -A INPUT -s 192.168.0.0/22 -j ACCEPT# 允许内网访问 $IPTAB -A INPUT -s 119.161.189.1 -j ACCEPT #允许跳板机访问 #-------------------------------------------------------------------------- $IPTAB -A INPUT -p icmp -j ACCEPT #允许icmp 也就是ping $IPTAB -A INPUT -p tcp -m multiport --dport 80,22,3306,2188 -j ACCEPT #对外开放的端口 $IPTAB -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT #对进来的包的状态进行检测。已经建立tcp连接的包以及该连接相关的包允许通过! $IPTAB -P INPUT DROP #除了上面的fiter表input链 $IPTAB -P FORWARD DROP $IPTAB -P OUTPUT ACCEPT #-------------------------------------------------------------------------- $IPTAB-save > /etc/sysconfig/iptables
2,iptables日常配置
1,防火墙是否开启[root@m01 ~] /etc/init.d/iptables status 开启iptables iptables -F #清空当前表规则 iptables -X #清空自定义链 iptables -Z #清空计数器
2,开启内核转发
将net.ipv4.ip_forward = 1 由0改为1 sed -i 'snet.ipv4.ip_forward = 0net.ipv4.ip_forward = 1g' sysctl -p 生效
3,添加iptables模块
lsmod 显示已经加载到内核中的模块的状态信息 lsmod|egrep "nat|filter" 查看nat 与filter表加载的模块信息 modprobe ip_tables modprobe ip_conntrack modprobe iptable_filter modprobe iptable_nat modprobe ip_conntrack_ftp 连接跟踪 modprobe ip_nat_ftp nat模块 modprobe ipt_state 状态模块
3,企业级的防火墙模式选择
运行自己可以访问,以免关在门外22端口开放 可以ssh连接
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
允许内网IP地址段访问
iptables -A INPUT -d 172.16.1.0/24 -j ACCEPT
允许回环地址可以进出
iptables -A INPUT -i lo -j ACCEPT iptables -A OUTPUT -o lo -j ACCEPT
4.1 修改默认规则 (看电影模式)
iptables -P INPUT DROP iptables -P FORWARD DROP iptables -P OUTPUT ACCEPT
4.2 添加允许的服务及端口
允许访问http服务80 443
iptables -A INPUT -p tcp -m multiport --dport 80,443 -j ACCEPT
只允许内网ping
iptables -A INPUT -p icmp -m icmp --icmp-type 8 -s 172.16.1.0/24 -j ACCEPT
允许ftp请求
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
允许某端口映射
iptables -A INPUT -p icmp -m icmp --icmp-type 0 -j ACCEPT iptables -A INPUT -p udp -m udp --sport 53 -j ACCEPT iptables -A INPUT -p udp -m udp --dport 53 -j ACCEPT iptables -A INPUT -p tcp -m tcp --dport 53 -j ACCEPT iptables -A INPUT -p tcp -m tcp --sport 53 -j ACCEPT
4,NAT 共享上网
/proc/sys/net/ipv4/ip_forward 开启内核转发 iptables -t filter -A FORWARD -j ACCEPT iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -o eth0 -j SNAT --to-source 10.0.0.61 #所有内网172.16.1.0网段地址,都通过10.0.0.61这个IP 转发上外网 iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -j MASQUERADE #MASQUERADE 自动获取服务端IP,做IP地址转发
5,其他应用服务规则案例
1)http服务iptables -A INPUT -s 192.168.1.0/24 -p tcp -m multiport --dport 8080,8081,8082 -j ACCEPT iptables -A INPUT -p tcp --dport 22000:22030 -j ACCEPT #:冒号代表一段端口从22000-22030所有端口都允许
2) db数据服务
iptables -A INPUT -s 10.1.8.0/24 -p tcp --dport 3306 -j ACCEPT
3) snmp 网络管理
iptables -A INPUT -s 10.1.8.0/24 -p UDP --dport 161 -j ACCEPT
4) rsync 实时同步
iptables -A INPUT -s 10.1.8.0/24 -p tcp -m tcp --dport 873 -j ACCEPT
5) nfs 2049,rpc 111 (NFS网络文件共享、rpc)
iptables -A INPUT -s 10.1.8.0/24 -p udp -m multiport --dport 111,892,2049 -j ACCEPT iptables -A INPUT -s 10.1.8.0/24 -p tcp -m multiport --dport 111,892,2049 -j ACCEPT
6) 允许所有ping
iptables -A INPUT -p icmp -m icmp --icmp-type any -j ACCEPT
7)允许内网172.16.1.0 网段ping
iptables -A INPUT -p icmp -m icmp --icmp-type 8 -s 172.16.1.0/24 -j ACCEP
8) keepalived vrr
-A INPUT -d 172.16.1.0/24 -j ACCEPT -A INPUT -p vrrp -j ACCEPT
9) zabbix端口开放
-A INPUT -p tcp -m tcp --dport 10050:10051 -j ACCEPT -A INPUT -p tcp -m udp --dport 10050:10051 -j ACCEPT
6,iptable安全配置
1)限速iptables -A INPUT -p tcp --syn -m limit --limit 100/s --limit-burst 100 -j ACCEPT
2)没秒ping不超过10个
iptables -A FORWAD -p icmp --icmp-type echo-request -m limit --limit 1/s --limit-burst 10 -j ACCPET
3)将SYN及ACK SYN限制为每秒不超过200
iptables -A INPUT -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -m limit --limit 20/sec --limit-burst 200 -j ACCEPT
4)防范 SYN-Flood 碎片攻击
iptables -N syn-flood
iptables -A INPUT -syn -j syn-flood
iptables -A syn-flood -m limit -limit 5000/s -limit-burst 200 -j RETURN
iptables -A syn-flood -j DROP
prevent all Stealth Scans and TCP State Flags
iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
All of the bits are cleared
iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
iptables -A INPUT -p tcp --tcp-flags ALL FIN,URG,PSH -j DROP
SYN and RST are both set
iptables -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
SYN and FIN are both set
iptables -A INPUT -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
FIN and RST are both set
iptables -A INPUT -p tcp --tcp-flags FIN,RST FIN,RST -j DROP
FIN is the only bit set, without the expected accompanying ACK
iptables -A INPUT -p tcp --tcp-flags ACK,FIN FIN -j DROP
PSH is the only bit set, without the expected accompanying ACK
iptables -A INPUT -p tcp --tcp-flags ACK,PSH PSH -j DROP
URG is the only bit set, without the expected accompanying ACK
iptables -A INPUT -p tcp --tcp-flags ACK,URG URG -j DROP
相关文章推荐
- 企业级办公室iptables防火墙应用案例
- iptables企业级防火墙设置
- 企业级iptables防火墙配置方案
- iptables防火墙企业级模板
- (十三)洞悉linux下的Netfilter&iptables:为防火墙增添功能模块【实战】
- 摘自ubuntuer-Linux防火墙iptables学习笔记(五)linux+iptables构筑防火墙实例
- 分享一例iptables 防火墙配置
- iptables 防火墙规则 的备份恢复
- linux操作系统防火墙iptables配置
- 在线生成IPTABLES防火墙脚本的网站
- CentOS6.4防火墙iptables
- iptables防火墙规则整理
- Linux防火墙Iptables设置
- 防火墙之 iptables软件(Netfilter机制)
- 防火墙 之 iptables的基本认识
- Linux系统Iptables防火墙
- Iptables防火墙(二)
- 防火墙iptables原理及简单应用
- Ubuntu Server 12.04 配置防火墙iptables
- 实战Linux下防火墙iptables设置