forked from DaxxTrias/Process.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessSharp.cs
More file actions
265 lines (235 loc) · 9.54 KB
/
ProcessSharp.cs
File metadata and controls
265 lines (235 loc) · 9.54 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using ProcessNET.Extensions;
using ProcessNET.Memory;
using ProcessNET.Modules;
using ProcessNET.Native.Types;
using ProcessNET.Threads;
using ProcessNET.Utilities;
using ProcessNET.Windows;
namespace ProcessNET
{
/// <summary>
/// A class that offsers several tools to interact with a process.
/// </summary>
/// <seealso cref="IProcess" />
public class ProcessSharp : IProcess
{
/// <summary>
/// Initializes a new instance of the <see cref="ProcessSharp" /> class.
/// </summary>
/// <param name="native">The native process.</param>
/// <param name="type">The type of memory being manipulated.</param>
public ProcessSharp(System.Diagnostics.Process native, MemoryType type)
{
native.EnableRaisingEvents = true;
native.Exited += (s, e) =>
{
ProcessExited?.Invoke(s, e);
HandleProcessExiting();
};
Native = native;
Handle = MemoryHelper.OpenProcess(ProcessAccessFlags.AllAccess, Native.Id);
switch (type)
{
case MemoryType.Local:
Memory = new LocalProcessMemory(Handle);
break;
case MemoryType.Remote:
Memory = new ExternalProcessMemory(Handle);
break;
default:
throw new ArgumentOutOfRangeException(nameof(type), type, null);
}
native.ErrorDataReceived += OutputDataReceived;
native.OutputDataReceived += OutputDataReceived;
ThreadFactory = new ThreadFactory(this);
ModuleFactory = new ModuleFactory(this);
MemoryFactory = new MemoryFactory(this);
WindowFactory = new WindowFactory(this);
}
/// <summary>
/// Initializes a new instance of the <see cref="ProcessSharp"/> class.
/// </summary>
/// <param name="processName">Name of the process.</param>
/// <param name="type">The type of memory being manipulated.</param>
public ProcessSharp(string processName, MemoryType type) : this(ProcessHelper.FromName(processName), type)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ProcessSharp"/> class.
/// </summary>
/// <param name="processId">The process id of the process to open with all rights.</param>
/// <param name="type">The type of memory being manipulated.</param>
public ProcessSharp(int processId, MemoryType type) : this(ProcessHelper.FromProcessId(processId), type)
{
}
/// <summary>
/// Raises when the <see cref="ProcessSharp"/> object is disposed.
/// </summary>
public event EventHandler OnDispose;
/// <summary>
/// Class for reading and writing memory.
/// </summary>
public IMemory Memory { get; set; }
/// <summary>
/// Provide access to the opened process.
/// </summary>
public System.Diagnostics.Process Native { get; set; }
/// <summary>
/// The process handle opened with all rights.
/// </summary>
public SafeMemoryHandle Handle { get; set; }
/// <summary>
/// Factory for manipulating threads.
/// </summary>
public IThreadFactory ThreadFactory { get; set; }
/// <summary>
/// Factory for manipulating modules and libraries.
/// </summary>
public IModuleFactory ModuleFactory { get; set; }
/// <summary>
/// Factory for manipulating memory space.
/// </summary>
public IMemoryFactory MemoryFactory { get; set; }
/// <summary>
/// Factory for manipulating windows.
/// </summary>
public IWindowFactory WindowFactory { get; set; }
protected string ownerUser = null;
/// <summary>
/// Gets the name of the user this process belongs to.
/// </summary>
public string OwnerUser
{
get
{
if(string.IsNullOrWhiteSpace(ownerUser))
{
ownerUser = ProcessHelper.GetProcessUser(Native.Id);
}
return ownerUser;
}
}
/// <summary>
/// Gets the <see cref="IProcessModule"/> with the specified module name.
/// </summary>
/// <param name="moduleName">Name of the module.</param>
/// <returns>IProcessModule.</returns>
public IProcessModule this[string moduleName] => ModuleFactory[moduleName];
/// <summary>
/// Gets the <see cref="IPointer"/> with the specified address.
/// </summary>
/// <param name="intPtr">The address the pointer is located at in memory.</param>
/// <returns>IPointer.</returns>
public IPointer this[IntPtr intPtr] => new MemoryPointer(this, intPtr);
protected bool IsDisposed { get; set; }
protected bool MustBeDisposed { get; set; } = true;
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
public virtual void Dispose()
{
//TODO Consider adding a null check here, or a nasty crash deadlock can make it in here occasionally.
//TODO followup: did the invoke threadsafety characterstics fix the deadlock?
if (!IsDisposed)
{
IsDisposed = true;
OnDispose?.Invoke(this, EventArgs.Empty);
ThreadFactory?.Dispose();
ModuleFactory?.Dispose();
MemoryFactory?.Dispose();
WindowFactory?.Dispose();
Handle?.Close();
GC.SuppressFinalize(this);
}
}
/// <summary>
/// Handles the process exiting.
/// </summary>
/// <remarks>Created 2012-02-15</remarks>
protected virtual void HandleProcessExiting()
{
}
#region Pointers
/// <summary>
/// Calculates the final address a pointer is pointing at in a module, knowing it's base address and offsets.
/// </summary>
/// <param name="moduleName">The module name to calculate the pointer at.</param>
/// <param name="baseAddress">The base address of the pointer.</param>
/// <param name="offsets">The offsets of the pointer.</param>
/// <returns>The final address the pointer is pointing at.</returns>
public IntPtr GetPointerAddress(string moduleName, IntPtr baseAddress, params int[] offsets)
{
if (string.IsNullOrWhiteSpace(moduleName))
{
throw new ArgumentException($"'{nameof(moduleName)}' cannot be null or whitespace.", nameof(moduleName));
}
return GetPointerAddress(this[moduleName], baseAddress, offsets);
}
/// <summary>
/// Calculates the final address a pointer is pointing at in a module, knowing it's base address and offsets.
/// </summary>
/// <param name="module">The module to calculate the pointer at.</param>
/// <param name="baseAddress">The base address of the pointer.</param>
/// <param name="offsets">The offsets of the pointer.</param>
/// <returns>The final address the pointer is pointing at.</returns>
public IntPtr GetPointerAddress(IProcessModule module, IntPtr baseAddress, params int[] offsets)
{
if (module is null)
{
throw new ArgumentNullException(nameof(module));
}
IntPtr finalBaseAddress;
if (Memory.Is32Bit)
finalBaseAddress = module.BaseAddress + baseAddress.ToInt32();
else
finalBaseAddress = new IntPtr(module.BaseAddress.ToInt64() + baseAddress.ToInt64());
return GetPointerAddress(finalBaseAddress, offsets);
}
/// <summary>
/// Calculates the final address a pointer is pointing at, knowing it's base address and offsets.
/// </summary>
/// <param name="baseAddress">The base address of the pointer.</param>
/// <param name="offsets">The offsets of the pointer.</param>
/// <returns>The final address the pointer is pointing at.</returns>
public IntPtr GetPointerAddress(IntPtr baseAddress, params int[] offsets)
{
if (baseAddress.MayBeValid() == false)
return IntPtr.Zero;
IntPtr address = baseAddress;
if (offsets != null && offsets.Length > 0)
{
for(int i = 0; i < offsets.Length; i++)
{
try
{
address = Memory.Read<IntPtr>(address + offsets[i]);
}
catch(Exception ex)
{
return IntPtr.Zero;
}
}
}
return address;
}
#endregion
/// <summary>
/// Event queue for all listeners interested in ProcessExited events.
/// </summary>
public event EventHandler ProcessExited;
private static void OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Trace.WriteLine(e.Data);
}
~ProcessSharp()
{
if (MustBeDisposed)
{
Dispose();
}
}
}
}