#! /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 [ $# -ne 1 ]; then
    echo "[!] Error: 'restauration.sh' script expects exactly 1 argument, but received $#."
    echo "[!] Usage: $0 <Target IP Address>"
    exit 1
fi

ROOT_USERNAME=root
ROOT_PASSWORD=root
SENSOR_WEBSERVER_PORT=5000
CURRENT_DATE=$(date +"%m%d%H%M%Y")
target_ip_address=$1

sleep 1
echo "[+] Reboot the target system to remove Mirai."
echo "[+] IMPORTANT: Make sure to reboot all other infected devices simultaneously to fully eliminate Mirai from the network."
curl http://$target_ip_address:$SENSOR_WEBSERVER_PORT/reboot
if [ $? -ne 0 ]; then 
    echo "[!] Error: Failed reboot the target system."
    exit 1
fi

echo ""
echo "[+] Waiting for the target system to come back online..."
sleep 15
while ! nc -z -w 2 $target_ip_address $SENSOR_WEBSERVER_PORT; do
    echo "[+] Target still unreachable, retrying in 5 seconds."
    sleep 5
done

echo "[+] Target system is back online. Proceeding to update the system time."
sshpass -p "$ROOT_PASSWORD" ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR "$ROOT_USERNAME"@"$target_ip_address" date "$CURRENT_DATE"
if [ $? -ne 0 ]; then 
    echo "[!] Error: Failed to update the system time."
    exit 1
fi

echo "[+] System time successfully updated to $CURRENT_DATE."
echo "[+] Restoration process complete."

