-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathInjectableProcess.cs
More file actions
177 lines (145 loc) · 4.67 KB
/
InjectableProcess.cs
File metadata and controls
177 lines (145 loc) · 4.67 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
using System;
using System.Diagnostics;
using System.Linq;
using HoLLy.ManagedInjector.Injectors;
namespace HoLLy.ManagedInjector
{
public class InjectableProcess : IDisposable
{
private const Native.ProcessAccessFlags FlagsForInject = Native.ProcessAccessFlags.CreateThread |
Native.ProcessAccessFlags.QueryInformation |
Native.ProcessAccessFlags.VirtualMemoryOperation |
Native.ProcessAccessFlags.VirtualMemoryRead |
Native.ProcessAccessFlags.VirtualMemoryWrite;
private const Native.ProcessAccessFlags BasicFlags = Native.ProcessAccessFlags.QueryInformation;
private IntPtr _handle;
private bool _isHandleFull;
private bool? _is64Bit;
private ProcessStatus _status = ProcessStatus.Unknown;
private ProcessArchitecture _architecture = ProcessArchitecture.Unknown;
public InjectableProcess(uint pid)
{
Pid = pid;
}
public uint Pid { get; }
public bool Is64Bit => _is64Bit ??= NativeHelper.Is64BitProcess(Handle);
/// <summary>
/// Get a handle to the process. This is only guaranteed to have basic flags set.
/// </summary>
public IntPtr Handle
{
get
{
if (_handle == IntPtr.Zero)
_handle = NativeHelper.OpenProcess(BasicFlags, Pid);
return _handle;
}
}
/// <summary>
/// Gets a handle to the process that is guaranteed to have more flags set.
/// </summary>
public IntPtr FullHandle
{
get
{
if (!_isHandleFull)
{
Native.CloseHandle(_handle);
_handle = IntPtr.Zero;
}
if (_handle == IntPtr.Zero)
{
_handle = NativeHelper.OpenProcess(FlagsForInject, Pid);
_isHandleFull = true;
}
return _handle;
}
}
public ProcessStatus GetStatus()
{
if (_status != ProcessStatus.Unknown)
return _status;
try
{
// 64-bit process can target 32-bit, but not the other way around
if (!NativeHelper.In64BitProcess && NativeHelper.Is64BitProcess(Handle))
return _status = ProcessStatus.ArchitectureMismatch;
if (GetArchitecture() == ProcessArchitecture.Unknown)
return _status = ProcessStatus.NoRuntimeFound;
return _status = ProcessStatus.Ok;
}
catch (Exception)
{
return ProcessStatus.Unknown;
}
}
public ProcessArchitecture GetArchitecture()
{
if (_architecture != ProcessArchitecture.Unknown)
return _architecture;
try
{
using var process = Process.GetProcessById((int) Pid);
var modules = NativeHelper.GetModules(Pid);
bool HasModule(string s) =>
modules.Any(x => x.moduleName.Equals(s, StringComparison.InvariantCultureIgnoreCase));
// .NET 2 has mscoree and mscorwks
// .NET 4 has mscoree and clr
// .NET Core 3.1 has coreclr
// Some unity games have mono-2.0-bdwgc.dll
if (HasModule("mscoree.dll"))
{
if (HasModule("clr.dll"))
return _architecture = ProcessArchitecture.NetFrameworkV4;
if (HasModule("mscorwks.dll"))
return _architecture = ProcessArchitecture.NetFrameworkV2;
}
if (HasModule("coreclr.dll"))
return _architecture = ProcessArchitecture.NetCore;
// TODO: also check non-bleeding mono dll
if (HasModule("mono-2.0-bdwgc.dll"))
return _architecture = ProcessArchitecture.Mono;
return ProcessArchitecture.Unknown;
}
catch (Exception)
{
return ProcessArchitecture.Unknown;
}
}
public IInjector GetInjector()
{
var arch = GetArchitecture();
return arch switch
{
ProcessArchitecture.NetFrameworkV2 => new FrameworkV2Injector(),
ProcessArchitecture.NetFrameworkV4 => new FrameworkV4Injector(),
ProcessArchitecture.Mono => throw new NotImplementedException("Mono injector is not yet implemented"),
ProcessArchitecture.NetCore => new CoreClrInjector(),
ProcessArchitecture.Unknown => throw new Exception(
"Tried to inject into process with unknown architecture"),
_ => throw new NotSupportedException($"No injector found for architecture {arch}"),
};
}
public void Inject(string dllPath, string typeName, string methodName)
{
IInjector injector = GetInjector();
Inject(injector, dllPath, typeName, methodName);
}
public void Inject(IInjector injector, string dllPath, string typeName, string methodName) =>
injector.Inject(this, dllPath, typeName, methodName);
private void ReleaseUnmanagedResources()
{
if (_handle != IntPtr.Zero)
Native.CloseHandle(_handle);
}
public void Dispose()
{
ReleaseUnmanagedResources();
GC.SuppressFinalize(this);
}
~InjectableProcess()
{
ReleaseUnmanagedResources();
}
}
}