Esempio n. 1
0
        private unsafe bool ApplyCommDataPatch(IntPtr handle)
        {
            uint   pso2Base  = 0x00400000u;
            byte * bCommData = stackalloc byte[0x20];
            IntPtr dCommData = (IntPtr)bCommData;
            IntPtr pCommData = new IntPtr(pso2Base + PCOMMDATA);

            // Read out old data
            if (!Kernel32Imports.ReadProcessMemory(handle, pCommData, dCommData, 0x20, IntPtr.Zero))
            {
                return(LogLastWin32Error("ReadProcessMemory") == 0);
            }

            // Apply Functions Patch
            IntPtr pOldCommFunctions = new IntPtr(Marshal.ReadInt32(dCommData, 0x18));
            IntPtr pCommFunctions    = ApplyCommFunctionsPatch(handle, pOldCommFunctions);

            Marshal.Copy(Enumerable.Repeat((byte)0x00, 0x20).ToArray(), 0, dCommData, 0x18);
            Marshal.Copy(Encoding.ASCII.GetBytes("ARKSMT"), 0, dCommData, 6);
            Marshal.WriteInt32(dCommData, 0x10, Program.PID);
            Marshal.WriteInt32(dCommData, 0x14, m_commBuffer.ToInt32());
            Marshal.WriteInt32(dCommData, 0x18, pCommFunctions.ToInt32());

            if (!Kernel32Imports.WriteProcessMemory(handle, pCommData, dCommData, 0x20, IntPtr.Zero))
            {
                return(LogLastWin32Error("WriteProcessMemory") == 0);
            }

            return(true);
        }
Esempio n. 2
0
        private unsafe bool ApplyCrcPatch(IntPtr handle)
        {
            uint pso2Base = 0x00400000u;

            uint * bCrc = stackalloc uint[1];
            IntPtr pCrc = new IntPtr(pso2Base + 0x38);
            IntPtr dCrc = new IntPtr(bCrc);

            bCrc[0] = PATCHED_CRC;

            Protection newProtection = Protection.PAGE_EXECUTE_READWRITE;
            Protection oldProtection;

            if (!Kernel32Imports.VirtualProtectEx(handle, pCrc, 4, newProtection, out oldProtection))
            {
                return(LogLastWin32Error("VirtualProtectEx") == 0);
            }

            if (!Kernel32Imports.WriteProcessMemory(handle, pCrc, dCrc, 4, IntPtr.Zero))
            {
                return(LogLastWin32Error("WriteProcessMemory") == 0);
            }

            return(true);
        }
Esempio n. 3
0
        private unsafe IntPtr ApplyCommFunctionsPatch(IntPtr handle, IntPtr pOldCommFunctions)
        {
            if (pOldCommFunctions != IntPtr.Zero)
            {
                return(pOldCommFunctions);
            }

            string[] functions    = Resources.CommFunctions.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            var      byteCounts   = functions.Select(x => Encoding.ASCII.GetByteCount(x) + 1);
            var      paddedCounts = byteCounts.Select(x => (x + 0xF) & ~0xF);

            int[] offsets = paddedCounts.Select((x, i) => paddedCounts.Take(i).Sum()).ToArray();

            int cbFunctionPointers = ((functions.Length * 0x4) + 0x1F) & ~0x1F;
            int cbNamePointers     = cbFunctionPointers;
            int cbNames            = paddedCounts.Sum();
            int cbKernel32         = 0x10;
            int byteCount          = cbFunctionPointers + cbNamePointers + cbNames + cbKernel32;

            byte * bCommFunctions = stackalloc byte[byteCount];
            IntPtr dCommFunctions = new IntPtr(bCommFunctions);
            IntPtr pCommFunctions = VirtualAlloc(handle, byteCount);

            if (pCommFunctions == IntPtr.Zero)
            {
                LogLastWin32Error("VirtualAllocEx");
                return(IntPtr.Zero);
            }

            IntPtr dNamePointers = IntPtr.Add(dCommFunctions, cbFunctionPointers);

            for (int i = 0; i < functions.Length; ++i)
            {
                int    nameOffset  = cbFunctionPointers + cbNamePointers + offsets[i];
                IntPtr namePointer = IntPtr.Add(pCommFunctions, nameOffset);

                Marshal.WriteInt32(dNamePointers, i * 0x4, namePointer.ToInt32());
                Encoding.ASCII.WriteString(dCommFunctions, nameOffset, functions[i]);
            }

            int    k32nameOffset  = cbFunctionPointers + cbNamePointers + cbNames;
            IntPtr k32namePointer = IntPtr.Add(pCommFunctions, k32nameOffset);

            Marshal.WriteInt32(dCommFunctions, cbFunctionPointers - 0x4, k32namePointer.ToInt32());
            Encoding.ASCII.WriteString(dCommFunctions, k32nameOffset, "Kernel32.dll");

            if (!Kernel32Imports.WriteProcessMemory(handle, pCommFunctions, dCommFunctions, byteCount, IntPtr.Zero))
            {
                VirtualReleaseAll(handle, pCommFunctions);
                LogLastWin32Error("WriteProcessMemory");
                return(IntPtr.Zero);
            }

            return(pCommFunctions);
        }
Esempio n. 4
0
        private unsafe bool ApplyGamePatch(IntPtr handle, byte[] patchData)
        {
            uint pso2Base = 0x00400000u;

            fixed(byte *ptr = patchData)
            {
                uint   offset = *(uint *)ptr;
                IntPtr pPatch = new IntPtr(pso2Base + offset);
                IntPtr pData  = new IntPtr(ptr + 0x10);
                int    size   = patchData.Length - 0x10;

                if (!Kernel32Imports.WriteProcessMemory(handle, pPatch, pData, size, IntPtr.Zero))
                {
                    return(LogLastWin32Error("WriteProcessMemory") == 0);
                }
            }

            return(true);
        }