Esempio n. 1
0
        public MemoryResult ReadByte(IntPtr memoryAddress, out byte outByte)
        {
            ResetError();
            var buffer = new byte[1];

            int ptrBytesRead; // error checking

            if (!MemoryApi.ReadProcessMemory(_processHandle, memoryAddress, buffer, sizeof(byte), out ptrBytesRead) ||
                sizeof(byte) != ptrBytesRead)
            {
                HandleError("ReadByte - Address " + memoryAddress.ToString("X") + "; Read: " + ptrBytesRead);
            }

            outByte = buffer[0];
            return(LastMemoryResult);
        }
Esempio n. 2
0
        public MemoryResult ReadShort(IntPtr memoryAddress, out short outShort)
        {
            ResetError();
            byte[] buffer = new byte[2];

            int ptrBytesRead; // error checking

            if (!MemoryApi.ReadProcessMemory(_processHandle, memoryAddress, buffer, sizeof(short), out ptrBytesRead) ||
                sizeof(short) != ptrBytesRead)
            {
                HandleError("ReadShort - Address " + memoryAddress.ToString("X") + "; Read: " + ptrBytesRead);
            }

            outShort = BitConverter.ToInt16(buffer, 0);
            return(LastMemoryResult);
        }
Esempio n. 3
0
        public MemoryResult ReadFloat(IntPtr memoryAddress, out float outFloat)
        {
            ResetError();
            var buffer = new byte[sizeof(float)];

            int ptrBytesRead; // error checking

            if (!MemoryApi.ReadProcessMemory(_processHandle, memoryAddress, buffer, sizeof(float), out ptrBytesRead) ||
                sizeof(int) != ptrBytesRead)
            {
                HandleError("ReadFloat - Address " + memoryAddress.ToString("X") + "; Read: " + ptrBytesRead);
            }

            outFloat = BitConverter.ToSingle(buffer, 0);
            return(LastMemoryResult);
        }
Esempio n. 4
0
        /// <summary>
        /// Reads a pre-specified number of bytes and returns number of bytes read aas well as the values read, through its parameters
        /// </summary>
        public MemoryResult ReadMemory(IntPtr memoryAddress, int bytesToRead, out int bytesRead,
                                       out byte[] outByteArray)
        {
            ResetError();
            var buffer = new byte[bytesToRead];

            if (!MemoryApi.ReadProcessMemory(_processHandle, memoryAddress, buffer, bytesToRead, out bytesRead) ||
                bytesToRead != bytesRead)
            {
                HandleError("ReadMemory Array - Address " + memoryAddress.ToString("X") + "; Size: " + bytesToRead +
                            "; Read: " + bytesRead);
            }

            outByteArray = buffer;
            return(LastMemoryResult);
        }
Esempio n. 5
0
        public MemoryResult ReadMemory(IntPtr memoryAddress, int bytesToRead, ref byte[] outBuffer, out int bytesRead)
        {
            ResetError();
            bytesRead = 0;
            if (outBuffer == null || bytesToRead < 0)
            {
                return(MemoryResult.InvalidArguments);
            }

            int workingReadCount = Math.Min(outBuffer.Length, bytesToRead);

            if (!MemoryApi.ReadProcessMemory(_processHandle, memoryAddress, outBuffer, workingReadCount, out bytesRead) ||
                bytesToRead != bytesRead)
            {
                HandleError("ReadMemory Buffer - Address " + memoryAddress.ToString("X") + "; Size: " + workingReadCount +
                            "; Read: " + bytesRead);
            }

            return(LastMemoryResult);
        }
Esempio n. 6
0
        /// <summary>
        /// <para>Searches the loaded process for the given byte signature.</para>
        /// <para>Uses the character ? as a wildcard</para>
        /// </summary>
        /// <param name="signature">The hex pattern to search for</param>
        /// <param name="startAddress">An startAddress to add to the pointer VALUE</param>
        /// <param name="searchType">What type of result to return</param>
        /// <returns>The pointer found at the matching location</returns>
        public IntPtr FindSignature(string signature, int startAddress, ScanResultType searchType)
        {
            // make sure we have a valid signature
            if (signature.Length == 0 || signature.Length % 2 != 0)
            {
                throw new Exception("FindSignature(): Invalid signature");
            }

            for (var index = 0; index < _memoryRegionsSortedIndices.Count; index++)
            {
                int workingIndex = _memoryRegionsSortedIndices.Values[index];

                MemoryApi.MemoryBasicInformation region = MemoryRegions[_memoryRegionsSortedIndices.Values[index]];

                // Skip memory regions until we find one that contains the startAddress but startAddress of zero means skip none
                if (startAddress > 0)
                {
                    if (region.BaseAddress.ToInt32() + region.RegionSize < startAddress ||
                        region.BaseAddress.ToInt32() > startAddress)
                    {
                        continue;
                    }
                }

                var buffer    = new byte[region.RegionSize];
                var bytesRead = 0;

                // ReadProcessMemory will return 0 if some form of error occurs
                if (
                    !MemoryApi.ReadProcessMemory(_process.Handle, region.BaseAddress, buffer, (int)region.RegionSize,
                                                 out bytesRead))
                {
                    // get the error code thrown from ReadProcessMemory
                    int errorCode = Marshal.GetLastWin32Error();

                    // For now, if error reading, we will still search what amount was able to be read so no exception throwing.
                }

                var bufferOffset = 0;
                if (startAddress > 0 && region.BaseAddress.ToInt32() < startAddress &&
                    region.BaseAddress.ToInt32() + bytesRead > startAddress)
                {
                    // Since requested startAddress is somewhere in the current regions address space, set startAddress from beginning of region
                    bufferOffset = startAddress - region.BaseAddress.ToInt32();
                }
                IntPtr searchResult = FindSignature(buffer, signature, bufferOffset, searchType);

                // If we found our signature, we're done
                if (IntPtr.Zero == searchResult)
                {
                    continue;
                }

                // if we passed the ! flag we want the beginning address of where it found the sig
                if (ScanResultType.AddressStartOfSig == searchType)
                {
                    searchResult = new IntPtr(region.BaseAddress.ToInt32() + searchResult.ToInt32());
                }

                return(searchResult);
            }
            return(IntPtr.Zero);
        }