Linux

使用 ssh 代理轉發和 libpam-ssh 無需密碼即可連接到伺服器上的特定使用者

  • February 26, 2020

我正在努力實現設置。

  • 我可以使用帶有 ssh 的 RSA 密鑰將我的伺服器連接到使用者 A。
  • 但我不知道如何轉發代理以與 libpam-ssh 一起使用。

目標是製作無密碼的 sudo 命令。

我在Debian(Buster)順便說一句。

ssh       su
A ------> B:user X ------> B:userY
   ^                ^
using A's        using A's
ssh key           ssh key

甚至可能嗎?

Feb 22 11:00:11 jeremydev sshd[4050]: Accepted publickey for juniko from 127.0.0.1 port 55484 ssh2: RSA SHA256:N1N2OMouB8WTpQtYEAt69Iar79D3vOdhIHo96kgbDbU
Feb 22 11:00:11 jeremydev sshd[4050]: pam_unix(sshd:session): session opened for user juniko by (uid=0)
Feb 22 11:00:11 jeremydev systemd-logind[513]: New session 100 of user juniko.
Feb 22 11:00:14 jeremydev sudo[4169]: pam_ssh_agent_auth: Beginning pam_ssh_agent_auth for user juniko
Feb 22 11:00:14 jeremydev sudo[4169]: pam_ssh_agent_auth: Attempting authentication: `juniko' as `juniko' using /etc/ssh/sudo_authorized_keys
Feb 22 11:00:14 jeremydev sudo[4169]: pam_ssh_agent_auth: Contacted ssh-agent of user juniko (1000)
Feb 22 11:00:14 jeremydev sudo[4169]: pam_ssh_agent_auth: Failed Authentication: `juniko' as `juniko' using /etc/ssh/sudo_authorized_keys

這是一個連接日誌範例:

OpenSSH_8.1p1 Ubuntu-5, OpenSSL 1.1.1d  10 Sep 2019
debug1: Reading configuration data /home/kwaadpepper/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: resolving "example.ovh" port 443
debug2: ssh_connect_direct
debug1: Connecting to example.ovh [XXX.XXX.XXX.XXX] port 443.
debug1: Connection established.
debug1: identity file .ssh/id_rsa type 0
debug1: identity file .ssh/id_rsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.1p1 Ubuntu-5
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.9p1
debug1: match: OpenSSH_7.9p1 pat OpenSSH* compat 0x04000000
debug2: fd 3 setting O_NONBLOCK
debug1: Authenticating to example.ovh:443 as 'juniko'
debug3: put_host_port: [example.ovh]:443
debug3: hostkeys_foreach: reading file "/home/kwaadpepper/.ssh/known_hosts"
debug3: record_hostkey: found key type ECDSA in file /home/kwaadpepper/.ssh/known_hosts:1
debug3: load_hostkeys: loaded 1 keys from [example.ovh]:443
debug3: order_hostkeyalgs: prefer hostkeyalgs: ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
debug3: send packet: type 20
debug1: SSH2_MSG_KEXINIT sent
debug3: receive packet: type 20
debug1: SSH2_MSG_KEXINIT received
debug2: local client KEXINIT proposal
debug2: KEX algorithms: curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,ext-info-c
debug2: host key algorithms: ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519-cert-v01@openssh.com,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ssh-ed25519,rsa-sha2-512,rsa-sha2-256,ssh-rsa
debug2: ciphers ctos: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
debug2: ciphers stoc: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
debug2: MACs ctos: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: MACs stoc: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: compression ctos: none,zlib@openssh.com,zlib
debug2: compression stoc: none,zlib@openssh.com,zlib
debug2: languages ctos: 
debug2: languages stoc: 
debug2: first_kex_follows 0 
debug2: reserved 0 
debug2: peer server KEXINIT proposal
debug2: KEX algorithms: curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1
debug2: host key algorithms: rsa-sha2-512,rsa-sha2-256,ssh-rsa,ecdsa-sha2-nistp256,ssh-ed25519
debug2: ciphers ctos: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
debug2: ciphers stoc: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
debug2: MACs ctos: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: MACs stoc: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: compression ctos: none,zlib@openssh.com
debug2: compression stoc: none,zlib@openssh.com
debug2: languages ctos: 
debug2: languages stoc: 
debug2: first_kex_follows 0 
debug2: reserved 0 
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug3: send packet: type 30
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug3: receive packet: type 31
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:qWW1mY2LmZVvhYmdR4Z0z6DBLZiS2JdmmxfYu4vUR/A
debug3: put_host_port: [XXX.XXX.XXX.XXX]:443
debug3: put_host_port: [example.ovh]:443
debug3: hostkeys_foreach: reading file "/home/kwaadpepper/.ssh/known_hosts"
debug3: record_hostkey: found key type ECDSA in file /home/kwaadpepper/.ssh/known_hosts:1
debug3: load_hostkeys: loaded 1 keys from [example.ovh]:443
debug3: hostkeys_foreach: reading file "/home/kwaadpepper/.ssh/known_hosts"
debug3: record_hostkey: found key type ECDSA in file /home/kwaadpepper/.ssh/known_hosts:2
debug3: load_hostkeys: loaded 1 keys from [XXX.XXX.XXX.XXX]:443
debug1: Host '[example.ovh]:443' is known and matches the ECDSA host key.
debug1: Found key in /home/kwaadpepper/.ssh/known_hosts:1
debug3: send packet: type 21
debug2: set_newkeys: mode 1
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug3: receive packet: type 21
debug1: SSH2_MSG_NEWKEYS received
debug2: set_newkeys: mode 0
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: .ssh/id_rsa RSA SHA256:N1N2OMouB8WTpQtYEAt69Iar79D3vOdhIHo96kgbDbU explicit agent
debug2: pubkey_prepare: done
debug3: send packet: type 5
debug3: receive packet: type 7
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521>
debug3: receive packet: type 6
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug3: send packet: type 50
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey
debug3: start over, passed a different list publickey
debug3: preferred gssapi-with-mic,publickey,keyboard-interactive,password
debug3: authmethod_lookup publickey
debug3: remaining preferred: keyboard-interactive,password
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering public key: .ssh/id_rsa RSA SHA256:N1N2OMouB8WTpQtYEAt69Iar79D3vOdhIHo96kgbDbU explicit agent
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 60
debug1: Server accepts key: .ssh/id_rsa RSA SHA256:N1N2OMouB8WTpQtYEAt69Iar79D3vOdhIHo96kgbDbU explicit agent
debug3: sign_and_send_pubkey: RSA SHA256:N1N2OMouB8WTpQtYEAt69Iar79D3vOdhIHo96kgbDbU
debug3: sign_and_send_pubkey: signing using rsa-sha2-512
debug3: send packet: type 50
debug3: receive packet: type 52
debug1: Authentication succeeded (publickey).
Authenticated to example.ovh ([XXX.XXX.XXX.XXX]:443).
debug1: channel 0: new [client-session]
debug3: ssh_session2_open: channel_new: 0
debug2: channel 0: send open
debug3: send packet: type 90
debug1: Requesting no-more-sessions@openssh.com
debug3: send packet: type 80
debug1: Entering interactive session.
debug1: pledge: network
debug3: receive packet: type 80
debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
debug3: receive packet: type 4
debug1: Remote: /home/debian/.ssh/authorized_keys:1: key options: agent-forwarding port-forwarding pty user-rc x11-forwarding
debug3: receive packet: type 4
debug1: Remote: /home/debian/.ssh/authorized_keys:1: key options: agent-forwarding port-forwarding pty user-rc x11-forwarding
debug3: receive packet: type 91
debug2: channel_input_open_confirmation: channel 0: callback start
debug1: Requesting authentication agent forwarding.
debug2: channel 0: request auth-agent-req@openssh.com confirm 0
debug3: send packet: type 98
debug2: fd 3 setting TCP_NODELAY
debug3: ssh_packet_set_tos: set IP_TOS 0x10
debug2: client_session2_setup: id 0
debug2: channel 0: request pty-req confirm 1
debug3: send packet: type 98
debug1: Sending environment.
debug3: Ignored env GJS_DEBUG_TOPICS
debug3: Ignored env SSH_AUTH_SOCK
debug3: Ignored env SESSION_MANAGER
debug3: Ignored env GNOME_TERMINAL_SCREEN
debug3: Ignored env SSH_AGENT_PID
debug3: Ignored env XDG_CURRENT_DESKTOP
debug1: Sending env LANG = fr_FR.UTF-8
debug2: channel 0: request env confirm 0
debug3: send packet: type 98
debug3: Ignored env XDG_SESSION_CLASS
debug3: Ignored env COLORTERM
debug3: Ignored env QT_IM_MODULE
debug3: Ignored env GPG_AGENT_INFO
debug3: Ignored env DESKTOP_SESSION
debug3: Ignored env USER
debug3: Ignored env XDG_MENU_PREFIX
debug3: Ignored env HOME
debug3: Ignored env GJS_DEBUG_OUTPUT
debug3: Ignored env QT4_IM_MODULE
debug3: Ignored env DBUS_SESSION_BUS_ADDRESS
debug3: Ignored env PWD
debug3: Ignored env GTK_MODULES
debug3: Ignored env XDG_CONFIG_DIRS
debug3: Ignored env WINDOWPATH
debug3: Ignored env _
debug3: Ignored env JOURNAL_STREAM
debug3: Ignored env GTK_IM_MODULE
debug3: Ignored env GNOME_DESKTOP_SESSION_ID
debug3: Ignored env MANAGERPID
debug3: Ignored env CLUTTER_IM_MODULE
debug3: Ignored env XDG_SESSION_DESKTOP
debug3: Ignored env LOGNAME
debug3: Ignored env GNOME_TERMINAL_SERVICE
debug3: Ignored env VTE_VERSION
debug3: Ignored env GNOME_SHELL_SESSION_MODE
debug3: Ignored env PATH
debug3: Ignored env XMODIFIERS
debug3: Ignored env SHELL
debug3: Ignored env XDG_RUNTIME_DIR
debug3: Ignored env XDG_SESSION_TYPE
debug3: Ignored env QT_ACCESSIBILITY
debug3: Ignored env XDG_DATA_DIRS
debug3: Ignored env USERNAME
debug3: Ignored env INVOCATION_ID
debug3: Ignored env SHLVL
debug3: Ignored env XAUTHORITY
debug3: Ignored env IM_CONFIG_CHECK_ENV
debug3: Ignored env DISPLAY
debug3: Ignored env TERM
debug3: Ignored env GDMSESSION
debug3: Ignored env IM_CONFIG_PHASE
debug3: Ignored env OLDPWD
debug3: Ignored env ZSH
debug3: Ignored env PAGER
debug3: Ignored env LESS
debug3: Ignored env LSCOLORS
debug3: Ignored env LS_COLORS
debug2: channel 0: request shell confirm 1
debug3: send packet: type 98
debug2: channel_input_open_confirmation: channel 0: callback done
debug2: channel 0: open confirm rwindow 0 rmax 32768
debug3: receive packet: type 99
debug2: channel_input_status_confirm: type 99 id 0
debug2: PTY allocation request accepted on channel 0
debug2: channel 0: rcvd adjust 2097152
debug3: receive packet: type 99
debug2: channel_input_status_confirm: type 99 id 0
debug2: shell request accepted on channel 0

您正在尋找ForwardAgent指令/etc/ssh/ssh_configor ~/.ssh/config

轉發代理

指定是否將與身份驗證代理的連接(如果有)轉發到遠端機器。參數必須是 yes 或 no(預設值)。

應謹慎啟用代理轉發。能夠繞過遠端主機上的文件權限(對於代理的 Unix 域套接字)的使用者可以通過轉發連接訪問本地代理。攻擊者無法從代理獲取密鑰材料,但是他們可以對密鑰執行操作,使他們能夠使用載入到代理中的身份進行身份驗證。

在客戶端上啟用它後,您可以通過以下方式檢查轉發功能是否正確:

ssh-add -L

並驗證它是否包含pam_ssh_agent_auth所需的密鑰。

編輯:雖然上述配置選項(或-A您在評論中提到的命令行選項)幾乎可以肯定會在伺服器上打開一個 Unix 套接字,該套接字轉發本地 ssh 代理(確保使用該選項執行ssh-v並檢查行錯誤)您的debug1: Requesting authentication agent forwarding.shell 初始化腳本或其他系統組件可能會用其自己的(空)代理替換轉發的代理。

要進一步調試問題,請SSH_AUTH_SOCK在登錄伺服器後檢查變數:

printenv SSH_AUTH_SOCK

轉發代理套接字的路徑形式為/tmp/ssh-XXXXXXXX/agent.<pid>,其中XXXXXXXX是一些隨機符號,並且<pid>是 shell 父程序的程序 ID($PPIDbash 中的值)。即使某些東西覆蓋了這個變數,你仍然可以找到路徑:

ls /tmp/ssh-*/agent.$PPID

並恢復它:

export SSH_AUTH_SOCK=/tmp/ssh-*/agent.$PPID

那會留下一個問題,誰在覆蓋你的SSH_AUTH_SOCK變數。如果我不得不猜測,我會搜尋bash初始化文件(在 Debian 10 /etc/profile/etc/profile.d/*.sh~/.bash_profile~/.bash_login~/.profile/etc/bash.bashrc~/.bashrc)或者systemd-logind是否啟動了ssh-agent.service

systemctl --user status ssh-agent.service

引用自:https://serverfault.com/questions/1004196