#author("2023-11-05T03:39:32+00:00","default:sysosa","sysosa") #author("2023-11-05T03:40:22+00:00","default:sysosa","sysosa") [[sshでXを飛ばす>ssh/x]] [[ssh/PortForwardSSH]] [[SSHホストベース認証>ssh/ホストベース認証]] [[ssh/ssh-agent]] [[ssh/AllowUsers]] [[windowsServer/sshd]] [[windows/sshd]] [[ssh/reverse]] ***ポート番号が異なる別な口が欲しい [#hd7e453e] 1つのnicで複数のssh受信が可能なportを持たせてみる. 何に役立つか? 「22番ポートは[&color(orangered){PermitRootLogin no};]で[PasswordAuthentication no]として公開鍵アクセスのみ有効なセキュアで」 「2222番ポートのsshは[&color(orange){PermitRootLogin ''yes''};]で[PasswordAuthentication no][AllowUsers root][特定サイトからのみ]」 とかとか &ref(2023y03m06d_212945397.png,nolink,noborder); #code(nonumber){{ [root@rockylinux9 ~]# cat /etc/redhat-release Rocky Linux release 9.1 (Blue Onyx) [root@rockylinux9 ~]# getenforce Enforcing [root@rockylinux9 ~]# [root@rockylinux9 ~]# sed '/^#/d' /etc/ssh/sshd_config | sed '/^$/d' Port 22 PermitRootLogin no AuthorizedKeysFile .ssh/authorized_keys PasswordAuthentication no Subsystem sftp /usr/libexec/openssh/sftp-server X11Forwarding yes [root@rockylinux9 ~]# }} &size(10){「&color(magenta){/etc/ssh/sshd_config.d/};」の中身に注意. この中に「PermitRootLogin yes」が存在している}; これで #code(nonumber){{ [saber@c ~]$ ssh -l root rockylinux9 root@rockylinux9: Permission denied (publickey,keyboard-interactive). [saber@c ~]$ }} と拒否される. 公開鍵をroot@rockylinux9に登録してでも拒否されます. っでこれに穴をあける. 「port 2222」で穴をあけて、「Match &color(darkgreen){Address}; 192.168.0.3 &color(crimson){LocalPort}; 2222」で条件を組み込む #code(nonumber){{ (SELinux対応) [root@rockylinux9 ~]# semanage port --add --type ssh_port_t --proto tcp 2222 [root@rockylinux9 ~]# semanage port --list | grep ssh ssh_port_t tcp 2222, 22 [root@rockylinux9 ~]# (sshd_config設定) [root@rockylinux9 ~]# sed '/^#/d' /etc/ssh/sshd_config | sed '/^$/d' Port 22 Port 2222 PermitRootLogin no AuthorizedKeysFile .ssh/authorized_keys PasswordAuthentication no Subsystem sftp /usr/libexec/openssh/sftp-server X11Forwarding yes Match Address 192.168.0.3 LocalPort 2222 PermitRootLogin yes PasswordAuthentication no <--不要かな AllowUsers root [root@rockylinux9 ~]# systemctl restart sshd [root@rockylinux9 ~]# lsof -i:2222,22 -P COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME sshd 1829 root 3u IPv4 30875 0t0 TCP *:2222 (LISTEN) sshd 1829 root 4u IPv6 30884 0t0 TCP *:2222 (LISTEN) sshd 1829 root 5u IPv4 30886 0t0 TCP *:22 (LISTEN) sshd 1829 root 6u IPv6 30888 0t0 TCP *:22 (LISTEN) [root@rockylinux9 ~]# }} っで2222にアクセス #code(nonumber){{ [saber@c ~]$ ssh -l root rockylinux9 -p 2222 Last login: Mon Mar 6 22:11:56 2023 from 192.168.0.3 [root@rockylinux9 ~]# }} ***脆弱性 [#t8af2203] セキュリティー監査に引っかかった. 「使っている暗号」と「鍵交換のアルゴリズム」の取捨選択が必要みたい っで使われている暗号は「sshd -T」で判明する #code(nonumber){{ : ciphers chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,aes128-cbc,aes192-cbc,aes256-cbc,blowfish-cbc,cast128-cbc,3des-cbc : }} &size(10){「ssh -Q cipher」で表示される暗号は相手方へ繋がる際に使用する暗号でシステムが受ける暗号ではないです}; このうちblowfish-cbc、cast128-cbc、3des-cbcのCBC系は脆弱とのことで、それを除いて「/etc/sshd/sshd_config」の末尾に下記を追記する. そしてsshdを再起動させます #code(nonumber){{ Ciphers chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com }} っで確かめ。 この状態であえて「3des-cbc」を使って接続しようとすると下記のようにエラーで接続できません #code(nonumber){{ [saber@c ~]$ ssh -c 3des-cbc -l root centos7 Unable to negotiate with 192.168.0.107 port 22: no matching cipher found. Their offer: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com [saber@c ~]$ }} 鍵交換のアルゴリズムの変更 同じく「sshd -T」から使われている鍵交換アルゴリズムは下記になります #code(nonumber){{ kexalgorithms 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-group-exchange-sha1,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1 }} このうち「diffie-hellman-group1-sha1」が今後廃止されるそうな. これを除いて「/etc/ssh/sshd_config」の末尾に全て入れてみた. そしてsshdを再起動させます #code(nonumber){{ kexalgorithms 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-group-exchange-sha1,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1 }} これで対策は一応完了かな. ***アクセス制御 [#oe9b3ed6] /etc/ssh/sshd_configの AllowUsers/AllowGroups で一応の制御はできるが、 それ以外の方法として 「/etc/security/access.conf」 による制御について PAMを使うのだが、「/etc/pam.d/sshd」の末尾に「account required pam_access.so」を加える そして「/etc/security/access.conf」を設定する 特定のグループのみを許可するなら「-:ALL EXCEPT root <グループA> <グループB> <グループC> :ALL」とか ***踏み台PCにトンネルを掘る [#v067ed2d] #code(nonumber){{ ssh -L 33890:10.10.10.1:3389 user@192.168.0.200 }} ***/homeを共有していて authorized_keys もあるのにログインできない [#ef1e4f25] ssh-keygenでパスフレーズなしの鍵を作り、公開鍵の中身を authorized_keys に書き込んだ. でも他へsshするとpasswordが要求される... .sshのフォルダは 700 パーミッション .ssh/authorized_keys ファイルは 600 パーミッション で問題があるなら 原因は SELinux. 解消方法は接続先にてrootで「setsebool -P use_nfs_home_dirs 1」を実行する #code(nonumber){{ setsebool -P use_nfs_home_dirs 1 (確認方法) getsebool -a |grep use_nfs_home_dirs }} これで行けるかと. 原因解明にsshdのログをdebugレベルまで上げたら下記文言が見えた &size(10){sshd_configに「LogLevel debug」を入れてsshdを再起動}; #code(nonumber){{ Apr 18 15:15:58 n1 sshd[1968]: debug1: Could not open authorized keys '/home/illya/.ssh/authorized_keys': Permission denied }} ***公開鍵を他サーバーへ登録 [#m705290a] 他サーバーへの authorized_keys へ公開鍵を追記するのは結構面倒. ファイルを転送して追記と. っで簡単にする方法があって「&color(magenta){ssh-copy-id};」コマンド #code(nonumber){{ [illya@c ~]$ ssh-copy-id root@ms /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/illya/.ssh/id_dsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@ms's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@ms'" and check to make sure that only the key(s) you wanted were added. [illya@c ~]$ }} この場合「/home/illya/.ssh/id_dsa.pub」が送られます. もし .ssh/id_rsa.pub を送りたければ #code(nonumber){{ [illya@c ~]$ ssh-copy-id -i .ssh/id_rsa root@ms }} とします。 これは「openssh-clients」パッケージに同封されている ***鍵作成 [#p5746e39] 大抵は&color(green){ssh-keygen -t dsa};でパスフレーズを入力して鍵が得られます。 &color(red){パスフレーズを別に入れなくて、リターンキーで返しても鍵は作れます。};下記は、ファイル名を指定して鍵を作る場合です。 #code(nonumber){{ local $ ssh-keygen -t dsa -b 1024 -f id_remote <----- 鍵のファイル名を id_remote としてます。 enerating public/private dsa key pair. Enter passphrase (empty for no passphrase): <----- パスフレーズ入力。パスフレーズなしなら、リターンキーを押す Enter same passphrase again: <----- パスフレーズを再入力。パスフレーズなしなら、リターンキーを押す Your identification has been saved in id_remote. Your public key has been saved in id_remote.pub. The key fingerprint is: ce:d--------------------76:b5 foo@local The key's randomart image is: +--[ DSA 1024]----+ | .=+o=++. ..| | +o ++.. .| | .. =oo. E | | = + | | S . | | + | | o | | | | | +-----------------+ # }} っで&color(green){-f};を指定したことで、実行した場所に -秘密鍵 id_remote -公開鍵 id_remote.pub の2つ一組の鍵が作られます。 もし&color(green){-f};の指定がないと、使う暗号方式によるが -秘密鍵 $HOME/.ssh/id_dsa -公開鍵 $HOME/.ssh/id_dsa.pub と既定の場所、ファイル名となる ***つかう [#u51dda9a] 公開鍵(id_remote.pub、既定ではid_dsa.pub)をsshで接続したいサーバに送ります。ftpとか、usbメモリーとかで。 #code(nonumber){{ local $ ftp remote ftp> pub id_remote.pub ftp> bye }} そして、相手先でsshで接続したいアカウントにこの公開鍵を含んだファイル(authorized_keys)を作ります。authorized_keysファイルには複数の公開鍵が含ませても構いません。 #code(nonumber){{ remote $ remote $ mkdir .ssh; chmod 700 .ssh remote $ cat id_remote.pub >> .ssh/authorized_keys *このファイル名は接続先の/etc/ssh/sshd_configのAuthorizedKeysFileで決まる remote $ chmod 600 .ssh/authorized_keys remote $ rm id_remote.pub }} これでパスフレーズを使ってのssh接続の準備は整いました。パスフレーズを持たない公開鍵であっても同様にauthorized_keysファイルを作ります。 接続する相手方(remote)の/etc/ssh/sshd_configの内容が、 #code(nonumber){{ PasswordAuthentication yes }} なら、明示的に秘密鍵ファイルを指定すると、パスフレーズを問われ、 (割符が合わなければ)次に接続先のユーザの/etc/passwdを問われます。 #code(nonumber){{ local $ ssh -i ~/.ssh/id_remote -l foo remote <--- (-i)で手元に残った秘密鍵のファイルを指定します。 Enter passphrase for key '/home/foo/.ssh/id_remote ': <--- パスフレーズ入力 foo@remote's password: <--- /etc/passwdのパスワード remote $ }} パスフレーズのみを使うのであれば、接続先(remote)の/etc/ssh/sshd_configは #code(nonumber){{ PasswordAuthentication no }} とする。 ちなみに接続先(remote)の/etc/ssh/sshd_configで #code(nonumber){{ PermitEmptyPasswords no }} は、/etc/passwdでのパスワードがNULL禁止であって、パスフレーズがない公開鍵・秘密鍵には関わっていないようだ ***めも [#edfec3b8] どうしても公開鍵でしっぱいするなら恐らくSELinuxでしくじっているかも