Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add qemu-arm-static when chroot on non-arm platform, support Raspberry model 3+ by replacing minibian image with raspbian-strecth-lite #24

Merged
merged 2 commits into from
Jun 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions bin/create_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
# - debug
# - wlan config (optional)
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
LOG="${DIR}/${1}_output.txt"
PROGRESS="${DIR}/${1}_progress.txt"
LOG="${DIR}/${1}_$(date +%d-%m-%Y-%H-%M-%S)_output.txt"
PROGRESS="${DIR}/${1}_$(date +%d-%m-%Y-%H-%M-%S)_progress.txt"
IMAGE_DIR="${DIR}/../honeypot_images"
echo "0" > "${PROGRESS}"
echo "" > "${LOG}"
Expand All @@ -26,8 +26,8 @@ if [ -a "${IMAGE_DIR}/${2}" ]; then
rm "${IMAGE_DIR}/${2}" >> "${LOG}" 2>&1
fi
# Copy base to new name
echo "Creating 1.5GB image in ${IMAGE_DIR}/${2}" >> "${LOG}" 2>&1
dd bs=1M count=1536 if=/dev/zero of="${IMAGE_DIR}/${2}" >> "${LOG}" 2>&1
echo "Creating 3GB image in ${IMAGE_DIR}/${2}" >> "${LOG}" 2>&1
dd bs=1M count=3072 if=/dev/zero of="${IMAGE_DIR}/${2}" >> "${LOG}" 2>&1
echo "5" > "${PROGRESS}"
echo "Finished creation, creating loop devices" >> "${LOG}" 2>&1
losetup -f "${IMAGE_DIR}/base.img" >> "${LOG}" 2>&1
Expand All @@ -38,11 +38,13 @@ echo "Copy data from base (${ORIG_LD}) to new image (${NEW_LD}) using dd" >> "${
dd if="${ORIG_LD}" of="${NEW_LD}" >> "${LOG}" 2>&1
echo "10" > "${PROGRESS}"
echo "Parted partition reorganisation" >> "${LOG}" 2>&1
parted -s "${NEW_LD}" rm 2 >> "${LOG}" 2>&1
parted -s "${NEW_LD}" mkpart primary 64 1611 >> "${LOG}" 2>&1
startSector=$(fdisk -lu ${IMAGE_DIR}/${2} | grep Linux | sed -r 's/.*\.img2 *([0-9]+).*/\1/')
printf "d\n2\nn\np\n2\n${startSector}\n\nw\n" | fdisk ${IMAGE_DIR}/${2}
echo "20" > "${PROGRESS}"
echo "Creating loopback device for root partition" >> "${LOG}" 2>&1
losetup -f -o 64028672 "${IMAGE_DIR}/${2}" >> "${LOG}" 2>&1
echo "Get the image size" >> "${LOG}" 2>&1
rootOffset=$(expr ${startSector} \* 512)
echo "Creating loopback device for root partition with offset ${rootOffset}" >> "${LOG}" 2>&1
losetup -f -o ${rootOffset} "${IMAGE_DIR}/${2}" >> "${LOG}" 2>&1
ROOT_LD=$(losetup -a | grep "${2}" | grep "offset" | sed 's/\(.*\): .*/\1/')
echo "Checking partition ${ROOT_LD}" >> "${LOG}" 2>&1
e2fsck -f -y "${ROOT_LD}" >> "${LOG}" 2>&1
Expand All @@ -58,7 +60,7 @@ if [ ! -d /mnt/tmp/ ]; then
fi
echo "Mounting image" >> "${LOG}" 2>&1
# Mount image on temp folder
mount -o loop,offset=64028672 "${IMAGE_DIR}/${2}" /mnt/tmp/ >> "${LOG}" 2>&1
mount -o loop,offset=${rootOffset} "${IMAGE_DIR}/${2}" /mnt/tmp/ >> "${LOG}" 2>&1
echo "Mounting proc & sys" >> "${LOG}" 2>&1
# Assign proc & sysfs
mount proc /mnt/tmp/proc -t proc >> "${LOG}" 2>&1
Expand All @@ -68,20 +70,30 @@ cp -r "${DIR}/../../client" /mnt/tmp/usr/src/client >> "${LOG}" 2>&1
echo "${3}" > /tmp/profile.json
mv /tmp/profile.json /mnt/tmp/usr/src/client/honeypot_profile.json
cp -r "${DIR}/../pipot/services" /mnt/tmp/usr/src/client/pipot >> "${LOG}" 2>&1
cpuArch=$(lscpu | grep Architecture | sed 's/Architecture: *\(.*\)/\1/')
if [ $(arch) != arm* ]; then
echo "This is not arm machine, copy QemuUserEmulation binary to the chroot" >> "${LOG}" 2>&1
# Copy QemuUserEmulation binary to the chroot
cp /usr/bin/qemu-arm-static /mnt/tmp/usr/bin >> "${LOG}" 2>&1
fi
# Chroot into it
echo "40" > "${PROGRESS}"
echo "Chrooting into image" >> "${LOG}" 2>&1
chroot /mnt/tmp /usr/src/client/bin/chroot.sh "/install-log.txt" "${4}" "${5}" "${6}" "${7}" >> "${LOG}" 2>&1
cat /mnt/tmp/install-log.txt >> "${LOG}" 2>&1
echo "Exited chroot, unmounting proc & sys" >> "${LOG}" 2>&1
echo "90" > "${PROGRESS}"
if [ $(arch) != arm* ]; then
# Remove QemuUserEmulation
rm /mnt/tmp/usr/bin/qemu-arm-static >> "${LOG}" 2>&1
fi
# After exiting, unmount volumes
umount /mnt/tmp/proc && umount /mnt/tmp/sys >> "${LOG}" 2>&1
sleep 5
echo "Using fuser to kill any left processes" >> "${LOG}" 2>&1
fuser -k /mnt/tmp >> "${LOG}" 2>&1
echo "Unmount /mnt/tmp" >> "${LOG}" 2>&1
umount /mnt/tmp >> "${LOG}" 2>&1
umount -l /mnt/tmp >> "${LOG}" 2>&1
echo "Unmounted, created image should be ready now" >> "${LOG}" 2>&1
echo "100" > "${PROGRESS}"
echo "Total runtime: ${SECONDS}" >> "${LOG}" 2>&1
echo "Total runtime: ${SECONDS}" >> "${LOG}" 2>&1
14 changes: 10 additions & 4 deletions install/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ echo "* Installing nginx, python & pip "

apt-get -q -y install virtualenv dnsutils nginx python python-dev python-pip >> "$install_log" 2>&1

if [ $(arch) != arm* ]; then
echo "This is not arm machine, install qemu-user-static and binfmt-support for image chroot" >> "${LOG}" 2>&1
apt-get -q -y install qemu qemu-user-static binfmt-support >> "$install_log" 2>&1
fi

if [[ "$OSTYPE" == "linux-gnu" ]]; then
apt-get -q -y install build-essential libffi-dev libssl-dev >> "$install_log" 2>&1
fi
Expand Down Expand Up @@ -147,10 +152,11 @@ ln -s /etc/nginx/sites-available/pipot /etc/nginx/sites-enabled/pipot >> "$insta
echo "* Reloading nginx"
service nginx reload >> "$install_log" 2>&1
echo "* Downloading base image"
wget https://sourceforge.net/projects/minibian/files/2016-03-12-jessie-minibian.tar.gz >> "$install_log" 2>&1
tar -xvf 2016-03-12-jessie-minibian.tar.gz >> "$install_log" 2>&1
rm 2016-03-12-jessie-minibian.tar.gz >> "$install_log" 2>&1
mv 2016-03-12-jessie-minibian.img "${dir}/../honeypot_images/base.img" >> "$install_log" 2>&1
wget https://downloads.raspberrypi.org/raspbian_lite_latest >> "$install_log" 2>&1
mv raspbian_lite_latest raspbian_lite_latest.zip
unzip -o raspbian_lite_latest.zip >> "$install_log" 2>&1
rm raspbian_lite_latest.zip >> "$install_log" 2>&1
mv *raspbian-stretch-lite.img "${dir}/../honeypot_images/base.img" >> "$install_log" 2>&1
echo ""
echo "* Starting PiPot..."
service pipot start
Expand Down