如何查看 AWS ELB 后面的 SSH 服务器 运行 的 SSH 客户端的真实 IP

How to see real IPs of SSH client of SSH servers running behind AWS ELB

我们在 AWS 经典 ELB 后面 运行 两个 gitlab 实例。为了通过 git 启用 git SSH 推送和负载平衡 SSH 请求,我们在 AWS ELB 中添加了 SSH TCP 端口侦听器。在 SSH 日志中,我们看到的是 ELB 的 IP,而不是 git 用户的实际 IP。我试图在 ELB 为 SSH 侦听器启用 Proxy protocol,但它破坏了 SSH 服务器。有什么办法可以看到客户端的真实IP吗?

Nov 16 08:38:41 gitlab-1-1b sshd[14760]: Bad protocol version identification 'PROXY TCP4 x.y.z.a 0.0.0.0 61533 22' from x.y.z.a port 9407
Nov 16 08:39:08 gitlab-1-1b sshd[14825]: Bad protocol version identification 'PROXY TCP4 x.y.z.a 0.0.0.0 61554 22' from x.y.z.a port 9417

mentioned here, an ELB (Elastic Load Balancing, which does support multiple listener protocols):

acts as a forwarding proxy (i.e. the source IP is not preserved)

因此您只剩下 ELB Access Logs (as mentioned there),因为 X-Forwarded-For 是一个 "application-level" 协议,它本身不适用于 ssh。

所以使用 ssh 并不容易。


通过 https 侦听器启用相同的 GitLab 服务至少可以让您通过代理 SSL 支持将该用户 IP 放入 X-Forwarded-For,如 discussed in this GitLab thread, or this one.
请注意 recent GitLab (8.10 or more) would then require

proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Ssl on;
proxy_set_header X-Forwarded-Port 443;

实际上可能的。解决方案是由 CloudFlare 开发的 mmproxy。它本地部署在每台后端机器上,工作在 SSH 服务器前面。当您在云负载平衡器中启用 Proxy protocol 并建立连接时,“mmproxy”会解析额外的 header 并在将连接转发到本地 SSH 服务器时欺骗源 IP。

我已经用 Google Cloud TCP proxy load balancer service, but using AWS ELB 测试过它应该以相同的方式工作。传入连接由“mmproxy”转发到“localhost”,其中 OpenSSH 正在侦听备用端口。

这是我针对 Google Cloud TCP proxy load balancer 服务和 OpenSSH 的测试配置:

# cat /etc/mmproxy-allowed-networks.txt
130.211.0.0/22
35.191.0.0/16

# grep Port /etc/ssh/sshd_config 
Port 222

./mmproxy -v --allowed-networks /etc/mmproxy-allowed-networks.txt -l 0.0.0.0:22 -4 127.0.0.1:222 -6 [::1]:222

此外,您必须部署自定义路由 table,这会强制将 return 流量路由到“环回”。对于“localhost”,这是每次机器启动时必须执行的四个“ip”命令。