重命名用户定义的链

# iptables -E old_chain_name  new_chain_name
# iptables -E INTRANET EXTRANET
# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
EXTRANET   all  --  192.168.1.0/24       anywhere
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
Chain EXTRANET (1 references)
target     prot opt source               destination

如何删除用户定义的iptables 链

确保我们要删除的链中没有规则

# iptables -X INTRANET

重要的提示:

无法删除默认链

链策略(Chain Policies)

这个术语是指我们大多数人都错过的所有链的默认策略。
如果我们重新查看下面的链,请考虑突出显示的术语。

# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
INTRANET   all  --  192.168.0.0/24       anywhere
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
Chain INTRANET (1 references)
target     prot opt source               destination
DROP       tcp  --  anywhere             anywhere            tcp dpt:telnet

现在,正如我们所看到的,默认情况下所有链都有默认的 ACCEPT 策略,这对于关键机器来说可能是危险的,除了我们创建的任何规则之外,所有其他连接都是允许的。

更改链的默认策略

现在因为我们已经创建了一个新链,所以我们应该更改 INPUT 链的默认策略,因为我们只希望那些我们在规则中允许的用户与我们进行通信

# iptables -P INPUT DROP

说明:

默认 DROP 策略可能会阻止典型的 TCP/UDP/ICMP 通信

TCP 使用 3 次握手

  • 同步
  • 同步确认
  • 确认
# iptables -L INPUT
Chain INPUT (policy DROP)
target     prot opt source               destination
INTRANET   all  --  192.168.0.0/24       anywhere
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED

因此,正如我们现在看到的,只有那些机器将被允许与我们通信,我们将通过机器中的规则允许这些机器。

TCP 匹配 - 面向连接

  • TCP 工作在第 4 层,也称为面向连接的协议。
  • TCP 数据包包含发送方和接收方的信息。
  • 例如:HTTP、FTP、telnet 等

使用的语法

-p tcp, --protocol tcp
--sport,--source-port

说明:

源端口通常从> 1024 中任意选取

例子:
使用 EXTRANET 链阻止来自 192.168.1.0/24 的 localhost 的 telnet 连接

# iptables -A EXTRANET -p tcp --dport telnet -j DROP

UDP 匹配: - 无连接

  • UDP 工作在第 4 层,被认为是无连接协议。
  • 与 TCP 相比,UDP 考虑的数据包被认为要快得多,因为它们不像 TCP 那样重,因为它们不包含有关数据包的太多细节。
  • 这些通常用于广播。
  • 例如:DNS、DHCP、系统日志、NTP 等

使用的语法

-p udp, --protocol udp
--sport,--source-port - same source port as destination port

Internet 控制消息传递协议 (ICMP)

ICMP 类型:

  • echo-request - PING
  • echo-reply - pong
  • PING - 本地系统通过 OUTPUT 链发送回声请求(PING)

远程系统在其 INPUT 链中收到回声请求 => 远程系统以回声回复(Pong)进行响应

使用的语法

-p icmp, --protocol icmp
Linux iptables示例

在iptable中创建用户定义的链

现在默认情况下, iptables 中有 3 个链,它们是 NAT、过滤器和 mangle。

我们可以新创建一个新链并根据要求使用它,主要是为了降低复杂性。

创建新iptable链

# iptables -n INTRANET

这将创建一个新链。

# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
Chain INTRANET (o references)
target     prot opt source               destination

现在正如我们在上面看到的,一个新的链已经创建,现在我们需要根据要求将流量或者规则划分到 INTRANET 链。

为用户定义的链创建引用

如果有很多的规则,为了降低查找难度(便于管理),
所以我们想将 源 192.168.0.0/24 的所有规则都放在 INTRANET 链下。

# iptables -I INPUT 1 -s 192.168.0.0/24 -j INTRANET

根据上述规则,我正在创建并在 INPUT 链 1 的位置中插入一条新规则。
因为我想赋予此规则最大优先级,即 使用源 IP 192.168.0.0/24 创建的任何规则都将在 IntraNET 链下可见。

# iptables -I INTRANET -p tcp --dport telnet -j DROP

根据上述规则,我将在源 192.168.0.0/24 的内部网链中插入一条新规则(因为我们已经使用内部网链的最后一条规则定义了此 IP 源),应该删除到 telnet 端口 23 的连接。

现在让我们看看我们的规则是否已正确实施

# iptables -L INTRANET
Chain INTRANET (1 references)
target     prot opt source               destination
DROP       tcp  --  anywhere             anywhere            tcp dpt:telnet

根据上面的规则列表,任何来自 192.168.0.0/24 使用端口 23 的连接都将被丢弃。

任何用户定义的链中的各种sport、dport、源目的地的规则可以不同。

其他示例

阻止来自 192.168.0.30 的端口 22 用于内联网链中的本地机器。

# iptables -A INTRANET -s 192.168.0.30 -p tcp --dport 22 -j DROP

所以正如你在上面的规则中看到的,我在内部网链中为源 192.168.0.30 添加一个新规则并阻止来自端口号 22的通信。

日期:2020-06-02 22:17:07 来源:oir作者:oir