Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <# ::
- :: StartAllBack Patcher BAT Script - cevoj35548
- :: Works for all versions & future-proof.
- :: Tested up to 3.7.9 (latest at the time of writing)
- :: USAGE:
- :: Save as a .bat file and run as admin
- @echo off & setlocal
- net file >nul 2>&1
- if %errorlevel% NEQ 0 (echo;Please run as admin.&pause>nul &exit)
- powershell -noprofile -nologo "iex (${%~f0} | out-string)"
- goto:eof
- ::#>
- <# -- LIBRARY -- #>
- function Expand-EnvVar($Path) {
- return [System.Environment]::ExpandEnvironmentVariables($Path)
- }
- New-Alias -Name ee -Value Expand-EnvVar
- function ReplaceBytes($Path, $FindBytes, $ReplaceBytes) {
- $Path = ee "$Path"
- $bytes = [System.IO.File]::ReadAllBytes($Path)
- $findPattern = [byte[]] ($FindBytes -split " " | ForEach-Object { [Convert]::ToByte($_, 16) })
- $replacePattern = [byte[]] ($ReplaceBytes -split " " | ForEach-Object { [Convert]::ToByte($_, 16) })
- $offset = 0
- while (($offset = [System.Array]::IndexOf($bytes, $findPattern[0], $offset)) -ne -1) {
- $found = $true
- for ($i = 0; $i -lt $findPattern.Length; $i++) {
- if ($bytes[$offset + $i] -ne $findPattern[$i]) {
- $found = $false
- break
- }
- }
- if ($found) {
- Write-Host Patching "$Path"
- for ($i = 0; $i -lt $replacePattern.Length; $i++) {
- $bytes[$offset + $i] = $replacePattern[$i]
- }
- $offset += $replacePattern.Length
- } else {
- $offset += 1
- }
- }
- [System.IO.File]::WriteAllBytes($Path, $bytes)
- }
- function Kill-FileLockers($FilePath) {
- $TypeDefinition = @"
- using System;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- using System.Diagnostics;
- namespace MyCore.Utils {
- static public class FileLockUtil {
- [StructLayout(LayoutKind.Sequential)] struct RM_UNIQUE_PROCESS {
- public int dwProcessId;
- public System.Runtime.InteropServices.ComTypes.FILETIME ProcessStartTime;
- }
- enum RM_APP_TYPE {RmUnknownApp=0, RmMainWindow=1, RmOtherWindow=2, RmService=3, RmExplorer=4, RmConsole=5, RmCritical=1000}
- [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)] struct RM_PROCESS_INFO {
- public RM_UNIQUE_PROCESS Process;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)] public string strAppName;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)] public string strServiceShortName;
- public RM_APP_TYPE ApplicationType;
- public uint AppStatus;
- public uint TSSessionId;
- [MarshalAs(UnmanagedType.Bool)] public bool bRestartable;
- }
- [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);
- [DllImport("rstrtmgr.dll", CharSet=CharSet.Auto)] static extern int RmStartSession(out uint pSessionHandle, int dwSessionFlags, string strSessionKey);
- [DllImport("rstrtmgr.dll")] static extern int RmEndSession(uint pSessionHandle);
- [DllImport("rstrtmgr.dll")] static extern int RmGetList(uint dwSessionHandle, out uint pnProcInfoNeeded, ref uint pnProcInfo, [In, Out] RM_PROCESS_INFO[] rgAffectedApps, ref uint lpdwRebootReasons);
- static public List<int> WhoIsLocking(string path) {
- uint handle;
- string key = Guid.NewGuid().ToString();
- List<int> pids = new List<int>();
- int res = RmStartSession(out handle, 0, key);
- if (res != 0) throw new Exception("Could not begin restart session. Unable to determine file locker.");
- try {
- uint pnProcInfoNeeded = 0, pnProcInfo = 0, lpdwRebootReasons = 0;
- string[] resources = new string[] { path };
- res = RmRegisterResources(handle, (uint)resources.Length, resources, 0, null, 0, null);
- if (res != 0) throw new Exception("Could not register resource.");
- res = RmGetList(handle, out pnProcInfoNeeded, ref pnProcInfo, null, ref lpdwRebootReasons);
- if (res == 234) {
- RM_PROCESS_INFO[] processInfo = new RM_PROCESS_INFO[pnProcInfoNeeded];
- pnProcInfo = pnProcInfoNeeded;
- res = RmGetList(handle, out pnProcInfoNeeded, ref pnProcInfo, processInfo, ref lpdwRebootReasons);
- if (res == 0) {
- for (int i = 0; i < pnProcInfo; i++) {pids.Add(processInfo[i].Process.dwProcessId);}
- } else throw new Exception("Could not list processes locking resource.");
- } else if (res != 0) throw new Exception("Could not list processes locking resource. Failed to get size of result.");
- } finally {
- RmEndSession(handle);
- }
- return pids;
- }}}
- "@
- Add-Type -ReferencedAssemblies $ReferencedAssemblies -TypeDefinition $TypeDefinition
- [MyCore.Utils.FileLockUtil]::WhoIsLocking($FilePath) | ForEach-Object {taskkill /f /pid $_}
- }
- <# -- KILL TASKS -- #>
- Write-Host Killing tasks...
- taskkill /f /im explorer.exe
- taskkill /f /im StartAllBackCfg.exe 2>$null
- Kill-FileLockers '%programfiles%\StartAllBack\StartAllBackX64.dll'
- <# -- FILES -- #>
- Write-Host Replacing byte patterns...
- 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'
- Write-Host Launching explorer...
- start explorer.exe
- Write-Host Complete! Press any key to exit. -ForegroundColor Green
- [void][System.Console]::ReadKey($true)
Advertisement
Advertisement