Repository navigation
Expand file tree
/
Copy pathMemoryWriter.cs
More file actions
139 lines (121 loc) · 4.95 KB
/
Copy pathMemoryWriter.cs
File metadata and controls
139 lines (121 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Threading;
namespace Launcher
{
public static class MemoryWriter
{
#region PInvoke
[Flags]
private enum ProcessAccessFlags : uint
{
VMRead = 0x0010,
VMWrite = 0x0020,
VMOperation = 0x0008,
QueryInformation = 0x0400,
// 组合:读 + 写 + 操作 + 查询
AllForEdit = VMRead | VMWrite | VMOperation | QueryInformation
}
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr OpenProcess(
ProcessAccessFlags access, bool inheritHandle, int processId);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool ReadProcessMemory(
IntPtr hProcess, IntPtr lpBaseAddress,
byte[] lpBuffer, IntPtr nSize, out IntPtr lpNumberOfBytesRead);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool WriteProcessMemory(
IntPtr hProcess, IntPtr lpBaseAddress,
byte[] lpBuffer, IntPtr nSize, out IntPtr lpNumberOfBytesWritten);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool VirtualProtectEx(
IntPtr hProcess, IntPtr lpAddress,
IntPtr dwSize, uint flNewProtect, out uint lpflOldProtect);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool CloseHandle(IntPtr hObject);
// 常见保护常量
private const uint PAGE_EXECUTE_READWRITE = 0x40;
private const uint PAGE_READWRITE = 0x04;
#endregion
/// <summary>
/// 打开进程获取写入权限句柄
/// </summary>
public static IntPtr OpenProcessForEdit(int pid)
{
var h = OpenProcess(ProcessAccessFlags.AllForEdit, false, pid);
if (h == IntPtr.Zero)
throw new Win32Exception(Marshal.GetLastWin32Error(), "OpenProcess 失败,可能需要管理员权限。");
return h;
}
/// <summary>
/// 写一个 int32(小端)
/// </summary>
public static void WriteInt32(IntPtr hProcess, IntPtr address, int newValue, bool protectIfNeeded = true)
{
var bytes = BitConverter.GetBytes(newValue); // 小端
WriteBytes(hProcess, address, bytes, protectIfNeeded);
}
/// <summary>
/// 写任意字节序列
/// </summary>
public static void WriteBytes(IntPtr hProcess, IntPtr address, byte[] data, bool protectIfNeeded = true)
{
uint oldProtect = 0;
bool changedProtect = false;
if (protectIfNeeded)
{
// 尝试改成读写或执行读写(长度按 data.Length)
// 有的页原本就可写;如果 VirtualProtectEx 失败可以忽略继续写,看是否成功。
if (VirtualProtectEx(hProcess, address, (IntPtr)data.Length, PAGE_EXECUTE_READWRITE, out oldProtect))
{
changedProtect = true;
}
}
if (!WriteProcessMemory(hProcess, address, data, (IntPtr)data.Length, out var written) || written.ToInt32() != data.Length)
{
throw new Win32Exception(Marshal.GetLastWin32Error(), $"WriteProcessMemory 失败,写入字节数 {written} / {data.Length}");
}
// 恢复原保护
if (changedProtect)
{
VirtualProtectEx(hProcess, address, (IntPtr)data.Length, oldProtect, out _);
}
}
/// <summary>
/// 读回 int32 验证
/// </summary>
public static int ReadInt32(IntPtr hProcess, IntPtr address)
{
var buf = new byte[4];
if (!ReadProcessMemory(hProcess, address, buf, (IntPtr)4, out var read) || read.ToInt32() != 4)
throw new Win32Exception(Marshal.GetLastWin32Error(), "ReadProcessMemory 失败");
return BitConverter.ToInt32(buf, 0);
}
/// <summary>
/// 简单“冻结”功能:后台线程不断写入固定值
/// </summary>
public static Thread FreezeInt32(IntPtr hProcess, IntPtr address, int value, int intervalMs = 100)
{
var t = new Thread(() =>
{
try
{
while (true)
{
WriteInt32(hProcess, address, value, protectIfNeeded: false);
Thread.Sleep(intervalMs);
}
}
catch (ThreadInterruptedException) { }
catch (Exception ex)
{
Console.WriteLine("冻结线程异常: " + ex.Message);
}
});
t.IsBackground = true;
t.Start();
return t;
}
}
}