-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmemlib.nim
1227 lines (956 loc) · 40.1 KB
/
memlib.nim
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
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#====================================================================
#
# Memlib - Load Windows DLL from memory
# (c) Copyright 2021-2022 Ward
#
#====================================================================
## This module is designed to be a drop-in replacement for `dynlib pragma`
## and `dynlib module` in Windows. The main part of this module is a pure nim
## implementation of the famous MemoryModule library.
## So that the we can embed all DLLs into the main EXE file.
import tables, macros, md5, locks, os, terminal, strutils, dynlib
import winim/lean, minhook
import memlib/private/sharedseq
when (compiles do: import std/exitprocs):
import std/exitprocs
proc addQuitProc(cl: proc() {.noconv.}) = addExitProc(cl)
template atexit(body: untyped): untyped =
addQuitProc proc () {.noconv.} =
body
when not defined(windows):
{.fatal: "Only implementation for Windows".}
type
DllEntryProc = proc (dll: HINSTANCE, reason: DWORD, reserved: pointer): bool {.stdcall, gcsafe.}
ExeEntryProc = proc (): int {.stdcall, gcsafe.}
NameOrdinal = object
cname: LPCSTR
ordinal: int
MemoryModuleObj = object
headers: PIMAGE_NT_HEADERS
codeBase: pointer
initialized: bool
isDll: bool
isRelocated: bool
entry: pointer
modules: SharedSeq[HMODULE]
symbols: SharedSeq[NameOrdinal]
hash: MD5Digest
reference: int
name: string
MemoryModule* = ptr MemoryModuleObj
## Pointer to a MemoryModule object.
DllContent* = distinct string
## Represents DLL file in binary format.
var
memLibs: SharedSeq[MemoryModule]
rtLibs: SharedSeq[HMODULE]
gLock: Lock
hookEnabled: bool
proc `[]`[T](x: T, U: typedesc): U {.inline.} =
## syntax sugar for cast
when sizeof(U) > sizeof(x):
when sizeof(x) == 1: cast[U](cast[uint8](x).uint64)
elif sizeof(x) == 2: cast[U](cast[uint16](x).uint64)
elif sizeof(x) == 4: cast[U](cast[uint32](x).uint64)
else: cast[U](cast[uint64](x))
else:
cast[U](x)
proc `{}`[T](x: T, U: typedesc): U {.inline.} =
## syntax sugar for zero extends cast
when sizeof(x) == 1: x[uint8][U]
elif sizeof(x) == 2: x[uint16][U]
elif sizeof(x) == 4: x[uint32][U]
elif sizeof(x) == 8: x[uint64][U]
else: {.fatal.}
proc `{}`[T](p: T, x: SomeInteger): T {.inline.} =
## syntax sugar for pointer (or any other type) arithmetics
(p[int] +% x{int})[T]
template `++`[T](p: var ptr T) =
## syntax sugar for pointer increment
p = cast[ptr T](p[int] +% sizeof(T))
template alignUp[T: uint|pointer](value: T, alignment: uint): T =
cast[T]((cast[uint](value) + alignment - 1) and not (alignment - 1))
template alignDown[T: uint|pointer](value: T, alignment: uint): T =
cast[T](cast[uint](value) and not (alignment - 1))
template MAKEINTRESOURCE(i: untyped): untyped = (i and 0xffff)[LPTSTR]
iterator sections(ntHeader: PIMAGE_NT_HEADERS): var IMAGE_SECTION_HEADER =
let sections = IMAGE_FIRST_SECTION(ntHeader)[ptr UncheckedArray[IMAGE_SECTION_HEADER]]
for i in 0 ..< int ntHeader.FileHeader.NumberOfSections:
yield sections[i]
proc getPageSize(): uint {.inline.} =
var sysInfo: SYSTEM_INFO
GetNativeSystemInfo(sysInfo)
return sysInfo.dwPageSize{uint}
proc symErrorMessage(sym: LPCSTR): string =
let msg = if HIWORD(sym{uint}) == 0: "ordinal " & $(sym{uint}) else: "symbol " & $sym
result = "Could not find " & msg
proc validate(data: pointer, size: int): MD5Digest =
if data == nil or size < sizeof(IMAGE_DOS_HEADER):
raise newException(LibraryError, "Invalid data")
let dosHeader = data[PIMAGE_DOS_HEADER]
if dosHeader.e_magic != IMAGE_DOS_SIGNATURE:
raise newException(LibraryError, "Invalid data")
if size < dosHeader.e_lfanew + sizeof(IMAGE_NT_HEADERS):
raise newException(LibraryError, "Invalid data")
let ntHeader = data{dosHeader.e_lfanew}[PIMAGE_NT_HEADERS]
if ntHeader.Signature != IMAGE_NT_SIGNATURE:
raise newException(LibraryError, "Invalid data")
when defined(cpu64):
if ntHeader.FileHeader.Machine != IMAGE_FILE_MACHINE_AMD64:
raise newException(LibraryError, "Incorrect architecture")
else:
if ntHeader.FileHeader.Machine != IMAGE_FILE_MACHINE_I386:
raise newException(LibraryError, "Incorrect architecture")
# Only support section alignments that are a multiple of 2
if (ntHeader.OptionalHeader.SectionAlignment and 1) != 0:
raise newException(LibraryError, "Invalid data")
if size < ntHeader.OptionalHeader.SizeOfHeaders:
raise newException(LibraryError, "Invalid data")
var ctx: MD5Context
ctx.md5Init()
ctx.md5Update(data[cstring], size)
ctx.md5Final(result)
proc newMemoryModule(): MemoryModule =
result = createShared(MemoryModuleObj)
if result == nil:
raise newException(LibraryError, "Out of memory")
result.modules = newSharedSeq[HMODULE]()
result.symbols = newSharedSeq[NameOrdinal]()
proc dealloc(lib: MemoryModule) {.inline.} =
deallocShared(lib)
proc allocMemory(lib: MemoryModule, ntHeader: PIMAGE_NT_HEADERS, pageSize: uint) =
var lastSectionEnd = 0'u
for section in ntHeader.sections:
let endOfSection = section.VirtualAddress{uint}{
if section.SizeOfRawData == 0: ntHeader.OptionalHeader.SectionAlignment
else: section.SizeOfRawData
}
if endOfSection > lastSectionEnd:
lastSectionEnd = endOfSection
let alignedImageSize = alignUp(ntHeader.OptionalHeader.SizeOfImage{uint}, pageSize)
if alignedImageSize == 0 or alignedImageSize != alignUp(lastSectionEnd, pageSize):
raise newException(LibraryError, "Invalid data")
# reserve memory for image of library
var codeBase = VirtualAlloc(ntHeader.OptionalHeader.ImageBase[pointer],
alignedImageSize[SIZE_T], MEM_RESERVE or MEM_COMMIT, PAGE_READWRITE)
if codeBase == nil:
# try to allocate memory at arbitrary position
codeBase = VirtualAlloc(nil, alignedImageSize[SIZE_T], MEM_RESERVE or MEM_COMMIT, PAGE_READWRITE)
if codeBase == nil:
raise newException(LibraryError, "Out of memory")
when defined(cpu64):
var blocked: seq[pointer]
try:
# Memory block may not span 4 GB boundaries
while (codeBase[uint] shr 32) < ((codeBase[uint] + alignedImageSize) shr 32):
blocked.add codeBase
codeBase = VirtualAlloc(nil, alignedImageSize[SIZE_T], MEM_RESERVE or MEM_COMMIT, PAGE_READWRITE)
if codeBase == nil:
raise newException(LibraryError, "Out of memory")
finally:
for p in blocked:
VirtualFree(p, 0, MEM_RELEASE)
lib.codeBase = codeBase
proc copyHeaders(lib: MemoryModule, dosHeader: PIMAGE_DOS_HEADER, ntHeader: PIMAGE_NT_HEADERS) =
# commit memory for headers
let headers = VirtualAlloc(lib.codeBase, ntHeader.OptionalHeader.SizeOfHeaders, MEM_COMMIT, PAGE_READWRITE)
copyMem(headers, dosHeader, ntHeader.OptionalHeader.SizeOfHeaders)
lib.headers = headers{dosHeader.e_lfanew}[PIMAGE_NT_HEADERS]
lib.headers.OptionalHeader.ImageBase = lib.codeBase[ntHeader.OptionalHeader.ImageBase.type]
lib.isDll = (ntHeader.FileHeader.Characteristics and IMAGE_FILE_DLL) != 0
proc copySections(lib: MemoryModule, data: pointer, size: int, ntHeader: PIMAGE_NT_HEADERS) =
let codeBase = lib.codeBase
for section in lib.headers.sections:
if section.SizeOfRawData == 0:
# section doesn't contain data in the dll itself, but may define uninitialized data
let sectionSize = ntHeader.OptionalHeader.SectionAlignment
if sectionSize > 0:
var dest = VirtualAlloc(codeBase{section.VirtualAddress}, sectionSize, MEM_COMMIT, PAGE_READWRITE)
if dest == nil:
raise newException(LibraryError, "Out of memory")
# Always use position from file to support alignments smaller¡¡
# than page size (allocation above will align to page size).
dest = codeBase{section.VirtualAddress}
# NOTE: On 64bit systems we truncate to 32bit here but expand
# again later when "PhysicalAddress" is used.
section.Misc.PhysicalAddress = dest[DWORD]
zeroMem(dest, sectionSize)
continue
if size <% (section.PointerToRawData +% section.SizeOfRawData):
raise newException(LibraryError, "Invalid data")
# commit memory block and copy data from dll
var dest = VirtualAlloc(codeBase{section.VirtualAddress}, section.SizeOfRawData, MEM_COMMIT, PAGE_READWRITE)
if dest == nil:
raise newException(LibraryError, "Out of memory")
# Always use position from file to support alignments smaller
# than page size (allocation above will align to page size).
dest = codeBase{section.VirtualAddress}
# NOTE: On 64bit systems we truncate to 32bit here but expand
# again later when "PhysicalAddress" is used.
section.Misc.PhysicalAddress = dest[DWORD]
copyMem(dest, data{section.PointerToRawData}, section.SizeOfRawData)
proc performBaseRelocation(lib: MemoryModule, ntHeader: PIMAGE_NT_HEADERS) =
iterator relocations(codeBase: pointer, directory: IMAGE_DATA_DIRECTORY): PIMAGE_BASE_RELOCATION =
if directory.Size != 0:
var relocation = codeBase{directory.VirtualAddress}[PIMAGE_BASE_RELOCATION]
while relocation.VirtualAddress != 0:
yield relocation
relocation = relocation{relocation.SizeOfBlock}
iterator pairs(relocation: PIMAGE_BASE_RELOCATION): (int, int) =
let info = relocation{IMAGE_SIZEOF_BASE_RELOCATION}[ptr UncheckedArray[uint16]]
for i in 0 ..< (relocation.SizeOfBlock{uint} - IMAGE_SIZEOF_BASE_RELOCATION) div 2:
let
kind = info[i] shr 12 # the upper 4 bits define the type of relocation
off = info[i] and 0xfff # the lower 12 bits define the offset
yield (int kind, int off)
let delta = int(lib.headers.OptionalHeader.ImageBase - ntHeader.OptionalHeader.ImageBase)
if delta != 0:
let
codeBase = lib.codeBase
directory = lib.headers.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC]
if directory.Size == 0:
lib.isRelocated = false
return
for relocation in codeBase.relocations(directory):
let dest = codeBase{relocation.VirtualAddress}
for kind, off in relocation:
if kind == IMAGE_REL_BASED_HIGHLOW:
let p = dest{off}[ptr int32]
p[] = p[]{delta}
when defined(cpu64):
if kind == IMAGE_REL_BASED_DIR64:
let p = dest{off}[ptr int64]
p[] = p[]{delta}
lib.isRelocated = true
proc buildImportTable(lib: MemoryModule) =
iterator descriptors(codeBase: pointer, directory: IMAGE_DATA_DIRECTORY): IMAGE_IMPORT_DESCRIPTOR =
if directory.Size != 0:
var desc = codeBase{directory.VirtualAddress}[PIMAGE_IMPORT_DESCRIPTOR]
while (IsBadReadPtr(desc, UINT_PTR sizeof(IMAGE_IMPORT_DESCRIPTOR)) == 0) and (desc.Name != 0):
yield desc[]
++desc
iterator refs(codeBase: pointer, desc: IMAGE_IMPORT_DESCRIPTOR): (ptr pointer, ptr pointer) =
var
funcRef = codeBase{desc.FirstThunk}[ptr pointer]
thunkRef =
if desc.OriginalFirstThunk != 0: codeBase{desc.OriginalFirstThunk}[ptr pointer]
else: funcRef
while thunkRef[] != nil:
yield (thunkRef, funcRef)
++thunkRef
++funcRef
var
codeBase = lib.codeBase
directory = lib.headers.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]
for desc in codeBase.descriptors(directory):
let
cname = codeBase{desc.Name}[LPCSTR]
handle = LoadLibraryA(cname)
if handle == 0:
raise newException(LibraryError, $cname & " not found")
lib.modules.add handle
for thunkRef, funcRef in refs(codeBase, desc):
if IMAGE_SNAP_BY_ORDINAL(thunkRef[][int]):
let ordinal = IMAGE_ORDINAL(thunkRef[][int])
funcRef[] = GetProcAddress(handle, ordinal[LPCSTR])
if funcRef[] == nil:
raise newException(LibraryError, $ordinal & " not found in " & $cname)
else:
let
thunkData = codeBase{thunkRef[][int]}[PIMAGE_IMPORT_BY_NAME]
cfunc = thunkData.Name[0].addr[LPCSTR]
funcRef[] = GetProcAddress(handle, cfunc)
if funcRef[] == nil:
raise newException(LibraryError, $cfunc & " not found in " & $cname)
proc finalizeSections(lib: MemoryModule, pageSize: uint) =
type
SectionData = object
address: pointer
alignedAddr: pointer
size: uint
characteristics: DWORD
last: bool
proc realSize(lib: MemoryModule, section: IMAGE_SECTION_HEADER): uint =
result = section.SizeOfRawData{uint}
if result == 0:
if (section.Characteristics and IMAGE_SCN_CNT_INITIALIZED_DATA) != 0:
result = lib.headers.OptionalHeader.SizeOfInitializedData{uint}
elif (section.Characteristics and IMAGE_SCN_CNT_UNINITIALIZED_DATA) != 0:
result = lib.headers.OptionalHeader.SizeOfUninitializedData{uint}
proc finalizeSection(lib: MemoryModule, data: SectionData) =
if data.size == 0:
return
if (data.characteristics and IMAGE_SCN_MEM_DISCARDABLE) != 0:
if data.address == data.alignedAddr and
(data.last or
lib.headers.OptionalHeader.SectionAlignment{uint} == pageSize or
(data.size mod pageSize) == 0
):
# Only allowed to decommit whole pages
VirtualFree(data.address, data.size[SIZE_T], MEM_DECOMMIT)
return
const flags = [
[[PAGE_NOACCESS, PAGE_WRITECOPY],
[PAGE_READONLY, PAGE_READWRITE]],
[[PAGE_EXECUTE, PAGE_EXECUTE_WRITECOPY],
[PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE]]
]
# determine protection flags based on characteristics
var
executable = int((data.characteristics and IMAGE_SCN_MEM_EXECUTE) != 0)
readable = int((data.characteristics and IMAGE_SCN_MEM_READ) != 0)
writeable = int((data.characteristics and IMAGE_SCN_MEM_WRITE) != 0)
protect = DWORD flags[executable][readable][writeable]
oldProtect: DWORD
if (data.characteristics and IMAGE_SCN_MEM_NOT_CACHED) != 0:
protect = protect or PAGE_NOCACHE
if VirtualProtect(data.address, data.size[SIZE_T], protect, &oldProtect) == 0:
raise newException(LibraryError, "protecting page failed")
when defined(cpu64):
# "PhysicalAddress" might have been truncated to 32bit above, expand to 64bits again.
let imageOffset = lib.headers.OptionalHeader.ImageBase and 0xffffffff00000000
else:
const imageOffset = 0
var
firstSection = true
data: SectionData
for section in lib.headers.sections:
if firstSection:
data.address = (section.Misc.PhysicalAddress{uint} or imageOffset{uint})[pointer]
data.alignedAddr = alignDown(data.address, pageSize)
data.size = lib.realSize(section)
data.characteristics = section.Characteristics
data.last = false
firstSection = false
continue
let
address = (section.Misc.PhysicalAddress{uint} or imageOffset{uint})[pointer]
alignedAddr = alignDown(address, pageSize)
size = lib.realSize(section)
# Combine access flags of all sections that share a page
if data.alignedAddr == alignedAddr or data.address{data.size} > alignedAddr:
let combine = data.characteristics or section.Characteristics
if (section.Characteristics and IMAGE_SCN_MEM_DISCARDABLE) == 0 or
(data.characteristics and IMAGE_SCN_MEM_DISCARDABLE) == 0:
data.characteristics = combine and (not IMAGE_SCN_MEM_DISCARDABLE)
else:
data.characteristics = combine
data.size = address{size}[uint] - data.address[uint]
continue
lib.finalizeSection(data)
data.address = address
data.alignedAddr = alignedAddr
data.size = size
data.characteristics = section.Characteristics
data.last = true
lib.finalizeSection(data)
proc executeTLS(lib: MemoryModule) =
type
PIMAGE_TLS_CALLBACK = proc (DllHandle: PVOID, Reason: DWORD, Reserved: PVOID) {.stdcall, gcsafe.}
let
codeBase = lib.codeBase
directory = lib.headers.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS]
if directory.VirtualAddress != 0:
var
tls = codeBase{directory.VirtualAddress}[PIMAGE_TLS_DIRECTORY]
callback = tls.AddressOfCallBacks[ptr PIMAGE_TLS_CALLBACK]
if callback != nil:
while callback[] != nil:
callback[](codeBase, DLL_PROCESS_ATTACH, nil)
++callback
proc addFunctionTable(lib: MemoryModule) =
when defined(cpu64):
let
codeBase = lib.codeBase
directory = lib.headers.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION]
var
funcTablePtr = codeBase{directory.VirtualAddress}[PRUNTIME_FUNCTION]
RtlAddFunctionTable(funcTablePtr, (directory.Size div sizeof(RUNTIME_FUNCTION).DWORD), codeBase[DWORD64])
proc initialize(lib: MemoryModule) =
lib.entry = lib.codeBase{lib.headers.OptionalHeader.AddressOfEntryPoint}
if lib.entry != nil and lib.isDll:
let ok = lib.entry[DllEntryProc](lib.codeBase[HINSTANCE], DLL_PROCESS_ATTACH, nil)
if not ok:
raise newException(LibraryError, "Initialize failed")
lib.initialized = true
proc gatherSymbols(lib: MemoryModule) =
iterator entries(codeBase: pointer, exports: PIMAGE_EXPORT_DIRECTORY): (LPCSTR, int) =
var
nameRef = codeBase{exports.AddressOfNames}[ptr uint32]
ordinal = codeBase{exports.AddressOfNameOrdinals}[ptr uint16]
for i in 0 ..< exports.NumberOfNames:
let
name = codeBase{nameRef[]}[LPCSTR]
index = int ordinal[]
yield (name, index)
++nameRef
++ordinal
let
codeBase = lib.codeBase
directory = lib.headers.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT]
if directory.Size != 0:
let exports = codeBase{directory.VirtualAddress}[PIMAGE_EXPORT_DIRECTORY]
for cname, ordinal in codeBase.entries(exports):
lib.symbols.add NameOrdinal(cname: cname, ordinal: ordinal)
lib.symbols.sort() do (x, y: NameOrdinal) -> int:
result = lstrcmpA(x.cname, y.cname)
proc findSymbol(lib: MemoryModule, name: LPCSTR): pointer =
block main:
if lib == nil: break main
let
codeBase = lib.codeBase
directory = lib.headers.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT]
if directory.Size == 0: break main
let exports = codeBase{directory.VirtualAddress}[PIMAGE_EXPORT_DIRECTORY]
if exports.NumberOfFunctions == 0: break main
var index = 0
if HIWORD(name{uint}) == 0:
if LOWORD(name{uint})[DWORD] <% exports.Base: break main
index = LOWORD(name{uint})[DWORD] -% exports.Base
else:
var found = lib.symbols.binarySearch(name) do (x: NameOrdinal, y: LPCSTR) -> int:
result = lstrcmpA(x.cname, y)
if found < 0: break main
index = lib.symbols[found].ordinal
if index >% exports.NumberOfFunctions: break main
let rva = codeBase{exports.AddressOfFunctions}{index * 4}[ptr uint32][]
return codeBase{rva}
raise newException(LibraryError, symErrorMessage(name))
proc register(lib: MemoryModule, hash: MD5Digest) =
withLock(gLock):
lib.hash = hash
lib.reference = 1
memLibs.add lib
proc unregister(lib: MemoryModule) =
withLock(gLock):
let found = memLibs.find lib
if found >= 0:
memLibs.del found
proc reloadCheck(hash: MD5Digest, lib: var MemoryModule): bool =
withLock(gLock):
for i in 0 ..< memLibs.len:
if memLibs[i].hash == hash:
memLibs[i].reference.inc
lib = memLibs[i]
return true
proc unloadLib(lib: MemoryModule, force: bool) =
if lib != nil:
if lib.reference > 1 and not force:
lib.reference.dec
return
if lib.entry != nil and lib.isDll and lib.initialized:
discard lib.entry[DllEntryProc](lib.codeBase[HINSTANCE], DLL_PROCESS_DETACH, nil)
lib.unregister()
for handle in lib.modules:
FreeLibrary(handle)
lib.modules.dealloc()
lib.symbols.dealloc()
if lib.codeBase != nil:
VirtualFree(lib.codeBase, 0, MEM_RELEASE)
lib.name = ""
lib.dealloc()
proc loadLib(data: pointer, size: int): MemoryModule =
try:
let hash = validate(data, size)
if reloadCheck(hash, result): return
let
pageSize = getPageSize()
dosHeader = data[PIMAGE_DOS_HEADER]
ntHeader = data{dosHeader.e_lfanew}[PIMAGE_NT_HEADERS]
result = newMemoryModule()
result.allocMemory(ntHeader, pageSize)
result.copyHeaders(dosHeader, ntHeader)
result.copySections(data, size, ntHeader)
result.performBaseRelocation(ntHeader)
result.buildImportTable()
result.finalizeSections(pageSize)
result.executeTLS()
result.addFunctionTable()
result.initialize()
result.gatherSymbols()
result.register(hash)
except:
result.unloadLib(force=true)
raise getCurrentException()[ref LibraryError]
proc globalExit() =
for lib in @memLibs:
lib.unloadLib(force=true)
proc globalInit() =
gLock.initLock()
memLibs = newSharedSeq[MemoryModule]()
rtLibs = newSharedSeq[HMODULE]()
once:
globalInit()
atexit:
globalExit()
# exported procs from here
proc checkedLoadLib*(data: DllContent): MemoryModule {.inline.} =
## Loads a DLL from memory. Raise `LibraryError` if the DLL could not be loaded.
result = loadLib(&data.string, data.string.len)
proc checkedLoadLib*(data: openarray[byte|char]): MemoryModule {.inline.} =
## Loads a DLL from memory. Raise `LibraryError` if the DLL could not be loaded.
result = loadLib(unsafeaddr data[0], data.len)
proc checkedSymAddr*(lib: MemoryModule, name: string|LPCSTR): pointer {.inline.} =
## Retrieves the address of a procedure from DLL by name.
## Raise `LibraryError` if the symbol could not be found.
result = lib.findSymbol(name)
proc checkedSymAddr*(lib: MemoryModule, ordinal: range[0..65535]): pointer {.inline.} =
## Retrieves the address of a procedure from DLL by ordinal.
## Raise `LibraryError` if the ordinal out of range.
result = lib.findSymbol(ordinal[LPCSTR])
proc loadLib*(data: DllContent): MemoryModule {.inline.} =
## Loads a DLL from memory. Returns `nil` if the DLL could not be loaded.
try: result = checkedLoadLib(data)
except: result = nil
proc loadLib*(data: openarray[byte|char]): MemoryModule {.inline.} =
## Loads a DLL from memory. Returns `nil` if the DLL could not be loaded.
try: result = checkedLoadLib(data)
except: result = nil
proc symAddr*(lib: MemoryModule, name: string|LPCSTR): pointer {.inline.} =
## Retrieves the address of a procedure from DLL by name.
## Returns `nil` if the symbol could not be found.
try: result = checkedSymAddr(lib, name)
except: result = nil
proc symAddr*(lib: MemoryModule, ordinal: range[0..65535]): pointer {.inline.} =
## Retrieves the address of a procedure from DLL by ordinal.
## Returns `nil` if the ordinal out of range.
try: result = checkedSymAddr(lib, ordinal)
except: result = nil
proc unloadLib*(lib: MemoryModule) {.inline.} =
## Unloads the DLL.
lib.unloadLib(force = false)
proc run*(lib: MemoryModule): int {.discardable.} =
## Execute entry point (EXE only). The entry point can only be executed
## if the EXE has been loaded to the correct base address or it could
## be relocated (i.e. relocation information have not been stripped by
## the linker).
##
## Important: calling this function will not return, i.e. once the loaded
## EXE finished running, the process will terminate.
##
## Raise `LibraryError` if the entry point could not be executed.
assert lib != nil
if lib.codeBase == nil or lib.entry == nil:
raise newException(LibraryError, "No entry point")
if lib.isDll:
raise newException(LibraryError, "Cannot run a DLL file")
if not lib.isRelocated:
raise newException(LibraryError, "Cannot run without relocation")
result = lib.entry[ExeEntryProc]()
# for resources
proc findResource*(lib: HMODULE, name: LPCTSTR, typ: LPCTSTR, lang: WORD = 0): HRSRC =
## Find the location of a resource with the specified type, name and language.
proc RtlImageNtHeader(base: HMODULE): PIMAGE_NT_HEADERS {.stdcall, importc, dynlib: "ntdll".}
proc searchResourceEntry(root: pointer, resources: PIMAGE_RESOURCE_DIRECTORY, key: LPCTSTR): PIMAGE_RESOURCE_DIRECTORY_ENTRY =
let entries = resources{sizeof IMAGE_RESOURCE_DIRECTORY}[ptr UncheckedArray[IMAGE_RESOURCE_DIRECTORY_ENTRY]]
if IS_INTRESOURCE(key):
let
first = resources.NumberOfNamedEntries.int
last = first + resources.NumberOfIdEntries.int - 1
let found = binarySearch(entries.toOpenArray(first, last),
cast[WORD](key)) do (x: IMAGE_RESOURCE_DIRECTORY_ENTRY, y: WORD) -> int:
result = system.cmp(x.Name[WORD], y)
if found >= 0:
return &entries[found + first]
else:
let
first = 0
last = resources.NumberOfNamedEntries.int - 1
let found = binarySearch(entries.toOpenArray(first, last),
key) do (x: IMAGE_RESOURCE_DIRECTORY_ENTRY, y: LPCTSTR) -> int:
let
stru = root{x.NameOffset}[PIMAGE_RESOURCE_DIR_STRING_U]
length = int32 stru.Length
lpstr = addr stru.NameString[0]
result = CompareStringEx(LOCALE_NAME_INVARIANT, LINGUISTIC_IGNORECASE,
lpstr, length, y, -1, nil, nil, 0) - 2
if found >= 0:
return &entries[found + first]
let
codeBase = lib
headers = RtlImageNtHeader(lib)
if headers == nil:
raise newException(LibraryError, "Invalid handle")
let directory = headers.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE]
if directory.Size == 0:
raise newException(LibraryError, "Resource data not found")
let
lang =
if lang == 0: LANGIDFROMLCID(GetThreadLocale())
else: lang
root = codeBase{directory.VirtualAddress}[PIMAGE_RESOURCE_DIRECTORY]
var
typeDir, nameDir: PIMAGE_RESOURCE_DIRECTORY
typeEntry, nameEntry, langEntry: PIMAGE_RESOURCE_DIRECTORY_ENTRY
typeEntry = searchResourceEntry(root, root, typ)
if typeEntry == nil:
raise newException(LibraryError, "Resource type not found")
typeDir = root{typeEntry[].OffsetToDirectory}[PIMAGE_RESOURCE_DIRECTORY]
nameEntry = searchResourceEntry(root, typeDir, name)
if nameEntry == nil:
raise newException(LibraryError, "Resource name not found")
nameDir = root{nameEntry[].OffsetToDirectory}[PIMAGE_RESOURCE_DIRECTORY]
langEntry = searchResourceEntry(root, nameDir, lang[LPCTSTR])
if langEntry == nil:
# requested lang not found, use first available
if nameDir.NumberOfIdEntries == 0:
raise newException(LibraryError, "Resource language not found")
langEntry = nameDir{sizeof(IMAGE_RESOURCE_DIRECTORY)}[PIMAGE_RESOURCE_DIRECTORY_ENTRY]
return root{langEntry[].OffsetToDirectory}[HRSRC]
proc sizeOfResource*(lib: HMODULE, resource: HRSRC): DWORD =
## Get the size of the resource in bytes.
let entry = resource[PIMAGE_RESOURCE_DATA_ENTRY]
if entry != nil:
result = entry.Size
proc loadResource*(lib: HMODULE, resource: HRSRC): HGLOBAL =
## Get a pointer to the contents of the resource.
let entry = resource[PIMAGE_RESOURCE_DATA_ENTRY]
if entry != nil:
result = lib{entry[].OffsetToData}[HGLOBAL]
proc loadString*(lib: HMODULE, id: UINT, lang: WORD = 0): string =
## Load a string resource.
let resource = findResource(lib, MAKEINTRESOURCE((id shr 4) + 1), RT_STRING, lang)
var data = loadResource(lib, resource)[PIMAGE_RESOURCE_DIR_STRING_U]
for i in 0 ..< (id and 0x0f):
data = data{int(data.Length + 1) * sizeof(WCHAR)}[PIMAGE_RESOURCE_DIR_STRING_U]
if data.Length == 0:
raise newException(LibraryError, "Resource name not found")
let pucaWchar = cast[ptr UncheckedArray[WCHAR]](&data.NameString[0])
result = $$toOpenArray(pucaWchar, 0, int data.Length - 1)
proc findResource*(lib: MemoryModule, name: LPCTSTR, typ: LPCTSTR,
lang: WORD = 0): HRSRC {.inline.} =
## Find the location of a resource with the specified type, name and language.
if lib == nil:
raise newException(LibraryError, "Invalid handle")
result = findResource(lib.codeBase[HMODULE], name, typ, lang)
proc sizeOfResource*(lib: MemoryModule, resource: HRSRC): DWORD {.inline.} =
## Get the size of the resource in bytes.
if lib == nil:
raise newException(LibraryError, "Invalid handle")
result = sizeOfResource(lib.codeBase[HMODULE], resource)
proc loadResource*(lib: MemoryModule, resource: HRSRC): HGLOBAL {.inline.} =
## Get a pointer to the contents of the resource.
if lib == nil:
raise newException(LibraryError, "Invalid handle")
result = loadResource(lib.codeBase[HMODULE], resource)
proc loadString*(lib: MemoryModule, id: UINT, lang: WORD = 0): string {.inline.} =
## Load a string resource.
if lib == nil:
raise newException(LibraryError, "Invalid handle")
result = loadString(lib.codeBase[HMODULE], id, lang)
# for hooks
# hook LdrLoadDll will crash on some version of Windows by unknow reason.
# aoivd to use undocumented api ?
proc myLoadDll(name: string): HMODULE =
withLock(gLock):
for i in 0 ..< memLibs.len:
if memLibs[i].name.toLowerAscii == name.toLowerAscii:
return memLibs[i][HMODULE]
proc myLoadLibraryExA(lpLibFileName: LPCSTR, hFile: HANDLE, dwFlags: DWORD): HMODULE {.stdcall, minhook: LoadLibraryExA.} =
result = myLoadDll($lpLibFileName)
if result == 0:
result = LoadLibraryExA(lpLibFileName, hFile, dwFlags)
proc myLoadLibraryExW(lpLibFileName: LPCWSTR, hFile: HANDLE, dwFlags: DWORD): HMODULE {.stdcall, minhook: LoadLibraryExW.} =
result = myLoadDll($lpLibFileName)
if result == 0:
result = LoadLibraryExW(lpLibFileName, hFile, dwFlags)
proc myLoadLibraryA(lpLibFileName: LPCSTR): HMODULE {.stdcall, minhook: LoadLibraryA.} =
result = myLoadDll($lpLibFileName)
if result == 0:
result = LoadLibraryA(lpLibFileName)
proc myLoadLibraryW(lpLibFileName: LPCWSTR): HMODULE {.stdcall, minhook: LoadLibraryW.} =
result = myLoadDll($lpLibFileName)
if result == 0:
result = LoadLibraryW(lpLibFileName)
proc myGetProcAddress(hModule: HMODULE, lpProcName: LPCSTR): FARPROC {.stdcall, minhook: GetProcAddress.} =
result = GetProcAddress(hModule, lpProcName)
if result == nil:
withLock(gLock):
for i in 0 ..< memLibs.len:
if hModule == memLibs[i][HANDLE]:
return memLibs[i].symAddr(lpProcName)
proc unhook*(lib: MemoryModule) =
## Removes the hooks.
assert lib != nil
lib.name = ""
withLock(gLock):
for i in 0 ..< memLibs.len:
if memLibs[i].name != "":
return
if hookEnabled:
try:
queueDisableHook(LoadLibraryExA)
queueDisableHook(LoadLibraryExW)
queueDisableHook(LoadLibraryA)
queueDisableHook(LoadLibraryW)
queueDisableHook(GetProcAddress)
applyQueued()
except: discard
hookEnabled = false
proc hook*(lib: MemoryModule, name: string) =
## Hooks the system API (LoadLibrary and GetProcAddress only) with specified name.
## Following requests will be redirected to the memory module
assert lib != nil
lib.unhook()
lib.name = name
withLock(gLock):
if not hookEnabled:
try:
queueEnableHook(LoadLibraryExA)
queueEnableHook(LoadLibraryExW)
queueEnableHook(LoadLibraryA)
queueEnableHook(LoadLibraryW)
queueEnableHook(GetProcAddress)
applyQueued()
except: discard
hookEnabled = true
# for memlib macro
template memlookup(callPtr: ptr pointer, dll: DllContent, sym: LPCSTR, loadLib, symAddr: untyped) =
var
hash = toMD5(string dll)
lib: MemoryModule
withLock(gLock):
for i in 0 ..< memLibs.len:
if memLibs[i].hash == hash:
lib = memLibs[i]
break
if lib == nil:
lib = loadLib(dll)
callPtr[] = lib.symAddr(sym)
template rtlookup(callPtr: ptr pointer, name: string, sym: LPCSTR, errorLib, errorSym: untyped) =
var handle = LoadLibrary(name)
if handle == 0:
errorLib
else:
withLock(gLock):
var found = false
for i in 0 ..< rtLibs.len:
if rtLibs[i] == handle:
found = true
break
if found:
FreeLibrary(handle) # decrease reference count only
else:
rtLibs.add handle
callPtr[] = GetProcAddress(handle, sym)
if callPtr[] == nil:
errorSym
proc checkedMemlookup*(callPtr: ptr pointer, dll: DllContent, sym: LPCSTR) =
## A helper used by `memlib` macro.
memlookup(callPtr, dll, sym, checkedLoadLib, checkedSymAddr)
proc memlookup*(callPtr: ptr pointer, dll: DllContent, sym: LPCSTR) =
## A helper used by `memlib` macro.
memlookup(callPtr, dll, sym, loadLib, symAddr)
proc checkedLibLookup*(callPtr: ptr pointer, lib: MemoryModule, sym: LPCSTR) =
## A helper used by `memlib` macro.
callPtr[] = lib.checkedSymAddr(sym)
proc libLookup*(callPtr: ptr pointer, lib: MemoryModule, sym: LPCSTR) =
## A helper used by `memlib` macro.
callPtr[] = lib.symAddr(sym)
proc checkedRtlookup*(callPtr: ptr pointer, name: string, sym: LPCSTR) =
## A helper used by `memlib` macro.
rtlookup(callPtr, name, sym) do:
raise newException(LibraryError, "Could not load " & name)
do:
raise newException(LibraryError, symErrorMessage(sym))
proc rtlookup*(callPtr: ptr pointer, name: string, sym: LPCSTR) =
## A helper used by `memlib` macro.
rtlookup(callPtr, name, sym) do:
return
do:
return
proc rewritePragma(def: NimNode, hasRaises: bool): (NimNode, NimNode) =
var
sym = def.name.toStrLit
newPragma = newTree(nnkPragma)
typPragma = newTree(nnkPragma)
procty = newTree(nnkProcTy, def.params, typPragma)
# Procs imported from Dll implies gcsafe and raises: []
typPragma.add ident("gcsafe")
newPragma.add ident("gcsafe")
for node in def.pragma:
# ignore single importc
if node.kind == nnkIdent and $node == "importc":
continue
# ignore importc: symbol, but copy the symbol