diff --git a/.gitignore b/.gitignore index 1ed1207..7d065ae 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.img +*.img.bmap *.pcap __pycache__ _backups diff --git a/.travis.yml b/.travis.yml index b173709..4928b9e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,11 +13,11 @@ cache: - tmp/ before_script: - sudo apt-get -y update -- sudo apt-get -y install qemu-user-static binfmt-support qemu +- sudo apt-get -y install qemu-user-static binfmt-support qemu bmap-tools - sudo update-binfmts --display - unset GOROOT script: -- sudo ./scripts/create_sibling.sh -n pwnagotchi -o pwnagotchi.img -s 4 +- sudo ./scripts/create_sibling.sh -n pwnagotchi -o pwnagotchi.img -s 8 - zip -s 2g pwnagotchi.zip pwnagotchi.img # TODO: deploy! diff --git a/scripts/create_sibling.sh b/scripts/create_sibling.sh index 2ec4bdf..1cb446d 100755 --- a/scripts/create_sibling.sh +++ b/scripts/create_sibling.sh @@ -5,14 +5,17 @@ set -eu REQUIREMENTS=( wget gunzip git dd e2fsck resize2fs parted losetup qemu-system-x86_64 ) +DEBREQUIREMENTS=( wget gzip git parted qemu-system-x86 qemu-user-static bmap-tools ) REPO_DIR="$(dirname "$(dirname "$(realpath "$0")")")" TMP_DIR="${REPO_DIR}/tmp" MNT_DIR="${TMP_DIR}/mnt" +THIS_DIR=$(pwd) PWNI_NAME="pwnagotchi" PWNI_OUTPUT="pwnagotchi.img" PWNI_SIZE="4" +OPT_SPARSE=0 OPT_PROVISION_ONLY=0 OPT_CHECK_DEPS_ONLY=0 OPT_IMAGE_PROVIDED=0 @@ -27,6 +30,18 @@ if [[ "$EUID" -ne 0 ]]; then fi function check_dependencies() { + if [ -f /etc/debian_version ]; + then + echo "[+] Checking Debian dependencies" + + for REQ in "${DEBREQUIREMENTS[@]}"; do + if ! dpkg -s "$REQ" >/dev/null 2>&1; then + echo "Dependency check failed for ${REQ}; use 'apt-get install ${REQ}' to install" + exit 1 + fi + done + fi + echo "[+] Checking dependencies" for REQ in "${REQUIREMENTS[@]}"; do if ! type "$REQ" >/dev/null 2>&1; then @@ -77,8 +92,31 @@ function provide_raspbian() { } function setup_raspbian(){ - echo "[+] Resize image" - dd if=/dev/zero bs=1G count="$PWNI_SIZE" >> "${TMP_DIR}/raspbian.img" + # Detect the ability to create sparse files + if [ "${OPT_SPARSE}" -eq 0 ]; + then + which bmaptool >/dev/null 2>&1 + if [ $? -eq 0 ]; + then + echo "[+] Defaulting to sparse image generation as bmaptool is available" + OPT_SPARSE=1 + else + echo "[!] bmaptool not available, not creating a sparse image" + fi + fi + + # Note that we 'extend' the raspbian.img + if [ "${OPT_SPARSE}" -eq 1 ]; + then + # Resize sparse (so that we can use bmaptool later) + echo "[+] Resizing sparse image of ${PWNI_SIZE}GB (1000s)" + truncate -s ${PWNI_SIZE}GB "${TMP_DIR}/raspbian.img" + else + echo "[+] Resizing full image to ${PWNI_SIZE}G" + # Full disk-space using image (appends to raspbian image) + dd if=/dev/zero bs=1G count="${PWNI_SIZE}" >> "${TMP_DIR}/raspbian.img" + fi + echo "[+] Setup loop device" mkdir -p "${MNT_DIR}" LOOP_PATH="$(losetup --find --partscan --show "${TMP_DIR}/raspbian.img")" @@ -108,7 +146,7 @@ function provision_raspbian() { cd "${MNT_DIR}" sed -i'' 's/^\([^#]\)/#\1/g' etc/ld.so.preload # add comments echo "[+] Run chroot commands" - LANG=C chroot . bin/bash -x < # Provide the path of an already downloaded raspbian image -o # Name of the img-file (default: pwnagotchi.img) -s # Size which should be added to second partition (in Gigabyte) (default: 4) + -S # Create a sparse image and generate bmap file (default when bmaptool is available) -v # Version of raspbian (Supported: ${SUPPORTED_RASPBIAN_VERSIONS[*]}; default: latest) -p # Only run provisioning (assumes the image is already mounted) -d # Only run dependencies checks @@ -293,4 +336,14 @@ fi setup_raspbian provision_raspbian -echo -ne "[+] Congratz, it's a boy (⌐■_■)!\n[+] One more step: dd if=../$PWNI_OUTPUT of= bs=4M status=progress" +echo -e "[+] Congratz, it's a boy (⌐■_■)!" +echo -e "[+] One more step: dd if=../${PWNI_OUTPUT} of= bs=4M status=progress" + +if [ "${OPT_SPARSE}" -eq 1 ]; +then + echo -e "[t] To transfer use: rsync -vaS --progress $(whoami)@$(hostname -f):${THIS_DIR}/../${PWNI_OUTPUT} " + echo -e "[t] To burn with bmaptool: bmaptool copy ~/${PWNI_OUTPUT} /dev/" +fi + +# Helpful OSX reminder +echo -e "[t] Mac: use 'diskutil list' to figure out which device to burn to; 'diskutil unmountDisk' to unmount that disk'; then use /dev/rdiskX (note the 'r') for faster transfer"