-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUntEOF.pas
383 lines (313 loc) · 11 KB
/
UntEOF.pas
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
(*******************************************************************************
Author:
-> Jean-Pierre LESUEUR (@DarkCoderSc)
https://github.com/DarkCoderSc
https://gist.github.com/DarkCoderSc
https://www.phrozen.io/
Description:
-> Unit for EOF manipulation on Portable Executable Files (x86/x64).
-> Detection and Removal of EOF Data (Often used by Malware to store configuration / files etc..).
Category:
-> Malware Research & Detection
License:
-> MIT
Functions:
-> WritePEOF() : Write extra data at the end of a PE File.
-> ReadPEOF() : Read extra data stored at the end of a PE File.
-> FileIsValidPE() : Check whether or not a file is a valid Portable Executable File.
-> ClearPEOF() : Remove / Sanitize / Disinfect a PE File from any extra data stored at it end.
-> GetPEOFSize() : Get the size of the extra data stored at the end of a PE File.
-> GetFileSize() : Get the expected file size of a PE File following PE Header description.
-> ContainPEOF() : Return True if some extra data is detected at the end of a PE File.
*******************************************************************************)
unit UntEOF;
interface
uses WinAPI.Windows, System.SysUtils, Classes;
type
TBasicPEInfo = record
Valid : Boolean; // True = Valid PE; False = Invalid PE
Arch64 : Boolean; // True = 64bit Image; False = 32bit Image
ImageSize : Int64;
end;
function WritePEOF(APEFile : String; ABuffer : PVOID; ABufferSize : Integer) : Boolean;
function ReadPEOF(APEFile : String; ABuffer : PVOID; ABufferSize : Integer; ABufferPos : Integer = 0) : Boolean;
function FileIsValidPE(AFileName : String) : Boolean;
function ClearPEOF(APEFile : String) : Boolean;
function GetPEOFSize(APEFile : String) : Int64;
function GetFileSize(AFileName : String) : Int64;
function ContainPEOF(APEFile : String) : Boolean;
implementation
{-------------------------------------------------------------------------------
Get File Size (Nothing more to say)
-------------------------------------------------------------------------------}
function GetFileSize(AFileName : String) : Int64;
var AFileInfo : TWin32FileAttributeData;
begin
result := 0;
if NOT FileExists(AFileName) then
Exit();
if NOT GetFileAttributesEx(
PWideChar(AFileName),
GetFileExInfoStandard,
@AFileInfo)
then
Exit();
///
result := (Int64(AFileInfo.nFileSizeLow) or Int64(AFileInfo.nFileSizeHigh shl 32));
end;
{-------------------------------------------------------------------------------
Is target file a 64bit PE file
-------------------------------------------------------------------------------}
function GetBasicPEInfo(APEFile : String; var ABasicPEInfo : TBasicPEInfo) : Boolean;
var hFile : THandle;
AImageDosHeader : TImageDosHeader;
dwBytesRead : DWORD;
AImageFileHeader : TImageFileHeader;
AImageNtHeaderSignature : DWORD;
AOptionalHeader32 : TImageOptionalHeader32;
AOptionalHeader64 : TimageOptionalHeader64;
I : Integer;
AImageSectionHeader : TimageSectionHeader;
begin
result := False;
ABasicPEInfo.Valid := False;
ABasicPEInfo.Arch64 := False;
ABasicPEInfo.ImageSize := 0;
// Open Target File (Must Exists)
hFile := CreateFile(
PChar(APEFile),
GENERIC_READ,
FILE_SHARE_READ,
nil,
OPEN_EXISTING,
0,
0
);
if hFile = INVALID_HANDLE_VALUE then
Exit;
try
SetFilePointer(hFile, 0, nil, FILE_BEGIN);
// Read the Image Dos Header
if NOT ReadFile(
hFile,
AImageDosHeader,
SizeOf(TImageDosHeader),
dwBytesRead,
nil
) then
Exit();
// To be considered as a valid PE file, e_magic must be $5A4D (MZ)
if (AImageDosHeader.e_magic <> IMAGE_DOS_SIGNATURE) then
Exit();
// Move the cursor to Image NT Signature
SetFilePointer(hFile, AImageDosHeader._lfanew, nil, FILE_BEGIN);
// Read the Image NT Signature
if NOT ReadFile(
hFile,
AImageNtHeaderSignature,
SizeOf(DWORD),
dwBytesRead,
nil
) then
Exit();
// To be considered as a valid PE file, Image NT Signature must be $00004550 (PE00)
if (AImageNtHeaderSignature <> IMAGE_NT_SIGNATURE) then
Exit();
ABasicPEInfo.Valid := True;
// Read the Image File Header
if NOT ReadFile(
hFile,
AImageFileHeader,
sizeOf(TImageFileHeader),
dwBytesRead,
0
) then
Exit();
// TImageDosHeader.Machine contains the architecture of the file
ABasicPEInfo.Arch64 := (AImageFileHeader.Machine = IMAGE_FILE_MACHINE_AMD64);
if ABasicPEInfo.Arch64 then begin
// For 64bit Image
if NOT ReadFile(
hFile,
AOptionalHeader64,
sizeOf(TImageOptionalHeader64),
dwBytesRead,
0
) then
Exit();
Inc(ABasicPEInfo.ImageSize, AOptionalHeader64.SizeOfHeaders);
Inc(ABasicPEInfo.ImageSize, AOptionalHeader64.DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY].Size);
end else begin
// For 32bit Image
if NOT ReadFile(
hFile,
AOptionalHeader32,
sizeOf(TImageOptionalHeader32),
dwBytesRead,
0
) then
Exit();
Inc(ABasicPEInfo.ImageSize, AOptionalHeader32.SizeOfHeaders);
Inc(ABasicPEInfo.ImageSize, AOptionalHeader32.DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY].Size);
end;
// Iterate through each section to get the size of each for ImageSize calculation
for I := 0 to AImageFileHeader.NumberOfSections -1 do begin
if NOT ReadFile(
hFile,
AImageSectionHeader,
SizeOf(TImageSectionHeader),
dwBytesRead, 0
) then
Exit(); // Fatal
Inc(ABasicPEInfo.ImageSize, AImageSectionHeader.SizeOfRawData);
end;
// All steps successfully passed
result := True;
finally
CloseHandle(hFile);
end;
end;
{-------------------------------------------------------------------------------
Is target file a valid Portable Executable
-------------------------------------------------------------------------------}
function FileIsValidPE(AFileName : String) : Boolean;
var ABasicPEInfo : TBasicPEInfo;
begin
result := False;
///
GetBasicPEInfo(AFileName, ABasicPEInfo);
result := ABasicPEInfo.Valid;
end;
{-------------------------------------------------------------------------------
Write Data to the End of a PE File.
-------------------------------------------------------------------------------}
function WritePEOF(APEFile : String; ABuffer : PVOID; ABufferSize : Integer) : Boolean;
var hFile : THandle;
ABytesWritten : Cardinal;
begin
result := false;
if NOT FileIsValidPE(APEFile) then
Exit();
hFile := CreateFile(
PWideChar(APEFile),
GENERIC_WRITE,
0,
nil,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0
);
if hFile = INVALID_HANDLE_VALUE then
Exit;
try
SetFilePointer(hFile, 0, nil, FILE_END);
if NOT WriteFile(
hFile,
ABuffer^,
ABufferSize,
ABytesWritten,
0
) then
Exit();
result := true;
finally
CloseHandle(hFile);
end;
end;
{-------------------------------------------------------------------------------
Read Data from the End of a PE File.
-------------------------------------------------------------------------------}
function ReadPEOF(APEFile : String; ABuffer : PVOID; ABufferSize : Integer; ABufferPos : Integer = 0) : Boolean;
var hFile : THandle;
ABytesRead : Cardinal;
begin
result := false;
if NOT FileIsValidPE(APEFile) then
Exit();
hFile := CreateFile(
PWideChar(APEFile),
GENERIC_READ,
0,
nil,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0
);
if hFile = INVALID_HANDLE_VALUE then
Exit();
try
SetFilePointer(
hFile,
(-ABufferSize + ABufferPos),
nil,
FILE_END
);
if NOT ReadFile(
hFile,
ABuffer^,
ABufferSize,
ABytesRead,
0
) then
Exit();
result := true;
finally
CloseHandle(hFile);
end;
end;
{-------------------------------------------------------------------------------
Get Target PE File EOF Size
return codes:
-------------
-1 : Error
>= 0 : The length of EOF data found
-------------------------------------------------------------------------------}
function GetPEOFSize(APEFile : String) : Int64;
var ABasicPEInfo : TBasicPEInfo;
begin
result := -1;
if NOT GetBasicPEInfo(APEFile, ABasicPEInfo) then
raise Exception.Create('Error: Invalid PE File');
result := (GetFileSize(APEFile) - ABasicPEInfo.ImageSize);
end;
{-------------------------------------------------------------------------------
Clear unexpected data at the end of a PE File
-------------------------------------------------------------------------------}
function ClearPEOF(APEFile : String) : Boolean;
var ABasicPEInfo : TBasicPEInfo;
AFileStream : TMemoryStream;
AFileSize : Int64;
AImageSize : Int64;
begin
result := False;
if NOT GetBasicPEInfo(APEFile, ABasicPEInfo) then
raise Exception.Create('Error: Invalid PE File');
AFileSize := GetFileSize(APEFile);
AImageSize := ABasicPEInfo.ImageSize;
// No EOF but no error so far so we return true
if (AFileSize - AImageSize) = 0 then begin
Exit(True);
end;
{
One technique to patch the file. Ignore content after the ImageSize
grabbed from PE info.
}
AFileStream := TMemoryStream.Create();
try
AFileStream.LoadFromFile(APEFile);
AFileStream.Position := 0;
AFileStream.SetSize(AImageSize);
AFileStream.SaveToFile(APEFile);
finally
AFileStream.Free;
end;
result := True;
end;
{-------------------------------------------------------------------------------
Detect if a PE file contain some data at the end of the file
-------------------------------------------------------------------------------}
function ContainPEOF(APEFile : String) : Boolean;
begin
result := (GetPEOFSize(APEFile) > 0);
end;
end.