-
Notifications
You must be signed in to change notification settings - Fork 14
/
Main.pas
522 lines (451 loc) · 15.3 KB
/
Main.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
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
{*------------------------------------------------------------------------------
Data Shredder / Free Space Wiper
Description :
This application uses direct pointer to files for owerwriting content via
threading that speedup process and don't let app going to be freez.
Copyright 2020 MIT License.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Developer : Nima Nikjoo
Email : [email protected]
-------------------------------------------------------------------------------}
unit Main;
{$R+}{$Q+}
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,math,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.FileCtrl,IdBaseComponent, IdThreadComponent,
Vcl.ComCtrls, System.UITypes, System.Types, System.IOUtils, ActiveX,ComObj, System.StrUtils,Masks;
type
TfrmMain = class(TForm)
OpenFile: TOpenDialog;
FShredder: TIdThreadComponent;
Status: TStatusBar;
FreeSpaceWipe: TIdThreadComponent;
GB_FreeSWipe: TGroupBox;
lblDriveLetter: TLabel;
cbLogicalDrivers: TComboBoxEx;
btnFreeSWipe: TButton;
GB_Browse: TGroupBox;
btnSelectFiles: TButton;
lstWipe: TListView;
GB_Wipe: TGroupBox;
btnDestroyFiles: TButton;
GB_Methods: TGroupBox;
lblWipeStd: TLabel;
cbWipeMethods: TComboBox;
btnAddfromDir: TButton;
chFreeSpaceWipe: TCheckBox;
Progress: TProgressBar;
procedure btnSelectFilesClick(Sender: TObject);
procedure btnDestroyFilesClick(Sender: TObject);
procedure FShredderRun(Sender: TIdThreadComponent);
procedure FShredderTerminate(Sender: TIdThreadComponent);
procedure ShredFileAndDelete;
function RandomPassword(PLen: Integer): string;
procedure FormCreate(Sender: TObject);
procedure btnFreeSWipeClick(Sender: TObject);
procedure FreeSpaceWipeRun(Sender: TIdThreadComponent);
procedure FreeSpaceWipeTerminate(Sender: TIdThreadComponent);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure btnAddfromDirClick(Sender: TObject);
function MyGetFiles(const Path, Masks: string): TStringDynArray;
procedure chFreeSpaceWipeClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmMain: TfrmMain;
JobStatus:Boolean;
Free_SWipe:Boolean;
FilesInDir:TStringList;
implementation
{$R *.dfm}
procedure SetFileCreationTime(const FileName: string; const DateTime: TDateTime);
const
FILE_WRITE_ATTRIBUTES = $0100;
var
Handle: THandle;
SystemTime: TSystemTime;
FileTime: TFileTime;
begin
Handle := CreateFile(PChar(FileName), FILE_WRITE_ATTRIBUTES,
FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0);
if Handle=INVALID_HANDLE_VALUE then
RaiseLastOSError;
try
DateTimeToSystemTime(DateTime, SystemTime);
if not SystemTimeToFileTime(SystemTime, FileTime) then
RaiseLastOSError;
if not SetFileTime(Handle, @FileTime, nil, nil) then
RaiseLastOSError;
finally
CloseHandle(Handle);
end;
end;
procedure TfrmMain.btnFreeSWipeClick(Sender: TObject);
begin
Free_SWipe := False;
if Trim(cbLogicalDrivers.Text) = '' then
begin
MessageBoxA(self.Handle,'Please Select Drive You Want To Wipe !','Error',MB_ICONERROR);
end else
begin
btnFreeSWipe.Enabled := False;
FreeSpaceWipe.Start;
end;
end;
procedure TfrmMain.btnSelectFilesClick(Sender: TObject);
var
I:Integer;
begin
lstWipe.Clear;
if OpenFile.Execute(self.Handle) then
begin
for I := 0 to OpenFile.Files.Count - 1 do
begin
lstWipe.AddItem(OpenFile.Files[I],nil);
end;
end;
end;
procedure TfrmMain.chFreeSpaceWipeClick(Sender: TObject);
begin
if GB_FreeSWipe.Enabled = False then
begin
GB_FreeSWipe.Enabled := True;
lblDriveLetter.Enabled := True;
cbLogicalDrivers.Enabled := True;
btnFreeSWipe.Enabled := True;
end else
begin
GB_FreeSWipe.Enabled := False;
lblDriveLetter.Enabled := False;
cbLogicalDrivers.Enabled := False;
btnFreeSWipe.Enabled := False;
end;
end;
function TfrmMain.RandomPassword(PLen: Integer): string;
var
str: string;
begin
Randomize;
str := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()<>?"{}[]|\/';
Result := '';
repeat
Result := Result + str[Random(Length(str)) + 1];
until (Length(Result) = PLen)
end;
procedure TfrmMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
FShredder.Stop;
FreeSpaceWipe.Stop;
Application.Terminate;
end;
procedure TfrmMain.FormCreate(Sender: TObject);
var
Drivelist:TStringDynArray;
Drive:String;
begin
Drivelist := TDirectory.GetLogicalDrives;
for Drive in Drivelist do
begin
cbLogicalDrivers.Items.Add(Drive);
end;
end;
procedure TfrmMain.FreeSpaceWipeRun(Sender: TIdThreadComponent);
const
WbemUser ='';
WbemPassword ='';
WbemComputer ='localhost';
wbemFlagForwardOnly = $00000020;
var
FSWbemLocator : OLEVariant;
FWMIService : OLEVariant;
FWbemObjectSet: OLEVariant;
FWbemObject : OLEVariant;
oEnum : IEnumvariant;
iValue : LongWord;
CBlock,I:Integer;
F:file;
Data,FileName:string;
hFile:THandle;
FreeSize:UInt64;
FilesInDir:TStringDynArray;
begin
try
if (cbLogicalDrivers.Text <> '') then
begin
FShredder.Stop;
lstWipe.Clear;
btnDestroyFiles.Enabled := True;
//========================================//
CoInitialize(nil);
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
FWMIService := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2', WbemUser, WbemPassword);
FWbemObjectSet:= FWMIService.ExecQuery(Format('SELECT * FROM Win32_LogicalDisk Where Caption=%s',[QuotedStr(LeftStr(cbLogicalDrivers.Text,2))]),'WQL',wbemFlagForwardOnly);
oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
if oEnum.Next(1, FWbemObject, iValue) = 0 then
begin
FreeSize := FWbemObject.FreeSpace;
FWbemObject:=Unassigned;
end;
hFile := CreateFile(PChar(cbLogicalDrivers.Text + '$00000000.tmp'), GENERIC_ALL, FILE_SHARE_WRITE, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if hFile <> INVALID_HANDLE_VALUE then
begin
CloseHandle(hFile);
AssignFile(F,cbLogicalDrivers.Text + '$00000000.tmp');
ReWrite(F);
Reset(F,1);
CBlock := 0;
if cbLogicalDrivers.Text <> '' then
begin
repeat
Randomize;
Data := RandomPassword(1024);
BlockWrite(F,PChar(Data)^,RandomRange(1024,102400));
CBlock := CBlock + 1;
until (CBlock >= FreeSize - 2048);
end;
CloseFile(F);
end;
end;
except
Free_SWipe:= True;
CoUninitialize;
CloseFile(F);
DeleteFile(cbLogicalDrivers.Text + '$00000000.tmp');
// Erase MFT
TDirectory.CreateDirectory(cbLogicalDrivers.Text + 'Eliminator');
if System.SysUtils.DirectoryExists(cbLogicalDrivers.Text + 'Eliminator') then
begin
for I := 0 to 80000 do
begin
FileName := IntToStr(I) + '.elim';
hFile := CreateFile(PChar(cbLogicalDrivers.Text + 'Eliminator\' + FileName),
GENERIC_ALL, FILE_SHARE_WRITE, nil, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, 0);
CloseHandle(hFile);
end;
FilesInDir := MyGetFiles(cbLogicalDrivers.Text + 'Eliminator','*.*');
for I := 0 to High(FilesInDir) do
DeleteFile(FilesInDir[I]);
RemoveDir(cbLogicalDrivers.Text + 'Eliminator')
end;
FreeSpaceWipe.Stop;
FreeSpaceWipe.Terminate;
end;
end;
procedure TfrmMain.FreeSpaceWipeTerminate(Sender: TIdThreadComponent);
begin
if Free_SWipe then
begin
Status.Panels[1].Text := 'Done !';
MessageBoxA(self.Handle,'Free Space Wiped Successfully.','Info',MB_ICONINFORMATION);
btnFreeSWipe.Enabled := True;
end;
end;
procedure TfrmMain.FShredderRun(Sender: TIdThreadComponent);
begin
try
if ( OpenFile.Files.Count <> 0 ) or (lstWipe.Items.Count > 0) then
begin
btnDestroyFiles.Enabled := False;
ShredFileAndDelete;
JobStatus := True;
end else
begin
MessageBoxA(self.Handle,'No File/s Was Selected !','Error',MB_ICONERROR);
end;
finally
btnDestroyFiles.Enabled := True;
FShredder.Stop;
FShredder.Terminate;
end;
end;
procedure TfrmMain.FShredderTerminate(Sender: TIdThreadComponent);
begin
if JobStatus then
begin
Status.Panels[1].Text := 'Done !';
MessageBoxA(self.Handle,'You''r files wiped out successfully.','Info',MB_ICONINFORMATION);
btnDestroyFiles.Enabled := True;
end;
end;
function TfrmMain.MyGetFiles(const Path, Masks: string): TStringDynArray;
var
MaskArray: TStringDynArray;
Predicate: TDirectory.TFilterPredicate;
begin
MaskArray := SplitString(Masks, ';');
Predicate :=
function(const Path: string; const SearchRec: TSearchRec): Boolean
var
Mask: string;
begin
for Mask in MaskArray do
if MatchesMask(SearchRec.Name, Mask) then
exit(True);
exit(False);
end;
Result := TDirectory.GetFiles(Path, Predicate);
end;
procedure TfrmMain.ShredFileAndDelete();
var
F:file;
CBlock, FSize:UInt64;
s:String;
I,Passes:Integer;
FileHandle: THandle;
Const
Buf : Byte = 0;
begin
for I := lstWipe.Items.Count - 1 downto 0 do
begin
FileHandle := CreateFile(PChar(lstWipe.Items[I].Caption), GENERIC_READ,
0, {exclusive}
nil, {security}
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
FSize := GetFileSize(FileHandle,nil);
CloseHandle(FileHandle);
//=========================================//
AssignFile(F,lstWipe.Items[I].Caption);
Reset(F,1);
//FSize := FileSize(F);
CBlock := 0;
if FSize <> 0 then
begin
case cbWipeMethods.ItemIndex of
0:
begin
Progress.Max := FSize;
// Write Random Data.
repeat
Randomize;
s := RandomPassword(1024);
BlockWrite(F,PChar(s)^,length(s));
CBlock := CBlock + length(s);
Progress.Position := CBlock;
until (CBlock >= FSize);
// Empty File.
Rewrite(F);
Progress.Position := 0;
end;
1:
begin
Progress.Max := FSize;
for Passes := 0 to 2 do
begin
CBlock := 0;
// Write Random Data.
repeat
Randomize;
s := RandomPassword(1024);
BlockWrite(F,PChar(s)^,length(s));
CBlock := CBlock + length(s);
Progress.Position := CBlock;
until (CBlock >= FSize);
// Empty File.
Rewrite(F);
Progress.Position := 0;
end;
end;
2:
begin
Progress.Max := FSize;
for Passes := 0 to 6 do
begin
CBlock := 0;
// Write Random Data.
repeat
Randomize;
s := RandomPassword(1024);
BlockWrite(F,PChar(s)^,length(s));
CBlock := CBlock + length(s);
Progress.Position := CBlock;
until (CBlock >= FSize);
// Empty File.
Rewrite(F);
Progress.Position := 0;
end;
end;
3:
begin
Progress.Max := FSize;
for Passes := 0 to 34 do
begin
CBlock := 0;
// Write Random Data.
repeat
Randomize;
s := RandomPassword(1024);
BlockWrite(F,PChar(s)^,length(s));
CBlock := CBlock + length(s);
Progress.Position := CBlock;
until (CBlock >= FSize);
// Empty File.
Rewrite(F);
Progress.Position := 0;
end;
end;
end;
end;
CloseFile(F);
Randomize;
SetFileCreationTime(lstWipe.Items[I].Caption,RandomRange(100,25000));
RenameFile(lstWipe.Items[I].Caption,ExtractFilePath(lstWipe.Items[I].Caption) + '$000000.tmp');
DeleteFile(ExtractFilePath(lstWipe.Items[I].Caption) + '$000000.tmp');
lstWipe.Items[I].Delete;
end;
FShredder.Terminate;
end;
procedure TfrmMain.btnAddfromDirClick(Sender: TObject);
var
i:Integer;
FilesInDir:TStringDynArray;
begin
lstWipe.Clear;
with TFileOpenDialog.Create(nil) do
try
Options := [fdoPickFolders];
if Execute then
begin
if FileName <> '' then
begin
FilesInDir := TDirectory.GetFiles(FileName, '*.*', TSearchOption.soAllDirectories);
for I := 0 to High(FilesInDir) do
begin
lstWipe.AddItem(FilesInDir[I],nil);
end;
end;
end;
finally
Free;
end;
end;
procedure TfrmMain.btnDestroyFilesClick(Sender: TObject);
begin
if MessageDlg('Are you sure ?',mtConfirmation,mbYesNo,0) = mrYes then
begin
JobStatus := False;
FShredder.Start;
end;
end;
end.