通过 IPv6 成功进行 SSH 登录,同时使用 IPv4 登录同一主机,结果显示“权限被拒绝”

通过 IPv6 成功进行 SSH 登录,同时使用 IPv4 登录同一主机,结果显示“权限被拒绝”

我目前被一个奇怪的问题难住了……我有一个双栈主机,我想通过 SSH 连接到它。如果我通过 IPv6 连接,一切都会按预期进行

datenwolf@foo ~/ > ssh -6 bar.example.com
Password:

datenwolf@bar ~/ >

然而,当通过 IPv4 执行相同操作时,它会失败

datenwolf@foo ~/ > ssh -4 bar.example.com
Password:
Permission denied (publickey,keyboard-interactive).

datenwolf@foo ~/ >

摘自/var/log/sshd登录失败的内容

Apr 24 16:34:03 [sshd] SSH: Server;Ltype: Version;Remote: www.xxx.yyy.zzz-38427;Protocol: 2.0;Client: OpenSSH_5.9p1 Debian-5ubuntu1
Apr 24 16:34:03 [sshd] SSH: Server;Ltype: Kex;Remote: www.xxx.yyy.zzz-38427;Enc: aes128-ctr;MAC: hmac-md5;Comp: none [preauth]
Apr 24 16:34:04 [sshd] SSH: Server;Ltype: Authname;Remote: www.xxx.yyy.zzz-38427;Name: wolfgangd [preauth]
Apr 24 16:34:07 [sshd] pam_access(sshd:account): access denied for user `datenwolf' from `foo.example.com'
Apr 24 16:34:07 [sshd] error: PAM: User account has expired for datenwolf from foo.example.com
Apr 24 16:34:07 [sshd] Connection closed by www.xxx.yyy.zzz [preauth]

当然,该帐户没有过期,我可以通过 IPv6 完美登录。使用 Google,我发现了有关日志消息的各种报告,但没有一个与我的问题相符(从某种意义上说,应用建议的解决方案不适用于我的案例)。

我在这里几乎没有想法。


更新

/var/log/sshd成功登录 IPv6在同一台目标机器上:

Apr 24 16:56:42 [sshd] SSH: Server;Ltype: Version;Remote: 2001:x:x:x:x:x:x:x-46025;Protocol: 2.0;Client: OpenSSH_5.9p1 Debian-5ubuntu1
Apr 24 16:56:42 [sshd] SSH: Server;Ltype: Kex;Remote: 2001:x:x:x:x:x:x:x-46025;Enc: aes128-ctr;MAC: hmac-md5;Comp: none [preauth]
Apr 24 16:56:43 [sshd] SSH: Server;Ltype: Authname;Remote: 2001:x:x:x:x:x:x:x-46025;Name: datenwolf [preauth]
Apr 24 16:56:47 [sshd] Accepted keyboard-interactive/pam for datenwolf from 2001:x:x:x:x:x:x:x port 46025 ssh2
Apr 24 16:56:47 [sshd] pam_unix(sshd:session): session opened for user datenwolf by (uid=0)

我尝试从不同的机器登录,结果都是一样的:IPv6 可以工作,IPv4 不行。


更新2

这是所使用的 IP 表以供参考。注意这些都是经过实战考验的,即它们已经使用了好几年并且最近没有改变。通过 IPv4 远程登录做过与他们一起工作。

IPv4 iptables:

Chain INPUT (policy ACCEPT 2144 packets, 336K bytes)
 pkts bytes target     prot opt in     out     source               destination         
  132 20762 fail2ban-SSH  tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22
  12M   14G ACCEPT     all  --  ppp0   *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
 3111 95984 ACCEPT     icmp --  ppp0   *       0.0.0.0/0            0.0.0.0/0           
18692 1123K ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22
    2   112 ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0            udp dpt:1194
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:1194
 4633  288K ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0            udp dpts:6880:6899
 2826  154K ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpts:6880:6899
    4   160 ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0            udp dpt:123
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:123
44165 3069K REJECT     all  --  ppp0   *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable

Chain FORWARD (policy ACCEPT 48032 packets, 44M bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0            udp dpt:631 reject-with icmp-port-unreachable
    0     0 REJECT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0            udp dpt:515 reject-with icmp-port-unreachable
    0     0 REJECT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:631 reject-with icmp-port-unreachable
    0     0 REJECT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:515 reject-with icmp-port-unreachable
    0     0 REJECT     all  --  ppp0   ppp0    0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable
 133K 8347K TCPMSS     tcp  --  *      ppp0    0.0.0.0/0            0.0.0.0/0            tcp flags:0x06/0x02 TCPMSS clamp to PMTU

Chain OUTPUT (policy ACCEPT 14378 packets, 2172K bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain fail2ban-SSH (1 references)
 pkts bytes target     prot opt in     out     source               destination         
  132 20762 RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0 

IPv6 ip6 表

Chain INPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       all      *      *       ::/0                 ::/0                 rt type:0 segsleft:0
 484K   86M ACCEPT     icmpv6   *      *       ::/0                 ::/0                
 105K 7943K ACCEPT     tcp      *      *       ::/0                 ::/0                 tcp dpt:22
    0     0 ACCEPT     udp      *      *       ::/0                 ::/0                 udp dpt:1194
    0     0 ACCEPT     tcp      *      *       ::/0                 ::/0                 tcp dpt:1194
    0     0 ACCEPT     udp      *      *       ::/0                 ::/0                 udp dpts:6880:6899
    0     0 ACCEPT     tcp      *      *       ::/0                 ::/0                 tcp dpts:6880:6899
    0     0 ACCEPT     tcp      *      *       ::/0                 ::/0                 tcp dpt:123
    0     0 ACCEPT     udp      *      *       ::/0                 ::/0                 udp dpt:123
    0     0 ACCEPT     all      ppp0,sixxs *       ::/0                 ::/0                 ctstate RELATED,ESTABLISHED
4164K  466M ACCEPT     all      !ppp0,sixxs *       ::/0                 ::/0                
    0     0 REJECT     all      *      *       ::/0                 ::/0                 reject-with icmp6-port-unreachable

Chain FORWARD (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       all      *      *       ::/0                 ::/0                 rt type:0 segsleft:0
 2864  311K ACCEPT     icmpv6   *      *       ::/0                 ::/0                
    0     0 REJECT     tcp      *      *       ::/0                 ::/0                 multiport ports 631 reject-with icmp6-port-unreachable
    0     0 REJECT     udp      *      *       ::/0                 ::/0                 multiport ports 631 reject-with icmp6-port-unreachable
    0     0 REJECT     tcp      *      *       ::/0                 ::/0                 multiport ports 515 reject-with icmp6-port-unreachable
    0     0 REJECT     udp      *      *       ::/0                 ::/0                 multiport ports 515 reject-with icmp6-port-unreachable
    0     0 REJECT     all      ppp0,sixxs ppp0,sixxs  ::/0                 ::/0                 reject-with icmp6-port-unreachable
    0     0 accept_with_pmtu_clamp  tcp      ppp0,sixxs *      !2001:x:x::/48   2001:x:x::/48   tcp dpt:22
  18M   14G accept_with_pmtu_clamp  all      *      *       ::/0                 ::/0                 ctstate RELATED,ESTABLISHED
65503 5289K accept_with_pmtu_clamp  all      !ppp0,sixxs *       ::/0                 ::/0                
    0     0 REJECT     all      *      *       ::/0                 ::/0                 reject-with icmp6-port-unreachable

Chain OUTPUT (policy ACCEPT 8099K packets, 11G bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       all      *      *       ::/0                 ::/0                 rt type:0 segsleft:0

Chain accept_with_pmtu_clamp (3 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 TCPMSS     tcp      *      ppp0,sixxs  ::/0                 ::/0                 tcp flags:0x06/0x02 TCPMSS clamp to PMTU
  18M   14G ACCEPT     all      *      *       ::/0                 ::/0 

更新3

这是/etc/sshd/sshd_config我尝试连接的系统,删除了所有注释:

Port 22
ListenAddress 0.0.0.0
ListenAddress ::

PubkeyAuthentication yes
PasswordAuthentication no
UsePAM yes

AllowAgentForwarding yes
AllowTcpForwarding yes
X11Forwarding yes
X11DisplayOffset 10
X11UseLocalhost yes
PrintMotd no
PrintLastLog no
UseDNS yes

Subsystem       sftp    /usr/lib64/misc/sftp-server

答案1

在事情变得越来越陌生之后(请参阅我的问题中的评论线索),我终于弄清楚了。首先要做的事情:身份验证过程做过pam_access.so 失败,但并不是由于/etc/security/access.conf建议的某些配置错误。

为了理解其中的原因,我们必须特别看看这个盒子的设置:它充当一个路由器,通向 IPv4(本机通过 PPP 链路)和 IPv6(通过 6in4 隧道)。这个盒子还充当 DNS 递归解析器,这里变得很有趣。我确实配置了解析器,使得 IPv4 反向查找从 IPv4 根服务器开始递归解析,IPv6 反向查找从 IPv6 根服务器开始递归解析。当我第一次安装它时,这个设置确实有效。

现在我的 ISP 输入图片,人们不明白 DNS 放大攻击是如何工作的。长话短说:我确信我的 ISP 会随机处理传入的 DNS 数据包,也就是说,有些事情现在必须通过自己的解析器来解决一段时间,而其他 DNS 地址则由您自己递归解析 - 官方原因是为了减轻 DNS 放大攻击,但他们这样做是错误的^1。

由于我不想大幅改变我的设置,我只是将 ISP 的 DNS 解析器放在本地 DNS 解析器的末尾,作为非递归转发,因此如果递归解析尝试超时,它会尝试我的 ISP 解析器。到目前为止,这很有效。但是当我配置它时,我犯了一个小错误:我输入了 ISP 的 DNS 解析器,使其仅从本地范围内的主机(即 192.168.0.0/16)工作,但忘记了 localhost,也就是我的路由器,这是我尝试通过 SSH 进入的主机,解析器将不是考虑 ISP 的解析器。

pam_access.so 尝试对对等地址进行反向查找;这就结束了循环:因为对于 IPv6 反向查找,将访问 DNS IPv6 根服务器,数据包将通过 6in4 隧道,而不会受到 ISP 的干扰,从而获得响应。但是 IPv4 反向查找不会由我自己通过 ISP 的解析器完成,这将不会收到任何响应,并且最终会报告 NXHOST(或者会超时运行)。无论如何,pam_access.so 不会看到它喜欢的东西,只会说“你不会通过”。

在我修复了该解析器配置后,现在一切都再次变得魅力十足。但现在我真的必须对我的 ISP 采取行动......

至于我是怎么解决的呢?好吧,通过猛烈地研究日志的冗长/var/log/everything来了解事情展开的顺序。当我看到我的解析器记录反向查找尝试时,我知道发生了什么。


1:从 DNS 放大缓解的角度来看,这完全是无稽之谈,因为我确实测试过,传出的 DNS 数据包可以很好地通过 - 然而这些是它们应该过滤的数据包。事实上,每个最终客户 ISP 都应该丢弃所有发件人地址与客户地址不匹配的 UDP 数据包

答案2

如果您遇到此类问题,服务器上 sshd 的配置是最有趣的事情之一。它通常在/etc/ssh/sshd_config.

您的配置文件中很可能有一个部分:

Match Address 10.*.*.*,192.168.0.*
    PasswordAuthentication no

其中有一些特定于这些子网的规则(Address您文件中的规则很可能会有所不同)。如果 IPv4 地址是唯一匹配的地址(而不是 IPv6),则这些规则仅适用于 IPv4 地址,并且匹配中的规则仅适用于匹配的 IP 地址。因此,根据您是否通过 IPv4 和 IPv6 连接,sshd 有不同的规则。

并不是所有的东西都可以在 a 中设置Match,但它们足以产生影响:

AllowAgentForwarding, AllowTcpForwarding, AuthorizedKeysFile,
AuthorizedPrincipalsFile, Banner, ChrootDirectory, ForceCommand,
GatewayPorts, GSSAPIAuthentication, HostbasedAuthentication,
HostbasedUsesNameFromPacketOnly, KbdInteractiveAuthentication,
KerberosAuthentication, MaxAuthTries, MaxSessions,
PasswordAuthentication, PermitEmptyPasswords, PermitOpen,
PermitRootLogin, PermitTunnel, PubkeyAuthentication,
RhostsRSAAuthentication, RSAAuthentication, X11DisplayOffset,
X11Forwarding, X11UseLocalHost

答案3

我遇到了同样的问题,对齐iptablesip6表,然后解决了这个问题。

相关内容