LoginSignup
4

More than 5 years have passed since last update.

posted at

chroot環境の作成

下記条件でjail環境を作成しました
  • chrootを設定し自分のホームディレクトリ以上には行けない
  • 実行できるコマンドは制限

作成手順

sshd_configの編集

/etc/ssh/sshd_config
#Subsystem       sftp    /usr/libexec/openssh/sftp-server
Subsystem       sftp    internal-sftp
# For chroot
Match group chroot
    PasswordAuthentication yes
    AllowAgentForwarding no
    AllowTcpForwarding no
    ChrootDirectory /home/%u

chrootグループの作成

groupadd -g 790 chroot

制限ユーザーの作成

useradd guest
usermod -aG chroot guest

ユーザーディレクトリの設定

export CHROOTDIR=/home/guest
chmod 755 ${CHROOTDIR}
chown root. ${CHROOTDIR}
mkdir -p ${CHROOTDIR}/${CHROOTDIR}

システム系ディレクトリの設定

mkdir ${CHROOTDIR}/{etc,dev,proc,tmp,lib,lib64,bin,sbin}
chmod 755 ${CHROOTDIR}/{etc,dev,proc,lib,lib64,bin,sbin} 
mkdir -p ${CHROOTDIR}/usr/{lib,lib64,libexec,bin,sbin}
chmod 755 ${CHROOTDIR}/usr/{lib,lib64,libexec,bin,sbin}
mkdir -p ${CHROOTDIR}/usr/local/{lib,lib64,libexec,bin,sbin}
chmod 755 ${CHROOTDIR}/usr/local/{lib,lib64,libexec,bin,sbin}
mkdir -p ${CHROOTDIR}/usr/libexec/openssh/
chmod 755 ${CHROOTDIR}/usr/libexec/openssh/
mkdir -p ${CHROOTDIR}/usr/share/
chmod 755 ${CHROOTDIR}/usr/share/

システムディレクトリのマウント

ライブラリは1つ1つコピーするのは大変なのでディレクリをマウントさせる
DIRS="dev proc tmp lib lib64 usr/lib usr/lib64 usr/local/lib usr/local/lib64 usr/share etc/pam.d"
for DIR in $DIRS; do
    mount --bind /${DIR} ${CHROOTDIR}/${DIR}
done

GROUPSコマンド作成

${CHROOTDIR}/usr/bin/groups
#!/bin/bash
id -Gn

必要コマンドのコピー

コマンドは必要なものだけ限定してコピーする
COMMANDS="/usr/bin/tail /bin/sh /bin/date /bin/bash /bin/cat /bin/cp /bin/chmod /bin/ls /bin/more /bin/rmdir \
/bin/mkdir /bin/mv /bin/touch /bin/pwd /bin/rm /bin/vi /bin/env /bin/grep /usr/bin/whoami \
/usr/bin/id /usr/bin/scp /usr/bin/sftp /usr/bin/dirname /usr/bin/curl \
/usr/libexec/openssh/sftp-server /bin/ps /bin/netstat"

for COMMAND in $COMMANDS; do
    cp ${COMMAND} ${CHROOTDIR}/${COMMAND}
done
etcファイルのコピー
cp /etc/{ld.so.cache,ld.so.conf,localtime,resolv.conf} ${CHROOTDIR}/etc/
grep /etc/passwd -e "^root" -e "^guest" >> ${CHROOTDIR}/etc/passwd
grep /etc/group -e "^root" -e "^guest" >> ${CHROOTDIR}/etc/group
grep /etc/shadow -e "^root" -e "^guest" >> ${CHROOTDIR}/etc/shadow

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
4