SFTP chroot

出自 Arch Linux 中文维基

OpenSSH 4.9+ 包含一個用於 SFTP 的內置 chroot,但需要對普通安裝進行一些調整。

安裝[編輯 | 編輯原始碼]

安裝 並配置 OpenSSH。運行後,確保 sftp-server 已正確設置:

/etc/ssh/sshd_config
Subsystem sftp /usr/lib/ssh/sftp-server

使用 sftpSSHFS 訪問文件。許多標準的FTP 客戶端 也可能有用。

配置[編輯 | 編輯原始碼]

設置文件系統[編輯 | 編輯原始碼]

注意:
  • 讀者可以自行選擇文件訪問方案。例如,選擇性地為一塊傳入(可寫)空間和/或一塊只讀空間創建一個子文件夾。這不需要在 /srv/ssh/jail之下直接完成———它可以在 live 綁定掛載的磁盤分區完成。
  • 也可以 chroot 到 /home 目錄從而跳過綁定,但是目標用戶家目錄應歸屬根:
# chown root:root /home/<username>
# chmod 0755 /home/<username>

綁定掛載要共享的 live 文件系統 到此目錄。此例使用 /mnt/data/share,由 用戶 root 擁有並具有 755 的八進制 權限

# chown root:root /mnt/data/share
# chmod 755 /mnt/data/share
# mkdir -p /srv/ssh/jail
# mount -o bind /mnt/data/share /srv/ssh/jail

將以下添加到 fstab 以使綁定掛載在重新啟動後不失效:

/mnt/data/share /srv/ssh/jail  none   bind   0   0

創建一個無特權用戶[編輯 | 編輯原始碼]

注意: 您無需創建一個組,可以使用 Match User 以代替 Match Group

創建 sftponly 用戶組:

# groupadd sftponly 

創建一個使用 sftponly 作為主要組的 用戶 並且 shell 登錄訪問被拒:

# useradd -g sftponly -s /usr/bin/nologin -d /srv/ssh/jail username

設置一個(複雜的)密碼以防止 account is locked 錯誤(即使使用密鑰驗證也可能出現):

# passwd username

配置 OpenSSH[編輯 | 編輯原始碼]

注意: 您可能想使用 Match User 而不是 Match Group,如上一步所述。
/etc/ssh/sshd_config
Subsystem sftp /usr/lib/ssh/sftp-server

Match Group sftponly
  ChrootDirectory %h
  ForceCommand internal-sftp
  AllowTcpForwarding no
  X11Forwarding no
  PasswordAuthentication no

重啟 sshd.service 以應用更改。

為 authorized_keys 修復路徑[編輯 | 編輯原始碼]

提示:(pre)auth錯誤的情況下,在客戶端和服務器上使用 OpenSSH 的 調試模式

應用AuthorizedKeysFile的標準路徑時,已 chroot 的用戶 SSH 密鑰 驗證會失敗。欲修復此問題, 附加 一個 AuthorizedKeysFile 上的屬於根的目錄到 /etc/openssh/sshd_config,以 /etc/ssh/authorized_keys 為例:

/etc/ssh/sshd_config
AuthorizedKeysFile /etc/ssh/authorized_keys/%u .ssh/authorized_keys
PermitRootLogin no
PasswordAuthentication no
PermitEmptyPasswords no
Subsystem sftp /usr/lib/ssh/sftp-server

創建 authorized_keys 文件夾,在客戶端生成一個 SSH 密鑰複製 服務器密鑰 /etc/ssh/authorized_keys(或任何其他首選方法)的內容並 設置正確的權限:

# mkdir /etc/ssh/authorized_keys
# chown root:root /etc/ssh/authorized_keys
# chmod 755 /etc/ssh/authorized_keys
# echo 'ssh-rsa <key> <username@host>' >> /etc/ssh/authorized_keys/username
# chmod 644 /etc/ssh/authorized_keys/username

重啟 sshd.service.

技巧提示[編輯 | 編輯原始碼]

寫權限[編輯 | 編輯原始碼]

綁定 需要路徑完全歸屬 root,但文件和/或子文件夾不需要。 以下示例中,用戶 www-demo/srv/ssh/www/demo 作為 jail 目錄:

# mkdir /srv/ssh/www/demo/public_html
# chown www-demo:sftponly /srv/ssh/www/demo/public_html
# chmod 755 /srv/ssh/www/demo/public_html

用戶現在應可在此目錄中創建文件/子目錄。欲詳細信息,請參閱 文件權限和屬性

僅允許上傳[編輯 | 編輯原始碼]

欲僅允許通過 sftp 上傳並禁止下載文件,修改 ForceCommand internal-sftp 行:


/etc/ssh/sshd_config
	
Match Group sftponly
	
  ForceCommand internal-sftp -u 0666 -p realpath,open,write,close,lstat
	

日誌[編輯 | 編輯原始碼]

用戶無法訪問 /dev/log。當用戶連入並嘗試下載該文件,即可通過於進程上運行 strace 以見此。

創建子目錄[編輯 | 編輯原始碼]

ChrootDirectory 中創建子目錄 dev,例如:

# mkdir /usr/local/chroot/user/dev
# chmod 755 /usr/local/chroot/user/dev

現在您應於 /usr/local/chroot/user/dev/log 創建被 openssh 接受的套接字。您可以直接將該套接字與 /dev/log/run/systemd/journal/dev-log(若您使用 journald)綁定或用 syslog-ng/rsyslog 創建。

綁定 journald[編輯 | 編輯原始碼]

# touch /usr/local/chroot/user/dev/log
# mount --bind /run/systemd/journal/dev-log /usr/local/chroot/user/dev/log

Syslog-ng 配置[編輯 | 編輯原始碼]

添加新源到 /etc/syslog-ng/syslog-ng.conf 日誌的並添加配置,例如修改段:

source src {
  unix-dgram("/dev/log");
  internal();
  file("/proc/kmsg");
};

成為:

source src {
  unix-dgram("/dev/log");
  internal();
  file("/proc/kmsg");
  unix-dgram("/usr/local/chroot/theuser/dev/log");
};

並附加:

#sftp configuration
destination sftp { file("/var/log/sftp.log"); };
filter f_sftp { program("internal-sftp"); };
log { source(src); filter(f_sftp); destination(sftp); };

(可選)如果您想將 SSH 消息類似地記錄到其自身的文件中:

#sshd configuration
destination ssh { file("/var/log/ssh.log"); };
filter f_ssh { program("sshd"); };
log { source(src); filter(f_ssh); destination(ssh); };

(來自 Syslog-ng#Move log to another file

OpenSSH 配置[編輯 | 編輯原始碼]

編輯 /etc/ssh/sshd_config 以替換所有 internal-sftp 的實例為 internal-sftp -f AUTH -l VERBOSE

重啟服務[編輯 | 編輯原始碼]

重啟 syslog-ngsshd 服務。

/usr/local/chroot/theuser/dev/log 現應存在。

SFTP 的替代品[編輯 | 編輯原始碼]

安全複製協議 (SCP)[編輯 | 編輯原始碼]

openssh 提供 scp 命令來傳輸文件。SCP 可能比使用 SFTP 更快 [1].

安裝 rsshAURscponly 作為替代 shell 解決方案。

只使用 SCP (Scponly)[編輯 | 編輯原始碼]

安裝 scponly.

對於現有用戶,只需將用戶的 shell 設置為 scponly:

# usermod -s /usr/bin/scponly username

參見 Scponly 維基 以獲得更多細節。

添加 chroot jail[編輯 | 編輯原始碼]

該軟件包附帶一個用於創建 chroot 的腳本。要使用它,請運行:

# /usr/share/doc/scponly/setup_chroot.sh
  • 提供答案。
  • 核實 /path/to/chroot 屬於 root:root 和有對於其他用戶的 r-x
  • 將所選用戶的 shell 更改為 /usr/bin/scponlyc
  • sftp 服務器 可能請求一些 libnss 模塊例如 libnss_files。將它們複製到 chroot 的 /lib 路徑。

參見[編輯 | 編輯原始碼]