-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUYaffi.pas
3470 lines (3119 loc) · 138 KB
/
UYaffi.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
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
{ YAFFI - Yet Another Free Forensic Imager Copyright (C) <2015> <Ted Smith>
A free, cross platform, GUI based imager for acquiring images
in DD raw and EWF format:
https://github.com/tedsmith/yaffi
Provision of the libEWF C library by Joachim Metz is heavily
acknowledged and credited : https://github.com/libyal/libewf
My personal thanks to Joachim for the several e-mails he read
and helpfully replied to!
Contributions from Erwan Labalec for the provision of the
originating Delphi library that contained some of the converted
libEWF functions, a supporting zlib DLL and a compatabile
libewf.dll file (68Kb). These were needed for the opening of
an EWF image file. That is acknowledged: http://labalec.fr/erwan/?p=1235
That same library has then been adjusted and enhanced by Ted Smith :
* Addition of several other functions that are needed for imaging
* Better error reporting added for verbose errors on -1 return values
* Adjusted for use with the Freepascal Compiler and Lazarus IDE
* See unit LibEWFUnit.pas for more details.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
}
unit UYaffi;
{$mode objfpc}{$H+}
interface
uses
{$ifdef UNIX}
process,
{$IFDEF UseCThreads}
cthreads,
{$ENDIF}
{$endif}
{$ifdef Windows}
Process, Windows, ActiveX, ComObj, Variants,
win32proc, GPTMBR, uGPT, // for the OS name detection : http://free-pascal-lazarus.989080.n3.nabble.com/Lazarus-WindowsVersion-td4032307.html
{$endif}
LibEWFUnit, diskspecification, uProgress, Classes, SysUtils, FileUtil,
Forms, Controls, Graphics, LazUTF8, strutils,
Dialogs, StdCtrls, ComCtrls, ExtCtrls, Menus,
textsearch,
{HashLib4Pascal and xxHash64 libraries are both licensed under the MIT License
https://opensource.org/licenses/MIT
HashLib4Pascal : https://github.com/Xor-el/HashLib4Pascal
xxHash64 : https://github.com/Cyan4973/xxHash and http://cyan4973.github.io/xxHash/
}
HlpHashFactory,
HlpIHash,
HlpIHashResult;
type
{ TfrmYaffi }
TfrmYaffi = class(TForm)
btnAbort: TButton;
btnChooseImageName: TButton;
btnStartImaging: TButton;
btnRefreshDiskList: TButton;
btnSearchOptions: TButton;
cbdisks: TComboBox;
cbVerify: TCheckBox;
ComboCompression: TComboBox;
ComboSegmentSize: TComboBox;
ComboImageType: TComboBox;
comboHashChoice: TComboBox;
lblFreeSpaceReceivingDisk: TLabel;
ledtComputedHashA: TLabeledEdit;
ledtComputedHashB: TLabeledEdit;
ledtExaminersName: TLabeledEdit;
ledtCaseName: TLabeledEdit;
GroupBox1: TGroupBox;
ImageList1: TImageList;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
ledtImageHashA: TLabeledEdit;
ledtImageHashB: TLabeledEdit;
ledtImageName: TLabeledEdit;
ledtExhibitRef: TLabeledEdit;
ledtSelectedItem: TLabeledEdit;
lt: TLabel;
ls: TLabel;
lm: TLabel;
lv: TLabel;
memNotes: TMemo;
memGeneralCaseNotes: TMemo;
menShowDiskManager: TMenuItem;
menShowDiskTechData: TMenuItem;
memImageDisk: TMenuItem;
menHashDisk: TMenuItem;
MenuItem1: TMenuItem;
MenuItem2: TMenuItem;
PopupMenu1: TPopupMenu;
SaveImageDialog: TSaveDialog;
toggleSearchMode: TToggleBox;
TreeView1: TTreeView;
// http://forum.lazarus.freepascal.org/index.php/topic,28560.0.html
procedure btnAbortClick(Sender: TObject);
procedure btnSearchOptionsClick(Sender: TObject);
procedure btnStartImagingClick(Sender: TObject);
procedure btnRefreshDiskListClick(Sender: TObject);
procedure btnChooseImageNameClick(Sender: TObject);
procedure ComboCompressionSelect(Sender: TObject);
procedure ComboImageTypeSelect(Sender: TObject);
procedure ComboSegmentSizeSelect(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure ledtCaseNameEnter(Sender: TObject);
procedure ledtExaminersNameEnter(Sender: TObject);
procedure ledtExhibitRefEnter(Sender: TObject);
procedure memGeneralCaseNotesEnter(Sender: TObject);
procedure memNotesEnter(Sender: TObject);
procedure menHashDiskClick(Sender: TObject);
procedure menShowDiskManagerClick(Sender: TObject);
procedure MenuItem2Click(Sender: TObject);
procedure TreeView1SelectionChanged(Sender: TObject);
function InitialiseHashChoice(Sender : TObject) : Integer;
function InitialiseImageType(Sender : TObject) : Integer;
function InitialiseSegmentSize(Sender : TObject) : Int64;
function InitialiseCompressionChoice(Sender : TObject) : Integer;
procedure cbdisksChange(Sender: TObject);
function GetDiskTechnicalSpecs(Sender: TObject) : Integer;
function GetDiskTechnicalSpecsLinux(Sender: TObject) : Integer;
function GetFriendlyDiskDriveName (str : string) : string;
procedure memWipeDiskClick(Sender: TObject);
private
{ private declarations }
public
Stop : boolean;
BytesPerSector : integer;
{ public declarations }
end;
var
frmYaffi: TfrmYaffi;
PhyDiskNode, PartitionNoNode, DriveLetterNode : TTreeNode;
HashChoice : integer;
slOffsetsOfHits : TStringList;
{$ifdef Windows}
// These four functions are needed for traversing the attached disks in Windows.
// Yes, all these for just that!! The joy of Windows coding
// Credit to RRUZ at SO : https://stackoverflow.com/questions/12271269/how-can-i-correlate-logical-drives-and-physical-disks-using-the-wmi-and-delphi/12271778#comment49108167_12271778
// https://theroadtodelphi.wordpress.com/2010/12/01/accesing-the-wmi-from-pascal-code-delphi-oxygene-freepascal/#Lazarus
function ListDrives : string;
function VarStrNull(const V:OleVariant):string;
function GetWMIObject(const objectName: String): IDispatch;
function VarArrayToStr(const vArray: variant): string;
// Formatting functions
function GetDiskLengthInBytes(hSelectedDisk : THandle) : Int64;
function GetSectorSizeInBytes(hSelectedDisk : THandle) : Int64;
function GetJustDriveLetter(str : widestring) : string;
function GetDriveIDFromLetter(str : string) : Byte;
function GetVolumeName(DriveLetter: Char): string;
function GetOSName() : string;
{$endif}
{$ifdef Unix}
function ListDrivesLinux : string;
function GetOSNameLinux() : string;
function GetBlockCountLinux(s : string) : string;
function GetBlockSizeLinux(DiskDevName : string) : Integer;
function GetDiskLabels(DiskDevName : string) : string;
function GetByteCountLinux(DiskDevName : string) : QWord;
{$endif}
function ImageDiskDD(hDiskHandle : THandle; DiskSize : Int64; HashChoice : Integer; fsImageName : TFileStream) : Int64;
function VerifyDDImage(fsImageName : TFileStream; ImageFileSize : Int64) : string;
function ImageDiskE01(hDiskHandle : THandle; SegmentSize : Int64; DiskSize : Int64; HashChoice : Integer) : Int64;
function VerifyE01Image(strImageName : widestring) : string;
function HashDisk(hDiskHandle : THandle; DiskSize : Int64; HashChoice : Integer) : Int64;
function FormatByteSize(const bytes: QWord): string;
function ExtractNumbers(s: string): string;
function DoCaseSensitiveTextSearchOfBuffer (Buffer : array of byte; TotalBytesRead : Int64; BytesRead : Integer) : Integer;
function DoCaseINSensitiveTextSearchOfBuffer(Buffer : array of byte; TotalBytesRead : Int64; BytesRead : Integer) : Integer;
function DoHEXSearchOfBuffer (Buffer : array of byte; TotalBytesRead : Int64; BytesRead : Integer) : Integer;
implementation
{$R *.lfm}
{ TfrmYaffi }
// Enable or disable elements depending on the OS hosting the application
procedure TfrmYaffi.FormCreate(Sender: TObject);
var
MissingFileCount : integer;
begin
Stop := false;
MissingFileCount := 0;
ledtComputedHashA.Enabled := false;
ledtComputedHashB.Enabled := false;
slOffsetsOfHits := TStringList.Create;
{$ifdef Windows}
// These are the Linux centric elements, so disable them on Windows
cbdisks.Enabled := false;
cbdisks.Visible := false;
Label1.Enabled := false;
Label1.Visible := false;
Label2.Enabled := false;
Label2.Visible := false;
Label3.Enabled := false;
Label3.Visible := false;
Label4.Enabled := false;
Label4.Visible := false;
Label5.Enabled := false;
Label5.Visible := false;
lv.Enabled := false;
lv.Visible := false;
lm.Enabled := false;
lm.Visible := false;
ls.Enabled := false;
ls.Visible := false;
lt.Enabled := false;
lt.Visible := false;
if FileExists('libewf.dll') = false
then
begin
Showmessage('Libewf.dll not found. Only DD imaging will work.');
inc(MissingFileCount, 1);
end;
if FileExists('zlib.dll') = false
then
begin
Showmessage('Zlib.dll not found. Any E01 image compression will fail');
inc(MissingFileCount, 1);
end;
if FileExists('msvcr100.dll') = false
then
begin
Showmessage('msvcr100.dll not found. May only cause a problem if using Windows XP or earlier');
inc(MissingFileCount, 1);
end;
{$endif Windows}
{$ifdef UNIX}
if FileExists('libewf.so') = false
then
begin
Showmessage('Libewf.so not found. Only DD imaging will work.');
inc(MissingFileCount, 1);
end;
if FileExists('libz.so') = false
then
begin
Showmessage('libz.so (Linux zlip library) not found. Any E01 image compression will fail');
inc(MissingFileCount, 1);
end;
{$endif}
if MissingFileCount = 3 then
Showmessage('You are missing three important DLL files.' + #13#10 +
'Considering they came zipped with YAFFI, this is perplexing.' + #13#10 +
'Whilst continuing is indeed possible, expect problems!');
GroupBox1.Enabled := true;
GroupBox1.Visible := true;
end;
procedure TfrmYaffi.ledtCaseNameEnter(Sender: TObject);
begin
ledtCaseName.Clear;
end;
procedure TfrmYaffi.ledtExaminersNameEnter(Sender: TObject);
begin
ledtExaminersName.Clear;
end;
procedure TfrmYaffi.ledtExhibitRefEnter(Sender: TObject);
begin
ledtExhibitRef.Clear;
end;
procedure TfrmYaffi.memGeneralCaseNotesEnter(Sender: TObject);
begin
memGeneralCaseNotes.Clear;
end;
procedure TfrmYaffi.memNotesEnter(Sender: TObject);
begin
memNotes.Clear;
end;
procedure TfrmYaffi.btnRefreshDiskListClick(Sender: TObject);
begin
{$ifdef Windows}
try
TreeView1.Items.Clear;
ListDrives;
finally
end;
{$endif}
{$ifdef UNIX}
try
Treeview1.Items.Clear;
ListDrivesLinux;
finally
end;
{$endif}
end;
procedure TfrmYaffi.btnAbortClick(Sender: TObject);
begin
Stop := TRUE;
if Stop = TRUE then
begin
ledtComputedHashA.Text := 'Process aborted.';
ledtComputedHashB.Text := 'Process aborted.';
ledtImageHashA.Text := 'Process aborted.';
ledtImageHashB.Text := 'Process aborted.';
Abort;
end;
end;
procedure TfrmYaffi.btnSearchOptionsClick(Sender: TObject);
begin
frmTextSearch.Show;
end;
procedure TfrmYaffi.ComboImageTypeSelect(Sender: TObject);
begin
if frmYaffi.InitialiseImageType(nil) = 1 then
begin
ledtImageName.Text := ChangeFileExt(ledtImageName.Text, '.E01');
ComboCompression.Enabled := true;
ComboSegmentSize.Enabled := true;
end;
if frmYaffi.InitialiseImageType(nil) = 2 then
begin
ledtImageName.Text := ChangeFileExt(ledtImageName.Text, '.dd');
ComboCompression.Enabled := false;
ComboSegmentSize.Enabled := false;
end;
end;
procedure TfrmYaffi.ComboSegmentSizeSelect(Sender: TObject);
begin
frmYaffi.InitialiseSegmentSize(nil);
end;
procedure TfrmYaffi.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
if assigned(slSearchList) then slSearchList.Free;
if assigned(slOffsetsOfHits) then slOffsetsOfHits.Free;
end;
procedure TfrmYaffi.ComboCompressionSelect(Sender: TObject);
begin
frmYaffi.InitialiseCompressionChoice(nil);
end;
// For now, returns -1 if no hits found. 1 otherwise.
function DoCaseSensitiveTextSearchOfBuffer(Buffer : array of byte; TotalBytesRead : Int64; BytesRead : Integer) : Integer;
var
TextData : ansistring;
i, Counter, PositionFoundInBuffer : integer;
PositionFoundOnDisk : Int64;
begin
i := 0;
Counter := 0;
PositionFoundInBuffer := 0;
PositionFoundOnDisk := 0;
// Convert the binary byte array to an array of chars for searching in
TextData := textsearch.ByteArrayToString(Buffer);
// Now search the array of chars case sensitively
for i := 0 to slSearchList.Count -1 do
begin
if Pos(Trim(slSearchList.Strings[i]), TextData) > 0 then
begin
PositionFoundInBuffer := Pos(slSearchList.Strings[i], TextData);
PositionFoundOnDisk := (TotalBytesRead - BytesRead) + (PositionFoundInBuffer -1);
slOffsetsOfHits.Add(slSearchList.Strings[i] + ' at offset ' + IntToStr(PositionFoundOnDisk));
Counter := 1;
// Check the buffer again, from the point the first hit was found, for any more hits
if PosEx(slSearchList.Strings[i], TextData, (PositionFoundInBuffer + Length(slSearchList.Strings[i]))) > 0 then
repeat
PositionFoundInBuffer := PosEx(slSearchList.Strings[i], TextData, (PositionFoundInBuffer + Length(slSearchList.Strings[i])));
PositionFoundOnDisk := (TotalBytesRead - BytesRead) + (PositionFoundInBuffer -1);
inc(PositionFoundInBuffer, 1);
inc(Counter, 1);
slOffsetsOfHits.Add(slSearchList.Strings[i] + ' at offset ' + IntToStr(PositionFoundOnDisk));
until PosEx(slSearchList.Strings[i], TextData, PositionFoundInBuffer) = 0;
Result := Counter;
end
else Result := -1;
end;
end;
// For now, returns -1 if no hits found. 1 otherwise.
function DoCaseINSensitiveTextSearchOfBuffer(Buffer : array of byte; TotalBytesRead : Int64; BytesRead : Integer) : Integer;
var
TextData : ansistring;
i, Counter, PositionFoundInBuffer : integer;
PositionFoundOnDisk : Int64;
begin
i := 0;
TextData := '';
PositionFoundInBuffer := 0;
PositionFoundOnDisk := 0;
Counter := 0;
// Convert the binary byte array to an array of chars for searching in
TextData := textsearch.ByteArrayToString(Buffer);
// Now search the array of chars case INsensitively
for i := 0 to slSearchList.Count -1 do
begin
if textsearch.PosCaseInsensitive(Trim(slSearchList.Strings[i]), TextData) > 0 then
begin
PositionFoundInBuffer := PosCaseInsensitive(slSearchList.Strings[i], TextData);
PositionFoundOnDisk := (TotalBytesRead - BytesRead) + (PositionFoundInBuffer -1);
slOffsetsOfHits.Add(slSearchList.Strings[i] + ' at offset ' + IntToStr(PositionFoundOnDisk));
Counter := 1;
// Check the buffer again, from the point the first hit was found, for any more hits
if textsearch.InsensPosEx(slSearchList.Strings[i], TextData, (PositionFoundInBuffer + Length(slSearchList.Strings[i]))) > 0 then
repeat
PositionFoundInBuffer := textsearch.InsensPosEx(slSearchList.Strings[i], TextData, (PositionFoundInBuffer + Length(slSearchList.Strings[i])));
PositionFoundOnDisk := (TotalBytesRead - BytesRead) + (PositionFoundInBuffer -1);
inc(PositionFoundInBuffer, 1);
inc(Counter, 1);
slOffsetsOfHits.Add(slSearchList.Strings[i] + ' at offset ' + IntToStr(PositionFoundOnDisk));
until textsearch.InsensPosEx(slSearchList.Strings[i], TextData, PositionFoundInBuffer) = 0;
Result := Counter;
end
else Result := -1;
end;
end;
// For now, returns -1 if no hex entries are found. 1 otherwise.
function DoHEXSearchOfBuffer(Buffer : array of byte; TotalBytesRead : Int64; BytesRead : Integer) : Integer;
var
strHexVal : ansistring;
i, Counter, PosInBufferOfHexValue : integer;
intHexValAsDec : QWORD;
PositionFoundOnDisk : Int64;
begin
Result := -1;
PositionFoundOnDisk := 0;
intHexValAsDec := 0;
Counter := 0;
// Loop through the users list of hex entries and search the buffer for each one
for i := 0 to slSearchList.Count -1 do
begin
// Format the hex strings in the user list (remove spaces, add a $ before it etc)
strHexVal := DelSpace(slSearchList[i]);
intHexValAsDec := textsearch.Hex2DecBig(strHexVal);
// Search for the integer representation using bespoke, fast, IndexOfDWord function
PosInBufferOfHexValue := textsearch.IndexOfDWord(@Buffer[0], Length(Buffer), SwapEndian(intHexValAsDec)); //SwapEndian($4003E369)); //StrToInt('$' + Memo1.Lines[i]));
// If it was found, detail the find
if PosInBufferOfHexValue > -1 then
begin
PositionFoundOnDisk := (TotalBytesRead - BytesRead) + PosInBufferOfHexValue;
slOffsetsOfHits.Add(slSearchList.Strings[i] + ' at offset ' + IntToStr(PositionFoundOnDisk));
inc(Counter, 1);
Result := Counter;
end;
end;
end;
// Gets variosu disk properties like model, manufacturer etc, for Linux usage
// Returns a concatanated string for display in the Treeview
function GetDiskLabels(DiskDevName : string) : string;
const
smodel = 'ID_MODEL=';
sserial = 'ID_SERIAL_SHORT=';
stype = 'ID_TYPE=';
svendor = 'ID_VENDOR=';
var
DiskInfoProcess : TProcess;
DiskInfoProcessUDISKS : TProcess;
diskinfo, diskinfoUDISKS : TStringList;
i : Integer;
stmp, strModel, strVendor, strType, strSerial : String;
begin
// Probe all attached disks and populate the interface
DiskInfoProcess:=TProcess.Create(nil);
DiskInfoProcess.Options:=[poWaitOnExit, poUsePipes];
DiskInfoProcess.CommandLine:='/sbin/udevadm info --query=property --name='+DiskDevName; //get info about selected disk
DiskInfoProcess.Execute;
diskinfo:=TStringList.Create;
diskinfo.LoadFromStream(DiskInfoProcess.Output);
for i:=0 to diskinfo.Count-1 do
begin
if pos(smodel, diskinfo.Strings[i])>0 then
begin
stmp:=diskinfo.Strings[i];
Delete(stmp, 1, Length(smodel));
if pos('_', stmp)>0 then
begin
strModel:=Copy(stmp, 1, pos('_', stmp)-1);
Delete(stmp, 1, pos('_', stmp));
strModel :=stmp;
if Length(strModel) = 0 then strModel := 'No Value';
end
else
strModel:=stmp;
end
else if pos(sserial, diskinfo.Strings[i])>0 then
begin
strSerial := Copy(diskinfo.Strings[i], Length(sserial)+1, Length(diskinfo.Strings[i])-Length(sserial));
if Length(strSerial) = 0 then strSerial := 'No Value';
end
else if pos(stype, diskinfo.Strings[i])>0 then
begin
strType := Copy(diskinfo.Strings[i], Length(stype)+1, Length(diskinfo.Strings[i])-Length(stype));
if Length(strType) = 0 then strType := 'No Value';
end
else if pos(svendor, diskinfo.Strings[i])>0 then
begin
strVendor := Copy(diskinfo.Strings[i], Length(svendor)+1, Length(diskinfo.Strings[i])-Length(svendor));
if Length(strVendor) = 0 then strVendor := 'No Value';
end;
end;
result := '(Model: ' + strModel + ', Serial No: ' + strSerial + ', Type: ' + strType + ', Vendor: ' + strVendor + ')';
end;
// Delete this eventually, once youre sure the Combo box is redundant
procedure TfrmYaffi.cbdisksChange(Sender: TObject);
const
smodel = 'ID_MODEL=';
sserial = 'ID_SERIAL_SHORT=';
stype = 'ID_TYPE=';
svendor = 'ID_VENDOR=';
var
DiskInfoProcess : TProcess;
DiskInfoProcessUDISKS : TProcess;
diskinfo, diskinfoUDISKS : TStringList;
i : Integer;
stmp : String;
begin
if cbdisks.ItemIndex<0 then
exit;
lv.Caption:='';
lm.Caption:='';
ls.Caption:='';
lt.Caption:='';
// Probe all attached disks and populate the interface
DiskInfoProcess:=TProcess.Create(nil);
DiskInfoProcess.Options:=[poWaitOnExit, poUsePipes];
DiskInfoProcess.CommandLine:='/sbin/udevadm info --query=property --name='+cbdisks.Text; //get info about selected disk
DiskInfoProcess.Execute;
diskinfo:=TStringList.Create;
diskinfo.LoadFromStream(DiskInfoProcess.Output);
for i:=0 to diskinfo.Count-1 do
begin
if pos(smodel, diskinfo.Strings[i])>0 then
begin
stmp:=diskinfo.Strings[i];
Delete(stmp, 1, Length(smodel));
if pos('_', stmp)>0 then
begin
lv.Caption:=Copy(stmp, 1, pos('_', stmp)-1);
Delete(stmp, 1, pos('_', stmp));
lm.Caption:=stmp;
end
else
lm.Caption:=stmp;
end
else if pos(sserial, diskinfo.Strings[i])>0 then
ls.Caption:=Copy(diskinfo.Strings[i], Length(sserial)+1, Length(diskinfo.Strings[i])-Length(sserial))
else if pos(stype, diskinfo.Strings[i])>0 then
lt.Caption:=Copy(diskinfo.Strings[i], Length(stype)+1, Length(diskinfo.Strings[i])-Length(stype))
else if pos(svendor, diskinfo.Strings[i])>0 then
begin
lm.Caption:=lv.Caption+' '+lm.Caption;
lv.Caption:=Copy(diskinfo.Strings[i], Length(svendor)+1, Length(diskinfo.Strings[i])-Length(svendor));
end;
end;
// Get all technical specifications about a user selected disk and save it
DiskInfoProcessUDISKS := TProcess.Create(nil);
DiskInfoProcessUDISKS.Options := [poWaitOnExit, poUsePipes];
DiskInfoProcessUDISKS.CommandLine := 'udisks --show-info /dev/' + cbdisks.Text;
DiskInfoProcessUDISKS.Execute;
diskinfoUDISKS := TStringList.Create;
diskinfoUDISKS.LoadFromStream(diskinfoProcessUDISKS.Output);
diskinfoUDISKS.SaveToFile('TechnicalDiskDetails.txt');
// Free everything
diskinfo.Free;
diskinfoUDISKS.Free;
DiskInfoProcess.Free;
DiskInfoProcessUDISKS.Free;
end;
{$ifdef UNIX}
function ListDrivesLinux : string;
var
DisksProcess: TProcess;
i: Integer;
slDisklist: TSTringList;
PhyDiskNode, PartitionNoNode, DriveLetterNode : TTreeNode;
strPhysDiskSize, strLogDiskSize, DiskDevName, DiskLabels, dmCryptDiscovered : string;
begin
DisksProcess:=TProcess.Create(nil);
DisksProcess.Options:=[poWaitOnExit, poUsePipes];
DisksProcess.CommandLine:='cat /proc/partitions'; //get all disks/partitions list
DisksProcess.Execute;
slDisklist:=TStringList.Create;
slDisklist.LoadFromStream(DisksProcess.Output);
slDisklist.Delete(0); //delete columns name line
slDisklist.Delete(0); //delete separator line
//cbdisks.Items.Clear;
frmYAFFI.Treeview1.Images := frmYAFFI.ImageList1;
PhyDiskNode := frmYAFFI.TreeView1.Items.Add(nil,'Physical Disk') ;
PhyDiskNode.ImageIndex := 0;
DriveLetterNode := frmYAFFI.TreeView1.Items.Add(nil,'Logical Volume') ;
DriveLetterNode.ImageIndex := 1;
// List physical disks, e.g. sda, sdb, hda, hdb etc
for i:=0 to slDisklist.Count-1 do
begin
if Length(Copy(slDisklist.Strings[i], 26, Length(slDisklist.Strings[i])-25))=3 then
begin
DiskDevName := '/dev/' + Trim(RightStr(slDisklist.Strings[i], 3));
DiskLabels := GetDiskLabels(DiskDevName);
strPhysDiskSize := FormatByteSize(GetByteCountLinux(DiskDevName));
frmYaffi.TreeView1.Items.AddChild(PhyDiskNode, DiskDevName + ' | ' + strPhysDiskSize + ' ' + DiskLabels);
end;
//cbdisks.Items.Add(Copy(slDisklist.Strings[i], 26, Length(slDisklist.Strings[i])-25));
end;
// List Logical drives (partitions), e.g. sda1, sdb2, hda1, hdb2 etc
for i:=0 to slDisklist.Count-1 do
begin
dmCryptDiscovered := '';
if Length(Copy(slDisklist.Strings[i], 26, Length(slDisklist.Strings[i])-25))=4 then
begin
DiskDevName := '/dev/' + Trim(RightStr(slDisklist.Strings[i], 4));
DiskLabels := GetDiskLabels(DiskDevName);
if Pos('/dm', DiskDevName) > 0 then
begin
dmCryptDiscovered := '*** mounted dmCrypt drive! ***';
end;
strLogDiskSize := FormatByteSize(GetByteCountLinux(DiskDevName));
frmYaffi.TreeView1.Items.AddChild(DriveLetterNode, DiskDevName + ' | ' + strLogDiskSize + ' ' + dmCryptDiscovered +' ' + DiskLabels);
end;
end;
frmYaffi.Treeview1.AlphaSort;
slDisklist.Free;
DisksProcess.Free;
end;
{$endif}
// Returns a string holding the disk block COUNT (not size) as extracted from the string from
// /proc/partitions. e.g. : 8 0 312571224 sda
function GetBlockCountLinux(s : string) : string;
var
strBlockCount : string;
StartPos, EndPos : integer;
begin
strBlockCount := '';
StartPos := 0;
EndPos := 0;
EndPos := RPos(Chr($20), s);
StartPos := RPosEx(Chr($20), s, EndPos-1);
strBlockCount := Copy(s, StartPos, EndPos-StartPos);
result := strBlockCount;
end;
function GetBlockSizeLinux(DiskDevName : string) : Integer;
var
DiskProcess: TProcess;
BlockSize, StartOffset, i : Integer;
slDevDisk: TSTringList;
strBlockSize : string;
RelLine : boolean;
begin
RelLine := false;
BlockSize := 0;
DiskProcess:=TProcess.Create(nil);
DiskProcess.Options:=[poWaitOnExit, poUsePipes];
DiskProcess.CommandLine:='udisks --show-info ' + DiskDevName; //get all disks/partitions list
DiskProcess.Execute;
slDevDisk := TStringList.Create;
slDevDisk.LoadFromStream(DiskProcess.Output);
for i := 0 to slDevDisk.Count -1 do
begin
if pos('block size:', slDevDisk.Strings[i]) > 0 then
begin
strBlockSize := RightStr(slDevDisk.Strings[i], 4);
if Length(strBlockSize) > 0 then result := StrToInt(strBlockSize);
end;
end;
end;
// Extracts the byte value "Size: " from the output of udisks --show-info /dev/sdX
//
function GetByteCountLinux(DiskDevName : string) : QWord;
var
DiskProcess: TProcess;
StartOffset, i : Integer;
slDevDisk: TSTringList;
strByteCount : string;
ScanDiskData : boolean;
intByteCount : QWord;
begin
ScanDiskData := false;
result := 0;
intByteCount := 0;
strByteCount := '';
DiskProcess:=TProcess.Create(nil);
DiskProcess.Options:=[poWaitOnExit, poUsePipes];
DiskProcess.CommandLine:='udisks --show-info ' + DiskDevName; //get all disks/partitions list
DiskProcess.Execute;
slDevDisk := TStringList.Create;
slDevDisk.LoadFromStream(DiskProcess.Output);
for i := 0 to slDevDisk.Count -1 do
begin
// Search for 'Size:' in the output, but note there are two values.
// This function only wants the first value, so abort once it's found
if (pos('size:', slDevDisk.Strings[i]) > 0) and (ScanDiskData = false) then
begin
ScanDiskData := true;
strByteCount := ExtractNumbers(slDevDisk.Strings[i]);
if Length(strByteCount) > 0 then
begin
intByteCount := StrToQWord(strByteCount);
result := intByteCount;
end;
end;
end;
slDevDisk.free;
end;
procedure TfrmYaffi.btnStartImagingClick(Sender: TObject);
const
// These values are needed for For FSCTL_ALLOW_EXTENDED_DASD_IO to work properly
// on logical volumes. They are sourced from
// https://github.com/magicmonty/delphi-code-coverage/blob/master/3rdParty/JCL/jcl-2.3.1.4197/source/windows/JclWin32.pas
FILE_DEVICE_FILE_SYSTEM = $00000009;
FILE_ANY_ACCESS = 0;
METHOD_NEITHER = 3;
FSCTL_ALLOW_EXTENDED_DASD_IO = ((FILE_DEVICE_FILE_SYSTEM shl 16)
or (FILE_ANY_ACCESS shl 14)
or (32 shl 2) or METHOD_NEITHER);
var
SourceDevice, strImageName : widestring;
hSelectedDisk : THandle;
ExactDiskSize, SectorCount, ImageResult,
SegmentSize, ImageFileSize : Int64;
ImageTypeChoice, ExactSectorSize : integer;
slImagingLog : TStringList;
BytesReturned : DWORD;
VerificationHash : string;
ImageVerified : boolean;
StartedAt, EndedAt,
VerificationStartedAt, VerificationEndedAt,
TimeTakenToImage, TimeTakenToVerify : TDateTime;
fsImageName : TFileStream;
begin
BytesReturned := 0;
ExactDiskSize := 0;
ExactSectorSize := 0;
SectorCount := 0;
ImageResult := 0;
ImageFileSize := 0;
HashChoice := -1;
ImageTypeChoice := -1;
SegmentSize := 0;
SegmentSize := InitialiseSegmentSize(nil);
StartedAt := 0;
EndedAt := 0;
TimeTakenToImage:= 0;
VerificationStartedAt := 0;
VerificationEndedAt := 0;
TimeTakenToVerify := 0;
SourceDevice := ledtSelectedItem.Text;
strImageName := ledtImageName.Text;
ImageVerified := false;
ComboImageType.Enabled := false;
comboHashChoice.Enabled := false;
ComboSegmentSize.Enabled := false;
frmProgress.lblResult.Caption := 'Awaiting result...';
frmProgress.btnCloseProgressWindow.Enabled := false;
// Determine what hash algorithm to use. MD5 = 1, SHA-1 = 2, MD5 & SHA-1 = 3, Use Non = 4. -1 is false
HashChoice := frmYaffi.InitialiseHashChoice(nil);
if HashChoice = -1 then abort;
// Deterime whether to image as DD or E01
ImageTypeChoice := frmYaffi.InitialiseImageType(nil);
if ImageTypeChoice = -1 then abort;
// Avoid overwriting an existing image if the user just presses start again
// after already running an image
// TODO : For some weird reason, this returns true for DD images even though the file
// doesnt exist yet? But for the E01 images, it works as expected
{
if FileExists(strImageName) then
begin
ShowMessage(strImageName + ' already exists. Delete it first.');
ComboImageType.Enabled := true;
comboHashChoice.Enabled := true;
ComboSegmentSize.Enabled := true;
ComboCompression.Enabled := true;
Abort;
end;
}
// Create handle to source disk. Abort if fails
{$ifdef Windows}
hSelectedDisk := CreateFileW(PWideChar(SourceDevice),
FILE_READ_DATA,
FILE_SHARE_READ AND FILE_SHARE_WRITE,
nil,
OPEN_EXISTING,
FILE_FLAG_SEQUENTIAL_SCAN,
0);
// Check if handle is valid before doing anything else
if hSelectedDisk = INVALID_HANDLE_VALUE then
begin
RaiseLastOSError;
end
{$else ifdef UNIX}
hSelectedDisk := FileOpen(SourceDevice, fmOpenRead);
// Check if handle is valid before doing anything else
if hSelectedDisk = -1 then
begin
RaiseLastOSError;
end
{$endif}
else
begin
// If chosen device is logical volume, initiate FSCTL_ALLOW_EXTENDED_DASD_IO
// to ensure all sectors acquired, even those protected by the OS normally.
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa363147%28v=vs.85%29.aspx
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa364556%28v=vs.85%29.aspx
{$ifdef Windows}
if Pos('?', SourceDevice) > 0 then
begin
if not DeviceIOControl(hSelectedDisk, FSCTL_ALLOW_EXTENDED_DASD_IO, nil, 0,
nil, 0, BytesReturned, nil) then
raise Exception.Create('Unable to initiate FSCTL_ALLOW_EXTENDED_DASD_IO.');
end;
{$endif}
// Source disk handle is OK. So attempt imaging of it
// First, compute the exact disk size of the disk or volume
{$ifdef Windows}
ExactDiskSize := GetDiskLengthInBytes(hSelectedDisk);
{$endif}
{$ifdef UNIX}
ExactDiskSize := GetByteCountLinux(SourceDevice);
{$endif}
// Now query the sector size.
// 512 bytes is common with MBR but with GPT disks, 1024 or 4096 is likely
{$ifdef Windows}
ExactSectorSize := GetSectorSizeInBytes(hSelectedDisk);
{$endif}
{$ifdef Unix}
ExactSectorSize := GetBlockSizeLinux(SourceDevice);
{$endif}
// Now we can assign a sector count based on sector size and disk size
SectorCount := ExactDiskSize DIV ExactSectorSize;
frmProgress.lblTotalBytesSource.Caption := ' bytes captured of ' + IntToStr(ExactDiskSize);
// Now image the chosen device, passing the exact size and
// hash selection and Image name.
// If InitialiseImageType returns 1, we use E01 and libEWF C library. Otherwise, DD.
// E01 IMAGE
if InitialiseImageType(nil) = 1 then
begin
StartedAt := Now;
// libEWF takes care of assigning handles to image etc
ImageResult := ImageDiskE01(hSelectedDisk, SegmentSize, ExactDiskSize, HashChoice);
If ImageResult = ExactDiskSize then
begin
frmProgress.lblStatus.Caption := 'Imaged OK. ' + IntToStr(ExactDiskSize)+' bytes captured.';
EndedAt := Now;
TimeTakenToImage := EndedAt - StartedAt;
// Verify the E01 image, if desired by the user
if (cbVerify.Checked) and (ImageResult > -1) then
begin
VerificationStartedAt := Now;
VerificationHash := VerifyE01Image(strImageName);
if (Length(VerificationHash) = 32) or // MD5
(Length(VerificationHash) = 40) or // SHA-1
(Length(VerificationHash) = 73) then // Both hashes with a space char
begin
ImageVerified := true;
VerificationEndedAt := Now;
TimeTakenToVerify := VerificationEndedAt - VerificationStartedAt;
frmProgress.lblStatus.Caption := 'Image re-read OK. Verifies. See log file';
frmProgress.lblResult.Caption := 'Imaged and verified OK. Total time: ' + FormatDateTime('HHH:MM:SS', (TimeTakenToImage + TimeTakenToVerify));
end;
end;
end
else
begin
ShowMessage('Imaging failed\aborted. ' + IntToStr(ImageResult) + ' bytes captured of the reported ' + IntToStr(ExactDiskSize));
end;
end
// DD IMAGE
else if InitialiseImageType(nil) = 2 then
begin
StartedAt := Now;
// Create a filestream for the output image file
fsImageName := TFileStream.Create(Trim(ledtImageName.Text), fmCreate);
// Image hSelectedDisk to fsImageName, returning the number of bytes read
ImageResult := ImageDiskDD(hSelectedDisk, ExactDiskSize, HashChoice, fsImageName);
If ImageResult = ExactDiskSize then
begin
frmProgress.lblResult.Caption := 'Imaged OK. ' + IntToStr(ExactDiskSize)+' bytes captured.';
EndedAt := Now;
TimeTakenToImage := EndedAt - StartedAt;
Application.ProcessMessages;
// Verify the DD image, if desired by the user
if (cbVerify.Checked) and (ImageResult > -1) then
begin
ImageFileSize := fsImageName.Size;
VerificationStartedAt := Now;
VerificationHash := VerifyDDImage(fsImageName, fsImageName.Size);
if (Length(VerificationHash) = 32) or // MD5
(Length(VerificationHash) = 40) or // SHA-1
(Length(VerificationHash) = 73) then // Both hashes with a space char
begin
ImageVerified := true;
VerificationEndedAt := Now;