-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathsection.c
427 lines (344 loc) · 11.6 KB
/
section.c
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
422
423
424
425
426
427
/**
* @file section.c
* @brief Modify sections.
*
* @author Chen Zhenshuo (chenzs108@outlook.com)
* @version 1.0
* @date 2020-01-12
* @par GitHub
* https://github.com/czs108/
*/
#include "section.h"
#include "utility/error_handling.h"
#include <assert.h>
#include <string.h>
#include <stdbool.h>
/**
* @brief A callback method, called when enumerating sections.
*
* @param image_info The PE image.
* @param header The @em IMAGE_SECTION_HEADER structure.
* @param arg An optional custom argument.
* @return An optional custom value.
*
* @see EnumSection()
*
*/
typedef DWORD(*LPFN_ENUM_SECTION_CALLBACK)(
const PE_IMAGE_INFO *const image_info,
IMAGE_SECTION_HEADER *const header,
void *const arg);
/******************************************************************************/
// Callback methods
/******************************************************************************/
/**
* @brief
* A @em ::LPFN_ENUM_SECTION_CALLBACK method,
* used to clear section names.
*
* @param image_info The PE image.
* @param header The @em IMAGE_SECTION_HEADER structure.
* @param arg Useless.
* @return Useless.
*/
static DWORD ClearNameCallBack(
const PE_IMAGE_INFO *const image_info,
IMAGE_SECTION_HEADER *const header,
void *const arg);
/**
* @brief
* A @em ::LPFN_ENUM_SECTION_CALLBACK method,
* used to get the number of sections that can be encrypted.
*
* @param image_info The PE image.
* @param header The @em IMAGE_SECTION_HEADER structure.
* @param[in, out] count The number of sections that can be encrypted.
* @return Useless.
*/
static DWORD CheckEncryptableCallBack(
const PE_IMAGE_INFO *const image_info,
IMAGE_SECTION_HEADER *const header,
void *const count);
/**
* @brief
* A @em ::LPFN_ENUM_SECTION_CALLBACK method,
* used to Encrypt sections.
*
* @param image_info The PE image.
* @param header The @em IMAGE_SECTION_HEADER structure.
* @param[in, out] encry_info The space where the encryption information will be saved.
* @return Useless.
*/
static DWORD EncryCallBack(
const PE_IMAGE_INFO *const image_info,
IMAGE_SECTION_HEADER *const header,
void *const encry_info);
/******************************************************************************/
/**
* @brief Enumerate sections.
*
* @param image_info The PE image.
* @param callback
* A @em ::LPFN_ENUM_SECTION_CALLBACK method,
* it will be called on each @em IMAGE_SECTION_HEADER structure.
* @param arg An optional custom argument, it will be passed to @em callback.
*/
static void EnumSections(
const PE_IMAGE_INFO *const image_info,
const LPFN_ENUM_SECTION_CALLBACK callback,
void *const arg);
/**
* @brief Check if a section can be encrypted.
*
* @param header The @em IMAGE_SECTION_HEADER structure.
* @return @em true if the section can be encrypted, otherwise @em false.
*/
static bool IsEncryptable(
const IMAGE_SECTION_HEADER *const header);
/**
* @brief Calculate the size of a section, not including the zero at the end.
*
* @param image_info The PE image.
* @param header The @em IMAGE_SECTION_HEADER structure.
* @return The minimum size of the section.
*/
static DWORD CalcMinSize(
const PE_IMAGE_INFO *const image_info,
const IMAGE_SECTION_HEADER *const header);
/**
* @brief Save the encryption information of a section.
*
* @param buffer A buffer where the information will be saved.
* @param rva The relative virtual address of the section.
* @param size The size of the section.
* @return The new address to save information.
*/
static ENCRY_INFO *SaveEncryInfo(
ENCRY_INFO *const buffer,
const DWORD rva,
const DWORD size);
bool CanAppendNewSection(
const PE_IMAGE_INFO *const image_info)
{
assert(image_info != NULL);
const DWORD section_align = image_info->nt_header->OptionalHeader.SectionAlignment;
// Get the size of the current headers
const DWORD headers_size = CalcHeadersSize(image_info->image_base, NULL, NULL);
const DWORD headers_virtual_size = Align(headers_size, section_align);
// Calculate the new size of headers after appending a section
const DWORD new_headers_size = headers_size + sizeof(IMAGE_SECTION_HEADER);
const DWORD new_headers_virtual_size = Align(new_headers_size, section_align);
// Check the change of size after appending a section
return new_headers_virtual_size == headers_virtual_size;
}
bool AppendNewSection(
PE_IMAGE_INFO *const image_info,
const CHAR *const name,
const DWORD size,
IMAGE_SECTION_HEADER *const header)
{
assert(image_info != NULL);
assert(header != NULL);
assert(CanAppendNewSection(image_info));
ZeroMemory(header, sizeof(IMAGE_SECTION_HEADER));
const WORD section_num = image_info->nt_header->FileHeader.NumberOfSections;
assert(section_num > 0);
// Set the section attribute
header->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA
| IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE;
// Set the section name
if (name != NULL)
{
const size_t name_length = strnlen(name, IMAGE_SIZEOF_SHORT_NAME);
CopyMemory(header->Name, name, name_length);
}
// Align the section size
const DWORD file_align = image_info->nt_header->OptionalHeader.FileAlignment;
const DWORD section_align = image_info->nt_header->OptionalHeader.SectionAlignment;
const DWORD raw_size = Align(size, file_align);
const DWORD virtual_size = Align(size, section_align);
header->SizeOfRawData = raw_size;
header->Misc.VirtualSize = size;
// Get the size of the current headers
const DWORD headers_size = CalcHeadersSize(image_info->image_base, NULL, NULL);
const DWORD headers_raw_size = Align(headers_size, file_align);
// Calculate the new size of headers after appending a section
const DWORD new_headers_size = headers_size + sizeof(IMAGE_SECTION_HEADER);
const DWORD new_headers_raw_size = Align(new_headers_size, file_align);
// Get the offset of headers
assert(new_headers_raw_size >= headers_raw_size);
const DWORD headers_raw_offset = new_headers_raw_size - headers_raw_size;
// Set the address of the section
const IMAGE_SECTION_HEADER *const last_section_header =
&image_info->section_header[section_num - 1];
const DWORD last_section_end = last_section_header->PointerToRawData
+ last_section_header->SizeOfRawData;
header->VirtualAddress = image_info->image_size;
header->PointerToRawData = last_section_end + headers_raw_offset;
/* Adjust the PE_IMAGE_INFO structure */
// Allocate a new image
const DWORD new_image_size = image_info->image_size + virtual_size;
BYTE *const new_image_base = (BYTE*)VirtualAlloc(NULL,
new_image_size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if (new_image_base == NULL)
{
SetLastErrorCode();
return false;
}
// Move the image
MoveMemory(new_image_base, image_info->image_base, image_info->image_size);
VirtualFree(image_info->image_base, 0, MEM_RELEASE);
image_info->nt_header = (IMAGE_NT_HEADERS*)
(new_image_base + ((BYTE*)image_info->nt_header - image_info->image_base));
image_info->section_header = (IMAGE_SECTION_HEADER*)
(new_image_base + ((BYTE*)image_info->section_header - image_info->image_base));
image_info->image_base = new_image_base;
image_info->image_size = new_image_size;
image_info->nt_header->OptionalHeader.CheckSum = 0;
image_info->nt_header->OptionalHeader.SizeOfImage = new_image_size;
image_info->nt_header->OptionalHeader.SizeOfInitializedData += raw_size;
image_info->nt_header->OptionalHeader.SizeOfHeaders = new_headers_raw_size;
if (headers_raw_offset > 0)
{
// Adjust the address of sections
for (WORD i = 0; i != section_num; ++i)
{
image_info->section_header[i].PointerToRawData += headers_raw_offset;
}
}
CopyMemory(&image_info->section_header[section_num],
header, sizeof(IMAGE_SECTION_HEADER));
++image_info->nt_header->FileHeader.NumberOfSections;
return true;
}
WORD GetEncryptableSectionNumber(
const PE_IMAGE_INFO *const image_info)
{
assert(image_info != NULL);
WORD count = 0;
EnumSections(image_info, &CheckEncryptableCallBack, &count);
return count;
}
WORD EncryptSections(
const PE_IMAGE_INFO *const image_info,
ENCRY_INFO *const encry_info)
{
assert(image_info != NULL);
const WORD encry_count = GetEncryptableSectionNumber(image_info);
if (encry_info != NULL)
{
EnumSections(image_info, &EncryCallBack, (void*)&encry_info);
}
return encry_count;
}
void ClearSectionNames(
const PE_IMAGE_INFO *const image_info)
{
assert(image_info != NULL);
EnumSections(image_info, &ClearNameCallBack, NULL);
}
void EnumSections(
const PE_IMAGE_INFO *const image_info,
const LPFN_ENUM_SECTION_CALLBACK callback,
void *const arg)
{
assert(image_info != NULL);
assert(callback != NULL);
const WORD section_num = image_info->nt_header->FileHeader.NumberOfSections;
IMAGE_SECTION_HEADER *const header = image_info->section_header;
for (WORD i = 0; i != section_num; ++i)
{
callback(image_info, header + i, arg);
}
}
DWORD CalcMinSize(
const PE_IMAGE_INFO *const image_info,
const IMAGE_SECTION_HEADER *const header)
{
assert(image_info != NULL);
assert(header != NULL);
DWORD size = header->SizeOfRawData;
const BYTE *data = RvaToVa(image_info, header->VirtualAddress) + (size - 1);
while (size > 0 && *data == 0)
{
--data;
--size;
}
return size;
}
ENCRY_INFO *SaveEncryInfo(
ENCRY_INFO *const buffer,
const DWORD rva,
const DWORD size)
{
assert(buffer != NULL);
buffer->rva = rva;
buffer->size = size;
return buffer + 1;
}
DWORD ClearNameCallBack(
const PE_IMAGE_INFO *const image_info,
IMAGE_SECTION_HEADER *const header,
void *const arg)
{
assert(header != NULL);
ZeroMemory(header->Name, sizeof(header->Name));
return 0;
}
DWORD CheckEncryptableCallBack(
const PE_IMAGE_INFO *const image_info,
IMAGE_SECTION_HEADER *const header,
void *const count)
{
assert(header != NULL);
assert(count != NULL);
if (IsEncryptable(header))
{
*(WORD*)count += 1;
}
return 0;
}
DWORD EncryCallBack(
const PE_IMAGE_INFO *const image_info,
IMAGE_SECTION_HEADER *const header,
void *const encry_info)
{
assert(image_info != NULL);
assert(header != NULL);
assert(encry_info != NULL);
if (IsEncryptable(header) != true)
{
return 0;
}
DWORD min_size = CalcMinSize(image_info, header);
if (min_size > 0)
{
// Encrypt the section
const DWORD rva = header->VirtualAddress;
EncryptData(RvaToVa(image_info, rva), min_size);
// Save the encryption information
ENCRY_INFO *buffer = *(ENCRY_INFO**)encry_info;
*(ENCRY_INFO**)encry_info = SaveEncryInfo(buffer, rva, min_size);
header->Characteristics |= IMAGE_SCN_MEM_WRITE;
}
return 0;
}
bool IsEncryptable(
const IMAGE_SECTION_HEADER *const header)
{
assert(header != NULL);
// The sections can be encrypted
static const CHAR *const names[] =
{
".text", ".data", ".rdata", "CODE", "DATA"
};
for (size_t i = 0; i != sizeof(names) / sizeof(names[0]); ++i)
{
if (strncmp((CHAR*)header->Name, names[i],
IMAGE_SIZEOF_SHORT_NAME) == 0)
{
return true;
}
}
return false;
}