-
Notifications
You must be signed in to change notification settings - Fork 42
/
RawCopy.au3
3174 lines (3044 loc) · 140 KB
/
RawCopy.au3
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
#RequireAdmin
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=C:\Program Files (x86)\AutoIt3\Icons\au3.ico
#AutoIt3Wrapper_Outfile=RawCopy.exe
#AutoIt3Wrapper_Outfile_x64=RawCopy64.exe
#AutoIt3Wrapper_UseUpx=y
#AutoIt3Wrapper_Compile_Both=y
#AutoIt3Wrapper_UseX64=y
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_Res_Comment=Raw file copy
#AutoIt3Wrapper_Res_Description=Copy files from NTFS volumes by using low level disk access
#AutoIt3Wrapper_Res_Fileversion=1.0.0.22
#AutoIt3Wrapper_Res_LegalCopyright=Joakim Schicht
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#AutoIt3Wrapper_AU3Check_Parameters=-w 3 -w 5
#AutoIt3Wrapper_Run_Au3Stripper=y
#Au3Stripper_Parameters=/sf /sv /rm /pe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#Include <WinAPIEx.au3>
#include <Array.au3>
#Include <String.au3>
#Include <FileConstants.au3>
;
; https://github.com/jschicht
;
Global $LockedFileName,$DirArray,$NeedIndx=0, $ResidentIndx, $AttributesArr[18][4], $DoExtractMeta=False, $TargetFileName, $DATA_Name, $FN_FileName, $NameQ[5], $LogicalClusterNumberforthefileMFT, $SectorsPerCluster, $BytesPerSector, $SectorsPerMftRecord, $ClustersPerFileRecordSegment, $MftAttrListString, $SplitMftRecArr[1]
Global $TargetImageFile, $Entries, $InputFile, $IsShadowCopy=False, $IsPhysicalDrive=False, $IsImage=False, $hDisk, $sBuffer, $ComboPhysicalDrives, $Combo
Global $OutPutPath=@ScriptDir, $InitState = False, $DATA_Clusters, $AttributeOutFileName, $DATA_InitSize, $ImageOffset=0, $ADS_Name, $IndexNumber, $NonResidentFlag, $DATA_RealSize, $DataRun, $DATA_LengthOfAttribute
Global $TargetDrive = "", $ALInnerCouner, $MFTSize, $TargetOffset, $MFT_Record_Size, $BytesPerCluster, $MFT_Offset, $IsDirectory
Global $IsolatedAttributeList, $AttribListNonResident=0,$IsCompressed,$IsSparse, $_COMMON_KERNEL32DLL=DllOpen("kernel32.dll"), $Filetree[1]
Global $RUN_VCN[1],$RUN_Clusters[1],$MFT_RUN_Clusters[1],$MFT_RUN_VCN[1],$DataQ[1],$AttribX[1],$AttribXType[1],$AttribXCounter[1],$AttribXStreamName[1], $AttrQ[1]
Global $IndxEntryNumberArr[1],$IndxMFTReferenceArr[1],$IndxMFTRefSeqNoArr[1],$IndxIndexFlagsArr[1],$IndxMFTReferenceOfParentArr[1],$IndxMFTParentRefSeqNoArr[1],$IndxCTimeArr[1],$IndxATimeArr[1],$IndxMTimeArr[1],$IndxRTimeArr[1],$IndxAllocSizeArr[1],$IndxRealSizeArr[1],$IndxFileFlagsArr[1],$IndxFileNameArr[1],$IndxSubNodeVCNArr[1],$IndxNameSpaceArr[1]
Global $IRArr[12][2],$IndxArr[20][2]
Global $VolumesArray[1][3],$DetailMode=1, $WriteFSInfo=0, $OutputName, $TcpSend=0, $sIPAddress="", $iPort="", $iSocket
Global $DateTimeFormat = 6 ; YYYY-MM-DD HH:MM:SS:MSMSMS:NSNSNSNS = 2007-08-18 08:15:37:733:1234
Global $tDelta = _WinTime_GetUTCToLocalFileTimeDelta()
Global Const $RecordSignature = '46494C45' ; FILE signature
Global Const $RecordSignatureBad = '44414142' ; BAAD signature
Global Const $STANDARD_INFORMATION = '10000000'
Global Const $ATTRIBUTE_LIST = '20000000'
Global Const $FILE_NAME = '30000000'
Global Const $OBJECT_ID = '40000000'
Global Const $SECURITY_DESCRIPTOR = '50000000'
Global Const $VOLUME_NAME = '60000000'
Global Const $VOLUME_INFORMATION = '70000000'
Global Const $DATA = '80000000'
Global Const $INDEX_ROOT = '90000000'
Global Const $INDEX_ALLOCATION = 'A0000000'
Global Const $BITMAP = 'B0000000'
Global Const $REPARSE_POINT = 'C0000000'
Global Const $EA_INFORMATION = 'D0000000'
Global Const $EA = 'E0000000'
Global Const $PROPERTY_SET = 'F0000000'
Global Const $LOGGED_UTILITY_STREAM = '00010000'
Global Const $ATTRIBUTE_END_MARKER = 'FFFFFFFF'
Global $Timerstart = TimerInit()
$VolumesArray[0][0] = "Type"
$VolumesArray[0][1] = "ByteOffset"
$VolumesArray[0][2] = "Sectors"
ConsoleWrite("RawCopy v1.0.0.22" & @CRLF & @CRLF)
_GetInputParams()
;_ArrayDisplay($VolumesArray,"$VolumesArray")
$ParentDir = _GenDirArray($TargetFileName)
$FN_FileName = $LockedFileName
Global $MftRefArray[$DirArray[0]+1]
_ReadBootSector($TargetDrive)
$BytesPerCluster = $SectorsPerCluster*$BytesPerSector
$hDisk = _WinAPI_CreateFile($TargetDrive,2,2,7)
If $hDisk = 0 Then
ConsoleWrite("CreateFile: " & _WinAPI_GetLastErrorMessage() & @CRLF)
Exit
EndIf
$MFTEntry = _FindMFT(0)
If $MFTEntry = "" Then ;something wrong with record for $MFT
ConsoleWrite("Error: Getting MFT record 0" & @CRLF)
Exit
EndIf
$MFT = _DecodeMFTRecord0($MFTEntry, 0) ;produces DataQ for $MFT, record 0
If $MFT = "" Then
ConsoleWrite("Error: Parsing the MFT record 0" & @CRLF)
Exit
EndIf
_GetRunsFromAttributeListMFT0() ;produces datarun for $MFT and converts datarun to RUN_VCN[] and RUN_Clusters[]
$MFTSize = $DATA_RealSize
$MFT_RUN_VCN = $RUN_VCN
$MFT_RUN_Clusters = $RUN_Clusters
If $TcpSend Then
TCPStartup()
EndIf
If StringIsDigit($IndexNumber) Then
Global $DataQ[1],$AttribX[1],$AttribXType[1],$AttribXCounter[1],$AttribXStreamName[1]
_GenRefArray()
$RetRec = _FindFileMFTRecord($IndexNumber)
If Not IsArray($RetRec) Then Exit
$NewRecord = $RetRec[1]
_DecodeMFTRecord($NewRecord,1)
_DecodeNameQ($NameQ)
_MainExtract()
ConsoleWrite(@CRLF)
_End($Timerstart)
Exit
EndIf
_GenRefArray()
$NextRef = 5
$MftRefArray[1]=$NextRef
$ResolvedPath = $DirArray[1]
For $i = 2 To $DirArray[0]
Global $DataQ[1],$AttribX[1],$AttribXType[1],$AttribXCounter[1],$AttribXStreamName[1]
$RetRec = _FindFileMFTRecord($NextRef)
If Not IsArray($RetRec) Then Exit
$NewRecord = $RetRec[1]
_DecodeMFTRecord($NewRecord,1)
; ConsoleWrite("Parsing INDX records of Mft ref " & $NextRef & " to find " & $DirArray[$i] & @CRLF)
$NextRef = _ParseIndex($DirArray[$i])
If @error Then
ConsoleWrite("Error: Unable to find the file " & $DirArray[$i] & " by index scanning" & @CRLF)
Exit
EndIf
$MftRefArray[$i]=$NextRef
If @error Then
Global $DataQ[1],$AttribX[1],$AttribXType[1],$AttribXCounter[1],$AttribXStreamName[1]
$RetRec = _FindFileMFTRecord($MftRefArray[$i-1])
If Not IsArray($RetRec) Then Exit
$NewRecord = $RetRec[1]
_DecodeMFTRecord($NewRecord,1)
$LastCheck = _DisplayList($ResolvedPath)
ElseIf $i=$DirArray[0] Then
Global $DataQ[1],$AttribX[1],$AttribXType[1],$AttribXCounter[1],$AttribXStreamName[1]
$RetRec = _FindFileMFTRecord($MftRefArray[$i])
If Not IsArray($RetRec) Then Exit
$NewRecord = $RetRec[1]
_DecodeMFTRecord($NewRecord,1)
;
; Check if there are attributes of $INDEX_ROOT or $INDEX_ALLOCATION present
; if they are present only then assume that current level is not file
; and that is possible that it is a directory which can then be
; printed as with option /RawDirMode with two directory listing modes.
; Note that $NextRef is not set at all in this branch of code
; which is used for final file extraction.
;
if($AttributesArr[9][2]= "TRUE" or $AttributesArr[9][2]="TRUE") Then
$LastCheck = _DisplayList($ResolvedPath & "\" & $DirArray[$i])
$mymonkeyerror = @error
If $mymonkeyerror Then ; In case last part was a file and not a directory
Global $DataQ[1],$AttribX[1],$AttribXType[1],$AttribXCounter[1],$AttribXStreamName[1]
$RetRec = _FindFileMFTRecord($MftRefArray[$i-1])
If Not IsArray($RetRec) Then Exit
$NewRecord = $RetRec[1]
_DecodeMFTRecord($NewRecord,1)
$LastCheck = _DisplayList($ResolvedPath)
EndIf
EndIf
ElseIf StringIsDigit($NextRef) Then
$ResolvedPath &= "\" & $DirArray[$i]
ContinueLoop
Else
ConsoleWrite("Error: Something went wrong" & @CRLF)
ExitLoop
EndIf
Next
Global $DataQ[1],$AttribX[1],$AttribXType[1],$AttribXCounter[1],$AttribXStreamName[1]
_GenRefArray()
$RetRec = _FindFileMFTRecord($NextRef)
If Not IsArray($RetRec) Then Exit
$NewRecord = $RetRec[1]
_DecodeMFTRecord($NewRecord,1)
_MainExtract()
ConsoleWrite(@CRLF)
_End($Timerstart)
If $TcpSend Then
TCPShutDown()
EndIf
Exit
Func _MainExtract()
Local $LocalCoreFilename
If $OutputName <> "" Then
$FN_FileName = $OutputName
EndIf
If $TcpSend Then
$ConsoleText = "TcpSending"
$iSocket = TCPConnect($sIPAddress, $iPort)
If @error Then
ConsoleWrite("TCPConnect Error: " & @error & @CRLF)
Exit
EndIf
Else
$ConsoleText = "Writing"
EndIf
For $i = 1 To UBound($DataQ) - 1
_DecodeDataQEntry($DataQ[$i])
$AttributeOutFileName = $OutPutPath & "\" & $ADS_Name
ConsoleWrite($ConsoleText & ": " & $ADS_Name & @CRLF)
If $NonResidentFlag = '00' Then
_ExtractResidentFile($AttributeOutFileName, $DATA_LengthOfAttribute)
Else
Global $RUN_VCN[1], $RUN_Clusters[1]
$TotalClusters = $Data_Clusters
$RealSize = $DATA_RealSize ;preserve file sizes
If Not $InitState Then $DATA_InitSize = $DATA_RealSize
$InitSize = $DATA_InitSize
_ExtractDataRuns()
If $TotalClusters * $BytesPerCluster >= $RealSize Then
_ExtractFile($MFTEntry)
Else ;code to handle attribute list
$Flag = $IsCompressed ;preserve compression state
For $j = $i + 1 To UBound($DataQ) -1
_DecodeDataQEntry($DataQ[$j])
$TotalClusters += $Data_Clusters
_ExtractDataRuns()
If $TotalClusters * $BytesPerCluster >= $RealSize Then
$DATA_RealSize = $RealSize ;restore file sizes
$DATA_InitSize = $InitSize
$IsCompressed = $Flag ;recover compression state
_ExtractFile($MFTEntry)
ExitLoop
EndIf
Next
$i = $j
EndIf
EndIf
Next
If $TcpSend Then TCPCloseSocket($iSocket)
If Not $DoExtractMeta Then Return
$PreservedFileName = $FN_FileName
For $i = 1 To UBound($AttribX) - 1
$FN_FileName = ""
If $TcpSend Then
$iSocket = TCPConnect($sIPAddress, $iPort)
If @error Then
ConsoleWrite("TCPConnect Error: " & @error & @CRLF)
Exit
EndIf
EndIf
_DecodeDataQEntry($AttribX[$i])
If $AttribXCounter[$i] > 1 Then
If $AttribXStreamName[$i]="" Then
$LocalCoreFilename = $PreservedFileName & "_" & $IndexNumber & "_" & _TranslateAttributeType($AttribXType[$i]) & "_" & $AttribXCounter[$i] & ".bin"
Else
$LocalCoreFilename = $PreservedFileName & "_" & $IndexNumber & "_" & _TranslateAttributeType($AttribXType[$i]) & "_" & $AttribXStreamName[$i] & ".bin"
EndIf
Else
If $AttribXStreamName[$i]="" Then
$LocalCoreFilename = $PreservedFileName & "_" & $IndexNumber & "_" & _TranslateAttributeType($AttribXType[$i]) & ".bin"
Else
$LocalCoreFilename = $PreservedFileName & "_" & $IndexNumber & "_" & _TranslateAttributeType($AttribXType[$i]) & "_" & $AttribXStreamName[$i] & ".bin"
EndIf
EndIf
$AttributeOutFileName = $OutPutPath & "\" & $LocalCoreFilename
ConsoleWrite($ConsoleText & ": " & $LocalCoreFilename & @CRLF)
If $NonResidentFlag = '00' Then
_ExtractResidentFile($AttributeOutFileName, $DATA_LengthOfAttribute)
Else
Global $RUN_VCN[1], $RUN_Clusters[1]
$TotalClusters = $Data_Clusters
$RealSize = $DATA_RealSize ;preserve file sizes
If Not $InitState Then $DATA_InitSize = $DATA_RealSize
$InitSize = $DATA_InitSize
_ExtractDataRuns()
If $TotalClusters * $BytesPerCluster >= $RealSize Then
_ExtractFile($MFTEntry)
Else ;code to handle attribute list
$Flag = $IsCompressed ;preserve compression state
For $j = $i + 1 To UBound($AttribX) -1
_DecodeDataQEntry($AttribX[$j])
$TotalClusters += $Data_Clusters
_ExtractDataRuns()
If $TotalClusters * $BytesPerCluster >= $RealSize Then
$DATA_RealSize = $RealSize ;restore file sizes
$DATA_InitSize = $InitSize
$IsCompressed = $Flag ;recover compression state
_ExtractFile($MFTEntry)
ExitLoop
EndIf
Next
$i = $j
EndIf
EndIf
If $TcpSend Then TCPCloseSocket($iSocket)
Next
EndFunc
Func _GenDirArray($InPath)
Local $Reconstruct
$DirArray = StringSplit($InPath,"\")
$LockedFileName = $DirArray[$DirArray[0]]
For $i = 1 To $DirArray[0]-1
$Reconstruct &= $DirArray[$i]&"\"
Next
$Reconstruct = StringTrimRight($Reconstruct,1)
Return $Reconstruct
EndFunc
Func _ExtractSingleFile($MFTReferenceNumber)
Global $DataQ[1],$AttribX[1],$AttribXType[1],$AttribXCounter[1],$AttribXStreamName[1] ;clear array
$TestRecord = _FindFileMFTRecord($MFTReferenceNumber)
$MFTRecord = $TestRecord[1]
If $MFTRecord = "" Then
ConsoleWrite("Target " & $MFTReferenceNumber & " not found" & @CRLF)
;_DisplayInfo("Target " & $MFTReferenceNumber & " not found" & @CRLF)
Return SetError(1,0,0)
ElseIf StringMid($MFTRecord,3,8) <> $RecordSignature AND StringMid($MFTRecord,3,8) <> $RecordSignatureBad Then
ConsoleWrite("Found record is not valid:" & @CRLF)
;_DisplayInfo("Found record is not valid:" & @CRLF)
ConsoleWrite(_HexEncode($MFTRecord) & @crlf)
Return SetError(1,0,0)
EndIf
_DecodeMFTRecord($MFTRecord,1)
Return
EndFunc
Func _DecodeAttrList($TargetFile, $AttrList)
Local $offset, $nBytes, $hFile, $LocalName, $ALNameLength
If StringMid($AttrList, 17, 2) = "00" Then ;attribute list is in $AttrList
$offset = Dec(_SwapEndian(StringMid($AttrList, 41, 4)))
$List = StringMid($AttrList, $offset*2+1)
; $IsolatedAttributeList = $list
Else ;attribute list is found from data run in $AttrList
$size = Dec(_SwapEndian(StringMid($AttrList, $offset*2 + 97, 16)))
$offset = ($offset + Dec(_SwapEndian(StringMid($AttrList, $offset*2 + 65, 4))))*2
$DataRun = StringMid($AttrList, $offset+1, StringLen($AttrList)-$offset)
; ConsoleWrite("Attribute_List DataRun is " & $DataRun & @CRLF)
Global $RUN_VCN[1], $RUN_Clusters[1]
_ExtractDataRuns()
$tBuffer = DllStructCreate("byte[" & $BytesPerCluster & "]")
$hFile = _WinAPI_CreateFile($TargetDrive, 2, 6, 6)
If $hFile = 0 Then
ConsoleWrite("Error in function CreateFile when trying to locate Attribute List." & @CRLF)
;_DisplayInfo("Error in function CreateFile when trying to locate Attribute List." & @CRLF)
_WinAPI_CloseHandle($hFile)
Return SetError(1,0,0)
EndIf
$List = ""
For $r = 1 To Ubound($RUN_VCN)-1
_WinAPI_SetFilePointerEx($hFile, $ImageOffset+($RUN_VCN[$r]*$BytesPerCluster), $FILE_BEGIN)
For $i = 1 To $RUN_Clusters[$r]
_WinAPI_ReadFile($hFile, DllStructGetPtr($tBuffer), $BytesPerCluster, $nBytes)
$List &= StringTrimLeft(DllStructGetData($tBuffer, 1),2)
Next
Next
; _DebugOut("***AttrList New:",$List)
_WinAPI_CloseHandle($hFile)
$List = StringMid($List, 1, $size*2)
EndIf
$IsolatedAttributeList = $list
$offset=0
$str=""
While StringLen($list) > $offset*2
$type=StringMid($List, ($offset*2)+1, 8)
;$ALRecordLength = Dec(_SwapEndian(StringMid($List, $offset*2 + 9, 4)))
$ALNameLength = Dec(_SwapEndian(StringMid($List, $offset*2 + 13, 2)))
;$ALNameOffset = Dec(_SwapEndian(StringMid($List, $offset*2 + 15, 2)))
;$TestVCN = Dec(_SwapEndian(StringMid($List, $offset*2 + 17, 16)))
$ref=Dec(_SwapEndian(StringMid($List, $offset*2 + 33, 8)))
;$LocalAttribID = "0x" & StringMid($List, $offset*2 + 49, 2) & StringMid($List, $offset*2 + 51, 2)
If $ALNameLength > 0 Then
$LocalName = StringMid($List, $offset*2 + 53, $ALNameLength*2*2)
$LocalName = _UnicodeHexToStr($LocalName)
Else
$LocalName = ""
EndIf
If $ref <> $TargetFile Then ;new attribute
If Not StringInStr($str, $ref) Then $str &= $ref & "-"
EndIf
If $type=$DATA Then
;$DataInAttrlist=1
;$IsolatedData=StringMid($List, ($offset*2)+1, $ALRecordLength*2)
;If $TestVCN=0 Then $DataIsResident=1
EndIf
$offset += Dec(_SwapEndian(StringMid($List, $offset*2 + 9, 4)))
WEnd
If $str = "" Then
ConsoleWrite("No extra MFT records found" & @CRLF)
;_DisplayInfo("No extra MFT records found" & @CRLF)
Else
$AttrQ = StringSplit(StringTrimRight($str,1), "-")
; ConsoleWrite("Decode of $ATTRIBUTE_LIST reveiled extra MFT Records to be examined = " & _ArrayToString($AttrQ, @CRLF) & @CRLF)
EndIf
EndFunc
Func _StripMftRecord($MFTEntry)
Local $UpdSeqArrPart0, $UpdSeqArrPart1, $UpdSeqArrPart2, $RecordEnd1, $RecordEnd2
$UpdSeqArrOffset = Dec(_SwapEndian(StringMid($MFTEntry,11,4)))
$UpdSeqArrSize = Dec(_SwapEndian(StringMid($MFTEntry,15,4)))
$UpdSeqArr = StringMid($MFTEntry,3+($UpdSeqArrOffset*2),$UpdSeqArrSize*2*2)
If $MFT_Record_Size = 1024 Then
$UpdSeqArrPart0 = StringMid($UpdSeqArr,1,4)
$UpdSeqArrPart1 = StringMid($UpdSeqArr,5,4)
$UpdSeqArrPart2 = StringMid($UpdSeqArr,9,4)
$RecordEnd1 = StringMid($MFTEntry,1023,4)
$RecordEnd2 = StringMid($MFTEntry,2047,4)
If $UpdSeqArrPart0 <> $RecordEnd1 OR $UpdSeqArrPart0 <> $RecordEnd2 Then
_DebugOut("The record failed Fixup", $MFTEntry)
Return ""
EndIf
$MFTEntry = StringMid($MFTEntry,1,1022) & $UpdSeqArrPart1 & StringMid($MFTEntry,1027,1020) & $UpdSeqArrPart2
ElseIf $MFT_Record_Size = 4096 Then
$UpdSeqArrPart0 = StringMid($UpdSeqArr,1,4)
$UpdSeqArrPart1 = StringMid($UpdSeqArr,5,4)
$UpdSeqArrPart2 = StringMid($UpdSeqArr,9,4)
Local $UpdSeqArrPart3 = StringMid($UpdSeqArr,13,4)
Local $UpdSeqArrPart4 = StringMid($UpdSeqArr,17,4)
Local $UpdSeqArrPart5 = StringMid($UpdSeqArr,21,4)
Local $UpdSeqArrPart6 = StringMid($UpdSeqArr,25,4)
Local $UpdSeqArrPart7 = StringMid($UpdSeqArr,29,4)
;Local $UpdSeqArrPart8 = StringMid($UpdSeqArr,33,4)
$RecordEnd1 = StringMid($MFTEntry,1023,4)
$RecordEnd2 = StringMid($MFTEntry,2047,4)
Local $RecordEnd3 = StringMid($MFTEntry,3071,4)
Local $RecordEnd4 = StringMid($MFTEntry,4095,4)
Local $RecordEnd5 = StringMid($MFTEntry,5119,4)
Local $RecordEnd6 = StringMid($MFTEntry,6143,4)
Local $RecordEnd7 = StringMid($MFTEntry,7167,4)
Local $RecordEnd8 = StringMid($MFTEntry,8191,4)
If $UpdSeqArrPart0 <> $RecordEnd1 OR $UpdSeqArrPart0 <> $RecordEnd2 OR $UpdSeqArrPart0 <> $RecordEnd3 OR $UpdSeqArrPart0 <> $RecordEnd4 OR $UpdSeqArrPart0 <> $RecordEnd5 OR $UpdSeqArrPart0 <> $RecordEnd6 OR $UpdSeqArrPart0 <> $RecordEnd7 OR $UpdSeqArrPart0 <> $RecordEnd8 Then
_DebugOut("The record failed Fixup", $MFTEntry)
Return ""
Else
$MFTEntry = StringMid($MFTEntry,1,1022) & $UpdSeqArrPart1 & StringMid($MFTEntry,1027,1020) & $UpdSeqArrPart2 & StringMid($MFTEntry,2051,1020) & $UpdSeqArrPart3 & StringMid($MFTEntry,3075,1020) & $UpdSeqArrPart4 & StringMid($MFTEntry,4099,1020) & $UpdSeqArrPart5 & StringMid($MFTEntry,5123,1020) & $UpdSeqArrPart6 & StringMid($MFTEntry,6147,1020) & $UpdSeqArrPart7 & StringMid($MFTEntry,7171,1020)
EndIf
EndIf
$RecordSize = Dec(_SwapEndian(StringMid($MFTEntry,51,8)),2)
$HeaderSize = Dec(_SwapEndian(StringMid($MFTEntry,43,4)),2)
$MFTEntry = StringMid($MFTEntry,$HeaderSize*2+3,($RecordSize-$HeaderSize-8)*2) ;strip "0x..." and "FFFFFFFF..."
Return $MFTEntry
EndFunc
Func _DecodeDataQEntry($attr) ;processes data attribute
$NonResidentFlag = StringMid($attr,17,2)
$NameLength = Dec(StringMid($attr,19,2))
$NameOffset = Dec(_SwapEndian(StringMid($attr,21,4)))
If $NameLength > 0 Then ;must be ADS
$ADS_Name = _UnicodeHexToStr(StringMid($attr,$NameOffset*2 + 1,$NameLength*4))
$ADS_Name = $FN_FileName & "[ADS_" & $ADS_Name & "]"
Else
$ADS_Name = $FN_FileName ;need to preserve $FN_FileName
EndIf
$Flags = StringMid($attr,25,4)
If BitAND($Flags,"0100") Then $IsCompressed = 1
If BitAND($Flags,"0080") Then $IsSparse = 1
If $NonResidentFlag = '01' Then
$DATA_Clusters = Dec(_SwapEndian(StringMid($attr,49,16)),2) - Dec(_SwapEndian(StringMid($attr,33,16)),2) + 1
$DATA_RealSize = Dec(_SwapEndian(StringMid($attr,97,16)),2)
$DATA_InitSize = Dec(_SwapEndian(StringMid($attr,113,16)),2)
$Offset = Dec(_SwapEndian(StringMid($attr,65,4)))
$DataRun = StringMid($attr,$Offset*2+1,(StringLen($attr)-$Offset)*2)
ElseIf $NonResidentFlag = '00' Then
$DATA_LengthOfAttribute = Dec(_SwapEndian(StringMid($attr,33,8)),2)
$Offset = Dec(_SwapEndian(StringMid($attr,41,4)))
$DataRun = StringMid($attr,$Offset*2+1,$DATA_LengthOfAttribute*2)
EndIf
EndFunc
Func _DecodeMFTRecord0($record, $FileRef) ;produces DataQ
$MftAttrListString=","
$record = _DoFixup($record, $FileRef)
If $record = "" then Return "" ;corrupt, failed fixup
$RecordSize = Dec(_SwapEndian(StringMid($record,51,8)),2)
$AttributeOffset = (Dec(StringMid($record,43,2))*2)+3
While 1 ;only want Attribute List and Data Attributes
$Type = Dec(_SwapEndian(StringMid($record,$AttributeOffset,8)),2)
If $Type > 256 Then ExitLoop ;attributes may not be in numerical order
$AttributeSize = Dec(_SwapEndian(StringMid($record,$AttributeOffset+8,8)),2)
If $Type = 32 Then
$AttrList = StringMid($record,$AttributeOffset,$AttributeSize*2) ;whole attribute
$AttrList = _DecodeAttrList2($FileRef, $AttrList) ;produces $AttrQ - extra record list
If $AttrList = "" Then
_DebugOut($FileRef & " Bad Attribute List signature", $record)
Return ""
Else
If $AttrQ[0] = "" Then ContinueLoop ;no new records
$str = ""
For $i = 1 To $AttrQ[0]
$MftAttrListString &= $AttrQ[$i] & ","
; ConsoleWrite("$AttrQ[$i]: " & $AttrQ[$i] & @CRLF)
If Not IsNumber(Int($AttrQ[$i])) Then
_DebugOut($FileRef & " Overwritten extra record (" & $AttrQ[$i] & ")", $record)
Return ""
EndIf
; ConsoleWrite("$AttrQ[$i]: " & $AttrQ[$i] & @CRLF)
$rec = _GetAttrListMFTRecord(($AttrQ[$i]*$MFT_Record_Size)+($LogicalClusterNumberforthefileMFT*$BytesPerCluster))
If StringMid($rec,3,8) <> $RecordSignature Then
_DebugOut($FileRef & " Bad signature for extra record", $record)
_DebugOut($FileRef & " Bad signature for extra record", $rec)
Return ""
EndIf
If Dec(_SwapEndian(StringMid($rec,67,8)),2) <> $FileRef Then
_DebugOut($FileRef & " Bad extra record", $record)
Return ""
EndIf
; $rec = _StripMftRecord($rec, $FileRef)
$rec = _StripMftRecord($rec)
If $rec = "" Then
_DebugOut($FileRef & " Extra record failed Fixup", $record)
Return ""
EndIf
$str &= $rec ;no header or end marker
Next
$record = StringMid($record,1,($RecordSize-8)*2+2) & $str & "FFFFFFFF" ;strip end first then add
EndIf
ElseIf $Type = 128 Then
ReDim $DataQ[UBound($DataQ) + 1]
$DataQ[UBound($DataQ) - 1] = StringMid($record,$AttributeOffset,$AttributeSize*2) ;whole data attribute
EndIf
$AttributeOffset += $AttributeSize*2
WEnd
Return $record
EndFunc
Func _DecodeMFTRecord($MFTEntry,$MFTMode)
Global $IndxEntryNumberArr[1],$IndxMFTReferenceArr[1],$IndxIndexFlagsArr[1],$IndxMFTReferenceOfParentArr[1],$IndxCTimeArr[1],$IndxATimeArr[1],$IndxMTimeArr[1],$IndxRTimeArr[1],$IndxAllocSizeArr[1],$IndxRealSizeArr[1],$IndxFileFlagsArr[1],$IndxFileNameArr[1],$IndxSubNodeVCNArr[1],$IndxNameSpaceArr[1]
Local $FN_Number,$SI_Number,$ATTRIBLIST_Number,$OBJID_Number,$SECURITY_Number,$VOLNAME_Number,$VOLINFO_Number,$INDEXROOT_Number,$INDEXALLOC_Number,$BITMAP_Number,$REPARSEPOINT_Number,$EAINFO_Number,$EA_Number,$PROPERTYSET_Number,$LOGGEDUTILSTREAM_Number
Local $UpdSeqArrPart0, $UpdSeqArrPart1, $UpdSeqArrPart2, $RecordEnd1, $RecordEnd2
Local $INDEX_ROOT_ON="FALSE",$INDEX_ALLOCATION_ON="FALSE",$CoreAttribute,$CoreAttributeChunk,$CoreAttributeName
_SetArrays()
$HEADER_RecordRealSize = ""
$HEADER_MFTREcordNumber = ""
$UpdSeqArrOffset = Dec(_SwapEndian(StringMid($MFTEntry,11,4)))
$UpdSeqArrSize = Dec(_SwapEndian(StringMid($MFTEntry,15,4)))
$UpdSeqArr = StringMid($MFTEntry,3+($UpdSeqArrOffset*2),$UpdSeqArrSize*2*2)
If $MFT_Record_Size = 1024 Then
$UpdSeqArrPart0 = StringMid($UpdSeqArr,1,4)
$UpdSeqArrPart1 = StringMid($UpdSeqArr,5,4)
$UpdSeqArrPart2 = StringMid($UpdSeqArr,9,4)
$RecordEnd1 = StringMid($MFTEntry,1023,4)
$RecordEnd2 = StringMid($MFTEntry,2047,4)
If $UpdSeqArrPart0 <> $RecordEnd1 OR $UpdSeqArrPart0 <> $RecordEnd2 Then
ConsoleWrite("Error: the $MFT record is corrupt" & @CRLF)
ConsoleWrite(_HexEncode($MFTEntry) & @CRLF)
Return SetError(1,0,0)
EndIf
$MFTEntry = StringMid($MFTEntry,1,1022) & $UpdSeqArrPart1 & StringMid($MFTEntry,1027,1020) & $UpdSeqArrPart2
ElseIf $MFT_Record_Size = 4096 Then
$UpdSeqArrPart0 = StringMid($UpdSeqArr,1,4)
$UpdSeqArrPart1 = StringMid($UpdSeqArr,5,4)
$UpdSeqArrPart2 = StringMid($UpdSeqArr,9,4)
Local $UpdSeqArrPart3 = StringMid($UpdSeqArr,13,4)
Local $UpdSeqArrPart4 = StringMid($UpdSeqArr,17,4)
Local $UpdSeqArrPart5 = StringMid($UpdSeqArr,21,4)
Local $UpdSeqArrPart6 = StringMid($UpdSeqArr,25,4)
Local $UpdSeqArrPart7 = StringMid($UpdSeqArr,29,4)
;Local $UpdSeqArrPart8 = StringMid($UpdSeqArr,33,4)
$RecordEnd1 = StringMid($MFTEntry,1023,4)
$RecordEnd2 = StringMid($MFTEntry,2047,4)
Local $RecordEnd3 = StringMid($MFTEntry,3071,4)
Local $RecordEnd4 = StringMid($MFTEntry,4095,4)
Local $RecordEnd5 = StringMid($MFTEntry,5119,4)
Local $RecordEnd6 = StringMid($MFTEntry,6143,4)
Local $RecordEnd7 = StringMid($MFTEntry,7167,4)
Local $RecordEnd8 = StringMid($MFTEntry,8191,4)
If $UpdSeqArrPart0 <> $RecordEnd1 OR $UpdSeqArrPart0 <> $RecordEnd2 OR $UpdSeqArrPart0 <> $RecordEnd3 OR $UpdSeqArrPart0 <> $RecordEnd4 OR $UpdSeqArrPart0 <> $RecordEnd5 OR $UpdSeqArrPart0 <> $RecordEnd6 OR $UpdSeqArrPart0 <> $RecordEnd7 OR $UpdSeqArrPart0 <> $RecordEnd8 Then
ConsoleWrite("Error: the $MFT record is corrupt" & @CRLF)
Return SetError(1,0,0)
Else
$MFTEntry = StringMid($MFTEntry,1,1022) & $UpdSeqArrPart1 & StringMid($MFTEntry,1027,1020) & $UpdSeqArrPart2 & StringMid($MFTEntry,2051,1020) & $UpdSeqArrPart3 & StringMid($MFTEntry,3075,1020) & $UpdSeqArrPart4 & StringMid($MFTEntry,4099,1020) & $UpdSeqArrPart5 & StringMid($MFTEntry,5123,1020) & $UpdSeqArrPart6 & StringMid($MFTEntry,6147,1020) & $UpdSeqArrPart7 & StringMid($MFTEntry,7171,1020)
EndIf
EndIf
$HEADER_RecordRealSize = Dec(_SwapEndian(StringMid($MFTEntry,51,8)),2)
If $UpdSeqArrOffset = 48 Then
$HEADER_MFTREcordNumber = Dec(_SwapEndian(StringMid($MFTEntry,91,8)),2)
Else
$HEADER_MFTREcordNumber = "NT style"
EndIf
$AttributeOffset = (Dec(StringMid($MFTEntry,43,2))*2)+3
While 1
$AttributeType = StringMid($MFTEntry,$AttributeOffset,8)
$AttributeSize = StringMid($MFTEntry,$AttributeOffset+8,8)
$AttributeSize = Dec(_SwapEndian($AttributeSize),2)
Select
Case $AttributeType = $STANDARD_INFORMATION
; $STANDARD_INFORMATION_ON = "TRUE"
$SI_Number += 1
If $MFTMode = 1 Then
_ArrayAdd($AttribX, StringMid($MFTEntry,$AttributeOffset,$AttributeSize*2))
_ArrayAdd($AttribXType, $AttributeType)
_ArrayAdd($AttribXCounter, $SI_Number)
_ArrayAdd($AttribXStreamName, "")
EndIf
Case $AttributeType = $ATTRIBUTE_LIST
; $ATTRIBUTE_LIST_ON = "TRUE"
$ATTRIBLIST_Number += 1
If $MFTMode = 1 Then
_ArrayAdd($AttribX, StringMid($MFTEntry,$AttributeOffset,$AttributeSize*2))
_ArrayAdd($AttribXType, $AttributeType)
_ArrayAdd($AttribXCounter, $ATTRIBLIST_Number)
_ArrayAdd($AttribXStreamName, "")
EndIf
;$MFTEntryOrig = $MFTEntry
$AttrList = StringMid($MFTEntry,$AttributeOffset,$AttributeSize*2)
_DecodeAttrList($HEADER_MFTRecordNumber, $AttrList) ;produces $AttrQ - extra record list
$str = ""
For $i = 1 To $AttrQ[0]
;$record = _FindFileMFTRecord($AttrQ[$i])
$RetRec = _FindFileMFTRecord($AttrQ[$i])
If Not IsArray($RetRec) Then Exit
$record = $RetRec[1]
$str &= _StripMftRecord($record) ;no header or end marker
Next
$str &= "FFFFFFFF" ;add end marker
$MFTEntry = StringMid($MFTEntry,1,($HEADER_RecordRealSize-8)*2+2) & $str ;strip "FFFFFFFF..." first
Case $AttributeType = $FILE_NAME
; $FILE_NAME_ON = "TRUE"
$FN_Number += 1
If $MFTMode = 1 Then
_ArrayAdd($AttribX, StringMid($MFTEntry,$AttributeOffset,$AttributeSize*2))
_ArrayAdd($AttribXType, $AttributeType)
_ArrayAdd($AttribXCounter, $FN_Number)
_ArrayAdd($AttribXStreamName, "")
EndIf
$attr = StringMid($MFTEntry,$AttributeOffset,$AttributeSize*2)
$NameSpace = StringMid($attr,179,2)
Select
Case $NameSpace = "00" ;POSIX
$NameQ[2] = $attr
Case $NameSpace = "01" ;WIN32
$NameQ[4] = $attr
Case $NameSpace = "02" ;DOS
$NameQ[1] = $attr
Case $NameSpace = "03" ;DOS+WIN32
$NameQ[3] = $attr
EndSelect
Case $AttributeType = $OBJECT_ID
; $OBJECT_ID_ON = "TRUE"
$OBJID_Number += 1
If $MFTMode = 1 Then
_ArrayAdd($AttribX, StringMid($MFTEntry,$AttributeOffset,$AttributeSize*2))
_ArrayAdd($AttribXType, $AttributeType)
_ArrayAdd($AttribXCounter, $OBJID_Number)
_ArrayAdd($AttribXStreamName, "")
EndIf
Case $AttributeType = $SECURITY_DESCRIPTOR
; $SECURITY_DESCRIPTOR_ON = "TRUE"
$SECURITY_Number += 1
$CoreAttribute = _GetAttributeEntry(StringMid($MFTEntry,$AttributeOffset,$AttributeSize*2))
$CoreAttributeChunk = $CoreAttribute[0]
$CoreAttributeName = $CoreAttribute[1]
If $MFTMode = 1 Then
_ArrayAdd($AttribX, StringMid($MFTEntry,$AttributeOffset,$AttributeSize*2))
_ArrayAdd($AttribXType, $AttributeType)
_ArrayAdd($AttribXCounter, $SECURITY_Number)
_ArrayAdd($AttribXStreamName, $CoreAttributeName)
EndIf
Case $AttributeType = $VOLUME_NAME
; $VOLUME_NAME_ON = "TRUE"
$VOLNAME_Number += 1
If $MFTMode = 1 Then
_ArrayAdd($AttribX, StringMid($MFTEntry,$AttributeOffset,$AttributeSize*2))
_ArrayAdd($AttribXType, $AttributeType)
_ArrayAdd($AttribXCounter, $VOLNAME_Number)
_ArrayAdd($AttribXStreamName, "")
EndIf
Case $AttributeType = $VOLUME_INFORMATION
; $VOLUME_INFORMATION_ON = "TRUE"
$VOLINFO_Number += 1
If $MFTMode = 1 Then
_ArrayAdd($AttribX, StringMid($MFTEntry,$AttributeOffset,$AttributeSize*2))
_ArrayAdd($AttribXType, $AttributeType)
_ArrayAdd($AttribXCounter, $VOLINFO_Number)
_ArrayAdd($AttribXStreamName, "")
EndIf
Case $AttributeType = $DATA
; $DATA_ON = "TRUE"
;$DATA_Number += 1
_ArrayAdd($DataQ, StringMid($MFTEntry,$AttributeOffset,$AttributeSize*2))
Case $AttributeType = $INDEX_ROOT
; $INDEX_ROOT_ON = "TRUE"
$INDEXROOT_Number += 1
$CoreAttribute = _GetAttributeEntry(StringMid($MFTEntry,$AttributeOffset,$AttributeSize*2))
$CoreAttributeChunk = $CoreAttribute[0]
$CoreAttributeName = $CoreAttribute[1]
If $MFTMode = 1 Then
_ArrayAdd($AttribX, StringMid($MFTEntry,$AttributeOffset,$AttributeSize*2))
_ArrayAdd($AttribXType, $AttributeType)
_ArrayAdd($AttribXCounter, $INDEXROOT_Number)
_ArrayAdd($AttribXStreamName, $CoreAttributeName)
EndIf
ReDim $IRArr[12][$INDEXROOT_Number+1]
; $CoreIndexRoot = _GetAttributeEntry(StringMid($MFTEntry,$AttributeOffset,$AttributeSize*2))
; $CoreIndexRootChunk = $CoreIndexRoot[0]
; $CoreIndexRootName = $CoreIndexRoot[1]
If $CoreAttributeName = "$I30" Then
$INDEX_ROOT_ON = "TRUE"
_Get_IndexRoot($CoreAttributeChunk,$INDEXROOT_Number,$CoreAttributeName)
EndIf
Case $AttributeType = $INDEX_ALLOCATION
; $INDEX_ALLOCATION_ON = "TRUE"
$INDEXALLOC_Number += 1
$CoreAttribute = _GetAttributeEntry(StringMid($MFTEntry,$AttributeOffset,$AttributeSize*2))
$CoreAttributeChunk = $CoreAttribute[0]
$CoreAttributeName = $CoreAttribute[1]
If $MFTMode = 1 Then
_ArrayAdd($AttribX, StringMid($MFTEntry,$AttributeOffset,$AttributeSize*2))
_ArrayAdd($AttribXType, $AttributeType)
_ArrayAdd($AttribXCounter, $INDEXALLOC_Number)
_ArrayAdd($AttribXStreamName, $CoreAttributeName)
EndIf
; $CoreIndexAllocation = _GetAttributeEntry(StringMid($MFTEntry,$AttributeOffset,$AttributeSize*2))
; $CoreIndexAllocationChunk = $CoreIndexAllocation[0]
; $CoreIndexAllocationName = $CoreIndexAllocation[1]
; _Arrayadd($HexDumpIndxRecord,$CoreIndexAllocationChunk)
If $CoreAttributeName = "$I30" Then
$INDEX_ALLOCATION_ON = "TRUE"
_Get_IndexAllocation($CoreAttributeChunk)
EndIf
Case $AttributeType = $BITMAP
; $BITMAP_ON = "TRUE"
$BITMAP_Number += 1
$CoreAttribute = _GetAttributeEntry(StringMid($MFTEntry,$AttributeOffset,$AttributeSize*2))
$CoreAttributeChunk = $CoreAttribute[0]
$CoreAttributeName = $CoreAttribute[1]
If $MFTMode = 1 Then
_ArrayAdd($AttribX, StringMid($MFTEntry,$AttributeOffset,$AttributeSize*2))
_ArrayAdd($AttribXType, $AttributeType)
_ArrayAdd($AttribXCounter, $BITMAP_Number)
_ArrayAdd($AttribXStreamName, $CoreAttributeName)
EndIf
Case $AttributeType = $REPARSE_POINT
; $REPARSE_POINT_ON = "TRUE"
$REPARSEPOINT_Number += 1
$CoreAttribute = _GetAttributeEntry(StringMid($MFTEntry,$AttributeOffset,$AttributeSize*2))
$CoreAttributeChunk = $CoreAttribute[0]
$CoreAttributeName = $CoreAttribute[1]
If $MFTMode = 1 Then
_ArrayAdd($AttribX, StringMid($MFTEntry,$AttributeOffset,$AttributeSize*2))
_ArrayAdd($AttribXType, $AttributeType)
_ArrayAdd($AttribXCounter, $REPARSEPOINT_Number)
_ArrayAdd($AttribXStreamName, $CoreAttributeName)
EndIf
Case $AttributeType = $EA_INFORMATION
; $EA_INFORMATION_ON = "TRUE"
$EAINFO_Number += 1
If $MFTMode = 1 Then
_ArrayAdd($AttribX, StringMid($MFTEntry,$AttributeOffset,$AttributeSize*2))
_ArrayAdd($AttribXType, $AttributeType)
_ArrayAdd($AttribXCounter, $EAINFO_Number)
_ArrayAdd($AttribXStreamName, "")
EndIf
Case $AttributeType = $EA
; $EA_ON = "TRUE"
$EA_Number += 1
$CoreAttribute = _GetAttributeEntry(StringMid($MFTEntry,$AttributeOffset,$AttributeSize*2))
$CoreAttributeChunk = $CoreAttribute[0]
$CoreAttributeName = $CoreAttribute[1]
If $MFTMode = 1 Then
_ArrayAdd($AttribX, StringMid($MFTEntry,$AttributeOffset,$AttributeSize*2))
_ArrayAdd($AttribXType, $AttributeType)
_ArrayAdd($AttribXCounter, $EA_Number)
_ArrayAdd($AttribXStreamName, $CoreAttributeName)
EndIf
Case $AttributeType = $PROPERTY_SET
; $PROPERTY_SET_ON = "TRUE"
$PROPERTYSET_Number += 1
If $MFTMode = 1 Then
_ArrayAdd($AttribX, StringMid($MFTEntry,$AttributeOffset,$AttributeSize*2))
_ArrayAdd($AttribXType, $AttributeType)
_ArrayAdd($AttribXCounter, $PROPERTYSET_Number)
_ArrayAdd($AttribXStreamName, "")
EndIf
Case $AttributeType = $LOGGED_UTILITY_STREAM
; $LOGGED_UTILITY_STREAM_ON = "TRUE"
$LOGGEDUTILSTREAM_Number += 1
$CoreAttribute = _GetAttributeEntry(StringMid($MFTEntry,$AttributeOffset,$AttributeSize*2))
$CoreAttributeChunk = $CoreAttribute[0]
$CoreAttributeName = $CoreAttribute[1]
If $MFTMode = 1 Then
_ArrayAdd($AttribX, StringMid($MFTEntry,$AttributeOffset,$AttributeSize*2))
_ArrayAdd($AttribXType, $AttributeType)
_ArrayAdd($AttribXCounter, $LOGGEDUTILSTREAM_Number)
_ArrayAdd($AttribXStreamName, $CoreAttributeName)
EndIf
Case $AttributeType = $ATTRIBUTE_END_MARKER
ExitLoop
EndSelect
$AttributeOffset += $AttributeSize*2
WEnd
$AttributesArr[9][2] = $INDEX_ROOT_ON
$AttributesArr[10][2] = $INDEX_ALLOCATION_ON
EndFunc
Func _ExtractDataRuns()
$r=UBound($RUN_Clusters)
ReDim $RUN_Clusters[$r + $MFT_Record_Size], $RUN_VCN[$r + $MFT_Record_Size]
$i=1
$RUN_VCN[0] = 0
$BaseVCN = $RUN_VCN[0]
If $DataRun = "" Then $DataRun = "00"
Do
$RunListID = StringMid($DataRun,$i,2)
If $RunListID = "00" Then ExitLoop
$i += 2
$RunListClustersLength = Dec(StringMid($RunListID,2,1))
$RunListVCNLength = Dec(StringMid($RunListID,1,1))
$RunListClusters = Dec(_SwapEndian(StringMid($DataRun,$i,$RunListClustersLength*2)),2)
$i += $RunListClustersLength*2
$RunListVCN = _SwapEndian(StringMid($DataRun, $i, $RunListVCNLength*2))
;next line handles positive or negative move
$BaseVCN += Dec($RunListVCN,2)-(($r>1) And (Dec(StringMid($RunListVCN,1,1))>7))*Dec(StringMid("10000000000000000",1,$RunListVCNLength*2+1),2)
If $RunListVCN <> "" Then
$RunListVCN = $BaseVCN
Else
$RunListVCN = 0
EndIf
If (($RunListVCN=0) And ($RunListClusters>16) And (Mod($RunListClusters,16)>0)) Then
;may be sparse section at end of Compression Signature
$RUN_Clusters[$r] = Mod($RunListClusters,16)
$RUN_VCN[$r] = $RunListVCN
$RunListClusters -= Mod($RunListClusters,16)
$r += 1
ElseIf (($RunListClusters>16) And (Mod($RunListClusters,16)>0)) Then
;may be compressed data section at start of Compression Signature
$RUN_Clusters[$r] = $RunListClusters-Mod($RunListClusters,16)
$RUN_VCN[$r] = $RunListVCN
$RunListVCN += $RUN_Clusters[$r]
$RunListClusters = Mod($RunListClusters,16)
$r += 1
EndIf
;just normal or sparse data
$RUN_Clusters[$r] = $RunListClusters
$RUN_VCN[$r] = $RunListVCN
$r += 1
$i += $RunListVCNLength*2
Until $i > StringLen($DataRun)
ReDim $RUN_Clusters[$r], $RUN_VCN[$r]
EndFunc
Func _IsMftRecordSplit($MftRef)
For $i = 1 To UBound($SplitMftRecArr)-1
$SplitRecordPart2 = $SplitMftRecArr[$i]
$SplitRecordTestRef = StringMid($SplitRecordPart2, 1, StringInStr($SplitRecordPart2, "?")-1)
If $SplitRecordTestRef = $MftRef Then Return $i
Next
Return 0
EndFunc
Func _FindFileMFTRecord($MftRef)
Local $nBytes, $TmpOffset, $Counter, $Counter2, $RecordJumper, $TargetFileDec, $RecordsTooMuch, $RetVal[2]
$TargetFile = _DecToLittleEndian($MftRef)
$TargetFileDec = Dec(_SwapEndian($TargetFile),2)
$tBuffer = DllStructCreate("byte[" & $MFT_Record_Size & "]")
$hFile = _WinAPI_CreateFile($TargetDrive, 2, 6, 6)
If $hFile = 0 Then
ConsoleWrite("Error in function CreateFile: " & _WinAPI_GetLastErrorMessage() & @CRLF)
_WinAPI_CloseHandle($hFile)
Return SetError(1,0,0)
EndIf
$RecordSplitIndex = _IsMftRecordSplit($MftRef)
If $RecordSplitIndex Then
ConsoleWrite("Ref " & $MftRef & " has its record split across 2 dataruns" & @CRLF)
$SplitRecordPart2 = $SplitMftRecArr[$RecordSplitIndex]
$SplitRecordTestRef = StringMid($SplitRecordPart2, 1, StringInStr($SplitRecordPart2, "?")-1)
If $SplitRecordTestRef <> $MftRef Then ;then something is not quite right
ConsoleWrite("Error: The ref in the array did not match target ref." & @CRLF)
Return
EndIf
$SplitRecordPart3 = StringMid($SplitRecordPart2, StringInStr($SplitRecordPart2, "?")+1)
$SplitRecordArr = StringSplit($SplitRecordPart3,"|")
If UBound($SplitRecordArr) <> 3 Then
ConsoleWrite("Error: Array contained more elements than expected: " & UBound($SplitRecordArr) & @CRLF)
Return
EndIf
$record="0x"
For $k = 1 To Ubound($SplitRecordArr)-1
$SplitRecordOffset = StringMid($SplitRecordArr[$k], 1, StringInStr($SplitRecordArr[$k], ",")-1)
$SplitRecordSize = StringMid($SplitRecordArr[$k], StringInStr($SplitRecordArr[$k], ",")+1)
_WinAPI_SetFilePointerEx($hFile, $ImageOffset+$SplitRecordOffset, $FILE_BEGIN)
$kBuffer = DllStructCreate("byte["&$SplitRecordSize&"]")
_WinAPI_ReadFile($hFile, DllStructGetPtr($kBuffer), $SplitRecordSize, $nBytes)
$record &= StringMid(DllStructGetData($kBuffer,1),3)
ConsoleWrite(" part " & $k & " of record has " & $SplitRecordSize & " bytes at raw offset 0x" & Hex(Int($ImageOffset+$SplitRecordOffset)) & @CRLF)
$kBuffer=0
Next
; ConsoleWrite(_HexEncode($record) & @CRLF)
Else
Local $RecordsDivisor = $MFT_Record_Size/512
For $i = 1 To UBound($MFT_RUN_Clusters)-1
$CurrentClusters = $MFT_RUN_Clusters[$i]
$RecordsInCurrentRun = ($CurrentClusters*$SectorsPerCluster)/$RecordsDivisor
$Counter+=$RecordsInCurrentRun
If $Counter>$TargetFileDec Then
ExitLoop
EndIf
Next
$TryAt = $Counter-$RecordsInCurrentRun
;$TryAtArrIndex = $i
$RecordsPerCluster = $SectorsPerCluster/$RecordsDivisor
Do
$RecordJumper+=$RecordsPerCluster
$Counter2+=1
$Final = $TryAt+$RecordJumper
Until $Final>=$TargetFileDec
$RecordsTooMuch = $Final-$TargetFileDec
_WinAPI_SetFilePointerEx($hFile, $ImageOffset+$MFT_RUN_VCN[$i]*$BytesPerCluster+($Counter2*$BytesPerCluster)-($RecordsTooMuch*$MFT_Record_Size), $FILE_BEGIN)
_WinAPI_ReadFile($hFile, DllStructGetPtr($tBuffer), $MFT_Record_Size, $nBytes)
$record = DllStructGetData($tBuffer, 1)
$TmpOffset = DllCall('kernel32.dll', 'int', 'SetFilePointerEx', 'ptr', $hFile, 'int64', 0, 'int64*', 0, 'dword', 1)
;ConsoleWrite("Record number: " & $MftRef & " found at disk offset: " & $TmpOffset[3]-$MFT_Record_Size & " -> 0x" & Hex(Int($TmpOffset[3]-$MFT_Record_Size)) & @CRLF)
EndIf
If StringMid($record,91,8) = $TargetFile Then
$TmpOffset = DllCall('kernel32.dll', 'int', 'SetFilePointerEx', 'ptr', $hFile, 'int64', 0, 'int64*', 0, 'dword', 1)
_WinAPI_CloseHandle($hFile)
$FoundOffset = Int($TmpOffset[3])-Int($MFT_Record_Size)
$RetVal[0] = $FoundOffset
$RetVal[1] = $record
Return $RetVal
Else
ConsoleWrite("Error wrong ref: " & StringMid($record,91,8) & @CRLF)
_WinAPI_CloseHandle($hFile)
Return ""
EndIf
EndFunc
Func _FindMFT($TargetFile)
Local $nBytes;, $MFT_Record_Size=1024
$tBuffer = DllStructCreate("byte[" & $MFT_Record_Size & "]")
$hFile = _WinAPI_CreateFile($TargetDrive, 2, 2, 7)
If $hFile = 0 Then
ConsoleWrite("Error in function CreateFile when trying to locate MFT: " & _WinAPI_GetLastErrorMessage() & @CRLF)
;_DisplayInfo("Error in function CreateFile when trying to locate MFT: " & _WinAPI_GetLastErrorMessage() & @CRLF)
Return SetError(1,0,0)
EndIf
; ConsoleWrite("$MFT_Offset: " & $MFT_Offset & @CRLF)
_WinAPI_SetFilePointerEx($hFile, $ImageOffset+$MFT_Offset, $FILE_BEGIN)
_WinAPI_ReadFile($hFile, DllStructGetPtr($tBuffer), $MFT_Record_Size, $nBytes)
_WinAPI_CloseHandle($hFile)
$record = DllStructGetData($tBuffer, 1)
If NOT StringMid($record,1,8) = '46494C45' Then
ConsoleWrite("MFT record signature not found. "& @crlf)
;_DisplayInfo("MFT record signature not found. "& @crlf)
Return ""
EndIf
If StringMid($record,47,4) = "0100" AND Dec(_SwapEndian(StringMid($record,91,8))) = $TargetFile Then
; ConsoleWrite("MFT record found" & @CRLF)
Return $record ;returns record for MFT
EndIf
ConsoleWrite("MFT record not found" & @CRLF)
;_DisplayInfo("MFT record not found" & @CRLF)
Return ""
EndFunc
Func _DecToLittleEndian($DecimalInput)
Return _SwapEndian(Hex($DecimalInput,8))
EndFunc
Func _SwapEndian($iHex)
Return StringMid(Binary(Dec($iHex,2)),3, StringLen($iHex))
EndFunc
Func _UnicodeHexToStr($FileName)
$str = ""
For $i = 1 To StringLen($FileName) Step 4
$str &= ChrW(Dec(_SwapEndian(StringMid($FileName, $i, 4))))
Next
Return $str
EndFunc
Func _DebugOut($text, $var)
ConsoleWrite("Debug output for " & $text & @CRLF)
For $i=1 To StringLen($var) Step 32
$str=""
For $n=0 To 15
$str &= StringMid($var, $i+$n*2, 2) & " "
if $n=7 then $str &= "- "
Next
ConsoleWrite($str & @CRLF)
Next
EndFunc
Func _ReadBootSector($TargetDrive)
Local $nbytes
$tBuffer=DllStructCreate("byte[512]")
Local $hFile = _WinAPI_CreateFile($TargetDrive,2,2,7)
If $hFile = 0 then
ConsoleWrite("Error in function CreateFile: " & _WinAPI_GetLastErrorMessage() & " for: " & $TargetDrive & @crlf)
;_DisplayInfo("Error in function CreateFile: " & _WinAPI_GetLastErrorMessage() & " for: " & "\\.\" & $TargetDrive & @crlf)
Return SetError(1,0,0)
EndIf
_WinAPI_SetFilePointerEx($hFile, $ImageOffset, $FILE_BEGIN)