encryptboot.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #!/usr/bin/env bash
  2. DEVICE="${1}"
  3. BACKUP_DIR="./backups"
  4. DATE_FIX=$(date '+%Y%m%d-%H%M%S')
  5. DD_OPTS="bs=512 iflag=fullblock conv=notrunc"
  6. TARGET_BOOT="qubes_dom0-boot"
  7. welcome() {
  8. echo "################################"
  9. echo "This script will encrypt an unencrypted /boot partition"
  10. echo "Confirmation will be asked before writing"
  11. echo "################################"
  12. }
  13. warning() {
  14. echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  15. echo "The following steps may corrupt and lose your data, continue at your own risk"
  16. echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  17. read -r
  18. }
  19. adios() {
  20. echo "[+] Procedure completed!"
  21. }
  22. panic() {
  23. echo "[*] Something went wrong in a write operation, system may be in a corrupted state. Attempting recovery"
  24. restore
  25. exit 1
  26. }
  27. restore() {
  28. echo "[*] Attempting to restore original partition scheme"
  29. dd if=${BACKUP_DIR}/mbr-${DATE_FIX}.img of=${DEVICE} bs=512 iflag=fullblock conv=notrunc status=progress
  30. if [[ "${?}" -ne 0 ]]; then
  31. echo "[-] Something went wrong restoring, hope you made a backup as advised ☠"
  32. fi
  33. }
  34. check_params() {
  35. if [[ "${1}" -ne 1 ]]; then
  36. echo "Usage: ./encryptboot.sh <device>"
  37. echo "Example: ./encryptboot.sh /dev/sda"
  38. exit
  39. fi
  40. }
  41. check_root() {
  42. if [[ "${EUID}" -ne 0 ]]; then
  43. echo "[-] This script must be run as root; re-run prefixed with sudo"
  44. exit 1
  45. fi
  46. }
  47. check_device() {
  48. if [[ ! -b "${DEVICE}" ]]; then
  49. echo "[-] Device ${DEVICE} does not exists"
  50. exit 1
  51. fi
  52. }
  53. backup_boot() {
  54. echo "[+] Backing up boot device"
  55. mkdir -p "${BACKUP_DIR}"
  56. dd if=${DEVICE}1 of=${BACKUP_DIR}/boot-${DATE_FIX}.img ${DD_OPTS} status=progress
  57. if [[ "${?}" -ne 0 ]]; then
  58. echo "[-] Something went wrong backing up boot partition, exiting"
  59. exit 1
  60. fi
  61. BOOT_HASH=$(sha256sum ${DEVICE}1 | cut -d ' ' -f 1)
  62. BOOT_BACKUP_HASH=$(sha256sum ${BACKUP_DIR}/boot-${DATE_FIX}.img | cut -d ' ' -f 1)
  63. if [[ ${BOOT_HASH} != ${BOOT_BACKUP_HASH} ]]; then
  64. echo "[-] Backup ${BACKUP_DIR}/boot-${DATE_FIX}.img hash is not equal to ${DEVICE}1 hash, exiting"
  65. exit 1
  66. fi
  67. echo "[+] Backup successful"
  68. }
  69. backup_partition_table() {
  70. echo "[+] Backing up partition table"
  71. mkdir -p "${BACKUP_DIR}"
  72. dd if=${DEVICE} of=${BACKUP_DIR}/mbr-${DATE_FIX}.img ${DD_OPTS} count=1
  73. if [[ "${?}" -ne 0 ]]; then
  74. echo "[-] Something went wrong backing up partition table, exiting"
  75. exit 1
  76. fi
  77. }
  78. check_headers() {
  79. BOOT_HEADER=$(dd if=${DEVICE}1 ${DD_OPTS} count=16 2>/dev/null | file -s -)
  80. LUKS_HEADER=$(dd if=${DEVICE}2 ${DD_OPTS} count=16 2>/dev/null | file -s -)
  81. if [[ "${BOOT_HEADER}" != *"ext4"* ]]; then
  82. echo "[-] ${DEVICE}1 is not an ext4 filesystem"
  83. exit 1
  84. fi
  85. if [[ "${LUKS_HEADER}" != *"LUKS"* ]]; then
  86. echo "[-] ${DEVICE}2 is not a LUKS container"
  87. exit
  88. fi
  89. echo "[+] Headers check completed"
  90. }
  91. get_offsets() {
  92. echo "[+] Getting boot partition offsets"
  93. START_OFFSET=$(parted -s ${DEVICE} unit s print | grep boot | tr -s ' ' | cut -d ' ' -f 3 | tr -d 's')
  94. END_OFFSET=$(parted -s ${DEVICE} unit s print | grep boot | tr -s ' ' | cut -d ' ' -f 4 | tr -d 's')
  95. if [[ "${START_OFFSET}" -le 0 ]] || [[ "${END_OFFSET}" -le 0 ]] || [[ "${END_OFFSET}" -le ${START_OFFSET} ]]; then
  96. echo "[-] Error parsing boot partition get_offsets"
  97. exit 1
  98. fi
  99. #OFFSET=$((${END_OFFSET}-${START_OFFSET}))
  100. OFFSET=$((${END_OFFSET}+1))
  101. }
  102. delete_partitions() {
  103. echo "[+] Deleting old partition scheme"
  104. parted "${DEVICE}" rm 1
  105. if [[ "${?}" -ne 0 ]]; then
  106. echo "[-] Something went wrong deleting boot partition"
  107. panic
  108. fi
  109. parted "${DEVICE}" rm 2
  110. if [[ "${?}" -ne 0 ]]; then
  111. echo "[-] Something went wrong deleting LUKS partition"
  112. panic
  113. fi
  114. }
  115. create_partition() {
  116. echo "[+] Creating new full disk partition"
  117. parted -s ${DEVICE} mkpart primary luks 0% 100%
  118. if [[ "${?}" -ne 0 ]]; then
  119. echo "[-] Something went wrong creatig the new partition"
  120. panic
  121. fi
  122. }
  123. check_offsets() {
  124. echo ${START_OFFSET}
  125. echo ${END_OFFSET}
  126. echo ${OFFSET}
  127. LUKS_HEADER=$(dd if=${DEVICE}1 ${DD_OPTS} skip=${OFFSET} seek=0 count=16 2>/dev/null | file -s -)
  128. if [[ "${LUKS_HEADER}" != *"LUKS"* ]]; then
  129. echo "[-] Luks header not found at given offset "
  130. exit
  131. fi
  132. }
  133. move_data() {
  134. dd if=${DEVICE}1 of=${DEVICE}1 ${DD_OPTS} skip=${OFFSET} seek=0 status=progress
  135. if [[ "${?}" -ne 0 ]]; then
  136. echo "[-] Failed moving data backwards, hope you had backups because this is most likely total corruption. MBR and boot.img backups are in ${BACKUP_DIR}"
  137. exit
  138. fi
  139. }
  140. config_luks_lvm() {
  141. echo "[+] Extending LVM pool"
  142. cryptsetup luksOpen ${DEVICE}1 qubespv
  143. pvresize qubespv
  144. echo "[+] Creating LVM boot partition"
  145. lvcreate -n boot -l100%FREE ${TARGET_BOOT}
  146. }
  147. restore_boot() {
  148. echo "[+] Copying old boot image in new encrypted LVM volume "
  149. dd if=${BACKUP_DIR}/boot-${DATE_FIX}.img of=/dev/mapper/${TARGET_BOOT} ${DD_OPTS} status=progress
  150. if [[ "${?}" -ne 0 ]]; then
  151. echo "[-] Failed to copy back boot.img to LVM, probably a recoverable state but needs manual intervention"
  152. exit
  153. fi
  154. LVM_BOOT_HASH=$(sha256sum ${TARGET_BOOT} | cut -d ' ' -f 1)
  155. if [[ ${BOOT_HASH} != ${LVM_BOOT_HASH} ]]; then
  156. echo "[-] "
  157. exit 1
  158. fi
  159. echo "[+] Boot partition written back successfully"
  160. }
  161. check_params "${#}"
  162. welcome
  163. check_root
  164. check_device
  165. backup_partition_table
  166. backup_boot
  167. check_headers
  168. get_offsets
  169. check_offsets
  170. warning
  171. #delete_partitions
  172. #create_partition
  173. #move_data
  174. #config_luks_lvm
  175. #restore_boot
  176. adios