cyberchallenge-modem/build-tgr.sh
2020-09-21 01:22:08 +02:00

81 lines
2.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# for dependencies
# apt install -t build-essential libncurses-dev bison flex libssl-dev libelf-dev
#if [ $# -ne 3 ]
# then
# echo "No arguments supplied"
#fi
#flag1=$1
#flag2=$2
#flag3=$3
flag1="cc{prima_flag}"
flag2="cc{seconda_flag}"
flag3="cc{terza_flag}"
password=`hexdump -n 32 -e '4/4 "%08X"' /dev/urandom`
echo "[+] Starting build script..."
mkdir -p target/overlay/var/www/html
echo "[+] Cloning buildroot"
git clone https://github.com/buildroot/buildroot.git target/buildroot
echo "[+] Adding customization files"
cp -R buildroot/* target/buildroot # copy buildroot configs
sed -i "s/###ROOTPASSWORD###/$password/g" target/buildroot/configs/tiesse_tgr_defconfig
cp -R conf/* target/overlay # copy target system config files
cp -R webpanel/* target/overlay/var/www/html # copy the webpanel
cp -R update/update.sh target/overlay #copy the update script and certificate
echo "[+] Adding firmware"
mkdir -p target/overlay/lib
cp -R firmware target/overlay/lib/ # adding binary firmware for wifi driver
echo "[+] Writing flags"
sed -i "s/##FLAG1##/$flag1/g" target/overlay/var/www/html/includes/config.php
echo $flag2 > target/overlay/flag
chmod 444 target/overlay/flag
mkdir target/overlay/root
chmod 700 target/overlay/root
echo $flag3 > target/overlay/root/flag
chmod 400 target/overlay/root/flag
echo "[+] Building the keygen"
mkdir -p target/overlay/usr/sbin
aarch64-linux-gnu-gcc -o target/overlay/usr/sbin/cfgbin keygen/keygen.c -static -lm
aarch64-linux-gnu-strip target/overlay/usr/sbin/cfgbin
echo "[+] Generating Update Key"
mkdir -p target/keys
openssl genrsa -out target/keys/signingkey.pem 2048
openssl rsa -in target/keys/signingkey.pem -outform PEM -pubout -out target/keys/signingpub.pem
cp target/keys/signingpub.pem target/overlay/pub.pem
echo "[+] Generating sample update package"
mkdir -p home/update
echo "sample update" > home/update/sample.txt
tar -cvzf update.tgz home
openssl dgst -sha256 -sign target/keys/signingkey.pem -out update.tgz.sig update.tgz
cat update.tgz.sig > update.tgz.cc
cat update.tgz >> update.tgz.cc
mv update.tgz.cc target/overlay
rm -rf home update.tgz update.tgz.sig
echo "[+] Generating Monitoring SSH Key"
ssh-keygen -t ecdsa -f target/keys/sshkey -q -N ""
mkdir -p target/overlay/root/.ssh
cp target/keys/sshkey target/overlay/root/.ssh/authorized_keys
chmod -R 600 target/overlay/root/.ssh/
echo "[+] Saving the root password"
echo $password > target/keys/rootpassword
echo "[+] Building the image"
N=`grep -c '^processor' /proc/cpuinfo`
cd target/buildroot
make tiesse_tgr_defconfig
make -j$N