-
Notifications
You must be signed in to change notification settings - Fork 402
/
utility.cpp
840 lines (783 loc) · 31.5 KB
/
utility.cpp
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
////////////////////////////////////////////////////////////////////////////////
//
// Visual Leak Detector - Various Utility Functions
// Copyright (c) 2005-2009 Dan Moulding
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
// See COPYING.txt for the full terms of the GNU Lesser General Public License.
//
////////////////////////////////////////////////////////////////////////////////
#include <cassert>
#include <cstdio>
#include <windows.h>
#if _WIN32_WINNT < 0x0600 // Windows XP or earlier, no GetProcessIdOfThread()
#include <winternl.h>
#endif
#ifndef __out_xcount
#define __out_xcount(x) // Workaround for the specstrings.h bug in the Platform SDK.
#endif
#define DBGHELP_TRANSLATE_TCHAR
#include <dbghelp.h> // Provides portable executable (PE) image access functions.
#define VLDBUILD // Declares that we are building Visual Leak Detector.
#include "utility.h" // Provides various utility functions and macros.
#include "vldheap.h" // Provides internal new and delete operators.
// Imported Global Variables
extern CRITICAL_SECTION imagelock;
// Global variables.
static BOOL reportdelay = FALSE; // If TRUE, we sleep for a bit after calling OutputDebugString to give the debugger time to catch up.
static FILE *reportfile = NULL; // Pointer to the file, if any, to send the memory leak report to.
static BOOL reporttodebugger = TRUE; // If TRUE, a copy of the memory leak report will be sent to the debugger for display.
static encoding_e reportencoding = ascii; // Output encoding of the memory leak report.
// dumpmemorya - Dumps a nicely formatted rendition of a region of memory.
// Includes both the hex value of each byte and its ASCII equivalent (if
// printable).
//
// - address (IN): Pointer to the beginning of the memory region to dump.
//
// - size (IN): The size, in bytes, of the region to dump.
//
// Return Value:
//
// None.
//
VOID dumpmemorya (LPCVOID address, SIZE_T size)
{
WCHAR ascdump [18] = {0};
SIZE_T ascindex;
BYTE byte;
SIZE_T byteindex;
SIZE_T bytesdone;
SIZE_T dumplen;
WCHAR formatbuf [BYTEFORMATBUFFERLENGTH];
WCHAR hexdump [HEXDUMPLINELENGTH] = {0};
SIZE_T hexindex;
// Each line of output is 16 bytes.
if ((size % 16) == 0) {
// No padding needed.
dumplen = size;
}
else {
// We'll need to pad the last line out to 16 bytes.
dumplen = size + (16 - (size % 16));
}
// For each byte of data, get both the ASCII equivalent (if it is a
// printable character) and the hex representation.
bytesdone = 0;
for (byteindex = 0; byteindex < dumplen; byteindex++) {
hexindex = 3 * ((byteindex % 16) + ((byteindex % 16) / 4)); // 3 characters per byte, plus a 3-character space after every 4 bytes.
ascindex = (byteindex % 16) + (byteindex % 16) / 8; // 1 character per byte, plus a 1-character space after every 8 bytes.
if (byteindex < size) {
byte = ((PBYTE)address)[byteindex];
_snwprintf_s(formatbuf, BYTEFORMATBUFFERLENGTH, _TRUNCATE, L"%.2X ", byte);
formatbuf[3] = '\0';
wcsncpy_s(hexdump + hexindex, HEXDUMPLINELENGTH - hexindex, formatbuf, 4);
if (isgraph(byte)) {
ascdump[ascindex] = (WCHAR)byte;
}
else {
ascdump[ascindex] = L'.';
}
}
else {
// Add padding to fill out the last line to 16 bytes.
wcsncpy_s(hexdump + hexindex, HEXDUMPLINELENGTH - hexindex, L" ", 4);
ascdump[ascindex] = L'.';
}
bytesdone++;
if ((bytesdone % 16) == 0) {
// Print one line of data for every 16 bytes. Include the
// ASCII dump and the hex dump side-by-side.
report(L" %s %s\n", hexdump, ascdump);
}
else {
if ((bytesdone % 8) == 0) {
// Add a spacer in the ASCII dump after every 8 bytes.
ascdump[ascindex + 1] = L' ';
}
if ((bytesdone % 4) == 0) {
// Add a spacer in the hex dump after every 4 bytes.
wcsncpy_s(hexdump + hexindex + 3, HEXDUMPLINELENGTH - hexindex - 3, L" ", 4);
}
}
}
}
// dumpmemoryw - Dumps a nicely formatted rendition of a region of memory.
// Includes both the hex value of each byte and its Unicode equivalent.
//
// - address (IN): Pointer to the beginning of the memory region to dump.
//
// - size (IN): The size, in bytes, of the region to dump.
//
// Return Value:
//
// None.
//
VOID dumpmemoryw (LPCVOID address, SIZE_T size)
{
BYTE byte;
SIZE_T byteindex;
SIZE_T bytesdone;
SIZE_T dumplen;
WCHAR formatbuf [BYTEFORMATBUFFERLENGTH];
WCHAR hexdump [HEXDUMPLINELENGTH] = {0};
SIZE_T hexindex;
WORD word;
WCHAR unidump [18] = {0};
SIZE_T uniindex;
// Each line of output is 16 bytes.
if ((size % 16) == 0) {
// No padding needed.
dumplen = size;
}
else {
// We'll need to pad the last line out to 16 bytes.
dumplen = size + (16 - (size % 16));
}
// For each word of data, get both the Unicode equivalent and the hex
// representation.
bytesdone = 0;
for (byteindex = 0; byteindex < dumplen; byteindex++) {
hexindex = 3 * ((byteindex % 16) + ((byteindex % 16) / 4)); // 3 characters per byte, plus a 3-character space after every 4 bytes.
uniindex = ((byteindex / 2) % 8) + ((byteindex / 2) % 8) / 8; // 1 character every other byte, plus a 1-character space after every 8 bytes.
if (byteindex < size) {
byte = ((PBYTE)address)[byteindex];
_snwprintf_s(formatbuf, BYTEFORMATBUFFERLENGTH, _TRUNCATE, L"%.2X ", byte);
formatbuf[BYTEFORMATBUFFERLENGTH - 1] = '\0';
wcsncpy_s(hexdump + hexindex, HEXDUMPLINELENGTH - hexindex, formatbuf, 4);
if (((byteindex % 2) == 0) && ((byteindex + 1) < dumplen)) {
// On every even byte, print one character.
word = ((PWORD)address)[byteindex / 2];
if ((word == 0x0000) || (word == 0x0020)) {
unidump[uniindex] = L'.';
}
else {
unidump[uniindex] = word;
}
}
}
else {
// Add padding to fill out the last line to 16 bytes.
wcsncpy_s(hexdump + hexindex, HEXDUMPLINELENGTH - hexindex, L" ", 4);
unidump[uniindex] = L'.';
}
bytesdone++;
if ((bytesdone % 16) == 0) {
// Print one line of data for every 16 bytes. Include the
// ASCII dump and the hex dump side-by-side.
report(L" %s %s\n", hexdump, unidump);
}
else {
if ((bytesdone % 8) == 0) {
// Add a spacer in the ASCII dump after every 8 bytes.
unidump[uniindex + 1] = L' ';
}
if ((bytesdone % 4) == 0) {
// Add a spacer in the hex dump after every 4 bytes.
wcsncpy_s(hexdump + hexindex + 3, HEXDUMPLINELENGTH - hexindex - 3, L" ", 4);
}
}
}
}
// findimport - Determines if the specified module imports the named import
// from the named exporting module.
//
// - importmodule (IN): Handle (base address) of the module to be searched to
// see if it imports the specified import.
//
// - exportmodule (IN): Handle (base address) of the module that exports the
// import to be searched for.
//
// - exportmodulename (IN): ANSI string containing the name of the module that
// exports the import to be searched for.
//
// - importname (IN): ANSI string containing the name of the import to search
// for. May be an integer cast to a string if the import is exported by
// ordinal.
//
// Return Value:
//
// Returns TRUE if the module imports to the specified import. Otherwise
// returns FALSE.
//
BOOL findimport (HMODULE importmodule, HMODULE exportmodule, LPCSTR exportmodulename, LPCSTR importname)
{
IMAGE_THUNK_DATA *iate;
IMAGE_IMPORT_DESCRIPTOR *idte;
FARPROC import;
IMAGE_SECTION_HEADER *section;
ULONG size;
// Locate the importing module's Import Directory Table (IDT) entry for the
// exporting module. The importing module actually can have several IATs --
// one for each export module that it imports something from. The IDT entry
// gives us the offset of the IAT for the module we are interested in.
EnterCriticalSection(&imagelock);
idte = (IMAGE_IMPORT_DESCRIPTOR*)ImageDirectoryEntryToDataEx((PVOID)importmodule, TRUE,
IMAGE_DIRECTORY_ENTRY_IMPORT, &size, §ion);
LeaveCriticalSection(&imagelock);
if (idte == NULL) {
// This module has no IDT (i.e. it imports nothing).
return FALSE;
}
while (idte->OriginalFirstThunk != 0x0) {
if (_stricmp((PCHAR)R2VA(importmodule, idte->Name), exportmodulename) == 0) {
// Found the IDT entry for the exporting module.
break;
}
idte++;
}
if (idte->OriginalFirstThunk == 0x0) {
// The importing module does not import anything from the exporting
// module.
return FALSE;
}
// Get the *real* address of the import. If we find this address in the IAT,
// then we've found that the module does import the named import.
import = GetProcAddress(exportmodule, importname);
assert(import != NULL); // Perhaps the named export module does not actually export the named import?
// Locate the import's IAT entry.
iate = (IMAGE_THUNK_DATA*)R2VA(importmodule, idte->FirstThunk);
while (iate->u1.Function != 0x0) {
if (iate->u1.Function == (DWORD_PTR)import) {
// Found the IAT entry. The module imports the named import.
return TRUE;
}
iate++;
}
// The module does not import the named import.
return FALSE;
}
// findpatch - Determines if the specified module has been patched to use the
// specified replacement.
//
// - importmodule (IN): Handle (base address) of the module to be searched to
// see if it imports the specified replacement export.
//
// - exportmodulename (IN): ANSI string containing the name of the module that
// normally exports that import that would have been patched to use the
// replacement export.
//
// - replacement (IN): Address of the replacement, or destination, function or
// variable to search for.
//
// Return Value:
//
// Returns TRUE if the module has been patched to use the specified
// replacement export.
//
BOOL findpatch (HMODULE importmodule, LPCSTR exportmodulename, LPCVOID replacement)
{
IMAGE_THUNK_DATA *iate;
IMAGE_IMPORT_DESCRIPTOR *idte;
IMAGE_SECTION_HEADER *section;
ULONG size;
// Locate the importing module's Import Directory Table (IDT) entry for the
// exporting module. The importing module actually can have several IATs --
// one for each export module that it imports something from. The IDT entry
// gives us the offset of the IAT for the module we are interested in.
EnterCriticalSection(&imagelock);
idte = (IMAGE_IMPORT_DESCRIPTOR*)ImageDirectoryEntryToDataEx((PVOID)importmodule, TRUE,
IMAGE_DIRECTORY_ENTRY_IMPORT, &size, §ion);
LeaveCriticalSection(&imagelock);
if (idte == NULL) {
// This module has no IDT (i.e. it imports nothing).
return FALSE;
}
while (idte->OriginalFirstThunk != 0x0) {
if (_stricmp((PCHAR)R2VA(importmodule, idte->Name), exportmodulename) == 0) {
// Found the IDT entry for the exporting module.
break;
}
idte++;
}
if (idte->OriginalFirstThunk == 0x0) {
// The importing module does not import anything from the exporting
// module.
return FALSE;
}
// Locate the replacement's IAT entry.
iate = (IMAGE_THUNK_DATA*)R2VA(importmodule, idte->FirstThunk);
while (iate->u1.Function != 0x0) {
if (iate->u1.Function == (DWORD_PTR)replacement) {
// Found the IAT entry for the replacement. This patch has been
// installed.
return TRUE;
}
iate++;
}
// The module does not import the replacement. The patch has not been
// installed.
return FALSE;
}
// insertreportdelay - Sets the report function to sleep for a bit after each
// call to OutputDebugString, in order to allow the debugger to catch up.
//
// Return Value:
//
// None.
//
VOID insertreportdelay ()
{
reportdelay = TRUE;
}
// moduleispatched - Checks to see if any of the imports listed in the specified
// patch table have been patched into the specified importmodule.
//
// - importmodule (IN): Handle (base address) of the module to be queried to
// determine if it has been patched.
//
// - patchtable (IN): An array of patchentry_t structures specifying all of the
// import patches to search for.
//
// - tablesize (IN): Size, in entries, of the specified patch table.
//
// Return Value:
//
// Returns TRUE if at least one of the patches listed in the patch table is
// installed in the importmodule. Otherwise returns FALSE.
//
BOOL moduleispatched (HMODULE importmodule, patchentry_t patchtable [], UINT tablesize)
{
patchentry_t *entry;
BOOL found = FALSE;
UINT index;
// Loop through the import patch table, individually checking each patch
// entry to see if it is installed in the import module. If any patch entry
// is installed in the import module, then the module has been patched.
for (index = 0; index < tablesize; index++) {
entry = &patchtable[index];
found = findpatch(importmodule, entry->exportmodulename, entry->replacement);
if (found == TRUE) {
// Found one of the listed patches installed in the import module.
return TRUE;
}
}
// No patches listed in the patch table were found in the import module.
return FALSE;
}
// patchimport - Patches all future calls to an imported function, or references
// to an imported variable, through to a replacement function or variable.
// Patching is done by replacing the import's address in the specified target
// module's Import Address Table (IAT) with the address of the replacement
// function or variable.
//
// - importmodule (IN): Handle (base address) of the target module for which
// calls or references to the import should be patched.
//
// - exportmodule (IN): Handle (base address) of the module that exports the
// the function or variable to be patched.
//
// - exportmodulename (IN): ANSI string containing the name of the module that
// exports the function or variable to be patched.
//
// - importname (IN): ANSI string containing the name of the imported function
// or variable to be patched. May be an integer cast to a string if the
// import is exported by ordinal.
//
// - replacement (IN): Address of the function or variable to which future
// calls or references should be patched through to. This function or
// variable can be thought of as effectively replacing the original import
// from the point of view of the module specified by "importmodule".
//
// Return Value:
//
// Returns TRUE if the patch was installed into the import module. If the
// import module does not import the specified export, so nothing changed,
// then FALSE will be returned.
//
BOOL patchimport (HMODULE importmodule, HMODULE exportmodule, LPCSTR exportmodulename, LPCSTR importname,
LPCVOID replacement)
{
IMAGE_THUNK_DATA *iate;
IMAGE_IMPORT_DESCRIPTOR *idte;
FARPROC import;
DWORD protect;
IMAGE_SECTION_HEADER *section;
ULONG size;
// Locate the importing module's Import Directory Table (IDT) entry for the
// exporting module. The importing module actually can have several IATs --
// one for each export module that it imports something from. The IDT entry
// gives us the offset of the IAT for the module we are interested in.
EnterCriticalSection(&imagelock);
idte = (IMAGE_IMPORT_DESCRIPTOR*)ImageDirectoryEntryToDataEx((PVOID)importmodule, TRUE,
IMAGE_DIRECTORY_ENTRY_IMPORT, &size, §ion);
LeaveCriticalSection(&imagelock);
if (idte == NULL) {
// This module has no IDT (i.e. it imports nothing).
return FALSE;
}
while (idte->OriginalFirstThunk != 0x0) {
if (_stricmp((PCHAR)R2VA(importmodule, idte->Name), exportmodulename) == 0) {
// Found the IDT entry for the exporting module.
break;
}
idte++;
}
if (idte->OriginalFirstThunk == 0x0) {
// The importing module does not import anything from the exporting
// module.
return FALSE;
}
// Get the *real* address of the import. If we find this address in the IAT,
// then we've found the entry that needs to be patched.
import = GetProcAddress(exportmodule, importname);
assert(import != NULL); // Perhaps the named export module does not actually export the named import?
// Locate the import's IAT entry.
iate = (IMAGE_THUNK_DATA*)R2VA(importmodule, idte->FirstThunk);
while (iate->u1.Function != 0x0) {
if (iate->u1.Function == (DWORD_PTR)import) {
// Found the IAT entry. Overwrite the address stored in the IAT
// entry with the address of the replacement. Note that the IAT
// entry may be write-protected, so we must first ensure that it is
// writable.
VirtualProtect(&iate->u1.Function, sizeof(iate->u1.Function), PAGE_READWRITE, &protect);
iate->u1.Function = (DWORD_PTR)replacement;
VirtualProtect(&iate->u1.Function, sizeof(iate->u1.Function), protect, &protect);
// The patch has been installed in the import module.
return TRUE;
}
iate++;
}
// The import's IAT entry was not found. The importing module does not
// import the specified import.
return FALSE;
}
// patchmodule - Patches all imports listed in the supplied patch table, and
// which are imported by the specified module, through to their respective
// replacement functions.
//
// Note: If the specified module does not import any of the functions listed
// in the patch table, then nothing is changed for the specified module.
//
// - importmodule (IN): Handle (base address) of the target module which is to
// have its imports patched.
//
// - patchtable (IN): An array of patchentry_t structures specifying all of the
// imports to patch for the specified module.
//
// - tablesize (IN): Size, in entries, of the specified patch table.
//
// Return Value:
//
// Returns TRUE if at least one of the patches listed in the patch table was
// installed in the importmodule. Otherwise returns FALSE.
//
BOOL patchmodule (HMODULE importmodule, patchentry_t patchtable [], UINT tablesize)
{
patchentry_t *entry;
UINT index;
BOOL patched = FALSE;
// Loop through the import patch table, individually patching each import
// listed in the table.
for (index = 0; index < tablesize; index++) {
entry = &patchtable[index];
if (patchimport(importmodule, (HMODULE)entry->modulebase, entry->exportmodulename, entry->importname,
entry->replacement) == TRUE) {
patched = TRUE;
}
}
return patched;
}
// report - Sends a printf-style formatted message to the debugger for display
// and/or to a file.
//
// Note: A message longer than MAXREPORTLENGTH characters will be truncated
// to MAXREPORTLENGTH.
//
// - format (IN): Specifies a printf-compliant format string containing the
// message to be sent to the debugger.
//
// - ... (IN): Arguments to be formatted using the specified format string.
//
// Return Value:
//
// None.
//
VOID report (LPCWSTR format, ...)
{
va_list args;
size_t count;
CHAR messagea [MAXREPORTLENGTH + 1];
WCHAR messagew [MAXREPORTLENGTH + 1];
va_start(args, format);
_vsnwprintf_s(messagew, MAXREPORTLENGTH + 1, _TRUNCATE, format, args);
va_end(args);
messagew[MAXREPORTLENGTH] = L'\0';
if (reportencoding == unicode) {
if (reportfile != NULL) {
// Send the report to the previously specified file.
fwrite(messagew, sizeof(WCHAR), wcslen(messagew), reportfile);
}
if (reporttodebugger) {
OutputDebugStringW(messagew);
}
}
else {
if (wcstombs_s(&count, messagea, MAXREPORTLENGTH + 1, messagew, _TRUNCATE) == -1) {
// Failed to convert the Unicode message to ASCII.
assert(FALSE);
return;
}
messagea[MAXREPORTLENGTH] = '\0';
if (reportfile != NULL) {
// Send the report to the previously specified file.
fwrite(messagea, sizeof(CHAR), strlen(messagea), reportfile);
}
if (reporttodebugger) {
OutputDebugStringA(messagea);
}
}
if (reporttodebugger && (reportdelay == TRUE)) {
Sleep(10); // Workaround the Visual Studio 6 bug where debug strings are sometimes lost if they're sent too fast.
}
}
// restoreimport - Restores the IAT entry for an import previously patched via
// a call to "patchimport" to the original address of the import.
//
// - importmodule (IN): Handle (base address) of the target module for which
// calls or references to the import should be restored.
//
// - exportmodule (IN): Handle (base address) of the module that exports the
// function or variable to be patched.
//
// - exportmodulename (IN): ANSI string containing the name of the module that
// exports the function or variable to be patched.
//
// - importname (IN): ANSI string containing the name of the imported function
// or variable to be restored. May be an integer cast to a string if the
// import is exported by ordinal.
//
// - replacement (IN): Address of the function or variable which the import was
// previously patched through to via a call to "patchimport".
//
// Return Value:
//
// None.
//
VOID restoreimport (HMODULE importmodule, HMODULE exportmodule, LPCSTR exportmodulename, LPCSTR importname,
LPCVOID replacement)
{
IMAGE_THUNK_DATA *iate;
IMAGE_IMPORT_DESCRIPTOR *idte;
FARPROC import;
DWORD protect;
IMAGE_SECTION_HEADER *section;
ULONG size;
// Locate the importing module's Import Directory Table (IDT) entry for the
// exporting module. The importing module actually can have several IATs --
// one for each export module that it imports something from. The IDT entry
// gives us the offset of the IAT for the module we are interested in.
EnterCriticalSection(&imagelock);
idte = (IMAGE_IMPORT_DESCRIPTOR*)ImageDirectoryEntryToDataEx((PVOID)importmodule, TRUE,
IMAGE_DIRECTORY_ENTRY_IMPORT, &size, §ion);
LeaveCriticalSection(&imagelock);
if (idte == NULL) {
// This module has no IDT (i.e. it imports nothing).
return;
}
while (idte->OriginalFirstThunk != 0x0) {
if (_stricmp((PCHAR)R2VA(importmodule, idte->Name), exportmodulename) == 0) {
// Found the IDT entry for the exporting module.
break;
}
idte++;
}
if (idte->OriginalFirstThunk == 0x0) {
// The importing module does not import anything from the exporting
// module.
return;
}
// Get the *real* address of the import.
import = GetProcAddress(exportmodule, importname);
assert(import != NULL); // Perhaps the named export module does not actually export the named import?
// Locate the import's original IAT entry (it currently has the replacement
// address in it).
iate = (IMAGE_THUNK_DATA*)R2VA(importmodule, idte->FirstThunk);
while (iate->u1.Function != 0x0) {
if (iate->u1.Function == (DWORD_PTR)replacement) {
// Found the IAT entry. Overwrite the address stored in the IAT
// entry with the import's real address. Note that the IAT entry may
// be write-protected, so we must first ensure that it is writable.
VirtualProtect(&iate->u1.Function, sizeof(iate->u1.Function), PAGE_READWRITE, &protect);
iate->u1.Function = (DWORD_PTR)import;
VirtualProtect(&iate->u1.Function, sizeof(iate->u1.Function), protect, &protect);
break;
}
iate++;
}
}
// restoremodule - Restores all imports listed in the supplied patch table, and
// which are imported by the specified module, to their original functions.
//
// Note: If the specified module does not import any of the functions listed
// in the patch table, then nothing is changed for the specified module.
//
// - importmodule (IN): Handle (base address) of the target module which is to
// have its imports restored.
//
// - patchtable (IN): Array of patchentry_t structures specifying all of the
// imports to restore for the specified module.
//
// - tablesize (IN): Size, in entries, of the specified patch table.
//
// Return Value:
//
// None.
//
VOID restoremodule (HMODULE importmodule, patchentry_t patchtable [], UINT tablesize)
{
patchentry_t *entry;
UINT index;
// Loop through the import patch table, individually restoring each import
// listed in the table.
for (index = 0; index < tablesize; index++) {
entry = &patchtable[index];
restoreimport(importmodule, (HMODULE)entry->modulebase, entry->exportmodulename, entry->importname,
entry->replacement);
}
}
// setreportencoding - Sets the output encoding of report messages to either
// ASCII (the default) or Unicode.
//
// - encoding (IN): Specifies either "ascii" or "unicode".
//
// Return Value:
//
// None.
//
VOID setreportencoding (encoding_e encoding)
{
switch (encoding) {
case ascii:
case unicode:
reportencoding = encoding;
break;
default:
assert(FALSE);
}
}
// setreportfile - Sets a destination file to which all report messages should
// be sent. If this function is not called to set a destination file, then
// report messages will be sent to the debugger instead of to a file.
//
// - file (IN): Pointer to an open file, to which future report messages should
// be sent.
//
// - copydebugger (IN): If true, in addition to sending report messages to
// the specified file, a copy of each message will also be sent to the
// debugger.
//
// Return Value:
//
// None.
//
VOID setreportfile (FILE *file, BOOL copydebugger)
{
reportfile = file;
reporttodebugger = copydebugger;
}
// strapp - Appends the specified source string to the specified destination
// string. Allocates additional space so that the destination string "grows"
// as new strings are appended to it. This function is fairly infrequently
// used so efficiency is not a major concern.
//
// - dest (IN/OUT): Address of the destination string. Receives the resulting
// combined string after the append operation.
//
// - source (IN): Source string to be appended to the destination string.
//
// Return Value:
//
// None.
//
VOID strapp (LPWSTR *dest, LPCWSTR source)
{
SIZE_T length;
LPWSTR temp;
temp = *dest;
length = wcslen(*dest) + wcslen(source);
*dest = new WCHAR [length + 1];
wcsncpy_s(*dest, length + 1, temp, _TRUNCATE);
wcsncat_s(*dest, length + 1, source, _TRUNCATE);
delete [] temp;
}
// strtobool - Converts string values (e.g. "yes", "no", "on", "off") to boolean
// values.
//
// - s (IN): String value to convert.
//
// Return Value:
//
// Returns TRUE if the string is recognized as a "true" string. Otherwise
// returns FALSE.
//
BOOL strtobool (LPCWSTR s) {
WCHAR *end;
if ((_wcsicmp(s, L"true") == 0) ||
(_wcsicmp(s, L"yes") == 0) ||
(_wcsicmp(s, L"on") == 0) ||
(wcstol(s, &end, 10) == 1)) {
return TRUE;
}
else {
return FALSE;
}
}
// _GetProcessIdOfThread - Returns the ID of the process owns the thread.
//
// - thread (IN): The handle to the thread.
//
// Return Value:
//
// Returns the ID of the process that owns the thread. Otherwise returns 0.
//
DWORD _GetProcessIdOfThread (HANDLE thread)
{
typedef struct _CLIENT_ID {
HANDLE UniqueProcess;
HANDLE UniqueThread;
} CLIENT_ID, *PCLIENT_ID;
typedef LONG NTSTATUS;
typedef LONG KPRIORITY;
typedef struct _THREAD_BASIC_INFORMATION {
NTSTATUS ExitStatus;
PVOID TebBaseAddress;
CLIENT_ID ClientId;
KAFFINITY AffinityMask;
KPRIORITY Priority;
KPRIORITY BasePriority;
} THREAD_BASIC_INFORMATION, *PTHREAD_BASIC_INFORMATION;
const static THREADINFOCLASS ThreadBasicInformation = (THREADINFOCLASS)0;
typedef NTSTATUS (WINAPI *PNtQueryInformationThread) (HANDLE thread,
THREADINFOCLASS infoclass, PVOID buffer, ULONG buffersize,
PULONG used);
static PNtQueryInformationThread NtQueryInformationThread = NULL;
THREAD_BASIC_INFORMATION tbi;
NTSTATUS status;
HMODULE ntdll;
if (NtQueryInformationThread == NULL) {
ntdll = GetModuleHandle(L"ntdll.dll");
NtQueryInformationThread = (PNtQueryInformationThread)GetProcAddress(ntdll, "NtQueryInformationThread");
if (NtQueryInformationThread == NULL) {
return 0;
}
}
status = NtQueryInformationThread(thread, ThreadBasicInformation, &tbi, sizeof(tbi), NULL);
if(status < 0) {
// Shall we go through all the trouble of setting last error?
return 0;
}
return (DWORD)tbi.ClientId.UniqueProcess;
}