Advertisement
cevoj35548

StartAllBack Patcher for 3.x

May 21st, 2024 (edited)
1,046
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 5.73 KB | None | 0 0
  1. <# ::
  2. :: StartAllBack Patcher BAT Script - cevoj35548
  3. :: Works for all versions & future-proof.
  4. :: Tested up to 3.7.9 (latest at the time of writing)
  5.  
  6. :: USAGE:
  7. :: Save as a .bat file and run as admin
  8.  
  9. @echo off & setlocal
  10. net file >nul 2>&1
  11. if %errorlevel% NEQ 0 (echo;Please run as admin.&pause>nul &exit)
  12. powershell -noprofile -nologo "iex (${%~f0} | out-string)"
  13. goto:eof
  14. ::#>
  15. <# -- LIBRARY -- #>
  16. function Expand-EnvVar($Path) {
  17.     return [System.Environment]::ExpandEnvironmentVariables($Path)
  18. }
  19. New-Alias -Name ee -Value Expand-EnvVar
  20. function ReplaceBytes($Path, $FindBytes, $ReplaceBytes) {
  21.     $Path = ee "$Path"
  22.     $bytes = [System.IO.File]::ReadAllBytes($Path)
  23.     $findPattern = [byte[]] ($FindBytes -split " " | ForEach-Object { [Convert]::ToByte($_, 16) })
  24.     $replacePattern = [byte[]] ($ReplaceBytes -split " " | ForEach-Object { [Convert]::ToByte($_, 16) })
  25.     $offset = 0
  26.     while (($offset = [System.Array]::IndexOf($bytes, $findPattern[0], $offset)) -ne -1) {
  27.         $found = $true
  28.         for ($i = 0; $i -lt $findPattern.Length; $i++) {
  29.             if ($bytes[$offset + $i] -ne $findPattern[$i]) {
  30.                 $found = $false
  31.                 break
  32.             }
  33.         }
  34.         if ($found) {
  35.             Write-Host Patching "$Path"
  36.             for ($i = 0; $i -lt $replacePattern.Length; $i++) {
  37.                 $bytes[$offset + $i] = $replacePattern[$i]
  38.             }
  39.             $offset += $replacePattern.Length
  40.         } else {
  41.             $offset += 1
  42.         }
  43.     }
  44.     [System.IO.File]::WriteAllBytes($Path, $bytes)
  45. }
  46. function Kill-FileLockers($FilePath) {
  47.     $TypeDefinition = @"
  48.     using System;
  49.     using System.Collections.Generic;
  50.     using System.Runtime.InteropServices;
  51.     using System.Diagnostics;
  52.     namespace MyCore.Utils {
  53.     static public class FileLockUtil {
  54.         [StructLayout(LayoutKind.Sequential)] struct RM_UNIQUE_PROCESS {
  55.             public int dwProcessId;
  56.             public System.Runtime.InteropServices.ComTypes.FILETIME ProcessStartTime;
  57.         }
  58.         enum RM_APP_TYPE {RmUnknownApp=0, RmMainWindow=1, RmOtherWindow=2, RmService=3, RmExplorer=4, RmConsole=5, RmCritical=1000}
  59.         [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)] struct RM_PROCESS_INFO {
  60.             public RM_UNIQUE_PROCESS Process;
  61.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)] public string strAppName;
  62.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)] public string strServiceShortName;
  63.             public RM_APP_TYPE ApplicationType;
  64.             public uint AppStatus;
  65.             public uint TSSessionId;
  66.             [MarshalAs(UnmanagedType.Bool)] public bool bRestartable;
  67.         }
  68.         [DllImport("rstrtmgr.dll", CharSet=CharSet.Unicode)] static extern int RmRegisterResources(uint pSessionHandle, UInt32 nFiles, string[] rgsFilenames, UInt32 nApplications, [In] RM_UNIQUE_PROCESS[] rgApplications, UInt32 nServices, string[] rgsServiceNames);
  69.         [DllImport("rstrtmgr.dll", CharSet=CharSet.Auto)] static extern int RmStartSession(out uint pSessionHandle, int dwSessionFlags, string strSessionKey);
  70.         [DllImport("rstrtmgr.dll")] static extern int RmEndSession(uint pSessionHandle);
  71.         [DllImport("rstrtmgr.dll")] static extern int RmGetList(uint dwSessionHandle, out uint pnProcInfoNeeded, ref uint pnProcInfo, [In, Out] RM_PROCESS_INFO[] rgAffectedApps, ref uint lpdwRebootReasons);
  72.         static public List<int> WhoIsLocking(string path) {
  73.             uint handle;
  74.             string key = Guid.NewGuid().ToString();
  75.             List<int> pids = new List<int>();
  76.             int res = RmStartSession(out handle, 0, key);
  77.             if (res != 0) throw new Exception("Could not begin restart session. Unable to determine file locker.");
  78.             try {
  79.                 uint pnProcInfoNeeded = 0, pnProcInfo = 0, lpdwRebootReasons = 0;
  80.                 string[] resources = new string[] { path };
  81.                 res = RmRegisterResources(handle, (uint)resources.Length, resources, 0, null, 0, null);
  82.                 if (res != 0) throw new Exception("Could not register resource.");
  83.                 res = RmGetList(handle, out pnProcInfoNeeded, ref pnProcInfo, null, ref lpdwRebootReasons);
  84.                 if (res == 234) {
  85.                     RM_PROCESS_INFO[] processInfo = new RM_PROCESS_INFO[pnProcInfoNeeded];
  86.                     pnProcInfo = pnProcInfoNeeded;
  87.                     res = RmGetList(handle, out pnProcInfoNeeded, ref pnProcInfo, processInfo, ref lpdwRebootReasons);
  88.                     if (res == 0) {
  89.                         for (int i = 0; i < pnProcInfo; i++) {pids.Add(processInfo[i].Process.dwProcessId);}
  90.                     } else throw new Exception("Could not list processes locking resource.");
  91.                 } else if (res != 0) throw new Exception("Could not list processes locking resource. Failed to get size of result.");
  92.             } finally {
  93.                 RmEndSession(handle);
  94.             }
  95.             return pids;
  96.         }}}
  97. "@
  98.     Add-Type -ReferencedAssemblies $ReferencedAssemblies -TypeDefinition $TypeDefinition
  99.     [MyCore.Utils.FileLockUtil]::WhoIsLocking($FilePath) | ForEach-Object {taskkill /f /pid $_}
  100. }
  101. <# -- KILL TASKS -- #>
  102. Write-Host Killing tasks...
  103. taskkill /f /im explorer.exe
  104. taskkill /f /im StartAllBackCfg.exe 2>$null
  105. Kill-FileLockers '%programfiles%\StartAllBack\StartAllBackX64.dll'
  106. <# -- FILES -- #>
  107. Write-Host Replacing byte patterns...
  108. ReplaceBytes '%programfiles%\StartAllBack\StartAllBackX64.dll' '48 89 5C 24 08 55 56 57 48 8D AC 24 70 FF FF FF' '67 C7 01 01 00 00 00 B8 01 00 00 00 C3 90 90 90'
  109. Write-Host Launching explorer...
  110. start explorer.exe
  111. Write-Host Complete! Press any key to exit. -ForegroundColor Green
  112. [void][System.Console]::ReadKey($true)
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement