Skip to content

Instantly share code, notes, and snippets.

@nelsonaloysio
Created May 19, 2023 22:50
Show Gist options
  • Select an option

  • Save nelsonaloysio/3b6ab11b68348d6852cbd166021a4a72 to your computer and use it in GitHub Desktop.

Select an option

Save nelsonaloysio/3b6ab11b68348d6852cbd166021a4a72 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# A simple Bash CLI tool aimed at unlocking
# BitLocker-encrypted devices with <dislocker>.
USAGE="""usage: bitlocker /dev/sdX "my_password" [--relock]
"""
USER_ID="1000"
ROOT_DIR="/dev/dislocker"
TARGET_DIR="/media/dislocker"
# Argument parameters
DEVICE="$1"
PASSWD="$2"
# Check device and assign label
DEV="$(echo $DEVICE | sed 's:.*/::g')"
# Lock all encrypted patitions
if [[ "$@" =~ "--lock" || "$@" =~ "--relock" ]]; then
[[ "$DEV" = "" || "$DEV" = "$DEVICE" ]] && DEV="dislocker"
findmnt | grep dislocker -w | tr ' ' '\n' | grep -w "$DEV" | grep -E "$TARGET_DIR|$ROOT_DIR" | cut -c7- |
while read f; do
printf "[lock] $f " &&
fusermount -uz "$f" &&
echo "OK" || echo "FAIL"
done &&
exit 0
fi
# Check device and password
[[ "$DEVICE" = "" || "$DEVICE" = "-h" || "$DEVICE" = "--help" ]] &&
printf "$USAGE" &&
exit 1
[[ $(ls -1 "$DEVICE" 2>/dev/null | wc -l) = 0 ]] &&
echo "error: unable to find device '$DEVICE'." &&
exit 1
[[ "$PASSWD" = "" ]] &&
printf "Password to unlock BitLocker device:\n> " &&
read -s PASSWD && echo
# Unlock encrypted partition
[[ ! -f "${ROOT_DIR}/${DEV}/dislocker-file" ]] &&
printf "[unlock] $DEVICE => ${ROOT_DIR}/${DEV} " &&
mkdir -p "${ROOT_DIR}/${DEV}" &&
dislocker "${DEVICE}" "${ROOT_DIR}/${DEV}" -u"$PASSWD" &&
echo "OK"
# Mount to target folder
[[ -f "${ROOT_DIR}/${DEV}/dislocker-file" ]] &&
[[ $(findmnt | grep -c "${TARGET_DIR}/${DEV}") = 0 ]] &&
printf "[mount] ${ROOT_DIR}/${DEV}/dislocker-file => ${TARGET_DIR}/${DEV} " &&
mkdir -p "${TARGET_DIR}/${DEV}" &&
mount "${ROOT_DIR}/${DEV}/dislocker-file" "${TARGET_DIR}/${DEV}" -o uid=$USER_ID &&
echo "OK"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment