Skip to content

Commit

Permalink
Prevent auto exit and log all output to a file (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
KnugiHK committed Feb 25, 2025
1 parent d3eb939 commit 006e212
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 19 deletions.
38 changes: 32 additions & 6 deletions WhatsAppKeyDBExtract.bat
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

REM Create log file with timestamp
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /value') do set datetime=%%I
set timestamp=%datetime:~0,8%_%datetime:~8,6%
set log_file=WhatsApp_Extract_%timestamp%.log

REM Redirect output to both console and log file
call :LOG > "%log_file%" 2>&1
exit /b

:LOG
color 0a
title WhatsApp Key/DB Extractor 5.4 (Official)
title WhatsApp Key/DB Extractor 5.4

:cleanup_and_exit
bin\adb.exe kill-server
if "%~1"=="0" (
echo [SUCCESS] Script completed successfully!
) else (
echo [FAILED] Script failed with error code: %~1
)
echo.
echo Log file saved as: %log_file%
echo Press Enter to close this window...
pause >nul
exit /b %~1


echo.
echo =========================================================================
echo = This script will extract the WhatsApp Key file and DB on Android 4.0+ =
Expand All @@ -22,7 +48,7 @@ echo Exiting ...
echo.
bin\adb.exe kill-server
pause
exit
call :cleanup_and_exit 1
)
echo Please connect your Android device with USB Debugging enabled:
echo.
Expand All @@ -43,7 +69,7 @@ echo Exiting ...
echo.
bin\adb.exe kill-server
pause
exit
call :cleanup_and_exit 1
)
bin\adb.exe shell pm path com.whatsapp | bin\grep.exe package > tmp\wapath.txt
bin\adb.exe shell "echo $EXTERNAL_STORAGE" > tmp\sdpath.txt
Expand Down Expand Up @@ -79,7 +105,7 @@ if not exist tmp\LegacyWhatsApp.apk (
echo Downloading legacy WhatsApp 2.11.431 to local folder
bin\curl.exe -o tmp\LegacyWhatsApp.apk %apkfurl%
set exitcode=%ErrorLevel%
if %exitcode% neq 0 (echo "[FATAL] Curl failed with %exitcode%." && exit %exitcode%)
if %exitcode% neq 0 (echo "[FATAL] Curl failed with %exitcode%." && call :cleanup_and_exit %exitcode%)
) else (
echo Found legacy WhatsApp 2.11.431 in local folder
)
Expand Down Expand Up @@ -124,7 +150,7 @@ java -jar bin\abe.jar unpack tmp\whatsapp.ab tmp\whatsapp.tar
java -jar bin\abe.jar unpack tmp\whatsapp.ab tmp\whatsapp.tar "!password!"
)
set exitcode=%ErrorLevel%
if %exitcode% neq 0 (echo "[FATAL] ABE failed with %exitcode%." && exit %exitcode%)
if %exitcode% neq 0 (echo "[FATAL] ABE failed with %exitcode%." && call :cleanup_and_exit %exitcode%)
bin\tar.exe xvf tmp\whatsapp.tar -C tmp\ apps/com.whatsapp/f/key
bin\tar.exe xvf tmp\whatsapp.tar -C tmp\ apps/com.whatsapp/f/encrypted_backup.key
bin\tar.exe xvf tmp\whatsapp.tar -C tmp\ apps/com.whatsapp/db/msgstore.db
Expand Down Expand Up @@ -242,4 +268,4 @@ echo Operation complete
echo.
bin\adb.exe kill-server
pause
exit
call :cleanup_and_exit 0
44 changes: 34 additions & 10 deletions WhatsAppKeyDBExtract.ps1
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
$Host.UI.RawUI.WindowTitle = "WhatsApp Key/DB Extractor 5.4 (Official)"
Function TerminateWithReason([String] $reason)
{
"`r`n$reason`r`n`r`nExiting...`r`n"
Invoke-Expression "bin\adb.exe kill-server"
&cmd /c pause
exit
$Host.UI.RawUI.WindowTitle = "WhatsApp Key/DB Extractor 5.4"
# Create log file with timestamp
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
$log_file = "WhatsApp_Extract_${timestamp}.log"

# Start transcript for logging
Start-Transcript -Path $log_file -Append

Function CleanupAndExit {
param (
[int]$exitCode
)

Invoke-Expression "bin\adb.exe kill-server"

if ($exitCode -eq 0) {
Write-Host "`n[SUCCESS] Script completed successfully!"
} else {
Write-Host "`n[FAILED] Script failed with error code: $exitCode"
}

Write-Host "`nLog file saved as: $log_file"
Write-Host "Press Enter to close this window..."
Stop-Transcript
Read-Host
exit $exitCode
}

Function TerminateWithReason([String] $reason) {
Write-Host "`n$reason`n`nExiting...`n"
CleanupAndExit 1
}
"`r`n========================================================================="
"= This script will extract the WhatsApp Key file and DB on Android 4.0+ ="
Expand Down Expand Up @@ -62,7 +86,7 @@ Invoke-Expression "bin\curl.exe -o tmp\LegacyWhatsApp.apk $apkfurl"
""
if ($LASTEXITCODE -ne 0){
"[FATAL] Curl failed with $LASTEXITCODE."
exit $LASTEXITCODE
CleanupAndExit $LASTEXITCODE
}
}
If ($sdkver -ge 11)
Expand Down Expand Up @@ -110,7 +134,7 @@ Invoke-Expression "java -jar bin\abe.jar unpack tmp\whatsapp.ab tmp\whatsapp.tar
}
if ($LASTEXITCODE -ne 0){
"[FATAL] ABE failed with $LASTEXITCODE."
exit $LASTEXITCODE
CleanupAndExit $LASTEXITCODE
}
}
""
Expand Down Expand Up @@ -195,4 +219,4 @@ Remove-Item tmp\$apkname
"`r`nOperation complete`r`n"
Invoke-Expression "bin\adb.exe kill-server"
&cmd /c pause
exit
CleanupAndExit 0
25 changes: 22 additions & 3 deletions WhatsAppKeyDBExtract.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@ is_tar=1
[[ -z $(which tar) ]] && { is_tar=0; }
is_tr=1
[[ -z $(which tr) ]] && { is_tr=0; }
timestamp=$(date '+%Y%m%d_%H%M%S')
log_file="WhatsApp_Extract_${timestamp}.log"
exec 1> >(tee -a "$log_file")
exec 2> >(tee -a "$log_file" >&2)

cleanup_and_exit() {
local exit_code=$1
adb kill-server
if [ $exit_code -eq 0 ]; then
echo -e "\n[SUCCESS] Script completed successfully!"
else
echo -e "\n[FAILED] Script failed with error code: $exit_code"
fi
echo -e "\nLog file saved as: $log_file"
echo -e "Press Enter to close this window..."
read
exit $exit_code
}

trap 'cleanup_and_exit 1' INT TERM


echo -e "
=========================================================================
Expand Down Expand Up @@ -142,6 +163,4 @@ echo -e "Done\n\nOperation complete\n"
fi
fi
fi
adb kill-server
read -p "Please press Enter to quit..."
exit 0
cleanup_and_exit 0

0 comments on commit 006e212

Please sign in to comment.