-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathProgram.cs
More file actions
421 lines (348 loc) · 12.9 KB
/
Program.cs
File metadata and controls
421 lines (348 loc) · 12.9 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
//Output file is named IJ not InjectorLoader because "InjectorLoader" is picked up by some antiviruses
namespace InjectorLoader
{
/* Unused, but shows the layout we use in the byte array
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi, Size = 536)]
struct UserData
{
[FieldOffset(0)]
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
public byte[] ipcChannelNameRead;//Name will be 30 characters
[FieldOffset(256)]
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
public byte[] ipcChannelNameWrite;//Name will be 30 characters
[FieldOffset(512)]
public int hWnd;
[FieldOffset(516)]
public int controllerIndex;
[FieldOffset(520)]
public int allowedMouseHandle;
[FieldOffset(524)]
public bool updateAbsoluteFlagInMouseMessage;
[FieldOffset(525)]
public bool HookGetCursorPos;
[FieldOffset(526)]
public bool HookGetForegroundWindow;
[FieldOffset(527)]
public bool HookGetAsyncKeyState;
[FieldOffset(528)]
public bool HookGetKeyState;
[FieldOffset(529)]
public bool HookCallWindowProcW;
[FieldOffset(530)]
public bool HookRegisterRawInputDevices;
[FieldOffset(531)]
public bool HookSetCursorPos;
[FieldOffset(532)]
public bool HookXInput;
[FieldOffset(533)]
public bool useLegacyInput;
[FieldOffset(534)]
public bool hookMouseVisibility;
};*/
static class Program
{
class Injector32
{
[DllImport("EasyHook32.dll", CharSet = CharSet.Ansi)]
public static extern int RhInjectLibrary(
uint InTargetPID,
uint InWakeUpTID,
uint InInjectionOptions,
[MarshalAsAttribute(UnmanagedType.LPWStr)] string InLibraryPath_x86,
[MarshalAsAttribute(UnmanagedType.LPWStr)] string InLibraryPath_x64,
IntPtr InPassThruBuffer,
uint InPassThruSize
);
/*EASYHOOK_NT_EXPORT RhCreateAndInject(
WCHAR* InEXEPath,
WCHAR* InCommandLine,
ULONG InProcessCreationFlags, (documentation is missing this field)
ULONG InInjectionOptions,
WCHAR* InLibraryPath_x86,
WCHAR* InLibraryPath_x64,
PVOID InPassThruBuffer,
ULONG InPassThruSize,
ULONG* OutProcessId);*/
[DllImport("EasyHook32.dll", CharSet = CharSet.Ansi)]
public static extern int RhCreateAndInject(
[MarshalAsAttribute(UnmanagedType.LPWStr)] string InEXEPath,
[MarshalAsAttribute(UnmanagedType.LPWStr)] string InCommandLine,
uint InProcessCreationFlags,
uint InInjectionOptions,
[MarshalAsAttribute(UnmanagedType.LPWStr)] string InLibraryPath_x86,
[MarshalAsAttribute(UnmanagedType.LPWStr)] string InLibraryPath_x64,
IntPtr InPassThruBuffer,
uint InPassThruSize,
IntPtr OutProcessId //Pointer to a UINT (the PID of the new process)
);
}
class Injector64
{
[DllImport("EasyHook64.dll", CharSet = CharSet.Ansi)]
public static extern int RhInjectLibrary(
uint InTargetPID,
uint InWakeUpTID,
uint InInjectionOptions,
[MarshalAsAttribute(UnmanagedType.LPWStr)] string InLibraryPath_x86,
[MarshalAsAttribute(UnmanagedType.LPWStr)] string InLibraryPath_x64,
IntPtr InPassThruBuffer,
uint InPassThruSize
);
[DllImport("EasyHook64.dll", CharSet = CharSet.Ansi)]
public static extern int RhCreateAndInject(
[MarshalAsAttribute(UnmanagedType.LPWStr)] string InEXEPath,
[MarshalAsAttribute(UnmanagedType.LPWStr)] string InCommandLine,
uint InProcessCreationFlags,
uint InInjectionOptions,
[MarshalAsAttribute(UnmanagedType.LPWStr)] string InLibraryPath_x86,
[MarshalAsAttribute(UnmanagedType.LPWStr)] string InLibraryPath_x64,
IntPtr InPassThruBuffer,
uint InPassThruSize,
IntPtr OutProcessId //Pointer to a UINT (the PID of the new process)
);
}
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
static extern bool CreateProcess(
string lpApplicationName,
string lpCommandLine,
//ref SECURITY_ATTRIBUTES lpProcessAttributes,
//ref SECURITY_ATTRIBUTES lpThreadAttributes,
IntPtr lpProcessAttributes,
IntPtr lpThreadAttributes,
bool bInheritHandles,
uint dwCreationFlags,
IntPtr lpEnvironment,
string lpCurrentDirectory,
//IntPtr lpStartupInfo,
[In] ref STARTUPINFO lpStartupInfo,
out PROCESS_INFORMATION lpProcessInformation);
[DllImport("kernel32.dll")]
static extern uint ResumeThread(IntPtr hThread);
[DllImport("user32.dll")]
static extern uint WaitForInputIdle(IntPtr hProcess, uint dwMilliseconds);
[DllImport("kernel32.dll")]
static extern int SuspendThread(IntPtr hThread);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
struct STARTUPINFO
{
public Int32 cb;
public string lpReserved;
public string lpDesktop;
public string lpTitle;
public Int32 dwX;
public Int32 dwY;
public Int32 dwXSize;
public Int32 dwYSize;
public Int32 dwXCountChars;
public Int32 dwYCountChars;
public Int32 dwFillAttribute;
public Int32 dwFlags;
public Int16 wShowWindow;
public Int16 cbReserved2;
public IntPtr lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}
[StructLayout(LayoutKind.Sequential)]
struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public int dwProcessId;
public int dwThreadId;
}
[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
public int nLength;
public IntPtr lpSecurityDescriptor;
public int bInheritHandle;
}
public static void Main(string[] args)
{
const int argsLengthHooksCPP = 20;
const int argsLengthStartupHook = 10;
//dllpath, exePath, base64CmdArgs, useAppdataSwitch, appdataSwitchIndex, dinputHookEnabled, findWindowHookEnabled, controllerIndex, findMutexHookEnabled, mutexTargets
if (args.Length == argsLengthStartupHook)
{
bool Bfs(string s) => s.ToLower().Equals("true");
int ntFwh = CreateAndInjectStartupHook(
args[0],
args[1],
args[2],
Bfs(args[3]),
int.Parse(args[4]),
Bfs(args[5]),
Bfs(args[6]),
byte.Parse(args[7]),
Bfs(args[8]),
args[9]
);
Environment.Exit(ntFwh);
return;
}
if (args.Length != argsLengthHooksCPP)
{
throw new ArgumentException($"Need exactly {argsLengthHooksCPP} arguments");
}
//Arguments
int i = 0;
int.TryParse(args[i++], out int pid);
string injectionDllPath = args[i++];
int.TryParse(args[i++], out int _hWnd);
IntPtr hWnd = (IntPtr)_hWnd;
string ipcChannelNameRead = args[i++];
string ipcChannelNameWrite = args[i++];
int.TryParse(args[i++], out int controllerIndex);
int.TryParse(args[i++], out int allowedMouseHandle);
bool updateAbsoluteFlagInMouseMessage = args[i++].ToLower().Equals("true");
bool useLegacyInput = args[i++].ToLower().Equals("true");
bool nextBool() => args[i++].ToLower().Equals("true");
bool HookGetCursorPos = nextBool();
bool HookGetForegroundWindow = nextBool();
bool HookGetAsyncKeyState = nextBool();
bool HookGetKeyState = nextBool();
bool HookGetKeyboardState = nextBool();
bool HookCallWindowProcW = nextBool();
bool HookRegisterRawInputDevices = nextBool();
bool HookSetCursorPos = nextBool();
bool HookXInput = nextBool();
bool hookMouseVisibility = nextBool();
bool HookDInput = nextBool();
const int size = 1024;
byte[] data = new byte[size];
byte[] toByteArray(string str)
{
List<byte> c = str.ToCharArray().Select(_x => (byte)_x).ToList();
for (int _i = c.Count; _i < 256; _i++)
c.Add(0);
return c.ToArray();
}
void writeInt(int num, int offset)
{
data[offset] = (byte)(num >> 24);
data[offset+1] = (byte)(num >> 16);
data[offset+2] = (byte)(num >> 8);
data[offset+3] = (byte)(num);
}
void writeBool(bool b, int offset)
{
data[offset] = b == true ? (byte)1 : (byte)0;
}
var str1 = toByteArray(ipcChannelNameRead);
for (int j = 0; j < str1.Length; j++)
data[j] = str1[j];
var str2 = toByteArray(ipcChannelNameWrite);
for (int j = 0; j < str1.Length; j++)
data[256 + j] = str2[j];
writeInt((int)hWnd, 512);
writeInt(controllerIndex, 516);
writeInt(allowedMouseHandle, 520);
int x = 524;
writeBool(updateAbsoluteFlagInMouseMessage, x++);
writeBool(HookGetCursorPos, x++);
writeBool(HookGetForegroundWindow, x++);
writeBool(HookGetAsyncKeyState, x++);
writeBool(HookGetKeyState, x++);
writeBool(HookGetKeyboardState, x++);
writeBool(HookCallWindowProcW, x++);
writeBool(HookRegisterRawInputDevices, x++);
writeBool(HookSetCursorPos, x++);
writeBool(HookXInput, x++);
writeBool(useLegacyInput, x++);
writeBool(hookMouseVisibility, x++);
writeBool(HookDInput, x++);
IntPtr ptr = Marshal.AllocHGlobal(size);
Marshal.Copy(data, 0, ptr, size);
int nt = 0;
if (Environment.Is64BitProcess)
nt = Injector64.RhInjectLibrary((uint)pid, 0, 0, "", injectionDllPath, ptr, (uint)size);
else
nt = Injector32.RhInjectLibrary((uint)pid, 0, 0, injectionDllPath, "", ptr, (uint)size);
//Set exit code
Environment.Exit((int)nt);
}
private static int CreateAndInjectStartupHook(string hookDllPath, string exePath, string base64CommandLineArgs, bool useAppdataSwitch, int appdataSwitchIndex, bool dinputHookEnabled, bool findWindowHookEnabled, byte controllerIndex, bool findMutexHookEnabled, string mutexTargets)
{
string cmdLineArgs = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(base64CommandLineArgs));
IntPtr pOutPID = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(uint)));
//"hl2_singleton_mutex&&&&&ValveHalfLifeLauncherMutex&&&&&Overkill Engine Game"
var targetsBytes = Encoding.Unicode.GetBytes(mutexTargets);
int targetsBytesLength = targetsBytes.Length;
int size = 64 + targetsBytesLength;
var data = new byte[size];
data[0] = dinputHookEnabled ? (byte)1 : (byte)0;
data[1] = findWindowHookEnabled ? (byte)1 : (byte)0;
data[2] = controllerIndex;
data[3] = useAppdataSwitch ? (byte) 0 : (byte) 1;
data[4] = findMutexHookEnabled ? (byte) 1 : (byte) 0;
data[5] = (byte)(targetsBytesLength >> 24);
data[6] = (byte)(targetsBytesLength >> 16);
data[7] = (byte)(targetsBytesLength >> 8);
data[8] = (byte) targetsBytesLength;
Array.Copy(targetsBytes, 0, data, 9, targetsBytesLength);
IntPtr ptr = Marshal.AllocHGlobal(size);
Marshal.Copy(data, 0, ptr, size);
if (!(appdataSwitchIndex > 0 && useAppdataSwitch))
{
int ret = Environment.Is64BitProcess ?
Injector64.RhCreateAndInject(exePath, cmdLineArgs, 0, 0, "", hookDllPath, ptr, (uint)size, pOutPID) :
Injector32.RhCreateAndInject(exePath, cmdLineArgs, 0, 0, hookDllPath, "", ptr, (uint)size, pOutPID);
return ret;
}
var appdataIndex = "index" + appdataSwitchIndex;
var sb = new StringBuilder();
IDictionary envVars = Environment.GetEnvironmentVariables();
var username = Environment.UserName;
envVars["USERPROFILE"] = $@"C:\Users\{username}\UniversalSplitScreen\{appdataIndex}";
envVars["HOMEPATH"] = $@"\Users\{username}\UniversalSplitScreen\{appdataIndex}";
envVars["APPDATA"] = $@"C:\Users\{username}\UniversalSplitScreen\{appdataIndex}\AppData\Roaming";
envVars["LOCALAPPDATA"] = $@"C:\Users\{username}\UniversalSplitScreen\{appdataIndex}\AppData\Local";
//Some games will crash if the directories don't exist
Directory.CreateDirectory(envVars["USERPROFILE"].ToString());
Directory.CreateDirectory(Path.Combine(envVars["USERPROFILE"].ToString(), "Documents"));
Directory.CreateDirectory(envVars["APPDATA"].ToString());
Directory.CreateDirectory(envVars["LOCALAPPDATA"].ToString());
foreach (object envVarKey in envVars.Keys)
{
if (envVarKey != null)
{
string key = envVarKey.ToString();
string value = envVars[envVarKey].ToString();
sb.Append(key);
sb.Append("=");
sb.Append(value);
sb.Append("\0");
}
}
sb.Append("\0");
byte[] envBytes = Encoding.Unicode.GetBytes(sb.ToString());
IntPtr envPtr = Marshal.AllocHGlobal(envBytes.Length);
Marshal.Copy(envBytes, 0, envPtr, envBytes.Length);
string directoryPath = Path.GetDirectoryName(exePath);
STARTUPINFO startup = new STARTUPINFO();
startup.cb = Marshal.SizeOf(startup);
//startup.dwFlags = 0x1;//STARTF_USESHOWWINDOW
//startup.wShowWindow = 1;//SW_SHOWNORMAL
bool success = CreateProcess(exePath, cmdLineArgs, IntPtr.Zero, IntPtr.Zero, false,
0x00000004 | 0x00000400, //Suspended | CREATE_UNICODE_ENVIRONMENT
envPtr, directoryPath, ref startup, out PROCESS_INFORMATION processInformation);
if (!success) return -1;
int injectResult = Environment.Is64BitProcess ?
Injector64.RhInjectLibrary((uint)processInformation.dwProcessId, 0, 0, "", hookDllPath, ptr, (uint)size) :
Injector32.RhInjectLibrary((uint)processInformation.dwProcessId, 0, 0, hookDllPath, "", ptr, (uint)size);
ResumeThread(processInformation.hThread);
return injectResult;
}
}
}