大网数据广告活动
服务器运维问答

IPtables blocking SSH only if using conntrack

· 52CE 我爱测

Error As you can see, the only difference between the working and the not working configuration is -m conntrack --ctstate NEW,ESTABLISHED,RELATED . If I use -m conntrack --ctstate NEW,ESTABLISHED it does not work either... But Wh…

问题原文

原帖链接 · flexbrane · CC BY-SA 4.0

Error

As you can see, the only difference between the working and the not working configuration is -m conntrack --ctstate NEW,ESTABLISHED,RELATED. If I use -m conntrack --ctstate NEW,ESTABLISHED it does not work either... But Why?!

After few tests, I have noticed that every rules using conntrack does not work... Can you explain me why is SSH blocked by iptables only when I use conntrack?

Thanks :-)

Working configuration

#!/bin/bash

SERVER_IP="X.X.X.X"

iptables -F
iptables -X

# Setting default filter policy
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP

# Allow previous connections
iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT

# Allow unlimited traffic on loopback
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

# Allow incoming ssh only
iptables -A INPUT -p tcp -s 0/0 -d ${SERVER_IP} --dport 22 -j ACCEPT
iptables -A INPUT -p tcp -s 0/0 -d ${SERVER_IP} --dport 22 -j LOG --log-prefix "[ACCEPT INPUT][SSH] "


# make sure nothing comes or goes out of this box
iptables -A INPUT -j LOG --log-prefix "[DROP INPUT][DEFAULT] "
iptables -A INPUT -j DROP

Not working configuration

#!/bin/bash

SERVER_IP="X.X.X.X"

iptables -F
iptables -X

# Setting default filter policy
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP

# Allow previous connections
iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT

# Allow unlimited traffic on loopback
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

# Allow incoming ssh only
iptables -A INPUT -p tcp -s 0/0 -d ${SERVER_IP} --dport 22 -m conntrack --ctstate NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp -s 0/0 -d ${SERVER_IP} --dport 22 -m conntrack --ctstate NEW,ESTABLISHED,RELATED -j LOG --log-prefix "[ACCEPT INPUT][SSH] "


# make sure nothing comes or goes out of this box
iptables -A INPUT -j LOG --log-prefix "[DROP INPUT][DEFAULT] "
iptables -A INPUT -j DROP

[EDIT]

How am I sure that conntrack is involved?

Because in the log file I can see something like this :

[DROP][INPUT] IN=eth0 OUT= MAC=0c:c4:...:00 SRC=X.X.X.X DST=X.X.X.X LEN=67 TOS=0x08 PREC=0x20 TTL=41 ID=39550 DF PROTO=TCP SPT=51093 DPT=22 WINDOW=229 RES=0x00 ACK PSH URGP

[DROP][INPUT] is the last rule in my iptables configuration file.

已采纳解答

原帖链接 · flexbrane · CC BY-SA 4.0

This time I know how to make it work, without knowing why this is not working. Restarting iptables service make my script working: systemctl restart iptables.

Before working on this simple script, I was trying to implement another one, much more longer. It apply new rules in other CHAINS like PREROUTING and other customs. This script must have forced iptables to stop working with cstate or conntrack, maybe by filling a table to it maximum?

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-12-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。