Linux运维之如何流程化编写iptables脚本

版权声明:本文为博主原创文章,未经博主允许不得转载。原创不易,各位勉之。 https://blog.csdn.net/LL845876425/article/details/79246299

如何流程化编写iptables脚本

根据需求调整系统内核

例如tcp的SYN缓冲(syncookies)是一种快速检测和防御SYN洪水工具的机制,如果下命令可以启用SYN缓冲:

[root@centos7-2 ~]# echo "1" > /proc/sys/net/ipv4/tcp_syncookies
[root@centos7-2 ~]#

另外,如果以iptables作为NAT路由器,对于存在着多个网卡的情况,则要开启IP转发功能,用于多网卡之间数据的流通,命令如下:

[root@centos7-2 ~]# echo "1" > /proc/sys/net/ipv4/ip_forward
[root@centos7-2 ~]# cat /proc/sys/net/ipv4/ip_forward
1

其他适用于iptables防火墙的内核调整可以根据需求自行设定。

加载iptables模块

由于iptables不是以服务的方式启动iptables的,而是采用service的方式,所以需要手动加载iptables模块,例如:

[root@centos7-2 ~]# modprobe ip_tables
[root@centos7-2 ~]# modprobe iptable_nat
[root@centos7-2 ~]# modprobe ip_nat_ftp
[root@centos7-2 ~]# modprobe ip_nat_irc
[root@centos7-2 ~]# modprobe nf_conntrack
[root@centos7-2 ~]# modprobe ip_conntrack_ftp
[root@centos7-2 ~]# modprobe ipt_MASQUERADE

接下来可以使用lsmod来查看加载的模块,命令如下:

[root@centos7-2 ~]# lsmod | grep "ip"
ipt_MASQUERADE         12678  0 
nf_nat_masquerade_ipv4    13412  1 ipt_MASQUERADE
iptable_nat            12875  0 
nf_conntrack_ipv4      15053  1 
nf_defrag_ipv4         12729  1 nf_conntrack_ipv4
nf_nat_ipv4            14115  1 iptable_nat
nf_nat                 26787  4 nf_nat_ftp,nf_nat_irc,nf_nat_ipv4,nf_nat_masquerade_ipv4
nf_conntrack          133387  8 nf_nat_ftp,nf_nat_irc,nf_nat,nf_nat_ipv4,nf_nat_masquerade_ipv4,nf_conntrack_ftp,nf_conntrack_irc,nf_conntrack_ipv4
ip_tables              27115  1 iptable_nat
[root@centos7-2 ~]#

新版的iptables会自动使用新模块的名称来代替旧模块的名称。

清空所有表列规则(包括自定义的)

命令如下:

[root@centos7-2 ~]# iptables -F
[root@centos7-2 ~]# iptables -X
[root@centos7-2 ~]# iptables -Z
[root@centos7-2 ~]# iptables -F -t nat
[root@centos7-2 ~]# iptables -X -t nat
[root@centos7-2 ~]# iptables -Z -t nat
[root@centos7-2 ~]# iptables -X -t mangle

定义默认策略

一般来说为了搭建安全的防火墙,默认是拒绝一切流量连接的,所以三表五链默认规则都应该是DROP,但对于实际线上的iptables脚本,建议按如下方式配置(因为一般认为从服务器OUTPUT出去的数据和NAT出去的数据都是安全的):

[root@centos7-2 ~]# iptables -P INPUT DROP
[root@centos7-2 ~]# iptables -P FORWARD ACCEPT
[root@centos7-2 ~]# iptables -P OUTPUT ACCEPT
[root@centos7-2 ~]# iptables -t nat -P PERROUTING ACCEPT
[root@centos7-2 ~]# iptables -t nat -P POSRROUTING ACCEPT
[root@centos7-2 ~]# iptables -t nat -P OUTPUT ACCEPT

打开“回环”

这样做是为了不必要的麻烦,命令如下:

[root@centos7-2 ~]# iptables -A INPUT -i lo -j ACCEPT
[root@centos7-2 ~]# iptables -A OUTPUT -o lo -j ACCEPT

允许状态为ESTABLISHED 的数据包进入机器

命令如下:

[root@centos7-2 ~]# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

完整版的防火墙规则

[root@private ~]# cat iptables_script.sh 
#!/bin/bash
modprobe ip_tables
modprobe iptable_nat
modprobe pf_conntrack
iptables -F
iptables -X
iptables -Z
iptables -F -t nat
iptables -X -t nat
iptables -Z -t nat
iptables -X -t mangle
iptables -P INPUT DROP
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -t nat -P PERROUTING ACCEPT
iptables -t nat -P POSRROUTING ACCEPT
iptables -t nat -P OUTPUT ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
iptables -A INPUT -m state --state NEW -j DROP
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

猜你喜欢

转载自blog.csdn.net/LL845876425/article/details/79246299
今日推荐