-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUntMain.pas
350 lines (277 loc) · 8.52 KB
/
UntMain.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
(*******************************************************************************
Author:
-> Jean-Pierre LESUEUR (@DarkCoderSc)
https://github.com/DarkCoderSc
https://gist.github.com/DarkCoderSc
https://www.phrozen.io/
Description:
-> Demonstrate how to use the UntEOF.pas Library for EOF actions.
Category:
-> Malware Research & Detection
License:
-> MIT
*******************************************************************************)
unit UntMain;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Samples.Spin, Vcl.StdCtrls,
Vcl.ExtCtrls, Vcl.ComCtrls, System.ImageList, Vcl.ImgList;
type
TFrmMain = class(TForm)
OD: TOpenDialog;
page: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
TabSheet3: TTabSheet;
memoread: TMemo;
Img16: TImageList;
GroupBox1: TGroupBox;
Label1: TLabel;
edtstr: TEdit;
Label2: TLabel;
spint: TSpinEdit;
Label3: TLabel;
GroupBox2: TGroupBox;
edtpefile: TEdit;
btnloadpefile: TButton;
Panel1: TPanel;
btnReadData: TButton;
Panel2: TPanel;
btnWrite: TButton;
lstscan: TListView;
Panel3: TPanel;
btnScan: TButton;
btnErad: TButton;
Memo: TMemo;
btnRead: TButton;
procedure btnWriteClick(Sender: TObject);
procedure btnReadDataClick(Sender: TObject);
procedure btnloadpefileClick(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure btnScanClick(Sender: TObject);
procedure btnEradClick(Sender: TObject);
procedure btnReadClick(Sender: TObject);
private
{ Private declarations }
procedure ScanFolder(ADirectory : String);
public
{ Public declarations }
end;
var
FrmMain: TFrmMain;
implementation
uses UntEOF, UntUtils, JSON, math;
{$R *.dfm}
{-------------------------------------------------------------------------------
Another cool function I share with you (A bit old tho)
-------------------------------------------------------------------------------}
function BufferToHexView(ABuffer : PVOID; ABufferSize : Int64; pLastOffset : PNativeUINT = nil; AStartOffset : NativeUINT = 0) : String;
var ARows : DWORD;
i, n : integer;
AVal : Byte;
sBuilder : TStringBuilder;
HexVal : array[0..16-1] of TVarRec;
AsciiVal : array[0..16-1] of TVarRec;
HexMask : String; {%x}
AsciiMask : String; {%s}
begin
result := '';
///
ARows := ceil(ABufferSize / 16);
sBuilder := TStringBuilder.Create();
try
{
Row
}
for I := 0 to ARows -1 do begin
{
Col
}
for n := 0 to 16-1 do begin
AVal := PByte(NativeUInt(ABuffer) + (I * 16) + n)^;
HexVal[n].VType := vtInteger;
HexVal[n].VInteger := AVal;
AsciiVal[n].VType := vtChar;
if AVal in [32..255] then begin
AsciiVal[n].VChar := AnsiChar(AVal);
end else begin
AsciiVal[n].VChar := '.';
end;
end;
HexMask := '';
AsciiMask := '';
for n := 0 to 16-1 do begin
if ((I * 16) + n) > ABufferSize then begin
HexMask := HexMask + #32#32#32;
AsciiMask := AsciiMask + #32#32;
continue;
end;
HexMask := HexMask + '%.2x' + #32;
AsciiMask := AsciiMask + '%s';
end;
Delete(HexMask, length(HexMask), 1);
{
Draw
}
sBuilder.AppendLine(
Format('%.8x', [AStartOffset + (I * 16)]) + '|' +
Format(HexMask, HexVal) + '|' +
Format(AsciiMask, AsciiVal)
);
end;
finally
result := sBuilder.ToString();
if Assigned(pLastOffset) then begin
pLastOffset^ := (ARows * 16);
end;
sBuilder.Free;
end;
end;
procedure TFrmMain.ScanFolder(ADirectory : String);
var ASearchRec : TSearchRec;
AFullPath : String;
AItem : TListItem;
AEOFSize : Int64;
begin
if NOT DirectoryExists(ADirectory) then
raise Exception.Create('Target directory doesn''t exists.');
lstscan.Clear;
btnErad.Enabled := False;
ADirectory := IncludeTrailingPathDelimiter(ADirectory);
if (FindFirst(Format('%s*.*', [ADirectory]), (faAnyFile - faDirectory), ASearchRec) = 0) then begin
repeat
AFullPath := Format('%s%s', [ADirectory, ASearchRec.Name]);
///
if NOT FileIsValidPE(AFullPath) then
continue;
AItem := lstscan.Items.Add;
AEOFSize := GetPEOFSize(AFullPath);
AItem.Caption := ASearchRec.Name;
AItem.SubItems.Add(Format('%d bytes', [AEOFSize]));
AItem.SubItems.Add(ADirectory);
if AEOFSize > 0 then begin
AItem.ImageIndex := 3;
btnErad.Enabled := True;
end else
AItem.ImageIndex := 4;
until (FindNext(ASearchRec) <> 0);
FindClose(ASearchRec);
end;
end;
procedure TFrmMain.btnEradClick(Sender: TObject);
var i : integer;
AItem : TlistItem;
ATargetFolder : String;
begin
ATargetFolder := '';
for I := 0 to lstscan.Items.Count -1 do begin
AItem := lstscan.Items[i];
ATargetFolder := AItem.SubItems[1];
if (AItem.ImageIndex <> 3) then
continue;
ClearPEOF(IncludeTrailingPathDelimiter(AItem.SubItems[1]) + AItem.Caption);
end;
// Re launch a scan on the same folder
if ATargetFolder <> '' then
ScanFolder(ATargetFolder);
end;
procedure TFrmMain.btnloadpefileClick(Sender: TObject);
begin
OD.FileName := '';
if NOT OD.Execute then
Exit();
edtpefile.Text := OD.FileName;
end;
procedure TFrmMain.btnWriteClick(Sender: TObject);
var AJson : TJsonObject;
AJsonStr : String;
ARet : Boolean;
I : Integer;
begin
if NOT FileExists(edtpefile.Text) then
raise Exception.Create('Please load a valid PE file.');
ClearPEOF(edtpefile.Text); // Clear possible existing data
AJson := TJsonObject.Create();
try
AJson.AddPair('data1', TJSONString.Create(edtstr.text));
AJson.AddPair('data2', TJSONNumber.Create(spint.value));
AJson.AddPair('data3', TJSONString.Create(memo.Text));
AJsonStr := AJson.ToJSON;
ARet := WritePEOF(edtpefile.Text, @AJsonStr[1], (Length(AJsonStr) * SizeOf(WideChar)));
if NOT ARet then
raise Exception.Create('Could not write EOF Data to file');
finally
AJson.Free;
end;
edtstr.Clear;
memo.Clear;
///
MessageBoxW(self.Handle, 'Success', 'Write PEOF', MB_IconInformation);
end;
function GetJsonStringFromEOF(AFileName : String) : String;
var ASize : Int64;
begin
result := '';
if ContainPEOF(AFileName) = False then
raise Exception.Create('The file doesn''t contain any EOF Data or is not a valid PE File.');
{
Read the whole EOF as JSON String
}
ASize := GetPEOFSize(AFileName);
if ASize = 0 then
raise Exception.Create('Invalid EOF Size');
SetLength(result, (ASize div SizeOf(WideChar)));
if NOT ReadPEOF(AFileName, @result[1], ASize) then
raise Exception.Create('Could not read EOF Data');
end;
procedure TFrmMain.btnReadClick(Sender: TObject);
var AJson : TJsonValue;
AJsonStr : String;
begin
try
AJsonStr := GetJsonStringFromEOF(edtpefile.text);
AJson := TJsonObject.ParseJSONValue(AJsonStr); // If Invalid Json String, will trigger an exception
try
edtstr.Text := AJson.GetValue<string>('data1');
spint.Value := AJson.GetValue<integer>('data2');
Memo.Text := AJson.GetValue<string>('data3');
finally
AJson.Free;
end;
except
on E : Exception do
MessageBoxW(self.Handle, PWideChar('Error while retrieving EOF data : ' + E.Message), 'Error', MB_IconHand);
end;
end;
procedure TFrmMain.btnReadDataClick(Sender: TObject);
var AJsonStr : String;
begin
try
if NOT OD.Execute() then
exit();
AJsonStr := GetJsonStringFromEOF(OD.FileName);
Memoread.Text := BufferToHexView(@AJsonStr[1], Length(AJsonStr) * SizeOf(WideChar));
except
on E : Exception do
MessageBoxW(self.Handle, PWideChar('Error while retrieving EOF data : ' + E.Message), 'Error', MB_IconHand);
end;
end;
procedure TFrmMain.btnScanClick(Sender: TObject);
var AFolder : String;
begin
AFolder := BrowseForFolder('Select target folder');
if AFolder = '' then
Exit();
ScanFolder(AFolder);
end;
procedure TFrmMain.FormResize(Sender: TObject);
begin
btnWrite.Left := (Panel2.Width div 2) - btnWrite.Width - 4;
btnRead.Left := (Panel2.Width div 2) + 4;
btnReadData.Left := (Panel1.Width div 2) - (btnRead.Width div 2);
btnScan.Left := (Panel3.Width div 2) - btnScan.Width - 4;
btnErad.Left := (Panel3.Width div 2) + 4;
btnLoadPeFile.Left := edtpefile.Left + edtpefile.Width + 4;
end;
end.