Skip to content

Commit ff85688

Browse files
Add files via upload
1 parent e2e8de1 commit ff85688

25 files changed

+559
-0
lines changed

src/apfs.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/bash
2+
3+
target_root_directory=$1
4+
target_directory=$2
5+
keys_directory=$3
6+
drive_serial=$4
7+
current_partition=$5
8+
uuid=$6
9+
10+
11+
slices=`/opt/drivebadger/internal/generic/apple/get-apfs-filesystems.sh /dev/$current_partition`
12+
for slice in $slices; do
13+
slid="${slice%:*}"
14+
slname="${slice##*:}"
15+
16+
mountpoint=/media/$current_partition/$slid/mnt
17+
subtarget=$target_directory/$drive_serial/${current_partition}_apfs_${slid}_${slname}
18+
mkdir -p $mountpoint $subtarget
19+
20+
if /opt/drivebadger/internal/generic/apple/mount-apfs-filesystem.sh $keys_directory "$drive_serial" /dev/$current_partition $slid $mountpoint $subtarget >>$subtarget/rsync.log 2>>$subtarget/rsync.err; then
21+
/opt/drivebadger/internal/generic/process-hooks.sh $mountpoint $target_root_directory
22+
23+
logger "copying UUID=$uuid (partition $current_partition filesystem APFS slice $slid ($slname), mounted as $mountpoint, target directory $subtarget)"
24+
/opt/drivebadger/internal/generic/rsync-partition.sh $mountpoint $subtarget >>$subtarget/rsync.log 2>>$subtarget/rsync.err
25+
umount $mountpoint
26+
logger "copied UUID=$uuid"
27+
fi
28+
done

src/dump-debug-files.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/sh
2+
3+
directory=$1
4+
5+
ls -al /dev/disk/by-uuid/ >$directory/by-uuid.txt
6+
ls -al /dev/disk/by-id/ >$directory/by-id.txt
7+
8+
cat /sys/class/dmi/id/* 2>/dev/null >$directory/dmi.txt
9+
10+
ifconfig -a >$directory/ifconfig.txt
11+
route -ne >$directory/route.txt
12+
arp -an >$directory/arp.txt
13+
14+
dmesg >$directory/dmesg.txt
15+
lsusb >$directory/lsusb.txt
16+
lspci >$directory/lspci.txt
17+
18+
lsblk >$directory/lsblk.txt
19+
blkid >$directory/blkid.txt
20+
21+
cat /proc/cpuinfo >$directory/cpuinfo.txt
22+
cat /proc/meminfo >$directory/meminfo.txt
23+
cat /proc/partitions >$directory/partitions.txt
24+
25+
if [ -f /proc/mdstat ]; then
26+
cat /proc/mdstat >$directory/mdstat.txt
27+
fi

src/get-apfs-filesystems.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
3+
device=$1
4+
5+
fsapfsinfo $device \
6+
|grep -A2 Volume \
7+
|grep -v Identifier \
8+
|sed -e 's/[\t ]//g' -e 's/information://g' -e 's/Volume://g' -e 's/Name://g' -e 's/[^a-zA-Z0-9]//g' \
9+
|tr '\n' ':' \
10+
|sed -e 's/::/\n/g' -e 's/:$//g' \
11+
|egrep -v ':(Preboot|Recovery|VM)$'

src/get-computer-id.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
3+
comp_name=`cat /sys/class/dmi/id/product_name |sed -e 's/[ \t]*$//' -e 's/ /_/g'`
4+
local_ip=`ifconfig -a |grep -v 127.0.0.1 |grep 'inet ' |awk '{ print $2 }' |head -n 1`
5+
6+
logger "computer name $comp_name, primary IP $local_ip"
7+
8+
echo ${local_ip}_${comp_name}

src/get-drive-encryption-keys.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/sh
2+
3+
encryption_mode=$1
4+
keys_directory=$2
5+
drive_serial=$3
6+
7+
8+
# 1. Try to find key(s) assigned to given drive serial number:
9+
# - preconfigured manually
10+
# - found during previous Drive Badger runs
11+
# 2. Print generic keys, preconfigured using configuration repositories
12+
13+
file=""
14+
if [ "$keys_directory" != "" ] && [ "$drive_serial" != "" ]; then
15+
file=$keys_directory/$drive_serial.$encryption_mode
16+
fi
17+
18+
if [ "$file" != "" ] && [ -s $file ]; then
19+
cat $file
20+
cat /opt/drivebadger/config/*/$encryption_mode.keys 2>/dev/null |grep -v "^#" |grep -v ^$ |grep -vxFf $file
21+
else
22+
cat /opt/drivebadger/config/*/$encryption_mode.keys 2>/dev/null |grep -v "^#" |grep -v ^$
23+
fi

src/get-drive-serial.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
3+
drive=$1
4+
directory=$2
5+
6+
if [ ! -f $directory/$drive.txt ]; then
7+
if [[ $drive =~ ^mmc ]]; then
8+
udevadm info -a /dev/$drive >$directory/$drive.txt
9+
elif [[ $drive =~ ^nvme ]]; then
10+
nvme id-ctrl /dev/$drive >$directory/$drive.txt
11+
else
12+
hdparm -I /dev/$drive >$directory/$drive.txt
13+
fi
14+
fi
15+
16+
if [[ $drive =~ ^mmc ]]; then
17+
grep 'ATTRS{serial}' $directory/$drive.txt |tr -d ' \t\"' |tr '=' ' ' |awk '{ print $2 }'
18+
elif [[ $drive =~ ^nvme ]]; then
19+
cat $directory/$drive.txt |tr -d ' \t' |grep ^sn: |cut -d':' -f2
20+
elif grep -q "Serial Number" $directory/$drive.txt; then
21+
cat $directory/$drive.txt |tr -d ' \t' |grep ^SerialNumber: |cut -d':' -f2
22+
else
23+
ls -l /dev/disk/by-id/* |grep /$drive$ |grep -v wwn- |cut -d'/' -f5 |cut -d'-' -f2- |sed -e "s/ -> ..//g" -e "s/-0:0//g"
24+
fi

src/get-injector-script.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/sh
2+
3+
# The below logic is responsible for selecting, which script (only one!)
4+
# will be executed against the drive mounted in read/write mode.
5+
6+
fs=$1
7+
drive_serial=$2
8+
uuid=$3
9+
10+
11+
#
12+
# Match exact partition using its UUID.
13+
#
14+
# Downside: Bitlocker and VeraCrypt partitions can't be handled this way.
15+
#
16+
if [ "$uuid" != "" ] && [ -x /opt/drivebadger/injectors/uuid-$uuid/injector.sh ]; then
17+
echo /opt/drivebadger/injectors/uuid-$uuid/injector.sh
18+
19+
#
20+
# Match the drive and partition type, but not the exact partition - so this
21+
# injector will be run against all partitions of given type (eg. NTFS).
22+
#
23+
# You are responsible for implementing checks eg. for specific directory,
24+
# or any other condition(s) unique to the exact filesystem, that you're
25+
# trying to match.
26+
#
27+
elif [ "$drive_serial" != "" ] && [ -x /opt/drivebadger/injectors/$fs-$drive_serial/injector.sh ]; then
28+
echo /opt/drivebadger/injectors/$fs-$drive_serial/injector.sh
29+
30+
#
31+
# Run this injector for all partitions of given type (eg. NTFS),
32+
# that were not matched by any specific injector.
33+
#
34+
elif [ -x /opt/drivebadger/injectors/$fs/injector.sh ]; then
35+
echo /opt/drivebadger/injectors/$fs/injector.sh
36+
fi

src/get-partition-drive.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
partition=$1
4+
5+
if [[ $partition =~ ^dm ]] || [[ $partition =~ ^md ]]; then
6+
echo $partition # is it enough to properly support various RAID types?
7+
elif [[ $partition =~ ^mmc ]] || [[ $partition =~ ^nvme ]]; then
8+
echo $partition |cut -dp -f1
9+
else
10+
echo $partition |sed 's/[0-9]//g' # sdx or sdxy
11+
fi

src/get-partition-fs-type.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
3+
partition=$1
4+
directory=$2
5+
6+
if [ ! -f $directory/$partition.txt ]; then
7+
udevadm info --query=all --name=/dev/$partition >$directory/$partition.txt
8+
fi
9+
10+
grep ID_FS_TYPE $directory/$partition.txt |cut -d'=' -f2

src/get-ptp-device-name.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/sh
2+
3+
PORT=$1
4+
FILE=$2
5+
6+
gphoto2 --summary --port $PORT 2>/dev/null >$FILE
7+
8+
# data transfer from other PTP device in progress
9+
if grep -q '^For debugging messages' $FILE; then
10+
rm -f $FILE
11+
exit 0
12+
fi
13+
14+
if grep -q ^Manufacturer: $FILE; then
15+
vendor=`grep ^Manufacturer: $FILE |cut -d: -f2 |tr -d '[:space:]'`
16+
model=`grep ^Model: $FILE |cut -d: -f2 |tr -d '[:space:]'`
17+
serial=`grep 'Serial Number:' $FILE |cut -d: -f2 |tr -d '[:space:]'`
18+
echo "${vendor}_${model}_${serial}_PTP" |tr ',' '-' |tr -d '.()'
19+
else
20+
gphoto2 --abilities --port $PORT 2>/dev/null |grep -v MTP |grep ^Abilities |cut -d: -f2 |sed -e 's/^[[:space:]]*//' -e 's/ /_/g'
21+
fi

src/get-raw-device.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/sh
2+
3+
device=$1
4+
5+
if [ "$device" != "" ]; then
6+
dm=`readlink $device`
7+
8+
if [ "$dm" != "" ]; then # eg. /dev/mapper/sdb3 -> /dev/dm-0 -> echo dm-0
9+
basename $dm
10+
else # /dev/sdb3 -> echo sdb3
11+
basename $device
12+
fi
13+
fi

src/get-rsync-exclusion-lists.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/sh
2+
3+
for list in `ls /opt/drivebadger/config/*/exclude.list`; do
4+
echo -n " --exclude-from=$list"
5+
done
6+
7+
for script in `ls /opt/drivebadger/hooks/*/exclude.sh 2>/dev/null`; do
8+
$script $1
9+
done

src/get-udev-unrecognized-devices.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/sh
2+
3+
recognized=`ls -l /dev/disk/by-uuid |grep -v ^total |rev |cut -d'/' -f1 |rev |sort |tr '\n' '|' |sed 's/.$//'`
4+
5+
# 1. lsblk shows all drives and partitions, including not mounted
6+
# - basically all /dev/sd* and /dev/nvme* devices + some other, not
7+
# interesting entries (loop devices, LUKS encrypted volumes etc.)
8+
#
9+
# 2. grep " 0 part" leaves just partitions (UUID and non-UUID ones)
10+
#
11+
# 3. now filter out all partitions smaller than 1G - the intention
12+
# is to remove Microsoft Reserved Partitions, that look like
13+
# possibly encrypted, and trigger VeraCrypt key search, while
14+
# not having any useful data
15+
#
16+
# https://en.wikipedia.org/wiki/Microsoft_Reserved_Partition
17+
#
18+
# this possibly removes also other (UUID) partitions smaller than 1G,
19+
# but at this stage we are interested only in non-UUID ones, while
20+
# UUID partitions are already processed, and are removed below
21+
22+
lsblk |grep " 0 part" |egrep -v "(K|M) 0 part" |tr -d '─├└' |cut -d' ' -f1 |egrep -v "($recognized)"
23+
24+
25+
# TODO: filter out LUKS-encrypted Linux swap partitions:
26+
# https://wiki.archlinux.org/title/Dm-crypt/Swap_encryption
27+
#
28+
# these are tricky, since:
29+
# - they can have any size
30+
# - they HAVE UUID while initialized and attached as swap at current system run
31+
# - they have different UUID after each reboot
32+
# - they no longer have UUID (and are totally unrecognized) after removing
33+
# them from /etc/crypttab and reboot

src/list-mtp-devices.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
3+
jmtpfs -l 2>/dev/null |grep -v ^Available |grep -v ^Device |sort |uniq |while read line; do
4+
BUS=`echo "$line" |cut -d, -f1 |tr -d '[:space:]'`
5+
DEV=`echo "$line" |cut -d, -f2 |tr -d '[:space:]'`
6+
7+
vendor=`echo "$line" |cut -d, -f6 |tr -d '[:space:]'`
8+
model=`echo "$line" |cut -d, -f5 |cut -d'(' -f1 |sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]$//' -e 's/ /_/g'`
9+
10+
echo "${vendor}_${model}_MTP:$BUS,$DEV"
11+
done

src/list-ptp-ports.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
gphoto2 --auto-detect |grep usb: |cut -d: -f2 |sed -e "s/^/usb:/"

src/luks.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/bin/sh
2+
3+
target_root_directory=$1
4+
target_directory=$2
5+
keys_directory=$3
6+
drive_serial=$4
7+
current_partition=$5
8+
uuid=$6
9+
10+
11+
logger "attempting to decrypt LUKS encrypted partition $current_partition"
12+
mountpoint=/media/$current_partition/mnt
13+
subtarget=$target_directory/$drive_serial/${current_partition}_luks
14+
mkdir -p $mountpoint $subtarget
15+
16+
injector=`/opt/drivebadger/internal/generic/get-injector-script.sh luks $drive_serial $uuid`
17+
18+
for recovery_key in `/opt/drivebadger/internal/generic/keys/get-drive-encryption-keys.sh luks $keys_directory $drive_serial`; do
19+
echo "$recovery_key" |cryptsetup -q luksOpen /dev/$current_partition luks_$current_partition 2>>$subtarget/rsync.err
20+
if [ -e /dev/mapper/luks_$current_partition ]; then
21+
22+
echo $recovery_key >$subtarget/luks.key
23+
/opt/drivebadger/internal/generic/keys/save-drive-encryption-key.sh luks $keys_directory $drive_serial $recovery_key
24+
25+
mount -o ro /dev/mapper/luks_$current_partition $mountpoint >>$subtarget/rsync.log 2>>$subtarget/rsync.err
26+
/opt/drivebadger/internal/generic/process-hooks.sh $mountpoint $target_root_directory
27+
28+
logger "copying UUID=$uuid (partition $current_partition filesystem LUKS, mounted as $mountpoint, target directory $subtarget)"
29+
/opt/drivebadger/internal/generic/rsync-partition.sh $mountpoint $subtarget >>$subtarget/rsync.log 2>>$subtarget/rsync.err
30+
umount $mountpoint
31+
logger "copied UUID=$uuid"
32+
33+
if [ "$injector" != "" ]; then
34+
logger "attempting to inject UUID=$uuid (partition $current_partition filesystem LUKS, mounted as $mountpoint, injector $injector)"
35+
if mount -o rw /dev/mapper/luks_$current_partition $mountpoint >>$subtarget/injector.log 2>>$subtarget/injector.err; then
36+
$injector $mountpoint >>$subtarget/injector.log 2>>$subtarget/injector.err
37+
umount $mountpoint
38+
logger "injected UUID=$uuid"
39+
fi
40+
fi
41+
42+
cryptsetup luksClose luks_$current_partition
43+
break
44+
fi
45+
done

src/mount-apfs-filesystem.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/sh
2+
3+
keys_directory=$1
4+
drive_serial=$2
5+
device=$3
6+
slice=$4
7+
mountpoint=$5
8+
subtarget=$6
9+
10+
if fsapfsmount -f $slice $device $mountpoint; then
11+
exit 0
12+
fi
13+
14+
for recovery_key in `/opt/drivebadger/internal/generic/keys/get-drive-encryption-keys.sh apfs $keys_directory $drive_serial`; do
15+
if fsapfsmount -f $slice -p$recovery_key $device $mountpoint; then
16+
echo $recovery_key >$subtarget/apfs.key
17+
/opt/drivebadger/internal/generic/keys/save-drive-encryption-key.sh apfs $keys_directory $drive_serial $recovery_key
18+
exit 0
19+
fi
20+
done
21+
22+
exit 1

src/plain.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/sh
2+
3+
target_root_directory=$1
4+
target_directory=$2
5+
keys_directory=$3
6+
drive_serial=$4
7+
current_partition=$5
8+
uuid=$6
9+
fs=$7
10+
11+
12+
mountpoint=/media/$current_partition/mnt
13+
subtarget=$target_directory/$drive_serial/${current_partition}_${fs}
14+
mkdir -p $mountpoint $subtarget
15+
16+
injector=`/opt/drivebadger/internal/generic/get-injector-script.sh $fs $drive_serial $uuid`
17+
18+
if mount -t $fs -o ro /dev/$current_partition $mountpoint >>$subtarget/rsync.log 2>>$subtarget/rsync.err; then
19+
/opt/drivebadger/internal/generic/process-hooks.sh $mountpoint $target_root_directory
20+
21+
logger "copying UUID=$uuid (partition $current_partition filesystem $fs, mounted as $mountpoint, target directory $subtarget)"
22+
/opt/drivebadger/internal/generic/rsync-partition.sh $mountpoint $subtarget >>$subtarget/rsync.log 2>>$subtarget/rsync.err
23+
logger "copied UUID=$uuid"
24+
25+
if [ "$injector" != "" ]; then
26+
logger "attempting to inject UUID=$uuid (partition $current_partition filesystem $fs, mounted as $mountpoint, injector $injector)"
27+
if mount -t $fs -o remount,rw /dev/$current_partition $mountpoint >>$subtarget/injector.log 2>>$subtarget/injector.err; then
28+
$injector $mountpoint >>$subtarget/injector.log 2>>$subtarget/injector.err
29+
logger "injected UUID=$uuid"
30+
fi
31+
fi
32+
33+
# hook-virtual uses task spooler to queue exfiltration of VHD/VHDX containers stored
34+
# on plain NTFS, on Hyper-V servers - if such tasks for this particular partition are
35+
# in progress (or still queued), do not unmount, since it would make these containers
36+
# inaccessible.
37+
#
38+
# if this is not Hyper-V (or any other NTFS), or no VHD/VHDX containers were found,
39+
# then simply unmount and forget.
40+
#
41+
if [ "`ps aux |grep \"tsp /opt/drivebadger\" |grep $mountpoint`" = "" ]; then
42+
umount $mountpoint
43+
fi
44+
else
45+
logger "error mounting UUID=$uuid (partition $current_partition filesystem $fs, attempted mount as $mountpoint, target directory $subtarget)"
46+
fi

0 commit comments

Comments
 (0)