-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsection.cpp
More file actions
320 lines (281 loc) · 7.76 KB
/
Copy pathsection.cpp
File metadata and controls
320 lines (281 loc) · 7.76 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
#include "include/section.h"
ExtendedSection::ExtendedSection(HANDLE processHandle, ULONG sectionPageProtection, ULONG allocationAttributes, ULONG extendedParametersCount, POBJECT_ATTRIBUTES objectAttributes, PLARGE_INTEGER maximumSize, PMEM_EXTENDED_PARAMETER extendedParameters, HANDLE fileHandle)
{
NTSTATUS stat;
this->processhandle = processHandle;
this->sectionpageprotection = sectionPageProtection;
this->allocationattributes = allocationAttributes;
this->extendedparameterscount = extendedParametersCount;
this->extendedparameters = extendedParameters;
this->maximumsize = maximumSize;
stat = Sw3NtCreateSectionEx(&this->handle, SECTION_ALL_ACCESS, objectAttributes, maximumSize, sectionPageProtection, allocationAttributes, fileHandle, extendedParameters, extendedParametersCount);
if (stat != 0) {
printf("[%s] Failed at creating remote section (%x)\n", __FUNCTION__, stat);
handle = nullptr;
}
}
NTSTATUS ExtendedSection::map(SIZE_T mapSize, PLARGE_INTEGER sectionOffset, ULONG protect)
{
NTSTATUS stat;
if (handle == nullptr) {
return STATUS_INVALID_HANDLE;
}
LARGE_INTEGER offset = { 0 };
if (sectionOffset != nullptr) {
offset = *sectionOffset;
}
SIZE_T size = mapSize;
if (size == 0) {
if (maximumsize != nullptr) {
size = static_cast<SIZE_T>(maximumsize->QuadPart);
}
else {
return STATUS_INVALID_PARAMETER;
}
}
stat = Sw3NtMapViewOfSectionEx(handle, GetCurrentProcess(), &offset, &baseaddress, &size, 0, protect, extendedparameters, extendedparameterscount);
if (stat == 0) {
return 0;
}
return stat;
}
NTSTATUS ExtendedSection::map_remote(SIZE_T mapSize, PLARGE_INTEGER sectionOffset, ULONG protect)
{
NTSTATUS stat;
if (handle == nullptr) {
return STATUS_INVALID_HANDLE;
}
LARGE_INTEGER offset = { 0 };
if (sectionOffset != nullptr) {
offset = *sectionOffset;
}
SIZE_T size = mapSize;
if (size == 0) {
if (maximumsize != nullptr) {
size = static_cast<SIZE_T>(maximumsize->QuadPart);
}
else {
return STATUS_INVALID_PARAMETER;
}
}
stat = Sw3NtMapViewOfSectionEx(handle, processhandle, &offset, &baseaddress, &size, 0, protect, extendedparameters, extendedparameterscount);
if (stat == 0) {
return 0;
}
return stat;
}
NTSTATUS ExtendedSection::unmap()
{
NTSTATUS stat;
if (baseaddress == nullptr) {
return 0;
}
stat = Sw3NtUnmapViewOfSectionEx(GetCurrentProcess(), baseaddress, extendedparameterscount);
if (stat == 0) {
baseaddress = nullptr;
}
return stat;
}
NTSTATUS ExtendedSection::unmap_remote()
{
NTSTATUS stat;
if (baseaddress == nullptr) {
return 0;
}
stat = Sw3NtUnmapViewOfSectionEx(processhandle, baseaddress, extendedparameterscount);
if (stat == 0) {
baseaddress = nullptr;
}
return stat;
}
HANDLE ExtendedSection::get_file_handle()
{
return this->filehandle;
}
HANDLE ExtendedSection::get_handle()
{
return this->handle;
}
ULONG ExtendedSection::get_section_page_protection()
{
return this->sectionpageprotection;
}
ULONG ExtendedSection::get_extended_parameters_count()
{
return this->extendedparameterscount;
}
PLARGE_INTEGER ExtendedSection::get_maximum_size()
{
return this->maximumsize;
}
POBJECT_ATTRIBUTES ExtendedSection::get_object_attributes()
{
return this->objectattributes;
}
PMEM_EXTENDED_PARAMETER ExtendedSection::get_extended_parameters()
{
return this->extendedparameters;
}
PVOID ExtendedSection::get_base_address()
{
return this->baseaddress;
}
ExtendedSection::~ExtendedSection()
{
if (handle != nullptr) {
Sw3NtClose(handle);
}
}
NormalSection::NormalSection(HANDLE processHandle, HANDLE fileHandle, ULONG sectionPageProtection, ULONG allocationAttributes, PLARGE_INTEGER maximumSize, OBJECT_ATTRIBUTES objectAttributes)
{
NTSTATUS stat;
this->processhandle = processHandle;
this->filehandle = fileHandle;
this->sectionpageprotection = sectionPageProtection;
this->allocationattributes = allocationAttributes;
this->maximumsize = maximumSize;
this->objectattributes = objectAttributes;
stat = Sw3NtCreateSection(&this->handle, SECTION_ALL_ACCESS, &this->objectattributes, this->maximumsize, this->sectionpageprotection, this->allocationattributes, this->filehandle);
if (stat != 0) {
printf("[%s] Failed at creating section (%x)\n", __FUNCTION__, stat);
handle = nullptr;
}
}
NormalSection::NormalSection(HANDLE processHandle, PWSTR sectionName, ACCESS_MASK desiredAccess, OBJECT_ATTRIBUTES objectAttributes, HANDLE remoteHandle)
{
NTSTATUS status;
this->processhandle = processHandle;
this->filehandle = nullptr;
this->sectionpageprotection = 0;
this->handle = remoteHandle;
HANDLE localHandle = nullptr;
UNICODE_STRING name;
name.Buffer = sectionName;
name.Length = wcslen(sectionName) * sizeof(WCHAR);
name.MaximumLength = name.Length + sizeof(WCHAR); // Allow room for a null terminator if needed
#define OBJ_CASE_INSENSITIVE 0x00000040
InitializeObjectAttributes(&objectAttributes, &name, OBJ_CASE_INSENSITIVE, NULL, NULL);
status = Sw3NtOpenSection(&localHandle, desiredAccess, &objectAttributes);
if (!NT_SUCCESS(status)) {
if (status == STATUS_ACCESS_DENIED) {
return;
}
printf("[%s] Failed to open section %ws (%x)\n", __FUNCTION__, sectionName, status);
return;
}
SECTION_BASIC_INFORMATION sbi = {};
status = Sw3NtQuerySection(localHandle, SectionBasicInformation, &sbi, sizeof(sbi), NULL);
if (!NT_SUCCESS(status)) {
printf("[%s] Failed to query section information (%x)\n", __FUNCTION__, status);
CloseHandle(this->handle);
this->handle = nullptr;
return;
}
this->objectattributes = objectAttributes;
this->allocationattributes = sbi.Attributes;
this->maximumsize = new LARGE_INTEGER(sbi.MaximumSize);
CloseHandle(localHandle);
}
NTSTATUS NormalSection::map(SIZE_T mapSize, PLARGE_INTEGER sectionOffset, ULONG protect)
{
NTSTATUS stat;
if (handle == nullptr) {
return STATUS_INVALID_HANDLE;
}
LARGE_INTEGER offset = { 0 };
if (sectionOffset != nullptr) {
offset = *sectionOffset;
}
SIZE_T size = mapSize;
if (size == 0) {
if (maximumsize != nullptr) {
size = static_cast<SIZE_T>(maximumsize->QuadPart);
}
else {
return STATUS_INVALID_PARAMETER;
}
}
stat = Sw3NtMapViewOfSection(this->handle, GetCurrentProcess(), &baseaddress, 0, 0, &offset, &size, ViewUnmap, 0, protect);
if (stat == 0) {
return 0;
}
return stat;
}
NTSTATUS NormalSection::map_remote(SIZE_T mapSize, PLARGE_INTEGER sectionOffset, ULONG protect)
{
NTSTATUS stat;
if (handle == nullptr) {
return STATUS_INVALID_HANDLE;
}
LARGE_INTEGER offset = { 0 };
if (sectionOffset != nullptr) {
offset = *sectionOffset;
}
SIZE_T size = mapSize;
if (size == 0) {
if (maximumsize != nullptr) {
size = static_cast<SIZE_T>(maximumsize->QuadPart);
}
else {
return STATUS_INVALID_PARAMETER;
}
}
stat = Sw3NtMapViewOfSection(handle, processhandle, &baseaddress, 0, 0, &offset, &size, ViewUnmap, 0, protect);
if (stat == 0) {
return 0;
}
return stat;
}
NTSTATUS NormalSection::unmap()
{
NTSTATUS stat;
if (baseaddress == nullptr) {
return 0;
}
stat = Sw3NtUnmapViewOfSection(GetCurrentProcess(), baseaddress);
if (stat == 0) {
baseaddress = nullptr;
}
return stat;
}
NTSTATUS NormalSection::unmap_remote()
{
NTSTATUS stat;
if (baseaddress == nullptr) {
return 0;
}
stat = Sw3NtUnmapViewOfSection(processhandle, baseaddress);
if (stat == 0) {
baseaddress = nullptr;
}
return stat;
}
HANDLE NormalSection::get_file_handle()
{
return this->filehandle;
}
HANDLE NormalSection::get_handle()
{
return this->handle;
}
ULONG NormalSection::get_section_page_protection()
{
return this->sectionpageprotection;
}
PLARGE_INTEGER NormalSection::get_maximum_size()
{
return this->maximumsize;
}
OBJECT_ATTRIBUTES NormalSection::get_object_attributes()
{
return this->objectattributes;
}
PVOID NormalSection::get_base_address()
{
return this->baseaddress;
}
NormalSection::~NormalSection() {
if (handle != nullptr) {
Sw3NtClose(handle);
}
}