2021-09-16 06:42:05 +01:00
#!/usr/bin/env bash
#
2021-09-27 10:51:02 +01:00
# Kali Linux ARM build-script for Raspberry Pi Zero W (P4wnP1 A.L.O.A.) (32-bit)
# Source: https://gitlab.com/kalilinux/build-scripts/kali-arm
2021-09-16 06:42:05 +01:00
#
# This is a community script - you will need to generate your own image to use
2022-07-12 15:04:15 -05:00
# More information: https://www.kali.org/docs/arm/raspberry-pi-zero-w-p4wnp1-aloa/
2021-09-16 06:42:05 +01:00
#
2022-07-12 15:04:15 -05:00
# Due to the nexmon firmware's age, there is a lack of recognizing arm64.
# This script cannot be run on an arm64 host.
2021-09-16 06:42:05 +01:00
2022-07-12 15:04:15 -05:00
# Hardware model
hw_model = ${ hw_model :- "raspberry-pi-zero-w-p4wnp1-aloa" }
# Architecture
architecture = ${ architecture :- "armel" }
# Desktop manager (xfce, gnome, i3, kde, lxde, mate, e17 or none)
desktop = ${ desktop :- "none" }
2017-10-01 19:24:26 -05:00
2022-07-12 15:04:15 -05:00
# Load default base_image configs
source ./common.d/base_image.sh
2018-06-22 18:30:52 -05:00
2022-07-12 15:04:15 -05:00
# Network configs
basic_network
#add_interface eth0
# move P4wnP1 in (change to release blob when ready)
git clone -b 'master' --single-branch --depth 1 https://github.com/rogandawes/P4wnP1_aloa " ${ work_dir } " /root/P4wnP1
# Third stage
cat <<EOF >> " ${ work_dir } " /third-stage
status_stage3 'Copy rpi services'
cp -p /bsp/services/rpi/*.service /etc/systemd/system/
status_stage3 'Script mode wlan monitor START/STOP'
install -m755 /bsp/scripts/monstart /usr/bin/
install -m755 /bsp/scripts/monstop /usr/bin/
2017-10-01 19:24:26 -05:00
2018-12-07 03:26:39 +01:00
# haveged: assure enough entropy data for hostapd on startup
# avahi-daemon: allow mDNS resolution (apple bonjour) by remote hosts
# dhcpcd5: REQUIRED (P4wnP1 A.L.O.A. currently wraps this binary if a DHCP client is needed)
# dnsmasq: REQUIRED (P4wnP1 A.L.O.A. currently wraps this binary if a DHCP server is needed, currently not used for DNS)
2022-07-12 15:04:15 -05:00
# dosfstools: contains fatlabel (used to label FAT32 iamges for UMS)
2018-12-07 03:26:39 +01:00
# genisoimage: allow creation of CD-Rom iso images for CD-Rom USB gadget from existing folders on the fly
# iodine: allow DNS tunneling
2022-07-12 15:04:15 -05:00
status_stage3 'Install needed packages for P4wnp1 A.L.O.A'
eatmydata apt-get install -y apache2 atftpd autossh avahi-daemon bash-completion bluez bluez-firmware build-essential dhcpcd5 dnsmasq dosfstools fake-hwclock genisoimage golang haveged hostapd i2c-tools iodine openssh-server openvpn pi-bluetooth policykit-1 python3-configobj python3-dev python3-pip python3-requests python3-smbus wpasupplicant
2018-12-07 03:26:39 +01:00
2022-07-12 15:04:15 -05:00
status_stage3 'Remove NetworkManager'
eatmydata apt-get purge -y network-manager
2017-10-01 19:24:26 -05:00
2022-07-12 15:04:15 -05:00
status_stage3 'Enabling ssh by putting ssh or ssh.txt file in /boot'
systemctl enable enable-ssh
2017-10-01 19:24:26 -05:00
2022-07-12 15:04:15 -05:00
status_stage3 'Fixup wireless-regdb signature'
update-alternatives --set regulatory.db /lib/firmware/regulatory.db-upstream
2018-07-12 06:05:39 -05:00
2022-07-12 15:04:15 -05:00
status_stage3 'Enable hciuart for bluetooth'
systemctl enable hciuart
2017-10-01 19:24:26 -05:00
2022-07-12 15:04:15 -05:00
status_stage3 'Set root password to toor'
2017-10-01 19:24:26 -05:00
echo "root:toor" | chpasswd
2022-07-12 15:04:15 -05:00
status_stage3 'Remove persistent net rules file'
rm -f /etc/udev/rules.d/70-persistent-net.rules
2017-10-01 19:24:26 -05:00
2022-07-12 15:04:15 -05:00
status_stage3 'Allow root to ssh in'
sed -i -e 's/^#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
2017-10-01 19:24:26 -05:00
2022-07-12 15:04:15 -05:00
status_stage3 'Disable dhcpcd'
2018-12-07 03:26:39 +01:00
# dhcpcd is needed by P4wnP1, but started on demand
# installation of dhcpcd5 package enables a systemd unit starting dhcpcd for all
# interfaces, which results in conflicts with DHCP servers running on created
# bridge interface (especially for the bteth BNEP bridge). To avoid this we
# disable the service. If communication problems occur, although DHCP leases
# are handed out by dnsmasq, dhcpcd should be the first place to look
# (no interface should hava an APIPA addr assigned, unless the DHCP client
# was explcitely enabled by P4wnP1 for this interface)
systemctl disable dhcpcd
2022-07-12 15:04:15 -05:00
status_stage3 'Enable fake-hwclock'
2018-12-07 03:26:39 +01:00
# enable fake-hwclock (P4wnP1 is intended to reboot/loose power frequently without getting NTP access in between)
# a clean shutdown/reboot is needed, as fake-hwclock service saves time on stop
systemctl enable fake-hwclock
2017-10-01 19:24:26 -05:00
2022-07-12 15:04:15 -05:00
status_stage3 'Copy config.txt into place'
# Copy a default config, with everything commented out so people find it when
# they go to add something when they are following instructions on a website
cp /bsp/firmware/rpi/config.txt /boot/config.txt
2018-07-05 17:54:41 -05:00
2022-07-12 15:04:15 -05:00
status_stage3 'Run P4wnP1 A.L.O.A installer'
2018-12-07 03:26:39 +01:00
cd /root/P4wnP1
make installkali
2018-07-01 22:23:47 -05:00
2022-07-12 15:04:15 -05:00
status_stage3 'Enable dwc2 module'
2018-07-01 22:48:27 -05:00
echo "dwc2" | tee -a /etc/modules
2018-12-07 03:26:39 +01:00
2022-07-12 15:04:15 -05:00
status_stage3 'Enable root login over ttyGS0'
2018-12-07 03:26:39 +01:00
echo ttyGS0 >> /etc/securetty
2022-07-12 15:04:15 -05:00
status_stage3 'Add cronjob to update fake-hwclock'
2018-12-07 03:26:39 +01:00
echo '* * * * * root /usr/sbin/fake-hwclock' >> /etc/crontab
2018-07-01 22:23:47 -05:00
2022-07-12 15:04:15 -05:00
status_stage3 'Create rc.local to remove kernel output on the console'
echo "#!/bin/sh -e" > /etc/rc.local
echo "dmesg -D" >> /etc/rc.local
2017-10-01 19:24:26 -05:00
echo "exit 0" >> /etc/rc.local
2022-07-12 15:04:15 -05:00
chmod +x /etc/rc.local
2017-10-01 19:24:26 -05:00
2022-07-12 15:04:15 -05:00
# Despite the name, all this does is disable root login over ssh
# which we want to enable on this image.
status_stage3 'Remove ssh key check'
rm /etc/runonce.d/03-check-ssh-keys
2017-10-01 19:24:26 -05:00
EOF
2022-07-12 15:04:15 -05:00
# Run third stage
include third_stage
2017-10-01 19:24:26 -05:00
2022-07-12 15:04:15 -05:00
cd " ${ base_dir } "
2017-10-01 19:24:26 -05:00
2022-07-12 15:04:15 -05:00
status 'Clone bootloader and firmware'
git clone -b 1.20181112 --depth 1 https://github.com/raspberrypi/firmware.git " ${ work_dir } " /rpi-firmware
cp -rf " ${ work_dir } " /rpi-firmware/boot/* " ${ work_dir } " /boot/
2018-12-13 09:03:06 +01:00
# copy over Pi specific libs (video core) and binaries (dtoverlay,dtparam ...)
2022-07-12 15:04:15 -05:00
cp -rf " ${ work_dir } " /rpi-firmware/opt/* " ${ work_dir } " /opt/
rm -rf " ${ work_dir } " /rpi-firmware
2017-10-01 19:24:26 -05:00
2022-07-12 15:04:15 -05:00
status 'Clone nexmon firmware'
2021-09-19 10:11:39 +01:00
cd " ${ base_dir } "
git clone https://github.com/mame82/nexmon_wifi_covert_channel.git -b p4wnp1 " ${ base_dir } " /nexmon --depth 1
2018-12-07 03:26:39 +01:00
2022-07-12 15:04:15 -05:00
status 'Clone and build kernel'
cd " ${ base_dir } "
2018-12-07 03:26:39 +01:00
# Re4son kernel 4.14.80 with P4wnP1 patches (dwc2 and brcmfmac)
2022-07-12 15:04:15 -05:00
git clone --depth 1 https://github.com/Re4son/re4son-raspberrypi-linux -b rpi-4.14.80-re4son-p4wnp1 " ${ work_dir } " /usr/src/kernel
2018-12-07 03:26:39 +01:00
2022-07-12 15:04:15 -05:00
cd " ${ work_dir } " /usr/src/kernel
2021-06-25 09:08:16 -05:00
# Remove redundant yyloc global declaration
2022-07-12 15:04:15 -05:00
patch -p1 --no-backup-if-mismatch < " ${ repo_dir } " /patches/11647f99b4de6bc460e106e876f72fc7af3e54a6.patch
2018-12-07 03:26:39 +01:00
# Note: Compiling the kernel in /usr/src/kernel of the target file system is problematic, as the binaries of the compiling host architecture
2021-09-16 06:58:38 +01:00
# get deployed to the /usr/src/kernel/scripts subfolder (in this case linux-x64 binaries), which is symlinked to /usr/src/build later on
# This would f.e. hinder rebuilding single modules, like nexmon's brcmfmac driver, on the Pi itself (online compilation)
2018-12-07 03:26:39 +01:00
# The cause:building of modules relies on the pre-built binaries in /usr/src/build folder. But the helper binaries are compiled with the
# HOST toolchain and not with the crosscompiler toolchain (f.e. /usr/src/kernel/script/basic/fixdep would end up as x64 binary, as this helper
# is not compiled with the CROSS toolchain). As those scripts are used druing module build, it wouldn't work to build on the pi, later on,
2021-09-16 06:58:38 +01:00
# without recompiling the helper binaries with the proper crosscompiler toolchain
2018-12-07 03:26:39 +01:00
#
2021-09-16 06:58:38 +01:00
# To account for that, the 'script' subfolder could be rebuild on the target (online) by running `make scripts/` from /usr/src/kernel folder
# Rebuilding the script, again, depends on additional tooling, like `bc` binary, which has to be installed
2018-12-07 03:26:39 +01:00
#
# Currently the step of recompiling the kernel/scripts folder has to be done manually online, but it should be possible to do it after kernel
2021-09-16 06:58:38 +01:00
# build, by setting the host compiler (CC) to the gcc of the linaro-arm-linux-gnueabihf-raspbian-x64 toolchain (not only the CROSS_COMPILE)
2018-12-07 03:26:39 +01:00
# The problem is, that the used linaro toolchain builds for armhf (not a problem for kernel, as there're no dependencies on hf librearies),
2021-09-16 06:58:38 +01:00
# but the debian packages (and the provided gcc) are armel
2018-12-07 03:26:39 +01:00
#
# To clean up this whole "armel" vs "armhf" mess, the kernel should be compiled with a armel toolchain (best choice would be the toolchain
# which is used to build the kali armel packages itself, which is hopefully available for linux-x64)
#
2021-09-16 06:58:38 +01:00
# For now this is left as manual step, as the normal user shouldn't have a need to recompile kernel parts on the Pi itself
2018-12-07 03:26:39 +01:00
2017-10-01 19:24:26 -05:00
# Set default defconfig
2022-07-12 15:04:15 -05:00
make ARCH = arm CROSS_COMPILE = arm-linux-gnueabi- re4son_pi1_defconfig
2017-10-01 19:24:26 -05:00
# Build kernel
2022-07-12 15:04:15 -05:00
make ARCH = arm CROSS_COMPILE = arm-linux-gnueabi- -j$( nproc)
2017-10-01 19:24:26 -05:00
# Make kernel modules
2022-07-12 15:04:15 -05:00
make ARCH = arm CROSS_COMPILE = arm-linux-gnueabi- modules_install INSTALL_MOD_PATH = " ${ work_dir } "
2017-10-01 19:24:26 -05:00
# Copy kernel to boot
2022-07-12 15:04:15 -05:00
perl scripts/mkknlimg --dtok arch/arm/boot/zImage " ${ work_dir } " /boot/kernel.img
cp arch/arm/boot/dts/*.dtb " ${ work_dir } " /boot/
cp arch/arm/boot/dts/overlays/*.dtb* " ${ work_dir } " /boot/overlays/
cp arch/arm/boot/dts/overlays/README " ${ work_dir } " /boot/overlays/
2017-10-01 19:24:26 -05:00
2022-07-12 15:04:15 -05:00
make ARCH = arm CROSS_COMPILE = arm-linux-gnueabi- mrproper
make ARCH = arm CROSS_COMPILE = arm-linux-gnueabi- re4son_pi1_defconfig
2018-11-26 10:14:07 -06:00
2017-10-01 19:24:26 -05:00
# Fix up the symlink for building external modules
# kernver is used so we don't need to keep track of what the current compiled
# version is
2022-07-12 15:04:15 -05:00
kernver = $( ls " ${ work_dir } " /lib/modules/)
cd " ${ work_dir } " /lib/modules/" ${ kernver } "
2017-10-01 19:24:26 -05:00
rm build
rm source
ln -s /usr/src/kernel build
ln -s /usr/src/kernel source
2021-09-19 10:11:39 +01:00
cd " ${ base_dir } "
2017-10-01 19:24:26 -05:00
2022-07-12 15:04:15 -05:00
status 'Enable dwc2 dtb overlay'
cat << EOF >> " ${ work_dir } " /boot/config.txt
2017-10-01 19:24:26 -05:00
dtoverlay = dwc2
EOF
2018-12-07 03:26:39 +01:00
# git clone of nexmon moved in front of kernel compilation, to have poper brcmfmac driver ready
2022-07-12 15:04:15 -05:00
status 'Build nexmon firmware'
2021-09-19 10:11:39 +01:00
cd " ${ base_dir } " /nexmon
2018-12-07 03:26:39 +01:00
# Make sure we're not still using the armel cross compiler
unset CROSS_COMPILE
2018-07-09 22:19:21 -05:00
# Disable statistics
touch DISABLE_STATISTICS
source setup_env.sh
make
cd buildtools/isl-0.10
CC = $CCgcc
./configure
make
2022-07-12 15:04:15 -05:00
sed -i -e 's/all:.*/all: $(RAM_FILE)/g' " ${ NEXMON_ROOT } " /patches/bcm43430a1/7_45_41_46/nexmon/Makefile
cd " ${ NEXMON_ROOT } " /patches/bcm43430a1/7_45_41_46/nexmon
2018-07-09 22:19:21 -05:00
make clean
2021-09-16 06:58:38 +01:00
# We do this so we don't have to install the ancient isl version into /usr/local/lib on systems
2022-07-12 15:04:15 -05:00
LD_LIBRARY_PATH = " ${ NEXMON_ROOT } /buildtools/isl-0.10/.libs " make ARCH = arm CC = " ${ NEXMON_ROOT } /buildtools/gcc-arm-none-eabi-5_4-2016q2-linux-x86/bin/arm-none-eabi- "
2018-07-09 22:19:21 -05:00
# RPi0w->3B firmware
2018-12-07 03:26:39 +01:00
# disable nexmon by default
2022-07-12 15:04:15 -05:00
mkdir -p " ${ work_dir } " /lib/firmware/brcm
cp " ${ NEXMON_ROOT } /patches/bcm43430a1/7_45_41_46/nexmon/brcmfmac43430-sdio.bin " " ${ work_dir } " /lib/firmware/brcm/brcmfmac43430-sdio.nexmon.bin
cp " ${ NEXMON_ROOT } /patches/bcm43430a1/7_45_41_46/nexmon/brcmfmac43430-sdio.bin " " ${ work_dir } " /lib/firmware/brcm/brcmfmac43430-sdio.bin
wget https://raw.githubusercontent.com/RPi-Distro/firmware-nonfree/master/brcm/brcmfmac43430-sdio.txt -O " ${ work_dir } " /lib/firmware/brcm/brcmfmac43430-sdio.txt
2021-09-16 06:58:38 +01:00
# Make a backup copy of the rpi firmware in case people don't want to use the nexmon firmware
# The firmware used on the RPi is not the same firmware that is in the firmware-brcm package which is why we do this
2022-07-12 15:04:15 -05:00
wget https://raw.githubusercontent.com/RPi-Distro/firmware-nonfree/master/brcm/brcmfmac43430-sdio.bin -O " ${ work_dir } " /lib/firmware/brcm/brcmfmac43430-sdio.rpi.bin
2017-10-01 19:24:26 -05:00
2022-07-12 15:04:15 -05:00
# Set hostname
status 'Set hostname'
echo " ${ hostname } " > " ${ work_dir } " /etc/hostname
2017-10-01 19:24:26 -05:00
2022-07-12 15:04:15 -05:00
cd " ${ repo_dir } / "
# Clean system
include clean_system
2020-07-22 21:29:57 -05:00
2022-07-12 15:04:15 -05:00
# Calculate the space to create the image and create
make_image
2017-10-01 19:24:26 -05:00
2022-07-12 15:04:15 -05:00
# Create the disk partitions
status "Create the disk partitions"
2021-09-19 13:45:29 +01:00
parted -s " ${ image_dir } / ${ image_name } .img " mklabel msdos
2022-07-12 15:04:15 -05:00
parted -s " ${ image_dir } / ${ image_name } .img " mkpart primary fat32 4MiB " ${ bootsize } " MiB
parted -s -a minimal " ${ image_dir } / ${ image_name } .img " mkpart primary " $fstype " " ${ bootsize } " MiB 100%
2018-06-28 23:43:48 -05:00
# Set the partition variables
2022-07-12 15:04:15 -05:00
make_loop
2018-06-28 23:43:48 -05:00
# Create file systems
2022-07-12 15:04:15 -05:00
mkfs_partitions
# Make fstab,
make_fstab
# Configure Raspberry Pi firmware
include rpi_firmware
2018-06-28 23:43:48 -05:00
# Create the dirs for the partitions and mount them
2022-07-12 15:04:15 -05:00
status "Create the dirs for the partitions and mount them"
mkdir -p " ${ base_dir } " /root/
mount " ${ rootp } " " ${ base_dir } " /root
mkdir -p " ${ base_dir } " /root/boot
mount " ${ bootp } " " ${ base_dir } " /root/boot
status "Rsyncing rootfs into image file"
rsync -HPavz -q --exclude boot " ${ work_dir } " / " ${ base_dir } " /root/
sync
2018-07-05 17:54:41 -05:00
2022-07-12 15:04:15 -05:00
status "Rsyncing rootfs into image file (/boot)"
rsync -rtx -q " ${ work_dir } " /boot " ${ base_dir } " /root
2020-07-22 21:29:57 -05:00
sync
2018-06-28 23:43:48 -05:00
2022-07-12 15:04:15 -05:00
# Load default finish_image configs
include finish_image