根据IP地址进行过滤

防火墙如何限制某个ip访问。

通常,您可能希望从其原始IP地址过滤流量。为此,我们可以创建一个规则以允许特定的IP地址:

[root@rhel01 root]# iptables -A INPUT -s 192.168.0.27 -j ACCEPT

在这里,我们将一条规则附加到来自源192.168.0.27的ACCEPT数据包的INPUT链上。

如果我们想为本地网络上IP地址为192.168.9.x的所有连接打开网络,则可以添加一条规则:

[root@rhel01 root]# iptables -A INPUT -s 192.168.0.0/24 -j ACCEPT

我们还可以使用类似的规则来丢弃数据包:

[root@rhel01 root]# iptables -A INPUT -s 10.52.231.84 -j DROP

以上规则将丢弃来自IP地址10.52.231.84的所有入站流量

[root@rhel01 root]# iptables -A INPUT -s 192.168.0.0/24 -j DROP

上面的规则将阻止来自192.168.0.0/24网络的所有流量。

定义FORWARD链的默认策略

[root@rhel01 root]# iptables -P FORWARD DROP

此策略将阻止所有通过我们系统的数据包。

现在,我们的FORWARD政策如下所示:

Chain FORWARD (policy DROP)

定义规则

在下面的示例中,我们将分配一些基本规则。分配规则时请务必小心,如果远程登录服务器,则会失去连接!为了避免这种情况的发生,我们将首先将INPUT链上的默认策略设置为ACCEPT。

将INPUT链上的默认策略设置为ACCEPT

[root@rhel01 root]# iptables -P INPUT ACCEPT

清除所有现有规则

[root@rhel01 root]# iptables -F

保存iptables规则

一旦您对自己的规则满足要求感到满意,则将需要保存这些规则。这将确保它们在重启后就位。为此,我们将保存选项与iptables命令结合使用:

[root@rhel01 root]# service iptables save
Saving current rules to /etc/sysconfig/iptables:           [  OK  ]

现在,我们的规则已保存,将在下次重新启动时应用。我们可以通过查看以下文件再次验证我们的规则:

/etc/sysconfig/iptables

[root@rhel01 sysconfig]# pwd
/etc/sysconfig
[root@rhel01 sysconfig]# cat iptables
# Generated by iptables-save v1.2.5 on Wed May  8 13:31:38 2013
*filter
:INPUT DROP [2199:181749]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [660:49656]
[0:0] -A INPUT -i lo -j ACCEPT
[1141:101408] -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
[0:0] -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
COMMIT
# Completed on Wed May  8 13:31:38 2013

iptables中使用的三个主要链

输入input:所有发往主机的数据包。

输出output:来自主机的所有数据包。

转发forward:这些数据包不是发往主机的,也不是发自主机的。这些数据包从字面上传递。

然后,将规则添加到每个链中。然后针对这些规则中的每一个依次检查数据包。规则从顶部开始处理。如果匹配,则采取适当的措施。这可以是ACCEPTDROP。一旦与规则进行了匹配并采取了行动,则该链中将不再进行进一步的处理。如果数据包不通过匹配就通过所有规则,则使用默认策略来处理该数据包。此策略将为接受拒绝

iptables选项

显示带有数字行号的规则:

[root@rhel01 root]# iptables -n -L -v --line-numbers
Chain INPUT (policy DROP 7871 packets, 646K bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
2     2144  191K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0          state RELATED,ESTABLISHED
3        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0          tcp dpt:22
4        0     0 ACCEPT     all  --  *      *       192.168.0.27         0.0.0.0/0
5        0     0 ACCEPT     all  --  *      *       192.168.0.0/24       0.0.0.0/0
6        0     0 ACCEPT     all  --  *      *       192.168.0.32         0.0.0.0/0          MAC 00:50:56:AF:1F:74

Chain FORWARD (policy DROP 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 1907 packets, 137K bytes)
num   pkts bytes target     prot opt in     out     source               destination

如何删除规则

删除规则的最简单方法是使用以下命令找出与其关联的行号:

iptables -n -L -v -line-numbers


[root@rhel01 root]# iptables -n -L -v --line-numbers

[root@rhel01 root]# iptables -n -L -v --line-numbers
Chain INPUT (policy DROP 7871 packets, 646K bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
2     2144  191K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0          state RELATED,ESTABLISHED
3        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0          tcp dpt:22
4        0     0 ACCEPT     all  --  *      *       192.168.0.27         0.0.0.0/0
5        0     0 ACCEPT     all  --  *      *       192.168.0.0/24       0.0.0.0/0
6        0     0 ACCEPT     all  --  *      *       192.168.0.32         0.0.0.0/0          MAC 00:50:56:AF:1F:74

一旦确定了要删除的规则,就可以如下指定其行号(在我们的示例中,我们从INPUT链中删除了第6行):

[root@rhel01 root]# iptables -D INPUT 6

[root@rhel01 root]# iptables -n -L -v --line-numbers
Chain INPUT (policy DROP 8860 packets, 727K bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
2     2419  216K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0          state RELATED,ESTABLISHED
3        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0          tcp dpt:22
4        0     0 ACCEPT     all  --  *      *       192.168.0.27         0.0.0.0/0
5        0     0 ACCEPT     all  --  *      *       192.168.0.0/24       0.0.0.0/0

将规则附加到INPUT链

[root@rhel01 root]# iptables -A INPUT -i lo -j ACCEPT

-A标志用于附加规则,-i标志指定接口。在此示例中,我们指定本地主机。发布后,我们可以看到现在有一​​个条目:

Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere

打开一系列端口

iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 7000:7010 -j ACCEPT

上面的命令将端口7000打开到端口7010,以供入站流量使用。

按mac地址过滤

如果需要更高的访问安全性,则可以指定mac地址和IP地址。然后,这将仅允许来自具有匹配的mac地址访问权限的指定IP地址的流量:

[root@rhel01 root]# iptables -A INPUT -s 192.168.0.32 -m mac --mac-source 00:50:56:AF:1F:74 -j ACCEPT

上面的代码现在将在INPUT链中添加一条规则,以允许来自IP地址192.168.0.32的流量,其MAC地址为00:50:56:AF:1F:74。选项-m mac用于加载模块mac,而--mac-source用于允许我们指定mac地址。

定义要接受的输出链的策略

这将允许所有传出流量

[root@rhel01 root]# iptables -P OUTPUT ACCEPT

检查是否已安装iptables

大多数发行版都会默认提供安装或安装防火墙的功能。默认情况下,基于RPM的发行版(例如CentOS,Fedora和openSUSE)通常会安装iptables。基于Debian的系统通常使用ufw替代防火墙。我们将在单独的部分中介绍该产品。

要验证您的系统上是否安装了iptables,可以执行以下rpm查询命令:

[root@rhel01 root]# rpm -q iptables
iptables-1.2.5-3

[root@rhel01 root]# lsmod | grep iptab
iptable_filter          2752   0  (autoclean) (unused)
ip_tables              13984   1  [iptable_filter]

您还可以执行命令:iptables -L以显示当前加载的规则:

[root@rhel01 root]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

上面的代码表明我们目前没有分配任何规则给输入链(INPUT,FORWARD和OUTPUT)。

iptables

查看当前规则

要查看我们的规则是否有效,我们可以执行iptables -L -v命令。-L将列出规则,-v指定详细模式:

[root@rhel01 root]# iptables -L -v
Chain INPUT (policy DROP 1716 packets, 141K bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 ACCEPT     all  --  lo     any     anywhere             anywhere
  889 79108 ACCEPT     all  --  any    any     anywhere             anywhere           state RELATED,ESTABLISHED
    0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere           tcp dpt:ssh

Chain FORWARD (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 352 packets, 26164 bytes)
 pkts bytes target     prot opt in     out     source               destination

修改INPUT链上的默认策略

以下规则会将默认策略设置为DROP。这意味着所有不符合上述规则的数据包都将被丢弃。首先要有ssh规则,这一点很重要,否则,如果远程连接,您可能会将自己锁定在系统之外。

[root@rhel01 root]# iptables -P INPUT DROP

现在我们的规则定义如下。(请注意,默认策略已更改为DROP):

Chain INPUT (policy DROP)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere           state RELATED,ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere           tcp dpt:ssh

Linux iptables命令示例

iptables是配置实用程序的名称,该实用程序用于配置Linux内核防火墙提供的表。Netfilter是一个内核模块,负责实际的数据包过滤。该应用程序通常由系统的root用户运行。iptables用于在Linux内核中创建,维护和检查IPv4数据包筛选器规则表。每个表包含许多内置链,也可能包含用户定义的链。

每个链都是可以匹配一组数据包的规则列表。每个规则都指定如何处理匹配的数据包。这称为目标,可以是到同一表中用户定义的链的跳转。

防火墙规则指定数据包和目标的条件。如果数据包不匹配,则检查链中的下一个规则;否则,检查规则。如果没有匹配项,则下一个规则由目标值指定,这可以是用户定义链的名称,也可以是特殊值ACCEPTDROPQUEUERETURN之一。

iptables的基本功能是将规则放入预定义的链中。这些链是INPUTOUTPUTFORWARD。根据网络流量检查这些链,然后根据规则决定应采取的措施。数据包被接受或丢弃。这些动作称为目标。主要目标是接受拒绝

将状态模块添加到INPUT链

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

状态模块用于检查数据包的状态,并确定它是NEWESTABLISHED还是RELATED,到目前为止,我们现在可以看到两个新规则:

Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere           state RELATED,ESTABLISHED

NEW:传入的数据包是新的,不是由主机系统启动的。

RELATED/ESTABLISHED:这些是具有已建立连接或与已建立连接相关的传入数据包。

开放端口22,允许SSH连接

[root@rhel01 root]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT

上面的指令添加了一条规则,该规则允许通过tcp端口22进行连接。

到目前为止,我们的规则如下所示:

Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere           state RELATED,ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere           tcp dpt:ssh

显示特定的链条规则

[root@rhel01 root]# iptables -L INPUT -n -v

上面的命令显示INPUT链中的规则。

[root@rhel01 root]# iptables -L OUTPUT -n -v

上面的命令显示输出链中的规则。

日期:2019-04-29 03:17:38 来源:oir作者:oir