CentOS 5.6 上的 IPtables 问题

CentOS 5.6 上的 IPtables 问题

我在 CentOS 5.6 机器上使用 IPtables 时遇到了问题,之前它运行正常,但我不完全确定这是什么时候开始发生的,因为我只在允许端口上的新 IP 时才注意到这个问题。

基本上,当我尝试停止、启动或重新启动时,我会得到以下信息:

Flushing firewall rules: [  OK  ]
Setting chains to policy ACCEPT: security raw nat mangle filter [FAILED]
Unloading iptables modules: [  OK  ]

出于某种原因,无论我做什么,都无法重新配置规则。我甚至尝试过完全卸载 IPtables(通过 yum),但即使在系统重新启动后,它仍然只允许连接到问题出现之前打开的端口,并过滤其他所有内容。

我真的束手无策了,iptables status显示链完全空了,但就外部连接而言仍然没有任何乐趣。

任何想法都会受到赞赏。如果您希望我提供更多信息,请告诉我。

提前谢谢你,
山姆。

编辑:内容/etc/sysconfig/iptables(由于删除并重新安装了 iptables,它基本上是空的)。

# Firewall configuration written by system-config-securitylevel
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
COMMIT

编辑2:

如果我运行/etc/init.d/iptables stop我会得到:

Flushing firewall rules: [  OK  ]
Setting chains to policy ACCEPT: security raw nat mangle filter [FAILED]
Unloading iptables modules: [  OK  ]

以及空规则:

$ iptables -L -n -v
Chain INPUT (policy ACCEPT 81 packets, 6575 bytes)
 pkts bytes target     prot opt in     out     source               destination         

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

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

但它仍然会阻止和过滤端口。

类似地,启动结果是:

/etc/init.d/iptables start
Flushing firewall rules: [  OK  ]
Setting chains to policy ACCEPT: security raw nat mangle filter [FAILED]
Unloading iptables modules: [  OK  ]
Applying iptables firewall rules: [  OK  ]

iptables -L -n -v
Chain INPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  207 54155 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
    0     0 REJECT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp flags:!0x17/0x02 reject-with tcp-reset 
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0           state INVALID 
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    4   228 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:25 
    2   116 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:110 
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:143 
    1    52 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:993 
    2   128 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:995 
    0     0 ACCEPT     tcp  --  *      *       -sanitized-          0.0.0.0/0           tcp dpt:22 
    0     0 ACCEPT     tcp  --  *      *       192.168.0.0/16       0.0.0.0/0           tcp dpt:22 
    0     0 DROP       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:22 
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           icmp type 8 code 0 
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain FORWARD (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
    0     0 REJECT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp flags:!0x17/0x02 reject-with tcp-reset 
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0           state INVALID 
    0     0 ACCEPT     all  --  lo     lo      0.0.0.0/0            0.0.0.0/0           
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain OUTPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  158 25662 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
    0     0 REJECT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp flags:!0x17/0x02 reject-with tcp-reset 
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0           state INVALID 
    0     0 ACCEPT     all  --  *      lo      0.0.0.0/0            0.0.0.0/0           
   33  2351 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0

但我仍然无法连接到 SSH 端口 22(否则我使用串行控制台)。

答案1

这个线程有一个对我有用的解决方案: http://forum.linode.com/viewtopic.php?t=6981&postdays=0&postorder=asc&start=0

解决方案是在 /etc/init.d/iptables 中添加以下几行

    echo -n $"${IPTABLES}: Setting chains to policy $policy: "
    ret=0
    for i in $tables; do
        echo -n "$i "
        case "$i" in
+           security)
+               $IPTABLES -t filter -P INPUT $policy \
+                   && $IPTABLES -t filter -P OUTPUT $policy \
+                   && $IPTABLES -t filter -P FORWARD $policy \
+                   || let ret+=1
+               ;;
            raw)
                $IPTABLES -t raw -P PREROUTING $policy \
                    && $IPTABLES -t raw -P OUTPUT $policy \
                    || let ret+=1
                ;;

答案2

您是否在 Linode (或其他 VPS) 上运行此程序?我之所以问这个问题,是因为在 Google 中搜索“将链设置为策略接受:安全原始 nat mangle 过滤器”时,我发现了以下参考:

Linode.com 论坛 :: 查看主题 - IPTables
http://www.linode.com/forums/archive/o_t/t_3930/iptables.html

在该讨论中,相关消息是用户“疑难解答”,其中还提到了 CentOS(我粗体在我看来解决方案部分的答复):

我问了同样的问题,但并没有得到真正的回答。以下是我从支持人员那里得到的答复:

“问题是“最新的 2.6 Paravirt”内核有一个“安全”链,而 iptables 不知道如何处理它。通常,切换到“最新 2.6 稳定版”内核即可解决问题,无需进一步调整 iptables init 脚本(它经常忽略该链并正常启动)。我们的构建团队确实意识到了这个问题,但是我不知道它是否会/何时会得到解决。继续使用我们的“最新 2.6 稳定版”内核是完全没问题的——直到最近,这个内核实际上是 CentOS 部署的默认选择。使用稳定内核时,除了 iptables 之外,没有任何应用程序的运行方式会有所不同。”

这有帮助吗?

答案3

您是否恰好在该服务器上运行 Webmin 或其他控制面板?

我在 CentOS 5.6 服务器上遇到了同样的问题,我iptables通过命令行执行的所有操作在重启后都会被刷新;只有我通过 Webmin 界面输入的规则才会保留。

答案4

在错误消息中,列出的每一项都是一个单独的 iptables 表。单独的表由内核模块

  • 模块iptable_raw在加载时会注册一个钩子,该钩子将在任何其他 Netfilter 钩子之前被调用。它提供了一个名为 raw 的表,可用于在数据包到达更耗内存的操作(例如连接跟踪)之前对其进行过滤。
  • iptable_mangle模块注册一个钩子和 mangle 表以在连接跟踪之后运行(但仍在任何其他表之前),以便可以对可能影响进一步规则(例如 NAT 或过滤)的数据包进行修改。
  • iptable_nat模块注册了两个钩子:基于 DNAT 的转换在过滤钩子之前应用,基于 SNAT 的转换在过滤钩子之后应用。提供给 iptables 的 nat 表仅仅是一个仅用于 NAT 映射的“配置数据库”,并不用于任何类型的过滤。
  • iptable_filter模块注册过滤表,用于通用过滤(防火墙)。

security表相对较新,您使用的内核可能尚未编译它。您可以通过运行来确认这一点iptables -t <table> -L;即:

adam@adamc: $ sudo 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         

adam@adamc: $ sudo iptables -t mangle -L
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
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         
Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         

真正的窍门是,我不知道如何告诉 CentOS 不要费心加载表security。在 init 脚本中四处查看可能会发现一个配置文件,其中列出了这些行;如果失败,您可以直接编辑 init 脚本以删除任何您未安装的表。

相关内容