Uh oh!
There was an error while loading. Please reload this page.
forked from Fr0sT-Brutal/Delphi_MemoryModule
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLibraryInMemory.pas
More file actions
Latest commit
239 lines (204 loc) · 6.3 KB
/
Copy pathLibraryInMemory.pas
File metadata and controls
239 lines (204 loc) · 6.3 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
unit LibraryInMemory;
interface
uses
System.IOUtils, System.SysUtils;
type
TLibraryLoader = function(lpLibFileName: string; out aCodes: TBytes): Boolean;
procedureInstall(aLoader: TLibraryLoader);
procedureUninstall;
implementation
uses
Winapi.Windows, System.Classes, System.Generics.Collections,
DDetours, MemoryModule;
type
TLibrary = class
strict private
FRefCount: Integer;
FCodes: TBytes;
FHandle: HMODULE;
FName: string;
public
constructor Create(aName: string; aHandle: HMODULE; aCodes: TBytes);
procedureAfterConstruction; override;
procedureBeforeDestruction; override;
procedureAddRef;
procedureRelRef;
property Codes: TBytes read FCodes;
propertyName: string read FName;
property Handle: THandle read FHandle;
property RefCount: Integer read FRefCount;
end;
TDLLs = record
private
FItems: TObjectDictionary<string, TLibrary>;
FHandles: TArray<HMODULE>; // Additional container to Handles to speed up HandleExist used by GetProcAddressHook
public
functionHandleExist(aHandle: HMODULE): Boolean; inline;
procedureNewLibrary(Name: string; Codes: TBytes; aHandle: HMODULE);
functionRelease(aHandle: HMODULE): Boolean;
functionTryGetHandle(aName: string; out aHandle: HMODULE): Boolean;
property Handles: TArray<HMODULE> read FHandles;
classoperator Initialize(out Dest: TDLLs);
classoperator Finalize(var Dest: TDLLs);
end;
var DLLs: TDLLs;
LibraryLoader: TLibraryLoader;
LoadLibraryA_Old: function (lpLibFileName: LPCSTR): HMODULE; stdcall;
LoadLibrary_Old: function (lpLibFileName: LPCWSTR): HMODULE; stdcall;
GetProcAddress_Old: function (hModule: HMODULE; lpProcName: LPCSTR): FARPROC; stdcall;
FreeLibrary_Old: function (hLibModule: HMODULE): BOOL; stdcall;
functionLoadLibraryAHook(lpLibFileName: LPCSTR): HMODULE; stdcall;
begin
if DLLs.TryGetHandle(string(lpLibFileName), Result) then Exit;
var c: TBytes;
if SameText(TPath.GetExtension(string(lpLibFileName)), '.dll') and LibraryLoader(string(lpLibFileName), c) thenbegin
Result := HMODULE(MemoryLoadLibary(c));
if Result <> 0then
DLLs.NewLibrary(string(lpLibFileName), c, Result);
endelse
Exit(LoadLibraryA_Old(lpLibFileName));
end;
functionLoadLibraryHook(lpLibFileName: LPCWSTR): HMODULE; stdcall;
begin
if DLLs.TryGetHandle(lpLibFileName, Result) then Exit;
var c: TBytes;
if SameText(TPath.GetExtension(lpLibFileName), '.dll') and LibraryLoader(lpLibFileName, c) thenbegin
Result := HMODULE(MemoryLoadLibary(c));
if Result <> 0then
DLLs.NewLibrary(lpLibFileName, c, Result);
endelse
Exit(LoadLibrary_Old(lpLibFileName));
end;
functionGetProcAddressHook(hModule: HMODULE; lpProcName: LPCSTR): FARPROC; stdcall;
begin
if DLLs.HandleExist(hModule) then
Result := FARPROC(MemoryGetProcAddress(TMemoryModule(hModule), lpProcName))
else
Result := GetProcAddress_Old(hModule, lpProcName);
end;
functionFreeLibraryHook(hLibModule: HMODULE): BOOL; stdcall;
begin
ifnot DLLs.HandleExist(hLibModule) then
Result := FreeLibrary_Old(hLibModule)
elsebegin
if DLLs.Release(hLibModule) then
MemoryFreeLibrary(TMemoryModule(hLibModule));
Result := True;
end;
end;
procedureInstall(aLoader: TLibraryLoader);
begin
ifnot Assigned(aLoader) then Exit;
if Assigned(LibraryLoader) then raise Exception.Create('Library Loader already installed.');
var cs: RTL_CRITICAL_SECTION;;
InitializeCriticalSection(cs);
EnterCriticalSection(cs);
try
LoadLibraryA_Old := InterceptCreate(@LoadLibraryA, @LoadLibraryAHook);
LoadLibrary_Old := InterceptCreate(@LoadLibrary, @LoadLibraryHook);
GetProcAddress_Old := InterceptCreate(@GetProcAddress, @GetProcAddressHook);
FreeLibrary_Old := InterceptCreate(@FreeLibrary, @FreeLibraryHook);
finally
DeleteCriticalSection(cs);
end;
LibraryLoader := aLoader;
end;
procedureUninstall;
begin
ifnot Assigned(LibraryLoader) then Exit;
var cs: RTL_CRITICAL_SECTION;;
InitializeCriticalSection(cs);
EnterCriticalSection(cs);
try
forvar H in DLLs.Handles do
FreeLibrary(H);
InterceptRemove(@LoadLibraryA_Old);
InterceptRemove(@LoadLibrary_Old);
InterceptRemove(@GetProcAddress_Old);
InterceptRemove(@FreeLibrary_Old);
finally
DeleteCriticalSection(cs);
end;
LibraryLoader := nil;
end;
constructor TLibrary.Create(aName: string; aHandle: HMODULE; aCodes: TBytes);
begin
FName := aName;
FHandle := aHandle;
FCodes := aCodes;
end;
procedureTLibrary.AddRef;
begin
Inc(FRefCount);
end;
procedureTLibrary.AfterConstruction;
begin
inherited;
FRefCount := 0;
AddRef;
end;
procedureTLibrary.BeforeDestruction;
begin
SetLength(FCodes, 0);
inherited;
end;
procedureTLibrary.RelRef;
begin
if FRefCount = 0then raise Exception.Create('Invalid reference counter');
Dec(FRefCount);
end;
functionTDLLs.HandleExist(aHandle: HMODULE): Boolean;
begin
forvar o in FHandles do
if o = aHandle then
Exit(True);
Result := False;
end;
procedureTDLLs.NewLibrary(Name: string; Codes: TBytes; aHandle: HMODULE);
begin
FItems.Add(Name, TLibrary.Create(Name, aHandle, Codes));
FHandles := FHandles + [aHandle];
end;
functionTDLLs.Release(aHandle: HMODULE): Boolean;
begin
Result := False;
forvar o in FItems do
if o.Value.Handle = aHandle thenbegin
o.Value.RelRef;
if o.Value.RefCount = 0thenbegin
FItems.Remove(o.Value.Name);
var idx := -1;
forvar i := 0to Length(FHandles) - 1dobegin
if FHandles[i] = aHandle thenbegin
idx := i;
Break;
end;
end;
Assert(idx <> -1);
if idx < Length(FHandles) - 1then
Move(FHandles[idx + 1], FHandles[idx], (Length(FHandles) - idx + 1) * SizeOf(HMODULE));
SetLength(FHandles, Length(FHandles) - 1);
Exit(True);
end;
end;
end;
functionTDLLs.TryGetHandle(aName: string; out aHandle: HMODULE): Boolean;
begin
var L: TLibrary;
if FItems.TryGetValue(aName, L) thenbegin
aHandle := L.Handle;
L.AddRef;
Exit(True);
endelse
Exit(False);
end;
classoperator TDLLs.Finalize(var Dest: TDLLs);
begin
Dest.FItems.Free;
end;
classoperator TDLLs.Initialize(out Dest: TDLLs);
begin
Dest.FItems := TObjectDictionary<string, TLibrary>.Create([doOwnsValues]);
Dest.FHandles := [];
end;
end.