gpt4 book ai didi

c# - 读取所有进程内存以查找字符串变量的地址 c#

转载 作者:太空狗 更新时间:2023-10-29 21:48:22 47 4
gpt4 key购买 nike

我有 2 个用 C# 编写的程序,第一个名为“ScanMe”的程序包含一个包含值“FINDMEEEEEEE”的字符串变量,以及一个值为 1546.22915487 的 double 变量。另一个名为“MemoryScan”的程序读取第一个程序的所有内存。我想获取那个进程的字符串变量的内存地址

当我执行“MemoryScan”并读取“ScanMe”进程的所有内存时,我试图在所有扫描的数据中找到字符串的字节数组,但我什么也没得到。如果我试图找到 double,我会得到特定的内存地址,而且我还可以更改它的值,但是当我尝试用字符串变量这样做时,我不能因为我什至没有得到那个字符串变量的地址。

ScanMe 和 MemoryScan 代码:

class Program
{
public static string FindMeString = "FINDMEEEEEEE";
public static double FindMeDouble = 1546.22915487;
static void Main(string[] args)
{
while (FindMeDouble == 1546.22915487)
{
System.Threading.Thread.Sleep(2000);
}
Console.WriteLine(FindMeDouble.ToString());
Console.ReadLine();
}
}

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace MemoryScan
{
class MemoryController
{

// REQUIRED CONSTS
const int PROCESS_QUERY_INFORMATION = 0x0400;
const int MEM_COMMIT = 0x00001000;
const int PAGE_READWRITE = 0x04;
const int PROCESS_WM_READ = 0x0010;
readonly Dictionary<IntPtr, byte[]> Regions = new Dictionary<IntPtr, byte[]>();
readonly List<SearchResult> _results = new List<SearchResult>();

// REQUIRED METHODS
[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);

[DllImport("kernel32.dll")]
public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, uint dwSize, ref int lpNumberOfBytesRead);

[DllImport("kernel32.dll")]
static extern void GetSystemInfo(out SystemInfo lpSystemInfo);

[DllImport("kernel32.dll", SetLastError = true)]
static extern int VirtualQueryEx(IntPtr hProcess, IntPtr lpAddress, out MEMORY_BASIC_INFORMATION lpBuffer, int dwLength);

public enum ProcessorArchitecture
{
X86 = 0,
X64 = 9,
Arm = -1,
Itanium = 6,
Unknown = 0xFFFF
}

// REQUIRED STRUCTS
[StructLayout(LayoutKind.Sequential)]
public struct SystemInfo
{
public ProcessorArchitecture ProcessorArchitecture;
public uint PageSize;
public IntPtr MinimumApplicationAddress;
public IntPtr MaximumApplicationAddress;
public IntPtr ActiveProcessorMask;
public uint NumberOfProcessors;
public uint ProcessorType;
public uint AllocationGranularity;
public ushort ProcessorLevel;
public ushort ProcessorRevision;
}

[StructLayout(LayoutKind.Sequential)]
public struct MEMORY_BASIC_INFORMATION
{
public IntPtr BaseAddress;
public IntPtr AllocationBase;
public uint AllocationProtect;
public IntPtr RegionSize;
public uint State;
public uint Protect;
public uint Type;
}

public void FindProcessMemory(int processId)
{
// getting minimum & maximum address
SystemInfo sys_info;
GetSystemInfo(out sys_info);

uint max_Address = (uint)sys_info.MaximumApplicationAddress;
// opening the process with desired access level
IntPtr processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_WM_READ, false, processId);
IntPtr current = IntPtr.Zero;

int bytesRead = 0; // number of bytes read with ReadProcessMemory
int dwLength = Marshal.SizeOf(typeof(MEMORY_BASIC_INFORMATION));

while ((uint)current < max_Address && VirtualQueryEx(processHandle, current, out MEMORY_BASIC_INFORMATION mem_basic_info, dwLength) != 0)
{
// if this memory chunk is accessible
if (mem_basic_info.Protect == PAGE_READWRITE && mem_basic_info.State == MEM_COMMIT)
{
byte[] buffer = new byte[(int)mem_basic_info.RegionSize];

// read everything in the buffer above
if (ReadProcessMemory(processHandle, mem_basic_info.BaseAddress, buffer, (uint)mem_basic_info.RegionSize, ref bytesRead))
{
Regions.Add(mem_basic_info.BaseAddress, buffer);
}
else
Console.WriteLine($"Error code: Marshal.GetLastWin32Error()");
}

// move to the next memory chunk
current = IntPtr.Add(mem_basic_info.BaseAddress, mem_basic_info.RegionSize.ToInt32());
}
byte[] data = System.Text.Encoding.Unicode.GetBytes("FINDMEEEEEEE");
foreach (IntPtr address in Regions.Keys)
{
foreach (int i in ByteSearch.AllIndexOf(Regions[address], data))
_results.Add(new SearchResult(IntPtr.Add(address, i), data));
}
Console.ReadLine();
}

}
public static class ByteSearch
{
static int[] createTable(byte[] pattern)
{
int[] table = new int[256];

for (int i = 0; i < table.Length; i++)
table[i] = pattern.Length;

for (int i = 0; i < pattern.Length - 1; i++)
table[Convert.ToInt32(pattern[i])] = pattern.Length - i - 1;

return table;
}

public static bool matchAtOffset(byte[] toSearch, byte[] pattern, int index)
{
if (index + pattern.Length > toSearch.Length)
return false;

for (int i = 0; i < pattern.Length; i++)
{
if (toSearch[i + index] != pattern[i])
return false;
}

return true;
}

public static bool Contains(byte[] toSearch, byte[] pattern)
{
return FirstIndexOf(toSearch, pattern) != -1;
}

public static int FirstIndexOf(byte[] toSearch, byte[] pattern)
{
int[] table = createTable(pattern);
int position = pattern.Length - 1;

while (position < toSearch.Length)
{
int i;

for (i = 0; i < pattern.Length; i++)
{
if (pattern[pattern.Length - 1 - i] != toSearch[position - i])
break;

if (i == pattern.Length - 1)
return position - i;
}

position += table[Convert.ToInt32(toSearch[position - i])];
}

return -1;
}

public static int LastIndexOf(byte[] toSearch, byte[] pattern)
{
int ret = -1;
int[] table = createTable(pattern);
int position = pattern.Length - 1;

while (position < toSearch.Length)
{
int i;
bool found = false;

for (i = 0; i < pattern.Length; i++)
{
if (pattern[pattern.Length - 1 - i] != toSearch[position - i])
break;

if (i == pattern.Length - 1)
{
ret = position - i;
found = true;
}
}

if (found)
position++;
else
position += table[Convert.ToInt32(toSearch[position - i])];
}

return ret;
}

public static int[] AllIndexOf(byte[] toSearch, byte[] pattern)
{
List<int> indices = new List<int>();
int[] table = createTable(pattern);
int position = pattern.Length - 1;

while (position < toSearch.Length)
{
int i;
bool found = false;

for (i = 0; i < pattern.Length; i++)
{
if (pattern[pattern.Length - 1 - i] != toSearch[position - i])
break;

if (i == pattern.Length - 1)
{
indices.Add(position - i);
found = true;
}
}

if (found)
position++;
else
position += table[Convert.ToInt32(toSearch[position - i])];
}

return indices.ToArray();
}
}
public class SearchResult
{
public SearchResult(IntPtr add, byte[] value)
{
Address = add;
Buffer = value;
}

public IntPtr Address { get; set; }

public byte[] Buffer { get; set; }
}

为什么我找不到字符串,当我试图找到 double 时,我发现它没有问题,甚至我也可以用外部 writeprocessmemory 更改它的值?谢谢。

最佳答案

我不确定我能否重现您的确切情况,因为您没有提供 minimal reproducible example .但是,我启动并运行了它——结果相似。然后我想通了以下内容:

您没有检查 ReadProcessMemory 的返回值。 MSDN says

If the function fails, the return value is 0 (zero). To get extended error information, call GetLastError.

0 将映射到 false,具体取决于您使用的 PInvoke 签名。

要获取最后一个错误,请使用 Marshal.GetLastWin32Error()。在我的电脑上,错误代码是 299 并且 MSDN says

ERROR_PARTIAL_COPY

299 (0x12B)

Only part of a ReadProcessMemory or WriteProcessMemory request was completed.

同时读取的字节数(ref bytesRead)为0,所以没有读取进程内存。

参见 this related SO question about this error code .

关于c# - 读取所有进程内存以查找字符串变量的地址 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57717504/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com