Advertisement
Tritonio

Kali Linux on a Raspberry Pi (A/B+/2) with Disk Encryption

Mar 26th, 2021
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.89 KB | None | 0 0
  1.  
  2. Kali Linux on a Raspberry Pi (A/B+/2) with Disk Encryption
  3.  
  4. March 9, 2015 Kali Linux
  5.  
  6. With the advent of smaller, faster ARM hardware such as the new Raspberry Pi 2 (which now has a Kali image built for it), we’ve been seeing more and more use of these small devices as “throw-away hackboxes“. While this might be a new and novel technology, there’s one major drawback to this concept – and that is the confidentiality of the data stored on the device itself. Most of the setups we’ve seen do little to protect the sensitive information saved on the SD cards of these little computers. This fact, together with a nudge from friends is what prompted us to create a LUKS encrypted, NUKE capable Kali Linux image for our Raspberry Pi devices. The following blog post describes the process, so you can repeat it and make your own shiny shiny.
  7. Birds Eye View of the Disk Encryption Process
  8.  
  9. The process described in this blog post was tried and tested successfully on a Raspberry Pi B+ and a Raspberry Pi 2 (henceforth collectively called “RPi”). but it should be trivial to port these instructions to any ARM device running Kali. Before we begin, let’s take a minute to quickly describe what we’ll be doing – as while this process is not complicated, it is involved. This is basically our spiel:
  10.  
  11. We download the required Kali Raspberry Pi RPi image and dd it to an SD card.
  12. We chroot to the RPi image and install/update several files in preparation for our crypted boot.
  13. We create an initramfs file which includes Dropbear and freshly generated SSH keys.
  14. We rsync the modified rootfs to a temporary backup location and then delete the rootfs partition from the SD.
  15. We then recreate an encrypted partition to which we restore the root partition data. That’s it!
  16.  
  17. If all goes well, the RPi will boot and then LUKS will kick in and ask for a password to decrypt the root drive, while simultaneously opening a Dropbear SSH session through which you can SSH in and provide the boot decryption password. Oh yeah, did we mention this image also has LUKS NUKE capabilities?
  18. Getting Your Hands Dirty
  19.  
  20. As always, all our ARM dev is done on a Kali amd64 machine and we’ve made sure that we have all the dependencies we need. We download the latest Kali RPi image (1.1.0), extract it, and dd it to our SD card, which in our case showed up as sdb2 – adapt as necessary!
  21. dd if=/root/kali-1.1.0-rpi.img of=/dev/sdb bs=4M
  22.  
  23. Once dd‘d, we mount the various partitions and chroot into the Kali RPi image:
  24. mkdir -p /mnt/chroot/boot
  25.  
  26. mount /dev/sdb2 /mnt/chroot/
  27. mount /dev/sdb1 /mnt/chroot/boot/
  28.  
  29. mount -t proc none /mnt/chroot/proc
  30. mount -t sysfs none /mnt/chroot/sys
  31. mount -o bind /dev /mnt/chroot/dev
  32. mount -o bind /dev/pts /mnt/chroot/dev/pts
  33.  
  34. cp /usr/bin/qemu-arm-static /mnt/chroot/usr/bin/
  35. LANG=C chroot /mnt/chroot/
  36.  
  37. We then update our image and install some essential packages we will need for this process:
  38. apt-get update
  39. apt-get install busybox cryptsetup dropbear
  40.  
  41. We create an initial initramfs file, which will trigger the dropbear SSH key generation. We first find out the modules directory version number as follows (this will change between different image versions):
  42. root@kali:/# ls -l /lib/modules/ |awk -F" " '{print $9}'
  43. 3.12.36
  44.  
  45. We then use that version info to generate an initial initramfs file.
  46. mkinitramfs -o /boot/initramfs.gz 3.12.36
  47.  
  48. We configure the OpenSSH service to start at boot, while remembering to change the default password.
  49. update-rc.d ssh enable
  50. passwd
  51.  
  52. Next, we verify that the /boot directory is mounted, and if so, we modify the boot parameters in cmdline.txt and config.txt.
  53. nano /boot/cmdline.txt
  54.  
  55. …and add / change the following parameters:
  56. root=/dev/mapper/crypt_sdcard cryptdevice=/dev/mmcblk0p2:crypt_sdcard rootfstype=ext4
  57.  
  58. Next comes config.txt:
  59. echo initramfs initramfs.gz 0x00f00000 & /boot/config.txt
  60.  
  61. Now we deal with the Dropbear SSH access. We copy over the SSH private key to our laptop:
  62. cat /etc/initramfs-tools/root/.ssh/id_rsa
  63.  
  64. And limit the SSH connection to allow interaction with the cryptroot application only.
  65. nano /etc/initramfs-tools/root/.ssh/authorized_keys
  66.  
  67. We paste the following before the ssh public key begins.
  68. command="/scripts/local-top/cryptroot && kill -9 `ps | grep -m 1 'cryptroot' | cut -d ' ' -f 3`"
  69.  
  70. We then create a fstab and crypttab with our configured boot device and exit the chroot:
  71. cat <<EOF > /etc/fstab
  72. proc /proc proc defaults 0 0
  73. /dev/mmcblk0p1 /boot vfat defaults 0 2
  74. /dev/mapper/crypt_sdcard / ext4 defaults,noatime 0 1
  75. EOF
  76.  
  77. echo crypt_sdcard /dev/mmcblk0p2 none luks > /etc/crypttab
  78.  
  79. During our tests, we noticed that in some instances, the USB ports take a while to wake up, which can kill the initrd Dropbear network initialization. To fix this, we can introduce a 5-second sleep before the configure_networking function located in the initrd itself:
  80. nano /usr/share/initramfs-tools/scripts/init-premount/dropbear
  81.  
  82. change:
  83. configure_networking &
  84.  
  85. to:
  86. echo "Waiting 5 seconds for USB to wake"
  87. sleep 5
  88. configure_networking &
  89.  
  90. Save and exit the file them regenerate the initramfs and exit the chroot. You can ignore the cryptsetup and device-mapper warnings.
  91. mkinitramfs -o /boot/initramfs.gz 3.12.36
  92. exit
  93.  
  94. Now we proceed to tear down the chroot and backup our rootfs partition:
  95. umount /mnt/chroot/boot
  96. umount /mnt/chroot/sys
  97. umount /mnt/chroot/proc
  98. mkdir -p /mnt/backup
  99. rsync -avh /mnt/chroot/* /mnt/backup/
  100.  
  101. Once outside the chroot, we unmount everything:
  102. umount /mnt/chroot/dev/pts
  103. umount /mnt/chroot/dev
  104. umount /mnt/chroot
  105.  
  106. Once done, we delete the existing 2nd partition on the SD card and recreate an empty one, which we set up for LUKS encryption.
  107. echo -e "d\n2\nw" | fdisk /dev/sdb
  108. echo -e "n\np\n2\n\n\nw" | fdisk /dev/sdb
  109.  
  110. Unplug your SD card and plug it back in to have the new partitions register, then start setting up your encrypted partition.
  111. cryptsetup -v -y --cipher aes-cbc-essiv:sha256 --key-size 256 luksFormat /dev/sdb2
  112. cryptsetup -v luksOpen /dev/sdb2 crypt_sdcard
  113. mkfs.ext4 /dev/mapper/crypt_sdcard
  114.  
  115. Once ready, we restore the rootfs backup to the now encrypted partition.
  116. mkdir -p /mnt/encrypted
  117. mount /dev/mapper/crypt_sdcard /mnt/encrypted/
  118. rsync -avh /mnt/backup/* /mnt/encrypted/
  119. umount /mnt/encrypted/
  120. rm -rf /mnt/backup
  121. sync
  122.  
  123. Then we unmount and close the volume.
  124. cryptsetup luksClose /dev/mapper/crypt_sdcard
  125.  
  126. That’s it! Now all that remains is to boot up the RPi using the modified SD card. The initramfs will load Dropbear and get a DHCP address on your local LAN (you can also hardcode an IP), allowing you to SSH to the booting RPi and enter a decryption password. Once the password is accepted, Dropbear will exit and the RPi will continue to boot. You should see something like the following:
  127. root@kali:~# ssh -i key 192.168.0.52
  128. The authenticity of host '192.168.0.52 (192.168.0.52)' can't be established.
  129. RSA key fingerprint is a6:a2:ad:7d:cb:d8:70:58:d1:ed:81:e8:4a:d5:23:3a.
  130. Are you sure you want to continue connecting (yes/no)? yes
  131. Warning: Permanently added '192.168.0.52' (RSA) to the list of known hosts.
  132. Unlocking the disk /dev/mmcblk0p2 (crypt_sdcard)
  133. Enter passphrase: cryptsetup: crypt_sdcard set up successfully
  134. Connection to 192.168.0.52 closed.
  135. root@kali:~#
  136. Can I Have Some LUKS NUKE With That Pi?
  137.  
  138. If you are not familiar with the Kali Linux LUKS NUKE feature then you’re missing out. Although this stage is optional, it allows you to configure and apply an emergency self-destruct password to your LUKS encrypted drive. To do this, we simply define a Nuke password on our encrypted partition:
  139. root@kali:~# cryptsetup luksAddNuke /dev/sdb2
  140. Enter any existing passphrase: (existing passphrase)
  141. Enter new passphrase for key slot: (new nuke passphrase)
  142. root@kali:~#
  143.  
  144. With the Nuke password defined, you can now remotely wipe the LUKS decryption keyslots, making the data on the SD card inaccessible.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement