prepare.ps1 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # First-logon preparation for the PCL PoC demo VM.
  2. # ASCII only. Runs elevated in the interactive desktop session.
  3. $ErrorActionPreference = 'Stop'
  4. $Root = $PSScriptRoot
  5. $Ev = Join-Path $Root 'evidence'
  6. New-Item -ItemType Directory -Force -Path $Ev | Out-Null
  7. try { Start-Transcript -Path (Join-Path $Ev 'prepare-transcript.txt') -Append | Out-Null } catch { }
  8. function Log([string]$Message) {
  9. $line = '[{0:HH:mm:ss}] {1}' -f (Get-Date), $Message
  10. Write-Host $line
  11. try { Add-Content -LiteralPath (Join-Path $Ev 'prepare.log') -Value $line -Encoding UTF8 } catch { }
  12. }
  13. try {
  14. Log 'prepare: waiting for Explorer / desktop'
  15. for ($i = 0; $i -lt 90; $i++) {
  16. if (Get-Process explorer -ErrorAction SilentlyContinue) { break }
  17. Start-Sleep -Seconds 1
  18. }
  19. Start-Sleep -Seconds 8
  20. Log 'prepare: EULA / notice bookkeeping'
  21. & reg.exe add 'HKCU\Software\PCL' /v SystemEulaVersion /t REG_SZ /d 2 /f | Out-Null
  22. & reg.exe add 'HKCU\Software\PCL' /v HintNotice /t REG_SZ /d 0 /f | Out-Null
  23. Log 'prepare: disable toast notifications and tips'
  24. & reg.exe add 'HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\PushNotifications' /v ToastEnabled /t REG_DWORD /d 0 /f | Out-Null
  25. & reg.exe add 'HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\ContentDeliveryManager' /v SubscribedContent-338389Enabled /t REG_DWORD /d 0 /f | Out-Null
  26. Log 'prepare: power settings'
  27. & powercfg.exe /change monitor-timeout-ac 0 | Out-Null
  28. & powercfg.exe /change standby-timeout-ac 0 | Out-Null
  29. & powercfg.exe /change hibernate-timeout-ac 0 | Out-Null
  30. Log 'prepare: defender exclusions'
  31. try {
  32. Add-MpPreference -ExclusionPath @('C:\PCLPoC', (Join-Path $env:USERPROFILE 'Desktop\PCL'), 'C:\Users\Public') -ErrorAction SilentlyContinue
  33. Set-MpPreference -DisableRealtimeMonitoring $true -ErrorAction SilentlyContinue
  34. } catch {
  35. Log ('prepare: defender exclusion warning: ' + $_.Exception.Message)
  36. }
  37. Log 'prepare: copy PoC toolkit to C:\PCLPoC'
  38. if (Test-Path -LiteralPath 'C:\PCLPoC') {
  39. Remove-Item -LiteralPath 'C:\PCLPoC' -Recurse -Force -ErrorAction SilentlyContinue
  40. }
  41. Copy-Item -LiteralPath (Join-Path $Root 'PCLPoC') -Destination 'C:\PCLPoC' -Recurse -Force
  42. Log 'prepare: create retry wrapper and scheduled task'
  43. $retry = @'
  44. @echo off
  45. for %%d in (C D E F G H I J K) do (
  46. if exist "%%d:\pcl-test\retry.flag" (
  47. del "%%d:\pcl-test\retry.flag" >nul 2>&1
  48. call "%%d:\pcl-test\firstlogon.cmd"
  49. exit /b 0
  50. )
  51. )
  52. exit /b 0
  53. '@
  54. try {
  55. Set-Content -LiteralPath 'C:\pcl-retry.cmd' -Value $retry -Encoding ASCII
  56. try {
  57. $taskName = 'PCLPoCRetry'
  58. $user = $env:COMPUTERNAME + '\' + $env:USERNAME
  59. $action = New-ScheduledTaskAction -Execute 'C:\pcl-retry.cmd'
  60. $trigger = New-ScheduledTaskTrigger -AtLogOn -User $user
  61. $principal = New-ScheduledTaskPrincipal -UserId $user -LogonType Interactive -RunLevel Highest
  62. Register-ScheduledTask -TaskName $taskName -Action $action -Trigger $trigger -Principal $principal -Force | Out-Null
  63. Log 'prepare: scheduled task registered'
  64. } catch {
  65. Log ('prepare: scheduled task warning: ' + $_.Exception.Message)
  66. }
  67. } catch {
  68. Log ('prepare: retry wrapper warning: ' + $_.Exception.Message)
  69. }
  70. Log 'prepare: starting demo.ps1'
  71. Start-Process -FilePath 'powershell.exe' -ArgumentList @('-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', (Join-Path $Root 'demo.ps1')) -WindowStyle Normal
  72. Log 'prepare: done'
  73. } catch {
  74. Log ('prepare: ERROR ' + $_.Exception.Message)
  75. try {
  76. Add-Content -LiteralPath (Join-Path $Ev 'prepare-error.txt') -Value $_.Exception.ToString() -Encoding UTF8
  77. } catch { }
  78. } finally {
  79. try { Stop-Transcript | Out-Null } catch { }
  80. }