防火墙iptables和firewalld

摘要

Linux中有两种防火墙软件,ConterOS7.0以上使用的是firewall,ConterOS7.0以下使用的是iptables,本文将分别介绍两种防火墙软件的使用。

firewalld

开启防火墙:

systemctl start firewalld

关闭防火墙:

systemctl stop firewalld

查看防火墙状态:

systemctl status firewalld

设置开机启动:

systemctl enable firewalld

禁用开机启动:

systemctl disable firewalld

重启防火墙:

firewall-cmd --reload

开放端口(修改后需要重启防火墙方可生效):

firewall-cmd --zone=public --add-port=8080/tcp --permanent

查看开放的端口:

firewall-cmd --list-ports

关闭端口:

firewall-cmd --zone=public --remove-port=8080/tcp --permanent

Iptables

安装

由于CenterOS7.0以上版本并没有预装Iptables,我们需要自行装。
安装前先关闭firewall防火墙

systenctl stop firewalld

安装iptables:

yum install iptables

安装iptables-services:

yum install iptables-services

iptables配置文件

配置文件位置: /etc/sysconfig/iptables

使用

开启防火墙:

systemctl start iptables.service

关闭防火墙:

systemctl stop iptables.service

查看防火墙状态:

systemctl status iptables.service

设置开机启动:

systemctl enable iptables.service

禁用开机启动:

systemctl disable iptables.service

iptables常用命令及参数

常用指令

iptables -A 将一个规则添加到链末尾
iptables -D 将指定的链中删除规则
iptables -F 将指定的链中删除所有规则
iptables -I 将在指定链的指定编号位置插入一个规则
iptables -L 列出指定链中所有规则
iptables -t nat -L 列出所有NAT链中所有规则
iptables -N 建立用户定义链
iptables -X 删除用户定义链
iptables -P 修改链的默认设置,如将iptables -P INPUT DROP (将INPUT链设置为DROP)

常用参数

--dport 指定目标TCP/IP端口 如 –dport 80
--sport 指定源TCP/IP端口 如 –sport 80
-p tcp 指定协议为tcp
-p icmp 指定协议为ICMP
-p udp 指定协议为UDP
-j DROP 拒绝
-j ACCEPT 允许
-j REJECT 拒绝并向发出消息的计算机发一个消息
-j LOG 在/var/log/messages中登记分组匹配的记录
-m mac –mac 绑定MAC地址
-m limit –limit 1/s 1/m 设置时间策列
-s 10.10.0.0或10.10.0.0/16 指定源地址或地址段
-d 10.10.0.0或10.10.0.0/16 指定目标地址或地址段
-s ! 10.10.0.0 指定源地址以外的

配置Filter表防火墙

查看filter表的几条链规则(INPUT链可以看出开放了哪些端口):

iptables -L -n

查看NAT表的链规则:

iptables -t nat -L -n

清除防火墙所有规则:

iptables -F
iptables -X
iptables -Z

保存防火墙设置

/etc/init.d/iptables save
或
service iptables save

添加防火墙规则
1.开启SSH服务端口

iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT 

注:如果在预设设置把OUTPUT设置成DROP策略的话,就需要设置OUTPUT规则,否则无法进行SSH连接。
2.开启Web服务端口

iptables -A OUTPUT -p tcp --sport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT

3.开启邮件服务的25、110端口

iptables -A INPUT -p tcp --dport 110 -j ACCEPT
iptables -A INPUT -p tcp --dport 25 -j ACCEPT

4.设置icmp服务

iptables -A OUTPUT -p icmp -j ACCEPT (OUTPUT设置成DROP的话)
 iptables -A INPUT -p icmp -j ACCEPT    (INPUT设置成DROP的话)

5.允许loopback

IPTABLES -A INPUT -i lo -p all -j ACCEPT 
(如果是INPUT DROP)
 IPTABLES -A OUTPUT -o lo -p all -j ACCEPT
(如果是OUTPUT DROP)

6.减少不安全的端口连接

iptables -A OUTPUT -p tcp --sport 31337 -j DROP
iptables -A OUTPUT -p tcp --dport 31337 -j DROP

7.只允许某台主机或某个网段进行SSH连接

只允许192.168.0.3的机器进行SSH连接
iptables -A INPUT -s 192.168.0.3 -p tcp --dport 22 -j ACCEPT
如果允许或限制一段IP地址可用192.168.0.0/24表示192.168.0.1-255端的所有IP, 24表示子网掩码数。
iptables -A INPUT -s 192.168.0.0/24 -p tcp --dport 22 -j ACCEPT
如果只允许除了192.168.0.3的主机外都能进行SSH连接
iptables -A INPUT -s ! 192.168.0.3 -p tcp --dport 22 -j ACCEPT

8.开启转发功能
在做NAT网络配置时,FORWARD默认规则是DROP时,必须开启数据包转发功能

iptables -A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i eth1 -o eh0 -j ACCEPT

9.丢弃坏的TCP包

iptables -A FORWARD -p TCP ! --syn -m state --state NEW -j DROP

10.处理IP碎片数量,防止DDOS攻击,允许每秒100个

iptables -A FORWARD -f -m limit --limit 100/s --limit-burst 100 -j ACCEPT

11.设置ICMP包过滤, 允许每秒1个包, 限制触发条件是10个包

iptables -A FORWARD -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT

12.DROP非法连接

iptables -A INPUT   -m state --state INVALID -j DROP
iptables -A OUTPUT  -m state --state INVALID -j DROP
iptables -A FORWARD -m state --state INVALID -j DROP

13.允许所有已经建立的和相关的连接

[root@tp ~]# iptables-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
[root@tp ~]# iptables-A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
[root@tp ~]# /etc/rc.d/init.d/iptables save

配置NAT表防火墙

查看本机关于NAT的设置情况

iptables -t nat -L

清除NAT规则

[root@tp ~]# iptables -F -t nat
[root@tp ~]# iptables -X -t nat
[root@tp ~]# iptables -Z -t nat

添加规则
添加基本的NAT地址转换,添加规则时,我们只添加DROP链,因为默认链全是ACCEPT。
1.防止外网用内网IP欺骗

[root@tp sysconfig]# iptables -t nat -A PREROUTING -i eth0 -s 10.0.0.0/8 -j DROP
[root@tp sysconfig]# iptables -t nat -A PREROUTING -i eth0 -s 172.16.0.0/12 -j DROP
iptables -t nat -A PREROUTING -i eth0 -s` 192.168.0.0/16 -j DROP

2.禁止与211.101.46.253的所有连接

iptables -t nat -A PREROUTING -d 211.101.46.253 -j DROP

3.禁用FTP(21)端口

iptables -t nat -A PREROUTING -p tcp --dport 21 -j DROP

4.只禁用211.101.46.253地址的FTP连接,其他连接可以进行。

iptables -t nat -A PREROUTING -p tcp --dport 21 -d 211.101.46.253 -j DROP

更多分享微信搜索“安全info”公众号
添加群主进“安全交流”微信群
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流浪法师12

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值