Skip to content

Instantly share code, notes, and snippets.

@sinux-l5d
Forked from schnell18/create_chrootjail.sh
Last active September 22, 2020 10:09
Show Gist options
  • Save sinux-l5d/8a2899acd7acfbc910a321f942a67980 to your computer and use it in GitHub Desktop.
Save sinux-l5d/8a2899acd7acfbc910a321f942a67980 to your computer and use it in GitHub Desktop.
Script to automate the creation of chroot to run apache+php+mysql
#!/bin/bash
# script to automate the creation of chroot jail
# w/ minimal executables to run apache+php+mysql
CHROOT=/var/jail
DEB_CACHE=/tmp/jail_cache
if [ "$(whoami)" != "root" ]; then
echo "Script must be run as user: root"
exit 1
fi
function copy_binary() {
for i in $(ldd $* | grep -vE "dynamic|dynamique" | cut -d " " -f 3 | sed 's/://' | sort | uniq)
do
cp --parents $i $CHROOT
done
cp --parents /lib64/ld-linux-x86-64.so.2 $CHROOT
}
function install_package() {
mkdir $DEB_CACHE
cd $DEB_CACHE
for i in $*
do
apt-get download $i && apt-cache depends -i $i | awk '/Depends:/ {print $2}' | sed -E 's/<(.+):.+>/\1/' | xargs apt-get download
done
#dpkg --root=$CHROOT -i *.deb
echo ".deb to install in $DEB_CACHE"
}
# setup directory layout
mkdir $CHROOT
mkdir -p $CHROOT/{dev,etc,home,tmp,proc,root,var}
# setup device
mknod $CHROOT/dev/null c 1 3
mknod $CHROOT/dev/zero c 1 5
mknod $CHROOT/dev/tty c 5 0
mknod $CHROOT/dev/random c 1 8
mknod $CHROOT/dev/urandom c 1 9
chmod 0666 $CHROOT/dev/{null,tty,zero}
# copy programs and libraries
copy_binary /bin/{bash,ls,cp,rm,rmdir,cat,mkdir,ln,grep,cut,sed} /usr/bin/{vim,head,tail,which,id,find,xargs}
# install packages
install_package apache2
# copy vim resource files
cp -r --parents /usr/share/vim $CHROOT
# copy basic system level files
cp --parents /etc/group $CHROOT
cp --parents /etc/passwd $CHROOT
cp --parents /etc/shadow $CHROOT
cp --parents /etc/nsswitch.conf $CHROOT
cp --parents /etc/resolv.conf $CHROOT
cp --parents /etc/hosts $CHROOT
#cp --parents /lib/libnss_* $CHROOT
cp -r --parents /usr/share/terminfo $CHROOT
echo "chroot jail is created. type: chroot $CHROOT to access it"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment