public static void RestorePrivilege(TOKEN_PRIVILEGES tokenPrivilage)
    {
        IntPtr cpHandle = Process.GetCurrentProcess().Handle;
        IntPtr pHandle  = IntPtr.Zero;
        IntPtr tHandle  = IntPtr.Zero;

        TOKEN_PRIVILEGES tOldState = new TOKEN_PRIVILEGES();

        uint cb      = (uint)Marshal.SizeOf(typeof(TOKEN_PRIVILEGES));
        uint tAccess = advapi32Imports.TOKEN_QUERY | advapi32Imports.TOKEN_ADJUST_PRIVILEGES;

        advapi32Imports.OpenProcessToken(cpHandle, tAccess, ref tHandle);
        advapi32Imports.AdjustTokenPrivileges(tHandle, false, ref tokenPrivilage, cb, ref tOldState, out cb);

        Kernel32Imports.CloseHandle(tHandle);
    }
    public static TOKEN_PRIVILEGES SetPrivilege(string privilegeName)
    {
        IntPtr cpHandle = Process.GetCurrentProcess().Handle;
        IntPtr pHandle  = IntPtr.Zero;
        IntPtr tHandle  = IntPtr.Zero;

        TOKEN_PRIVILEGES tNewState = GetTokenPrivilege(privilegeName, true);
        TOKEN_PRIVILEGES tOldState = new TOKEN_PRIVILEGES();

        uint cb      = (uint)Marshal.SizeOf(typeof(TOKEN_PRIVILEGES));
        uint tAccess = advapi32Imports.TOKEN_QUERY | advapi32Imports.TOKEN_ADJUST_PRIVILEGES;

        advapi32Imports.OpenProcessToken(cpHandle, tAccess, ref tHandle);
        advapi32Imports.AdjustTokenPrivileges(tHandle, false, ref tNewState, cb, ref tOldState, out cb);

        Kernel32Imports.CloseHandle(tHandle);

        return(tOldState);
    }
Esempio n. 3
0
        private bool ApplyPatches(Process process)
        {
            if (process == null)
            {
                return(false);
            }

            TOKEN_PRIVILEGES privilege = PrivilegeHelper.SetPrivilege("SeDebugPrivilege");
            IntPtr           handle    = OpenProcessVirtualAccess(process.Id);
            bool             isOkay    = true;

            isOkay = isOkay && handle != IntPtr.Zero;
            isOkay = isOkay && CheckCrc(handle);
            isOkay = isOkay && ApplyCrcPatch(handle);
            isOkay = isOkay && ApplyCommPatch(handle);
            isOkay = isOkay && ApplyGamePatches(handle);

            Kernel32Imports.CloseHandle(handle);
            PrivilegeHelper.RestorePrivilege(privilege);

            return(isOkay);
        }