144 lines
6.8 KiB
PowerShell
144 lines
6.8 KiB
PowerShell
# decompile_kaiyu.ps1 - Download jadx, pull KAIYU APK, decompile to Java source
|
|
#
|
|
# Run from D:\HereMyHope\hh-bt-controller:
|
|
# powershell -ExecutionPolicy Bypass -File sniffer/decompile_kaiyu.ps1
|
|
#
|
|
# Automatically picks jadx version based on your Java:
|
|
# Java 8 -> jadx 1.2.0 (last version supporting Java 8)
|
|
# Java 11+ -> jadx 1.5.0
|
|
|
|
$BaseDir = "D:\Claude\HH-BT-Controller\decompile"
|
|
$JadxDir = "$BaseDir\jadx"
|
|
$JadxZip = "$BaseDir\jadx.zip"
|
|
$ApkPath = "$BaseDir\kaiyu.apk"
|
|
$OutputDir = "$BaseDir\kaiyu_src"
|
|
$Adb = if (Test-Path "D:\platform-tools\adb.exe") { "D:\platform-tools\adb.exe" } else { "adb" }
|
|
$Pkg = "com.qunyu.kaiyu"
|
|
|
|
New-Item -ItemType Directory -Force -Path $BaseDir | Out-Null
|
|
|
|
Write-Host "============================================" -ForegroundColor Cyan
|
|
Write-Host " KAIYU APK Decompiler" -ForegroundColor Cyan
|
|
Write-Host "============================================" -ForegroundColor Cyan
|
|
|
|
# ── Detect Java version ───────────────────────────────────────────────────────
|
|
Write-Host "`nDetecting Java version..." -ForegroundColor Gray
|
|
$javaVer = 8
|
|
try {
|
|
$javaOut = & java -version 2>&1 | Select-Object -First 1
|
|
Write-Host " $javaOut" -ForegroundColor Gray
|
|
if ($javaOut -match '"(\d+)[\._]') {
|
|
$major = [int]$Matches[1]
|
|
# Java 8 reports as "1.8.x", Java 11+ reports as "11.x"
|
|
$javaVer = if ($major -eq 1) {
|
|
[int]($javaOut -replace '.*"1\.(\d+).*','$1')
|
|
} else { $major }
|
|
}
|
|
} catch { Write-Host " Could not detect Java version, assuming Java 8" -ForegroundColor Yellow }
|
|
|
|
Write-Host " Java major version: $javaVer" -ForegroundColor Gray
|
|
|
|
# Pick jadx version accordingly
|
|
if ($javaVer -ge 11) {
|
|
$jadxVersion = "1.5.0"
|
|
$jadxUrl = "https://github.com/skylot/jadx/releases/download/v1.5.0/jadx-1.5.0.zip"
|
|
Write-Host " Using jadx 1.5.0 (Java 11+ detected)" -ForegroundColor Gray
|
|
} else {
|
|
$jadxVersion = "1.2.0"
|
|
$jadxUrl = "https://github.com/skylot/jadx/releases/download/v1.2.0/jadx-1.2.0.zip"
|
|
Write-Host " Using jadx 1.2.0 (Java 8 detected - last version supporting Java 8)" -ForegroundColor Yellow
|
|
}
|
|
|
|
# ── 1. Download jadx ──────────────────────────────────────────────────────────
|
|
$jadxBat = Get-ChildItem -Path $JadxDir -Recurse -Filter "jadx.bat" -ErrorAction SilentlyContinue |
|
|
Select-Object -First 1 -ExpandProperty FullName
|
|
|
|
if (-not $jadxBat) {
|
|
Write-Host "`n[1/4] Downloading jadx $jadxVersion..." -ForegroundColor Yellow
|
|
try {
|
|
Invoke-WebRequest -Uri $jadxUrl -OutFile $JadxZip -UseBasicParsing
|
|
$sizeMB = [math]::Round((Get-Item $JadxZip).Length / 1MB, 1)
|
|
Write-Host " Downloaded ($sizeMB MB)"
|
|
Write-Host " Extracting..."
|
|
Expand-Archive -Path $JadxZip -DestinationPath $JadxDir -Force
|
|
Remove-Item $JadxZip -Force
|
|
$jadxBat = Get-ChildItem -Path $JadxDir -Recurse -Filter "jadx.bat" |
|
|
Select-Object -First 1 -ExpandProperty FullName
|
|
Write-Host " jadx ready: $jadxBat" -ForegroundColor Green
|
|
} catch {
|
|
Write-Host "[ERROR] Download failed: $_" -ForegroundColor Red
|
|
Write-Host " Download manually: $jadxUrl"
|
|
Write-Host " Extract to: $JadxDir"
|
|
Read-Host; exit 1
|
|
}
|
|
} else {
|
|
Write-Host "`n[1/4] jadx already present: $jadxBat" -ForegroundColor Green
|
|
}
|
|
|
|
if (-not $jadxBat) {
|
|
Write-Host "[ERROR] jadx.bat not found after extraction." -ForegroundColor Red
|
|
Read-Host; exit 1
|
|
}
|
|
|
|
# ── 2. Pull APK ───────────────────────────────────────────────────────────────
|
|
if (-not (Test-Path $ApkPath) -or (Get-Item $ApkPath).Length -lt 1MB) {
|
|
Write-Host "`n[2/4] Pulling APK from phone..." -ForegroundColor Yellow
|
|
$pmOut = & $Adb shell "pm path $Pkg" 2>&1
|
|
if ($pmOut -notmatch "package:") {
|
|
Write-Host "[ERROR] KAIYU not found on device." -ForegroundColor Red
|
|
& $Adb devices; Read-Host; exit 1
|
|
}
|
|
$apkOnDevice = ($pmOut -split "`n" | Where-Object { $_ -match "package:" } |
|
|
Select-Object -First 1).Replace("package:", "").Trim()
|
|
Write-Host " Device path: $apkOnDevice"
|
|
& $Adb pull $apkOnDevice $ApkPath
|
|
if (-not (Test-Path $ApkPath) -or (Get-Item $ApkPath).Length -lt 1MB) {
|
|
& $Adb shell "cp '$apkOnDevice' /sdcard/kaiyu_tmp.apk"
|
|
& $Adb pull /sdcard/kaiyu_tmp.apk $ApkPath
|
|
& $Adb shell "rm /sdcard/kaiyu_tmp.apk" 2>&1 | Out-Null
|
|
}
|
|
if (-not (Test-Path $ApkPath)) {
|
|
Write-Host "[ERROR] APK pull failed." -ForegroundColor Red; Read-Host; exit 1
|
|
}
|
|
Write-Host " APK: $([math]::Round((Get-Item $ApkPath).Length/1MB,1)) MB" -ForegroundColor Green
|
|
} else {
|
|
Write-Host "`n[2/4] APK already pulled ($([math]::Round((Get-Item $ApkPath).Length/1MB,1)) MB) -- skipping." -ForegroundColor Green
|
|
}
|
|
|
|
# ── 3. Decompile ──────────────────────────────────────────────────────────────
|
|
Write-Host "`n[3/4] Decompiling with jadx $jadxVersion (1-3 min)..." -ForegroundColor Yellow
|
|
if (Test-Path $OutputDir) {
|
|
Write-Host " Removing previous output..."
|
|
Remove-Item -Recurse -Force $OutputDir
|
|
}
|
|
|
|
& $jadxBat --deobf --show-bad-code -d $OutputDir $ApkPath
|
|
|
|
$count = (Get-ChildItem -Recurse -Filter "*.java" -Path $OutputDir -ErrorAction SilentlyContinue).Count
|
|
if ($count -eq 0) {
|
|
Write-Host "[ERROR] No Java files produced. Check jadx output above." -ForegroundColor Red
|
|
Read-Host; exit 1
|
|
}
|
|
Write-Host " $count Java files decompiled." -ForegroundColor Green
|
|
|
|
# ── 4. Analyse ────────────────────────────────────────────────────────────────
|
|
Write-Host "`n[4/4] Analysing for BLE protocol..." -ForegroundColor Yellow
|
|
|
|
$analysePy = @(
|
|
"sniffer\analyse_jadx.py",
|
|
"D:\Claude\HH-BT-Controller\sniffer\analyse_jadx.py"
|
|
) | Where-Object { Test-Path $_ } | Select-Object -First 1
|
|
|
|
if ($analysePy) {
|
|
python $analysePy --src $OutputDir
|
|
} else {
|
|
Write-Host " analyse_jadx.py not found, run manually:"
|
|
Write-Host " python sniffer\analyse_jadx.py --src `"$OutputDir`""
|
|
}
|
|
|
|
Write-Host "`n============================================" -ForegroundColor Green
|
|
Write-Host " Done! Source at: $OutputDir" -ForegroundColor Green
|
|
Write-Host " Open in VS Code: code `"$OutputDir`"" -ForegroundColor Green
|
|
Write-Host "============================================" -ForegroundColor Green
|
|
Read-Host "`nPress Enter to exit"
|