-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInjector.cs
93 lines (77 loc) · 3.33 KB
/
Injector.cs
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
using System.Runtime.InteropServices;
using System.Text;
namespace XybLauncher
{
internal class Injector
{
public const int PROCESS_CREATE_THREAD = 2;
public const int PROCESS_VM_OPERATION = 8;
public const int PROCESS_VM_WRITE = 32;
public const int PROCESS_VM_READ = 16;
public const int PROCESS_QUERY_INFORMATION = 1024;
public const uint PAGE_READWRITE = 4;
public const uint MEM_COMMIT = 4096;
public const uint MEM_RESERVE = 8192;
[DllImport("kernel32.dll")]
public static extern IntPtr OpenThread(
int dwDesiredAccess,
bool bInheritHandle,
int dwThreadId);
[DllImport("kernel32.dll")]
public static extern int SuspendThread(IntPtr hThread);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("kernel32.dll")]
public static extern int ResumeThread(IntPtr hThread);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool AllocConsole();
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetConsoleCtrlHandler(
Injector.HandlerRoutine HandlerRoutine,
bool Add);
[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(
int dwDesiredAccess,
bool bInheritHandle,
int dwProcessId);
[DllImport("kernel32", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr VirtualAllocEx(
IntPtr hProcess,
IntPtr lpAddress,
uint dwSize,
uint flAllocationType,
uint flProtect);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool WriteProcessMemory(
IntPtr hProcess,
IntPtr lpBaseAddress,
byte[] lpBuffer,
uint nSize,
out UIntPtr lpNumberOfBytesWritten);
[DllImport("kernel32.dll")]
public static extern IntPtr CreateRemoteThread(
IntPtr hProcess,
IntPtr lpThreadAttributes,
uint dwStackSize,
IntPtr lpStartAddress,
IntPtr lpParameter,
uint dwCreationFlags,
IntPtr lpThreadId);
public static void Inject(int processId, string path)
{
IntPtr hProcess = Injector.OpenProcess(1082, false, processId);
IntPtr procAddress = Injector.GetProcAddress(Injector.GetModuleHandle("kernel32.dll"), "LoadLibraryA");
uint num1 = (uint)((path.Length + 1) * Marshal.SizeOf(typeof(char)));
IntPtr num2 = Injector.VirtualAllocEx(hProcess, IntPtr.Zero, num1, 12288U, 4U);
Injector.WriteProcessMemory(hProcess, num2, Encoding.Default.GetBytes(path), num1, out UIntPtr _);
Injector.CreateRemoteThread(hProcess, IntPtr.Zero, 0U, procAddress, num2, 0U, IntPtr.Zero);
}
public delegate bool HandlerRoutine(int dwCtrlType);
}
}