#!/bin/bash
######################################################################
######################################################################
## Author: Adam Michael Danischewski
## GitHub: https://github.com/AdamDanischewski/chrootjailer
## Created Date: 2019-10-05
## Name: chrootjailer.bsh
## Version: v0.01
## Last Modified: 2019-10-05
## Issues: If you find any issues emai1 me at <my first name> (dot)
## <my last name> (at) gmail (dot) com.
##
## This script creates a quick and simple chroot, for working commands
## it looks for libraries via ldd. If libraries is all your commands
## need it should work. If you need additional files for a particular
## command to work you need to manually copy the files to their
## requisite locations.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
##
## Released under Creative Commons License: CC BY-SA 4.0
## https://creativecommons.org/licenses/by-sa/4.0/
######################################################################
######################################################################
declare DEFAULT_CHROOT="/tmp/myjail"
declare DEFAULT_TERM=vt100
declare DEFAULT_CMDS="bash ls cat vi rm mkdir date ldd"
CMD_CP=$(type -P cp);declare -r CMD_CP
CMD_MKDIR=$(type -P mkdir);declare -r CMD_MKDIR
CMD_SUDO=$(type -P sudo);declare -r CMD_SUDO
CMD_GREP=$(type -P grep);declare -r CMD_GREP
CMD_AWK=$(type -P awk);declare -r CMD_AWK
CMD_LDD=$(type -P ldd);declare -r CMD_LDD
CMD_CHROOT=$(type -P chroot);declare -r CMD_CHROOT
declare CHROOTJAILER_LOGO=""
function init() {
IFS= read -r -d '' CHROOTJAILER_LOGO <<'EOF'
_ _ _ _ _
___| |__ _ __ ___ ___ | |_ (_) __ _(_) | ___ _ __
/ __| '_ \| '__/ _ \ / _ \| __| | |/ _` | | |/ _ \ '__|
| (__| | | | | | (_) | (_) | |_ | | (_| | | | __/ |
\___|_| |_|_| \___/ \___/ \__| _/ |\__,_|_|_|\___|_|
|__/
EOF
}
function print_logo() {
clear
echo -e "\e[38;05;226m${CHROOTJAILER_LOGO}\e[0m"
}
function get_directory() {
echo -en "\e[38;05;82m"
read -p "Enter the Chroot directory (default is ${DEFAULT_CHROOT}): " DEFAULT_CHROOT
echo -en "\e[0m"
DEFAULT_CHROOT="$(readlink -f "${DEFAULT_CHROOT}")"
}
function get_cmds() {
echo -ne "\e[38;05;82m"
read -p 'Add additional commands space separated (E.g. cmd1 cmd2): ' ADDITIONAL_COMMANDS
echo -ne "\e[0m"
}
function get_term() {
local NEWTERM=""
echo -ne "\e[38;05;82m"
read -p 'Enter new TERM: ' NEWTERM
echo -ne "\e[0m"
DEFAULT_TERM=${NEWTERM:-${DEFAULT_TERM}}
}
function print_config() {
printf "\e[38;05;226m%s\n" "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
printf "%s\n" "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
printf "++\e[0m \e[38;05;82m chroot jailer- configuration\e[0m \e[38;05;226m++\n"
printf "%s\n" "++ ------------------------------------------------- ++"
printf "++ 1)%1s %s\n" "Chroot Location:" "${DEFAULT_CHROOT}"
printf "++ 2)%1s %s\n" "Default TERM:" "${DEFAULT_TERM}"
printf "++ 3)%1s %s\n" "Additional Commands:" "${ADDITIONAL_COMMANDS:- <none>}"
printf "%s\n" "++ Default Commands: ${DEFAULT_CMDS}"
printf "%s\n" "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
printf "%s\n\e[0m" "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
}
function user_config() {
while :; do
print_config
echo -ne "\e[38;05;82m"
read -p 'Create chroot? Enter y for yes or [1-3] to change a setting: ' PROCEED
echo -ne "\e[0m"
case "${PROCEED}" in
1)
get_directory
;;
2)
get_term
;;
3)
get_cmds
;;
y)
echo "Continuing... "
break;
;;
*)
echo -en "\e[38;05;196m***Error: \"${PROCEED}\" is not an option.\e[0m"
sleep 1;
esac
print_logo
done
create_chroot
}
function create_chroot() {
echo "Creating Chroot Jail: ${DEFAULT_CHROOT}"
echo "Creating directory structure..."
echo -ne "\e[38;05;107m"
"${CMD_MKDIR}" -vp "${DEFAULT_CHROOT%/}/dev/"
"${CMD_MKDIR}" -vp "${DEFAULT_CHROOT%/}/bin/"
"${CMD_MKDIR}" -vp "${DEFAULT_CHROOT%/}/lib64/"
"${CMD_MKDIR}" -vp "${DEFAULT_CHROOT%/}/lib/"
"${CMD_MKDIR}" -vp "${DEFAULT_CHROOT%/}/usr/share/terminfo/${DEFAULT_TERM:0:1}/"
echo -ne "\e[0m"
echo "Copying ${DEFAULT_TERM} terminfo..."
echo -ne "\e[38;05;107m"
"${CMD_SUDO}" "${CMD_CP}" -v "/usr/share/terminfo/${DEFAULT_TERM:0:1}/${DEFAULT_TERM}" "${DEFAULT_CHROOT%/}/usr/share/terminfo/${DEFAULT_TERM:0:1}/"
echo -ne "\e[0m"
echo "Creating devices..."
echo -ne "\e[38;05;107m"
"${CMD_SUDO}" mknod -m 666 "${DEFAULT_CHROOT%/}/dev/null" c 1 3
"${CMD_SUDO}" mknod -m 666 "${DEFAULT_CHROOT%/}/dev/tty" c 5 0
"${CMD_SUDO}" mknod -m 666 "${DEFAULT_CHROOT%/}/dev/zero" c 1 5
"${CMD_SUDO}" mknod -m 666 "${DEFAULT_CHROOT%/}/dev/random" c 1 8
"${CMD_SUDO}" chown root:root "${DEFAULT_CHROOT}"
"${CMD_SUDO}" chmod 0755 ${DEFAULT_CHROOT}
echo -ne "\e[0m"
echo "Adding commands..."
for a in ${DEFAULT_CMDS} ${ADDITIONAL_COMMANDS}; do
echo "Processing command: $a"
echo -ne "\e[38;05;107m"
"${CMD_CP}" -v "$(type -P "${a}")" "${DEFAULT_CHROOT%/}/bin/"
while read -r b;do
[[ -z ${b} ]] && continue
if "${CMD_GREP}" -q "lib64"<<<"${b}"; then
"${CMD_CP}" -v "${b}" "${DEFAULT_CHROOT%/}/lib64/"
else
"${CMD_CP}" -v "${b}" "${DEFAULT_CHROOT%/}/lib/"
fi
done < <("${CMD_AWK}" '{if($1~/\/lib/){print $1}else if($3~/\/lib/){print $3}}' <("${CMD_LDD}" $(type -P "${a}")))
echo -ne "\e[0m"
done
}
function main() {
init
print_logo
user_config
echo -e "\e[38;05;190mEnter chroot with the following command: TERM=${DEFAULT_TERM} \"${CMD_SUDO}\" ${CMD_CHROOT} \"${DEFAULT_CHROOT}\"\e[0m"
echo -ne "\e[38;05;82m"
read -p 'Entering chroot... Press any key to continue or Ctl-C to quit..' GO
echo -ne "\e[0m"
cd "${DEFAULT_CHROOT%/}/"
TERM=${DEFAULT_TERM} "${CMD_SUDO}" "${CMD_CHROOT}" .
}
main
exit 0
Get the latest on GitHub.
No comments:
Post a Comment