Readin the $MFT

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im into being able to read the NTFS MFT. Theres a routine, originally written by StcroixSkipper in C# which I have translated into VB. Each MFT record provides two FIDs (File IDs), one for the file in question and one for its directory in the MFT
of course. The routine that is supplying a directory spec is failing miserably returnin nothing and looks like this:
<img alt="" src="http://social.msdn.microsoft.com/Forums/getfile/85403
The code that is failing looks like this:

<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; color:blue; font-size:9.5pt Private<span style="font-family:Consolas; font-size:9.5pt
<span style="color:blue Function PathFromFrn(<span style="color:blue ByVal Id
<span style="color:blue As <span style="color:blue Long) <span style="color:blue
As <span style="color:blue String
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue Dim fOk <span style="color:blue As
<span style="color:blue Integer
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue Dim FileName <span style="color:blue As
<span style="color:blue String = <span style="color:blue String.Empty
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue Dim UnicodeString <span style="color:blue
As <span style="color:#2b91af UNICODE_STRING
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue Dim ObjAttributes <span style="color:blue
As <span style="color:#2b91af OBJECT_ATTRIBUTES
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue Dim IoStatusBlock <span style="color:blue
As <span style="color:#2b91af IO_STATUS_BLOCK
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue Dim hFile <span style="color:blue As
<span style="color:#2b91af IntPtr <span style="color:green out handle

<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue Dim Buffer <span style="color:blue As
<span style="color:#2b91af IntPtr = <span style="color:#2b91af Marshal.AllocHGlobal(4096)
<span style="color:green Raw buffer
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue Dim Refptr <span style="color:blue As
<span style="color:#2b91af IntPtr = <span style="color:#2b91af Marshal.AllocHGlobal(8)
<span style="color:green 8 byte FileID
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue Dim ObjAtt <span style="color:blue As
<span style="color:#2b91af IntPtr = <span style="color:#2b91af Marshal.AllocHGlobal(<span style="color:#2b91af Marshal.SizeOf(ObjAttributes))
<span style="color:green pointer to the unicode string struct
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:green pointer>>fileid
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:#2b91af Marshal.WriteInt64(Refptr, 0, Id)
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:green 8 byte file id
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
UnicodeString.Length = 8
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
UnicodeString.MaximumLength = 8
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
UnicodeString.Buffer = Refptr
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:green copy unicode structure to pointer
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:#2b91af Marshal.StructureToPtr(UnicodeString, ObjAtt,
<span style="color:blue True)
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:green InitializeObjectAttributes Macro
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
ObjAttributes.Length = <span style="color:#2b91af Marshal.SizeOf(ObjAttributes)
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
ObjAttributes.ObjectName = ObjAtt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
ObjAttributes.RootDirectory = m_hCJ
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
ObjAttributes.Attributes = OBJ_CASE_INSENSITIVE
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
fOk = NtCreateFile(hFile, 0, ObjAttributes, IoStatusBlock, 0, 0, _
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
FILE_SHARE_READ <span style="color:blue Or FILE_SHARE_WRITE, _
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
FILE_OPEN, FILE_OPEN_BY_FILE_ID <span style="color:blue Or FILE_OPEN_FOR_BACKUP_INTENT, 0, 0)
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue If fOk <> INVALID_HANDLE_VALUE <span style="color:blue
Then
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
fOk = NtQueryInformationFile(hFile, IoStatusBlock, Buffer, 4096, FileNameInformationClass)
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span> <span style="color:blue If fOk = 0
<span style="color:blue Then
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:green The first 4 bytes is the length
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue Dim FileLength <span style="color:blue As
<span style="color:blue Integer = <span style="color:#2b91af Marshal.ReadInt32(Buffer, 0)
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:green The filename is just after the first 4 bytes.
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
FileName = <span style="color:#2b91af Marshal.PtrToStringUni(<span style="color:blue New
<span style="color:#2b91af IntPtr(Buffer.ToInt32() + 4), FileLength / 2)
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue End <span style="color:blue If
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue End <span style="color:blue If
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:green free allocated memory and handles
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
CloseHandle(hFile)
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:#2b91af Marshal.FreeHGlobal(Buffer)
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:#2b91af Marshal.FreeHGlobal(ObjAtt)
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:#2b91af Marshal.FreeHGlobal(Refptr)
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue Return FileName
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Consolas; font-size:9.5pt <span>
<span style="color:blue End <span style="color:blue Function

I realize that it will be luck to catch this. I hope someone here can help me.
Renee <hr class="sig "MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me

View the full article
 

Similar threads

Back
Top