start_auto_dismiss.ps1 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. param(
  2. [string]$Package = "com.jihuanshe",
  3. [int]$DelayMs = 12000,
  4. [string]$Adb = "D:\platform-tools\adb.exe",
  5. [string]$Frida = "C:\Python\Python312\Scripts\frida.exe",
  6. [string]$Hook = "",
  7. [switch]$NoRestart,
  8. [switch]$Foreground
  9. )
  10. # Usage:
  11. # .\start_auto_dismiss.ps1 # restart App -> wait gadget -> attach dismiss hook (background)
  12. # .\start_auto_dismiss.ps1 -NoRestart # App already running, attach directly
  13. # .\start_auto_dismiss.ps1 -Foreground # foreground mode, show live log
  14. #
  15. # Stop: .\stop_auto_dismiss.ps1
  16. $ErrorActionPreference = "Stop"
  17. $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
  18. $StateFile = Join-Path $ScriptDir "auto_dismiss_bg_state.json"
  19. $StdoutLog = Join-Path $ScriptDir "auto_dismiss_stdout.log"
  20. $StderrLog = Join-Path $ScriptDir "auto_dismiss_stderr.log"
  21. if ([string]::IsNullOrWhiteSpace($Hook)) {
  22. $Hook = Join-Path $ScriptDir "auto_dismiss_update.js"
  23. }
  24. if (-not (Test-Path $Adb)) { throw "adb not found: $Adb" }
  25. if (-not (Test-Path $Frida)) { throw "frida not found: $Frida" }
  26. if (-not (Test-Path $Hook)) { throw "hook script not found: $Hook" }
  27. # kill previous background frida if any
  28. if (Test-Path $StateFile) {
  29. try {
  30. $oldState = Get-Content $StateFile -Raw | ConvertFrom-Json
  31. if ($oldState.pid) {
  32. $oldProc = Get-Process -Id ([int]$oldState.pid) -ErrorAction SilentlyContinue
  33. if ($oldProc) {
  34. Write-Host "[cleanup] stopping previous frida pid=$($oldState.pid)"
  35. Stop-Process -Id ([int]$oldState.pid) -Force
  36. Start-Sleep -Milliseconds 500
  37. }
  38. }
  39. } catch {
  40. Write-Host "[cleanup] ignore invalid state file"
  41. }
  42. }
  43. if (-not $NoRestart) {
  44. Write-Host "[1/4] force-stop $Package"
  45. & $Adb shell "su -c 'am force-stop $Package'" | Out-Host
  46. Start-Sleep -Milliseconds 800
  47. Write-Host "[2/4] launch $Package"
  48. & $Adb shell "su -c 'monkey -p $Package -c android.intent.category.LAUNCHER 1'" | Out-Host
  49. Write-Host "[3/4] wait $DelayMs ms for ZygiskFrida gadget injection"
  50. Start-Sleep -Milliseconds $DelayMs
  51. } else {
  52. Write-Host "[1/4] skip restart"
  53. Write-Host "[2/4] skip launch"
  54. Write-Host "[3/4] skip wait"
  55. }
  56. if (Test-Path $StdoutLog) { Remove-Item $StdoutLog -Force }
  57. if (Test-Path $StderrLog) { Remove-Item $StderrLog -Force }
  58. $argList = @(
  59. "-U",
  60. "-N", $Package,
  61. "-l", $Hook,
  62. "-q"
  63. )
  64. if ($Foreground) {
  65. Write-Host "[4/4] start frida in FOREGROUND (Ctrl+C to stop)"
  66. & $Frida @argList
  67. return
  68. }
  69. Write-Host "[4/4] start frida in background"
  70. $proc = Start-Process -FilePath $Frida `
  71. -ArgumentList $argList `
  72. -RedirectStandardOutput $StdoutLog `
  73. -RedirectStandardError $StderrLog `
  74. -PassThru `
  75. -WindowStyle Hidden
  76. $state = [ordered]@{
  77. package = $Package
  78. pid = $proc.Id
  79. delay_ms = $DelayMs
  80. hook = $Hook
  81. started_at = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
  82. stdout_log = $StdoutLog
  83. stderr_log = $StderrLog
  84. no_restart = [bool]$NoRestart
  85. }
  86. $state | ConvertTo-Json | Set-Content -Path $StateFile -Encoding UTF8
  87. Write-Host ""
  88. Write-Host "auto-dismiss background task started"
  89. Write-Host "pid : $($proc.Id)"
  90. Write-Host "hook : $Hook"
  91. Write-Host "stdout log : $StdoutLog"
  92. Write-Host "stderr log : $StderrLog"
  93. Write-Host "state file : $StateFile"
  94. Write-Host ""
  95. Write-Host "tip: tail stdout to watch dismiss events:"
  96. Write-Host " Get-Content -Path $StdoutLog -Wait -Tail 20"