2021-02-08 22:09:42 +08:00
|
|
|
#!/bin/bash
|
|
|
|
|
2021-04-30 00:34:04 +08:00
|
|
|
WORKSPACE=${WORKSPACE:-$HOME}
|
|
|
|
|
|
|
|
APPDIR="$PWD/squashfs-root"
|
2021-02-08 22:09:42 +08:00
|
|
|
INSTALL_DIR="$APPDIR/usr/src"
|
|
|
|
|
2021-04-30 00:34:04 +08:00
|
|
|
GIT=$WORKSPACE/git
|
2021-02-08 22:09:42 +08:00
|
|
|
|
2021-04-30 03:17:02 +08:00
|
|
|
DEFAULT_REPO_MONKEY_HOME=$GIT/monkey
|
2021-02-08 22:09:42 +08:00
|
|
|
|
|
|
|
ISLAND_PATH="$INSTALL_DIR/monkey_island"
|
|
|
|
MONGO_PATH="$ISLAND_PATH/bin/mongodb"
|
|
|
|
ISLAND_BINARIES_PATH="$ISLAND_PATH/cc/binaries"
|
|
|
|
|
2021-04-30 00:36:29 +08:00
|
|
|
MONKEY_ORIGIN_URL="https://github.com/guardicore/monkey.git"
|
|
|
|
CONFIG_URL="https://raw.githubusercontent.com/guardicore/monkey/develop/deployment_scripts/config"
|
2021-04-13 01:11:16 +08:00
|
|
|
NODE_SRC=https://deb.nodesource.com/setup_12.x
|
|
|
|
APP_TOOL_URL=https://github.com/AppImage/AppImageKit/releases/download/12/appimagetool-x86_64.AppImage
|
2021-04-21 19:32:35 +08:00
|
|
|
PYTHON_VERSION="3.7.10"
|
|
|
|
PYTHON_APPIMAGE_URL="https://github.com/niess/python-appimage/releases/download/python3.7/python${PYTHON_VERSION}-cp37-cp37m-manylinux1_x86_64.AppImage"
|
2021-04-13 01:11:16 +08:00
|
|
|
|
2021-05-03 21:28:40 +08:00
|
|
|
exit_if_missing_argument() {
|
|
|
|
if [ -z "$2" ] || [ "${2:0:1}" == "-" ]; then
|
|
|
|
echo "Error: Argument for $1 is missing" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
2021-04-30 01:48:19 +08:00
|
|
|
}
|
|
|
|
|
2021-04-30 03:39:17 +08:00
|
|
|
echo_help() {
|
|
|
|
echo "usage: build_appimage.sh [--help] [--agent-binary-dir <PATH>] [--branch <BRANCH>]"
|
|
|
|
echo " [--monkey-repo <PATH>] [--version <MONKEY_VERSION>]"
|
|
|
|
echo ""
|
|
|
|
echo "Creates an AppImage package for Infection Monkey."
|
|
|
|
echo ""
|
|
|
|
echo "--agent-binary-dir A directory containing the agent binaries that"
|
|
|
|
echo " you'd like to include with the AppImage. If this"
|
|
|
|
echo " parameter is unspecified, the latest release"
|
|
|
|
echo " binaries will be downloaded from GitHub."
|
|
|
|
echo ""
|
2021-04-30 19:36:18 +08:00
|
|
|
echo "--as-root Throw caution to the wind and allow this script"
|
|
|
|
echo " to be run as root."
|
|
|
|
echo ""
|
2021-04-30 03:39:17 +08:00
|
|
|
echo "--branch The git branch you'd like the AppImage to be"
|
|
|
|
echo " built from. (Default: develop)"
|
|
|
|
echo ""
|
|
|
|
echo "--monkey-repo A directory containing the Infection Monkey git"
|
|
|
|
echo " repository. If the directory is empty or does"
|
|
|
|
echo " not exist, a new repo will be cloned from GitHub."
|
|
|
|
echo " If the directory is already a valid GitHub repo,"
|
|
|
|
echo " it will be used as-is and the --branch parameter"
|
|
|
|
echo " will have no effect."
|
|
|
|
echo " (Default: $DEFAULT_REPO_MONKEY_HOME)"
|
|
|
|
echo ""
|
|
|
|
echo "--version A version number for the AppImage package."
|
|
|
|
echo " (Default: dev)"
|
|
|
|
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
2021-02-08 22:09:42 +08:00
|
|
|
is_root() {
|
2021-04-05 09:23:35 +08:00
|
|
|
return "$(id -u)"
|
2021-02-08 22:09:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
has_sudo() {
|
|
|
|
# 0 true, 1 false
|
2021-04-05 09:23:35 +08:00
|
|
|
sudo -nv > /dev/null 2>&1
|
|
|
|
return $?
|
2021-02-08 22:09:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
handle_error() {
|
|
|
|
echo "Fix the errors above and rerun the script"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
log_message() {
|
|
|
|
echo -e "\n\n"
|
2021-04-30 19:49:28 +08:00
|
|
|
echo -e "APPIMAGE BUILDER: $1"
|
2021-02-08 22:09:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
install_nodejs() {
|
|
|
|
log_message "Installing nodejs"
|
2021-04-02 00:25:06 +08:00
|
|
|
|
|
|
|
curl -sL $NODE_SRC | sudo -E bash -
|
2021-02-08 22:09:42 +08:00
|
|
|
sudo apt-get install -y nodejs
|
|
|
|
}
|
|
|
|
|
|
|
|
install_build_prereqs() {
|
2021-04-30 19:40:05 +08:00
|
|
|
sudo apt-get update
|
|
|
|
sudo apt-get upgrade -y
|
2021-04-02 00:25:06 +08:00
|
|
|
|
2021-04-30 00:54:49 +08:00
|
|
|
# monkey island prereqs
|
2021-04-30 19:40:05 +08:00
|
|
|
sudo apt-get install -y curl libcurl4 openssl git build-essential moreutils
|
2021-04-30 00:54:49 +08:00
|
|
|
install_nodejs
|
2021-02-08 22:09:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
install_appimage_tool() {
|
2021-04-30 20:18:55 +08:00
|
|
|
log_message "Installing appimagetool"
|
2021-04-30 00:54:49 +08:00
|
|
|
APP_TOOL_BIN=$WORKSPACE/bin/appimagetool
|
2021-04-02 00:25:06 +08:00
|
|
|
|
2021-04-30 00:54:49 +08:00
|
|
|
mkdir -p "$WORKSPACE"/bin
|
|
|
|
curl -L -o "$APP_TOOL_BIN" "$APP_TOOL_URL"
|
|
|
|
chmod u+x "$APP_TOOL_BIN"
|
2021-02-08 22:09:42 +08:00
|
|
|
|
2021-04-30 00:54:49 +08:00
|
|
|
PATH=$PATH:$WORKSPACE/bin
|
2021-02-08 22:09:42 +08:00
|
|
|
}
|
|
|
|
|
2021-04-30 03:17:02 +08:00
|
|
|
is_valid_git_repo() {
|
|
|
|
pushd "$1" 2>/dev/null || return 1
|
|
|
|
git status >/dev/null 2>&1
|
|
|
|
success="$?"
|
|
|
|
popd || exit 1
|
|
|
|
|
|
|
|
return $success
|
|
|
|
}
|
|
|
|
|
2021-02-08 22:09:42 +08:00
|
|
|
clone_monkey_repo() {
|
2021-04-30 03:17:02 +08:00
|
|
|
local repo_dir=$1
|
|
|
|
local branch=$2
|
|
|
|
|
|
|
|
if [[ ! -d "$repo_dir" ]]; then
|
|
|
|
mkdir -p "$repo_dir"
|
2021-02-08 22:09:42 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
log_message "Cloning files from git"
|
2021-04-30 03:17:02 +08:00
|
|
|
git clone --single-branch --recurse-submodules -b "$branch" "$MONKEY_ORIGIN_URL" "$repo_dir" 2>&1 || handle_error
|
2021-02-08 22:09:42 +08:00
|
|
|
}
|
|
|
|
|
2021-04-13 00:42:18 +08:00
|
|
|
setup_appdir() {
|
2021-04-30 03:17:02 +08:00
|
|
|
local agent_binary_dir=$1
|
|
|
|
local monkey_repo=$2
|
|
|
|
|
2021-04-30 00:54:49 +08:00
|
|
|
setup_python_37_appdir
|
2021-04-13 00:42:18 +08:00
|
|
|
|
2021-04-30 03:17:02 +08:00
|
|
|
copy_monkey_island_to_appdir "$monkey_repo"/monkey
|
|
|
|
add_agent_binaries_to_appdir "$agent_binary_dir"
|
2021-04-13 00:42:18 +08:00
|
|
|
|
2021-04-30 00:54:49 +08:00
|
|
|
install_monkey_island_python_dependencies
|
|
|
|
install_mongodb
|
2021-04-13 00:42:18 +08:00
|
|
|
|
2021-04-30 00:54:49 +08:00
|
|
|
generate_ssl_cert
|
|
|
|
build_frontend
|
2021-04-13 00:42:18 +08:00
|
|
|
|
2021-04-30 03:17:02 +08:00
|
|
|
add_monkey_icon "$monkey_repo"/monkey
|
2021-04-30 00:54:49 +08:00
|
|
|
add_desktop_file
|
|
|
|
add_apprun
|
2021-04-13 00:42:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
setup_python_37_appdir() {
|
2021-04-30 00:54:49 +08:00
|
|
|
PYTHON_APPIMAGE="python${PYTHON_VERSION}_x86_64.AppImage"
|
|
|
|
rm -rf "$APPDIR" || true
|
2021-04-16 00:20:33 +08:00
|
|
|
|
2021-04-30 00:54:49 +08:00
|
|
|
log_message "downloading Python3.7 Appimage"
|
|
|
|
curl -L -o "$PYTHON_APPIMAGE" "$PYTHON_APPIMAGE_URL"
|
2021-04-13 00:42:18 +08:00
|
|
|
|
2021-04-30 00:54:49 +08:00
|
|
|
chmod u+x "$PYTHON_APPIMAGE"
|
2021-04-13 00:42:18 +08:00
|
|
|
|
2021-04-30 00:54:49 +08:00
|
|
|
./"$PYTHON_APPIMAGE" --appimage-extract
|
|
|
|
rm "$PYTHON_APPIMAGE"
|
|
|
|
mkdir -p "$INSTALL_DIR"
|
2021-04-13 00:42:18 +08:00
|
|
|
}
|
|
|
|
|
2021-02-08 22:09:42 +08:00
|
|
|
copy_monkey_island_to_appdir() {
|
2021-04-30 03:17:02 +08:00
|
|
|
cp "$1"/__init__.py "$INSTALL_DIR"
|
|
|
|
cp "$1"/monkey_island.py "$INSTALL_DIR"
|
|
|
|
cp -r "$1"/common "$INSTALL_DIR/"
|
|
|
|
cp -r "$1"/monkey_island "$INSTALL_DIR/"
|
2021-04-05 09:23:35 +08:00
|
|
|
cp ./server_config.json.standard "$INSTALL_DIR"/monkey_island/cc/
|
2021-02-11 20:27:56 +08:00
|
|
|
|
|
|
|
# TODO: This is a workaround that may be able to be removed after PR #848 is
|
|
|
|
# merged. See monkey_island/cc/environment_singleton.py for more information.
|
2021-04-05 09:23:35 +08:00
|
|
|
cp ./server_config.json.standard "$INSTALL_DIR"/monkey_island/cc/server_config.json
|
2021-02-08 22:09:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
install_monkey_island_python_dependencies() {
|
|
|
|
log_message "Installing island requirements"
|
2021-02-18 04:40:33 +08:00
|
|
|
|
2021-04-16 22:28:00 +08:00
|
|
|
log_message "Installing pipenv"
|
|
|
|
"$APPDIR"/AppRun -m pip install pipenv || handle_error
|
|
|
|
|
2021-02-08 22:09:42 +08:00
|
|
|
requirements_island="$ISLAND_PATH/requirements.txt"
|
2021-04-30 22:12:24 +08:00
|
|
|
generate_requirements_from_pipenv_lock "$requirements_island"
|
2021-02-18 04:40:33 +08:00
|
|
|
|
2021-04-16 22:28:00 +08:00
|
|
|
log_message "Installing island python requirements"
|
2021-04-13 00:25:16 +08:00
|
|
|
"$APPDIR"/AppRun -m pip install -r "${requirements_island}" --ignore-installed || handle_error
|
2021-02-08 22:09:42 +08:00
|
|
|
}
|
|
|
|
|
2021-04-16 22:28:00 +08:00
|
|
|
generate_requirements_from_pipenv_lock () {
|
|
|
|
log_message "Generating a requirements.txt file with 'pipenv lock -r'"
|
2021-04-30 22:12:24 +08:00
|
|
|
cd "$ISLAND_PATH" || exit 1
|
2021-04-16 22:28:00 +08:00
|
|
|
"$APPDIR"/AppRun -m pipenv --python "$APPDIR/AppRun" lock -r > "$1" || handle_error
|
2021-04-30 22:12:24 +08:00
|
|
|
cd - || exit 1
|
2021-04-16 22:28:00 +08:00
|
|
|
}
|
|
|
|
|
2021-04-30 01:48:19 +08:00
|
|
|
add_agent_binaries_to_appdir() {
|
|
|
|
if [ -z "$1" ]; then
|
|
|
|
download_monkey_agent_binaries_to_appdir
|
|
|
|
else
|
2021-04-30 22:12:24 +08:00
|
|
|
copy_agent_binaries_to_appdir "$1"
|
2021-04-30 01:48:19 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
make_linux_binaries_executable
|
|
|
|
}
|
|
|
|
|
|
|
|
download_monkey_agent_binaries_to_appdir() {
|
2021-04-30 00:36:29 +08:00
|
|
|
log_message "Downloading monkey agent binaries to ${ISLAND_BINARIES_PATH}"
|
|
|
|
|
|
|
|
load_monkey_binary_config
|
|
|
|
|
2021-02-08 22:09:42 +08:00
|
|
|
mkdir -p "${ISLAND_BINARIES_PATH}" || handle_error
|
2021-04-05 09:23:35 +08:00
|
|
|
curl -L -o "${ISLAND_BINARIES_PATH}/${LINUX_32_BINARY_NAME}" "${LINUX_32_BINARY_URL}"
|
|
|
|
curl -L -o "${ISLAND_BINARIES_PATH}/${LINUX_64_BINARY_NAME}" "${LINUX_64_BINARY_URL}"
|
|
|
|
curl -L -o "${ISLAND_BINARIES_PATH}/${WINDOWS_32_BINARY_NAME}" "${WINDOWS_32_BINARY_URL}"
|
|
|
|
curl -L -o "${ISLAND_BINARIES_PATH}/${WINDOWS_64_BINARY_NAME}" "${WINDOWS_64_BINARY_URL}"
|
2021-04-30 01:48:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
copy_agent_binaries_to_appdir() {
|
|
|
|
cp "$1"/* "$ISLAND_BINARIES_PATH/"
|
|
|
|
}
|
2021-02-08 22:09:42 +08:00
|
|
|
|
2021-04-30 01:48:19 +08:00
|
|
|
make_linux_binaries_executable() {
|
|
|
|
chmod a+x "$ISLAND_BINARIES_PATH"/monkey-linux-*
|
2021-02-08 22:09:42 +08:00
|
|
|
}
|
|
|
|
|
2021-04-30 00:36:29 +08:00
|
|
|
load_monkey_binary_config() {
|
|
|
|
tmpfile=$(mktemp)
|
|
|
|
|
|
|
|
log_message "Downloading prebuilt binary configuration"
|
|
|
|
curl -L -s -o "$tmpfile" "$CONFIG_URL"
|
|
|
|
|
|
|
|
log_message "Loading configuration"
|
|
|
|
source "$tmpfile"
|
|
|
|
}
|
|
|
|
|
2021-02-08 22:09:42 +08:00
|
|
|
install_mongodb() {
|
|
|
|
log_message "Installing MongoDB"
|
|
|
|
|
2021-04-05 09:23:35 +08:00
|
|
|
mkdir -p "$MONGO_PATH"
|
|
|
|
"${ISLAND_PATH}"/linux/install_mongo.sh "${MONGO_PATH}" || handle_error
|
2021-02-08 22:09:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
generate_ssl_cert() {
|
|
|
|
log_message "Generating certificate"
|
|
|
|
|
|
|
|
chmod u+x "${ISLAND_PATH}"/linux/create_certificate.sh
|
2021-04-05 09:23:35 +08:00
|
|
|
"${ISLAND_PATH}"/linux/create_certificate.sh "${ISLAND_PATH}"/cc
|
2021-02-08 22:09:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
build_frontend() {
|
2021-04-21 08:18:22 +08:00
|
|
|
pushd "$ISLAND_PATH/cc/ui" || handle_error
|
2021-02-08 22:09:42 +08:00
|
|
|
|
2021-04-21 08:18:22 +08:00
|
|
|
log_message "Generating front end"
|
2021-04-30 01:58:30 +08:00
|
|
|
npm ci
|
2021-04-21 08:18:22 +08:00
|
|
|
npm run dist
|
2021-04-30 01:58:30 +08:00
|
|
|
|
2021-04-21 08:18:22 +08:00
|
|
|
popd || handle_error
|
|
|
|
|
|
|
|
remove_node_modules
|
|
|
|
}
|
|
|
|
|
|
|
|
remove_node_modules() {
|
|
|
|
# Node has served its purpose. We don't need to deliver the node modules with
|
|
|
|
# the AppImage.
|
|
|
|
rm -rf "$ISLAND_PATH"/cc/ui/node_modules
|
2021-02-08 22:09:42 +08:00
|
|
|
}
|
|
|
|
|
2021-04-13 00:42:18 +08:00
|
|
|
add_monkey_icon() {
|
2021-04-30 00:54:49 +08:00
|
|
|
unlink "$APPDIR"/python.png
|
|
|
|
mkdir -p "$APPDIR"/usr/share/icons
|
2021-04-30 03:17:02 +08:00
|
|
|
cp "$1"/monkey_island/cc/ui/src/images/monkey-icon.svg "$APPDIR"/usr/share/icons/infection-monkey.svg
|
2021-04-30 00:54:49 +08:00
|
|
|
ln -s "$APPDIR"/usr/share/icons/infection-monkey.svg "$APPDIR"/infection-monkey.svg
|
2021-04-13 00:42:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
add_desktop_file() {
|
2021-04-30 00:54:49 +08:00
|
|
|
unlink "$APPDIR/python${PYTHON_VERSION}.desktop"
|
|
|
|
cp ./infection-monkey.desktop "$APPDIR"/usr/share/applications
|
|
|
|
ln -s "$APPDIR"/usr/share/applications/infection-monkey.desktop "$APPDIR"/infection-monkey.desktop
|
2021-04-13 00:42:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
add_apprun() {
|
2021-04-30 00:54:49 +08:00
|
|
|
cp ./AppRun "$APPDIR"
|
2021-04-13 00:42:18 +08:00
|
|
|
}
|
|
|
|
|
2021-02-19 00:33:59 +08:00
|
|
|
build_appimage() {
|
2021-04-30 00:54:49 +08:00
|
|
|
log_message "Building AppImage"
|
|
|
|
ARCH="x86_64" appimagetool "$APPDIR"
|
2021-04-30 01:22:04 +08:00
|
|
|
apply_version_to_appimage "$1"
|
|
|
|
}
|
|
|
|
|
|
|
|
apply_version_to_appimage() {
|
2021-04-30 20:18:55 +08:00
|
|
|
log_message "Renaming Infection_Monkey-x86_64.AppImage -> Infection_Monkey-$1-x86_64.AppImage"
|
2021-04-30 01:22:04 +08:00
|
|
|
mv "Infection_Monkey-x86_64.AppImage" "Infection_Monkey-$1-x86_64.AppImage"
|
2021-02-19 00:33:59 +08:00
|
|
|
}
|
|
|
|
|
2021-05-03 21:28:40 +08:00
|
|
|
agent_binary_dir=""
|
2021-04-30 19:36:18 +08:00
|
|
|
as_root=false
|
2021-05-03 21:28:40 +08:00
|
|
|
branch="develop"
|
2021-04-30 03:17:02 +08:00
|
|
|
monkey_repo="$DEFAULT_REPO_MONKEY_HOME"
|
2021-04-30 01:22:04 +08:00
|
|
|
monkey_version="dev"
|
2021-05-03 21:28:40 +08:00
|
|
|
|
2021-04-30 01:22:04 +08:00
|
|
|
|
|
|
|
while (( "$#" )); do
|
2021-05-03 21:32:07 +08:00
|
|
|
case "$1" in
|
|
|
|
--agent-binary-dir)
|
|
|
|
exit_if_missing_argument "$1" "$2"
|
|
|
|
|
|
|
|
agent_binary_dir=$2
|
|
|
|
shift 2
|
|
|
|
;;
|
|
|
|
--as-root)
|
|
|
|
as_root=true
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--branch)
|
|
|
|
exit_if_missing_argument "$1" "$2"
|
|
|
|
|
|
|
|
branch=$2
|
|
|
|
shift 2
|
|
|
|
;;
|
|
|
|
-h|--help)
|
|
|
|
echo_help
|
|
|
|
;;
|
|
|
|
--monkey-repo)
|
|
|
|
exit_if_missing_argument "$1" "$2"
|
|
|
|
|
|
|
|
monkey_repo=$2
|
|
|
|
shift 2
|
|
|
|
;;
|
|
|
|
--version)
|
|
|
|
exit_if_missing_argument "$1" "$2"
|
|
|
|
|
|
|
|
monkey_version=$2
|
|
|
|
shift 2
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Error: Unsupported parameter $1" >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
2021-04-30 01:22:04 +08:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2021-04-30 19:49:28 +08:00
|
|
|
log_message "Building Monkey Island AppImage package."
|
|
|
|
|
2021-04-30 19:36:18 +08:00
|
|
|
if ! $as_root && is_root; then
|
2021-04-30 03:40:38 +08:00
|
|
|
log_message "Please don't run this script as root"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! has_sudo; then
|
|
|
|
log_message "You need root permissions for some of this script operations. \
|
|
|
|
Run \`sudo -v\`, enter your password, and then re-run this script."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-02-08 22:09:42 +08:00
|
|
|
|
|
|
|
install_build_prereqs
|
2021-04-13 00:25:16 +08:00
|
|
|
install_appimage_tool
|
|
|
|
|
2021-04-30 03:17:02 +08:00
|
|
|
if ! is_valid_git_repo "$monkey_repo"; then
|
|
|
|
clone_monkey_repo "$monkey_repo" "$branch"
|
|
|
|
fi
|
2021-02-08 22:09:42 +08:00
|
|
|
|
2021-04-30 03:17:02 +08:00
|
|
|
setup_appdir "$agent_binary_dir" "$monkey_repo"
|
2021-02-08 22:09:42 +08:00
|
|
|
|
2021-04-30 01:22:04 +08:00
|
|
|
build_appimage "$monkey_version"
|
2021-02-08 22:09:42 +08:00
|
|
|
|
2021-04-30 19:49:28 +08:00
|
|
|
log_message "AppImage build script finished."
|
2021-02-08 22:09:42 +08:00
|
|
|
exit 0
|