no funciona ReadFile y overlapped

Iniciado por Belial & Grimoire, 4 Febrero 2014, 00:48 AM

0 Miembros y 2 Visitantes están viendo este tema.

Belial & Grimoire

hola

tengo un problema con Readfile


d1 = (DWORD*)&crbig;
overlapped.Offset = d1[1];
overlapped.OffsetHigh = d1[0];

ret = ReadFile(hDevice, buff, 1024, &numerobyte, &overlapped);


tengo a crbig que es igual a 322122547

los coloco en overlapped low y high

offsethigh = 00000003
offset = 22122547;

supuestamente ReadFile deberia leer los datos de la unidad hDevice, en este caso 1024 bytes y deberia empezar a leer desde donde le digo a overlapped que lo haga

al inicio lo hago de esta manera ya que quiero leer el inicio de la unidad


ZeroMemory(&overlapped ,sizeof(OVERLAPPED));

hDevice = CreateFileW(zwpath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,  FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);

if(hDevice != INVALID_HANDLE_VALUE){

ret = ReadFile(hDevice, buff, 1024, &numerobyte, &overlapped);

}

si coloco un for de esta manera para saber que aparecio, me salen los bytes sin probemas en un printf

for (int n=0; n<sizeof(buff); n++) 
    { 
        printf("0x%02X ", buff[n]); 
    }


pero despues es cuando modifico overlapped y uso por segunda vez ReadFile


d1 = (DWORD*)&crbig;
overlapped.Offset = d1[1];
overlapped.OffsetHigh = d1[0];

ret = ReadFile(hDevice, buff, 1024, &numerobyte, &overlapped);


pero me siguen apareciendo los mismo bytes en el for(), ya intente antes de usar por segunda vez ReadFile, borrar el buffer usando memset e incluso usando un buffer diferente, pero me sigue mostrando lo mismo

y supuestamente como les mencione, deberia avanzar hacia donde overlapped dice, deberia leer los 1024 bytes de esa zona, y lo que deberia pasar es que el for me deberia mostrar otros bytes, pero no parece funcionar

alguien sabe o tiene alguna idea de porque no funciona?

salu2
.                                 

x64core

¿Porqué razon necesitas hacer lecturas asincrónicas?
¿Es tu aplicación de multihilos?

Sino, pues se usa la funcion SetFilePointer para leer algun archivo o dispositivo apartir de un offset.

Belial & Grimoire

pues como le mencione en varios post, estoy siguiendo ejemplos de VB.net sobre MFT, ahorita trato de convertir un codigo de VB a C para ir aprendiendo que hace el programa, ya entendi la mitad junto con varios post teoricos sobre MFT, en este momento con overlapped estoy por llegar a MFT para empezar a tomar los atributos, donde se encuentran los nombres de archivos que estan en la computadora o fueron eliminados pero no puedo avanzar con readfile y overlapped

en VB usan overlapped para moverse por bytes, por ejemplo

Código (vbnet) [Seleccionar]
Private Sub SetReadFileOffset(ByRef NO As System.Threading.NativeOverlapped, ByRef curBig As Int64)

        Dim lowoffset() As Byte = BitConverter.GetBytes(curBig)
        Dim highoffset As Int32 = BitConverter.ToInt32(lowoffset, 0)
        Dim high As Int32
        Dim lastbytes(3) As Byte
        Array.Copy(lowoffset, 4, lastbytes, 0, 4)
        high = BitConverter.ToInt32(lastbytes, 0)
        NO.OffsetLow = highoffset
        NO.OffsetHigh = high
    End Sub


Esto es hacer lo mismo que esto

crbig = (sectperclusters * (bytespercluster * MFTCluster));

d1 = (DWORD*)&crbig;
overlapped.Offset = d1[1];
overlapped.OffsetHigh = d1[0];


pero usandolo en Readfile parece que no funciona
.                                 

x64core

Cita de: Belial & Grimoire en  4 Febrero 2014, 02:50 AM
pues como le mencione en varios post, estoy siguiendo ejemplos de VB.net sobre MFT, ahorita trato de convertir un codigo de VB a C para ir aprendiendo que hace el programa, ya entendi la mitad junto con varios post teoricos sobre MFT, en este momento con overlapped estoy por llegar a MFT para empezar a tomar los atributos, donde se encuentran los nombres de archivos que estan en la computadora o fueron eliminados pero no puedo avanzar con readfile y overlapped

en VB usan overlapped para moverse por bytes, por ejemplo

Código (vbnet) [Seleccionar]
Private Sub SetReadFileOffset(ByRef NO As System.Threading.NativeOverlapped, ByRef curBig As Int64)

        Dim lowoffset() As Byte = BitConverter.GetBytes(curBig)
        Dim highoffset As Int32 = BitConverter.ToInt32(lowoffset, 0)
        Dim high As Int32
        Dim lastbytes(3) As Byte
        Array.Copy(lowoffset, 4, lastbytes, 0, 4)
        high = BitConverter.ToInt32(lastbytes, 0)
        NO.OffsetLow = highoffset
        NO.OffsetHigh = high
    End Sub


Esto es hacer lo mismo que esto

crbig = (sectperclusters * (bytespercluster * MFTCluster));

d1 = (DWORD*)&crbig;
overlapped.Offset = d1[1];
overlapped.OffsetHigh = d1[0];


pero usandolo en Readfile parece que no funciona

Lo mismo he visto que pasa en los anteriores temas, no podes esperar que ambos languajes traten de igual manera los diferentes tipos
de variables menos una framework como .NET puede tener sus propias maneras de como manipular array de bytes, cadenas ,etc.
Omite la lectura asincrónica y usa SetFilePointer para establecer el inicio en donde ReadFile queres que haga la lectura.
Acerca de porqué no aparecen los bytes, en el código C puede seer debido a que estas haciendo lectura asincrónica y los bytes no
son aun escritos en el buffer cuando estas intentado usarlos. no podria estar seguro ya que el código es incompleto.
Luego si tenes problemas con esto pues publica toda la función.

Belial & Grimoire

Este es el codigo que llevo hecho, por el momento todo me ha funcionado bien, hasta ahorita con overlapped



#include <Windows.h>
#include <winioctl.h>
#include <stdio.h>
#include <stdlib.h>

#define zwpath L"\\\\.\\C:"


long endianhextodec(BYTE *buffers, int offs){

BYTE tmp[2] = {0};
BYTE tmp2[1] = {0};
BYTE tmp3[3] = {0};
BYTE tmp4[1] = {0};

if(offs == 11){
tmp[0] = buffers[11];
tmp[1] = buffers[12];

return tmp[1] << 8;

}

if(offs == 13){

tmp2[0] = buffers[13];

return tmp2[0];

}

if(offs == 30){

tmp3[0] = buffers[48];
tmp3[1] = buffers[49];
tmp3[2] = buffers[50];

return tmp3[2] << 16;
}else{return 0;}
}


int main(int argc, char *argv[]){

HANDLE hDevice;
OVERLAPPED overlapped;
DWORD crbig;
BYTE buff[1024] = {0};
DWORD numerobyte = 0, nbytes = 0;
UINT32 ret;
DWORD *d1;
int offset1 = 11, offset2 = 13, offset3 = 30;
long MFTCluster = 0, bytespercluster = 0, sectperclusters = 0, offet = 0;
unsigned long mult = 0;

ZeroMemory(&overlapped ,sizeof(OVERLAPPED));

hDevice = CreateFileW(zwpath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,  FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);

if(hDevice != INVALID_HANDLE_VALUE){

ret = ReadFile(hDevice, buff, 1024, &numerobyte, &overlapped);

}else
{
return NULL;
}

if(ret == 0){


ret = WaitForSingleObject(hDevice,INFINITE );

switch (ret)
{
case WAIT_OBJECT_0:break;
case WAIT_TIMEOUT:break;
default:
break;
}
}
else
{
return NULL;
}


if((int)buff[3] == 'N' && (int)buff[4] == 'T' && (int)buff[5] == 'F' && (int)buff[6] == 'S' ){
printf("Volumen es formato NTFS\n\n\n");
}

bytespercluster = endianhextodec(buff, offset1);
sectperclusters = endianhextodec(buff, offset2);
MFTCluster = endianhextodec(buff, offset3);

printf("Bytes por clusters = %d\nsectores por clusters = %d\nMFTClusters = %ld\n\n", bytespercluster, sectperclusters, MFTCluster);

crbig = (sectperclusters * (bytespercluster * MFTCluster));

d1 = (DWORD*)&crbig;
overlapped.Offset = d1[1];
overlapped.OffsetHigh = d1[0];

ret = ReadFile(hDevice, buff, 1024, &numerobyte, &overlapped);

if(ret == 0){


ret = WaitForSingleObject(hDevice,INFINITE );

switch (ret)
{
case WAIT_OBJECT_0:break;
default:
break;
}
}
else
{
return NULL;
}

for (int n=0; n<sizeof(buff); n++) 
    { 
        printf("0x%02X ", buff[n]); 
    }

CloseHandle(hDevice);

getchar();
}


Y este es la parte del codigo de VB.NET que he hecho en C

Código (vbnet) [Seleccionar]
Public Function IsNFTSDrive(ByVal strDrive As String) As Boolean
        Dim Hnd As Integer, nRead As Integer
        Dim ret As UInt32
        Dim Buffer(1024) As Byte
        Hnd = CreateFile("\\.\" & Mid(strDrive, 1, 2), GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, _
        Nothing, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL Or FILE_FLAG_OVERLAPPED, IntPtr.Zero)
        If (Hnd <> INVALID_HANDLE_VALUE) Then
            ret = ReadFile(Hnd, Buffer, 1024, nRead, New System.Threading.NativeOverlapped)
            Console.WriteLine(Buffer(200))
        Else
            Return False
        End If
        If ret = 0 Then
            ret = WaitForSingleObject(Hnd, INFINITE)
            Select Case ret
                Case WAIT_OBJECT_0
                Case WAIT_TIMEOUT
            End Select
        Else
            Return False
        End If
        CloseHandle(Hnd)
        Return Buffer(3) = 78 And Buffer(4) = 84 And Buffer(5) = 70 And Buffer(6) = 83
    End Function

    Public Function GetFiles(ByVal strDrive As String, ByVal bnAllFiles As Boolean) As DataTable
        Dim Hnd As Integer, nRead As Integer
        Dim ret As UInt32
        Dim Buffer(1024) As Byte

        Dim BytesPerSect As Long
        Dim SectperCluster As Long
        Dim MFTCluster As Long
        Dim intIgnoredFiles As Integer = 0

        Dim NO As System.Threading.NativeOverlapped
        'Read Partition Info
        Hnd = CreateFile("\\.\" & Mid(strDrive, 1, 2), GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, _
        Nothing, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL Or FILE_FLAG_OVERLAPPED, IntPtr.Zero)
        If (Hnd <> INVALID_HANDLE_VALUE) Then
            ret = ReadFile(Hnd, Buffer, 1024, nRead, NO)
        Else
            Return Nothing
        End If
        If ret = 0 Then
            ret = WaitForSingleObject(Hnd, INFINITE)
            Select Case ret
                Case WAIT_OBJECT_0
                Case WAIT_TIMEOUT
            End Select
        Else
            Return Nothing
        End If
        BytesPerSect = LittleEndianHEXToDecimal(Buffer, &HB, 2)
        SectperCluster = LittleEndianHEXToDecimal(Buffer, &HD, 1)
        MFTCluster = LittleEndianHEXToDecimal(Buffer, &H30, 8)
        'Read MFT
        SetReadFileOffset(NO, MFTCluster * SectperCluster * BytesPerSect)
        ret = ReadFile(Hnd, Buffer, 1024, nRead, NO)
        If ret = 0 Then
            ret = WaitForSingleObject(Hnd, INFINITE)
            Select Case ret
                Case WAIT_OBJECT_0
                Case Else
            End Select
        Else
            Return Nothing
        End If



Código (vbnet) [Seleccionar]
Private Function LittleEndianHEXToDecimal(ByRef Buffer As Byte(), ByVal offset As Long, ByVal Length As Long) As Long
        If Length = 1 Then
            Return BitConverter.ToInt16(Buffer, offset)
        End If
        If Length = 2 Then
            Return BitConverter.ToInt16(Buffer, offset)
        End If
        If Length = 4 Then
            Return BitConverter.ToInt32(Buffer, offset)
        End If
        If Length = 8 Then
            Return BitConverter.ToInt64(Buffer, offset)
        End If

    End Function


Código (vbnet) [Seleccionar]
Private Sub SetReadFileOffset(ByRef NO As System.Threading.NativeOverlapped, ByRef curBig As Int64)

        Dim lowoffset() As Byte = BitConverter.GetBytes(curBig)
        Dim highoffset As Int32 = BitConverter.ToInt32(lowoffset, 0)
        Dim high As Int32
        Dim lastbytes(3) As Byte
        Array.Copy(lowoffset, 4, lastbytes, 0, 4)
        high = BitConverter.ToInt32(lastbytes, 0)
        NO.OffsetLow = highoffset
        NO.OffsetHigh = high
    End Sub


Donde empieza mi problema es en la ultima parte donde dice 'Read MFT del codigo VB
.                                 

x64core

Bien hoy si esta más claro.
Nota la declaración de crbig y como lo uso.


#define zwpath L"\\\\.\\C:"
     
     
long endianhextodec(BYTE *buffers, int offs)
{     
    BYTE tmp[2] = {0};
    BYTE tmp2[1] = {0};
    BYTE tmp3[3] = {0};
    BYTE tmp4[1] = {0};
     
    if(offs == 11){
    tmp[0] = buffers[11];
    tmp[1] = buffers[12];
     
    return tmp[1] << 8;
     
    }
     
    if(offs == 13){
     
    tmp2[0] = buffers[13];
     
    return tmp2[0];
     
    }
     
    if(offs == 30){
     
    tmp3[0] = buffers[48];
    tmp3[1] = buffers[49];
    tmp3[2] = buffers[50];
     
    return tmp3[2] << 16;
    }else{return 0;}

}
     
int main(int argc, char *argv[]){
{
    HANDLE hDevice;
    OVERLAPPED overlapped;
    LARGE_INTEGER crbig;
    BYTE buff[1024] = {0};
    DWORD numerobyte = 0, nbytes = 0;
    UINT32 ret;
    DWORD *d1;
    int offset1 = 11, offset2 = 13, offset3 = 30;
    long MFTCluster = 0, bytespercluster = 0, sectperclusters = 0, offet = 0;
    unsigned long mult = 0;
     
    ZeroMemory(&overlapped ,sizeof(OVERLAPPED));
     
    hDevice = CreateFileW(zwpath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,  FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
     
    if(hDevice != INVALID_HANDLE_VALUE){
     
    ret = ReadFile(hDevice, buff, 1024, &numerobyte, &overlapped);
     
    }else
    {
    return NULL;
    }
     
    if(ret == 0){
     
     
    ret = WaitForSingleObject(hDevice,INFINITE );
     
    switch (ret)
    {
    case WAIT_OBJECT_0:break;
    case WAIT_TIMEOUT:break;
    default:
    break;
    }
    }
    else
    {
    return NULL;
    }
     
     
    if((int)buff[3] == 'N' && (int)buff[4] == 'T' && (int)buff[5] == 'F' && (int)buff[6] == 'S' ){
    printf("Volumen es formato NTFS\n\n\n");
    }
     
    bytespercluster = endianhextodec(buff, offset1);
    sectperclusters = endianhextodec(buff, offset2);
    MFTCluster = endianhextodec(buff, offset3);
     
    printf("Bytes por clusters = %d\nsectores por clusters = %d\nMFTClusters = %ld\n\n", bytespercluster, sectperclusters, MFTCluster);
     

    RtlZeroMemory(&overlapped ,sizeof(OVERLAPPED));
    crbig.QuadPart = (sectperclusters * (bytespercluster * MFTCluster));
    overlapped.Offset = crbig.LowPart;
    overlapped.OffsetHigh = crbig.HighPart;
     
    ret = ReadFile(hDevice, buff, 1024, &numerobyte, &overlapped);
     
    if(ret == 0){

     
    ret = WaitForSingleObject(hDevice,INFINITE );
     
    switch (ret)
    {
    case WAIT_OBJECT_0:break;
    default:
    break;
    }
    }
    else
    {
    return NULL;
    }
     
    for (int n=0; n<sizeof(buff); n++) 
       { 
           printf("0x%02X ", buff[n]); 
       }
     
    CloseHandle(hDevice);
     
    getchar();
}




por cierto, yo no sé porqué estos tios usaron lectura asincronica, no sé si la aplicación es de multihilos de todos modos se puede
usar SetFilePointer en lugar de FILE_FLAG_OVERLAPPED y WaitForSingleObject lo veo totalmente inutil, quizas no conocian SetFilePointer...

Belial & Grimoire

coincido contigo sobre WaitForSingleObject, pero no quiero empezar a hacer preguntas de algo que no me funciono y darme cuenta que fue por no colocar algo que en el otro codigo si habia, pero terminando hare lo que me mencionas sobre SetFilePointer y quitare y optimizare otras cosas que las estoy dejando pasar por ahora, solo que no me habia tocado el momento de trabajar con varias funciones y API's de ahorita, por eso se me esta dificultando entender

y el codigo que me pusiste, no se si estoy haciendo algo mal, porque el prinft me sigue mostrando los mismo bytes, como que no avanza, y necesito avanzar dependiendo el valor de overlapped, porque en el codigo de VB.net me sale un offset de 56 + 1

Código (vbnet) [Seleccionar]
Dim offset As Long = LittleEndianHEXToDecimal(Buffer, 20, 2) + 1

al principio creia que no encontraba ese offset, pero lo busque y ninguno del offset 20 me sale 56 en decimal, es la suma de 2 bytes segun bitconverter, entonces es donde me di cuenta que readfile no avanza y parece que lee los mismo bytes

acabo de compilar el codigo y avanza igual de bien, pero me sigue mostrando los mismos bytes del inicio, en el tercer offset empieza NTFS, asi que sigue siendo el inicio y eso es lo que no entiendo, porque no avanza

0xEB 0x52 0x90 0x4E 0x54 0x46 0x53 0x20 0x20 0x20 0x20 0x00 0x02 0x08 0x00 0x00
0x00 0x00 0x00 0x00 0x00 0xF8 0x00 0x00 0x3F 0x00 0xFF 0x00 0x00 0x28 0x03 0x00
0x00 0x00 0x00 0x00 0x80 0x00 0x80 0x00 0xFF 0xAF 0xEE 0x0C 0x00 0x00 0x00 0x00
0x00 0x00 0x0C 0x00 0x00 0x00 0x00 0x00 0x02 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0xF6 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x87 0xFF 0x92 0x82 0x24 0x93 0x82 0x1E
0x00 0x00 0x00 0x00 0xFA 0x33 0xC0 0x8E 0xD0 0xBC 0x00 0x7C 0xFB 0x68 0xC0 0x07
0x1F 0x1E 0x68 0x66 0x00 0xCB 0x88 0x16 0x0E 0x00 0x66 0x81 0x3E 0x03 0x00 0x4E
0x54 0x46 0x53 0x75 0x15 0xB4 0x41 0xBB 0xAA 0x55 0xCD 0x13 0x72 0x0C 0x81 0xFB
0x55 0xAA 0x75 0x06 0xF7 0xC1 0x01 0x00 0x75 0x03 0xE9 0xDD 0x00 0x1E 0x83 0xEC
0x18 0x68 0x1A 0x00 0xB4 0x48 0x8A 0x16 0x0E 0x00 0x8B 0xF4 0x16 0x1F 0xCD 0x13
0x9F 0x83 0xC4 0x18 0x9E 0x58 0x1F 0x72 0xE1 0x3B 0x06 0x0B 0x00 0x75 0xDB 0xA3
0x0F 0x00 0xC1 0x2E 0x0F 0x00 0x04 0x1E 0x5A 0x33 0xDB 0xB9 0x00 0x20 0x2B 0xC8
0x66 0xFF 0x06 0x11 0x00 0x03 0x16 0x0F 0x00 0x8E 0xC2 0xFF 0x06 0x16 0x00 0xE8
0x4B 0x00 0x2B 0xC8 0x77 0xEF 0xB8 0x00 0xBB 0xCD 0x1A 0x66 0x23 0xC0 0x75 0x2D
0x66 0x81 0xFB 0x54 0x43 0x50 0x41 0x75 0x24 0x81 0xF9 0x02 0x01 0x72 0x1E 0x16
0x68 0x07 0xBB 0x16 0x68 0x70 0x0E 0x16 0x68 0x09 0x00 0x66 0x53 0x66 0x53 0x66
0x55 0x16 0x16 0x16 0x68 0xB8 0x01 0x66 0x61 0x0E 0x07 0xCD 0x1A 0x33 0xC0 0xBF
0x28 0x10 0xB9 0xD8 0x0F 0xFC 0xF3 0xAA 0xE9 0x5F 0x01 0x90 0x90 0x66 0x60 0x1E
0x06 0x66 0xA1 0x11 0x00 0x66 0x03 0x06 0x1C 0x00 0x1E 0x66 0x68 0x00 0x00 0x00
0x00 0x66 0x50 0x06 0x53 0x68 0x01 0x00 0x68 0x10 0x00 0xB4 0x42 0x8A 0x16 0x0E
0x00 0x16 0x1F 0x8B 0xF4 0xCD 0x13 0x66 0x59 0x5B 0x5A 0x66 0x59 0x66 0x59 0x1F
0x0F 0x82 0x16 0x00 0x66 0xFF 0x06 0x11 0x00 0x03 0x16 0x0F 0x00 0x8E 0xC2 0xFF
0x0E 0x16 0x00 0x75 0xBC 0x07 0x1F 0x66 0x61 0xC3 0xA0 0xF8 0x01 0xE8 0x09 0x00
0xA0 0xFB 0x01 0xE8 0x03 0x00 0xF4 0xEB 0xFD 0xB4 0x01 0x8B 0xF0 0xAC 0x3C 0x00
0x74 0x09 0xB4 0x0E 0xBB 0x07 0x00 0xCD 0x10 0xEB 0xF2 0xC3 0x0D 0x0A 0x45 0x72
0x72 0x6F 0x72 0x20 0x64 0x65 0x20 0x64 0x69 0x73 0x63 0x6F 0x00 0x0D 0x0A 0x46
0x61 0x6C 0x74 0x61 0x20 0x62 0x6F 0x6F 0x74 0x6D 0x67 0x72 0x00 0x0D 0x0A 0x42
0x6F 0x6F 0x74 0x6D 0x67 0x72 0x20 0x63 0x6F 0x6D 0x70 0x72 0x69 0x6D 0x69 0x64
0x6F 0x00 0x0D 0x0A 0x50 0x72 0x65 0x73 0x2E 0x20 0x43 0x74 0x72 0x6C 0x2B 0x41
0x6C 0x74 0x2B 0x53 0x75 0x70 0x72 0x20 0x70 0x61 0x72 0x61 0x20 0x72 0x65 0x69
0x6E 0x69 0x63 0x69 0x61 0x72 0x0D 0x0A 0x00 0x6C 0x20 0x74 0x6F 0x20 0x72 0x65
0x73 0x74 0x61 0x72 0x74 0x0D 0x0A 0x00 0x8C 0x9D 0xAD 0xC2 0x00 0x00 0x55 0xAA
0x07 0x00 0x42 0x00 0x4F 0x00 0x4F 0x00 0x54 0x00 0x4D 0x00 0x47 0x00 0x52 0x00
0x04 0x00 0x24 0x00 0x49 0x00 0x33 0x00 0x30 0x00 0x00 0xD4 0x00 0x00 0x00 0x24
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x00 0x00 0x00 0x00 0x00 0x00 0xEB 0x22 0x90 0x90 0x05 0x00 0x4E 0x00 0x54 0x00
0x4C 0x00 0x44 0x00 0x52 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x66 0x0F 0xB7 0x06 0x0B 0x00
0x66 0x0F 0xB6 0x1E 0x0D 0x00 0x66 0xF7 0xE3 0x66 0xA3 0x52 0x02 0x66 0x8B 0x0E
0x40 0x00 0x80 0xF9 0x00 0x0F 0x8F 0x0E 0x00 0xF6 0xD9 0x66 0xB8 0x01 0x00 0x00
0x00 0x66 0xD3 0xE0 0xEB 0x08 0x90 0x66 0xA1 0x52 0x02 0x66 0xF7 0xE1 0x66 0xA3
0x66 0x02 0x66 0x0F 0xB7 0x1E 0x0B 0x00 0x66 0x33 0xD2 0x66 0xF7 0xF3 0x66 0xA3
0x56 0x02 0xE8 0x95 0x04 0x66 0x8B 0x0E 0x4E 0x02 0x66 0x89 0x0E 0x26 0x02 0x66
0x03 0x0E 0x66 0x02 0x66 0x89 0x0E 0x2A 0x02 0x66 0x03 0x0E 0x66 0x02 0x66 0x89
0x0E 0x2E 0x02 0x66 0x03 0x0E 0x66 0x02 0x66 0x89 0x0E 0x3E 0x02 0x66 0x03 0x0E
0x66 0x02 0x66 0x89 0x0E 0x46 0x02 0x66 0xB8 0x90 0x00 0x00 0x00 0x66 0x8B 0x0E
0x26 0x02 0xE8 0x83 0x09 0x66 0x0B 0xC0 0x0F 0x84 0x5E 0xFE 0x66 0xA3 0x32 0x02
0x66 0xB8 0xA0 0x00 0x00 0x00 0x66 0x8B 0x0E 0x2A 0x02 0xE8 0x6A 0x09 0x66 0xA3
0x36 0x02 0x66 0xB8 0xB0 0x00 0x00 0x00 0x66 0x8B 0x0E 0x2E 0x02 0xE8 0x58 0x09
0x66 0xA3 0x3A 0x02 0x66 0xA1 0x32 0x02 0x66 0x0B 0xC0 0x0F 0x84 0x2B 0xFE 0x67
0x80 0x78 0x08 0x00 0x0F 0x85 0x22 0xFE 0x67 0x66 0x8D 0x50 0x10 0x67 0x03 0x42
0x04 0x67 0x66 0x0F 0xB6 0x48 0x0C 0x66 0x89 0x0E 0x72 0x02 0x67 0x66 0x8B 0x48
0x08 0x66 0x89 0x0E 0x6E 0x02 0x66 0xA1 0x6E 0x02 0x66 0x0F 0xB7 0x0E 0x0B 0x00
0x66 0x33 0xD2 0x66 0xF7 0xF1 0x66 0xA3 0x76 0x02 0x66 0xA1 0x46 0x02 0x66 0x03
0x06 0x6E 0x02 0x66 0xA3 0x4A 0x02 0x66 0x83 0x3E 0x36 0x02 0x00 0x0F 0x84 0x1D
0x00 0x66 0x83 0x3E 0x3A 0x02 0x00 0x0F 0x84 0xCF 0xFD 0x66 0x8B 0x1E 0x3A 0x02
0x1E 0x07 0x66 0x8B 0x3E 0x4A 0x02 0x66 0xA1 0x2E 0x02 0xE8 0xE0 0x01 0x66 0x0F
0xB7 0x0E 0x00 0x02 0x66 0xB8 0x02 0x02 0x00 0x00 0xE8 0x22 0x08 0x66 0x0B 0xC0
0x0F 0x85 0x16 0x00 0x66 0x0F 0xB7 0x0E 0x5A 0x02 0x66 0xB8 0x5C 0x02 0x00 0x00
0xE8 0x0C 0x08 0x66 0x0B 0xC0 0x0F 0x84 0x42 0x0C 0x67 0x66 0x8B 0x00 0x1E 0x07
0x66 0x8B 0x3E 0x3E 0x02 0xE8 0x3F 0x06 0x66 0xA1 0x3E 0x02 0x66 0xBB 0x20 0x00
0x00 0x00 0x66 0xB9 0x00 0x00 0x00 0x00 0x66 0xBA 0x00 0x00 0x00 0x00 0xE8 0xE4





.                                 

x64core

#7
Hice esta traducción rapida, nota que use la estructura que creo que es la estabas buscando la vez pasada, de esta manera accedemos
a los campos directamente. pero sino, pues como dije no hay necesidad de hacer conversion a diferencia como en NET. simplemente haces
algo como esto:

variable = *((SizeOfType*)&Pointer[offset]); / SizeOfType = BYTE, WORD, etc

y obtendras el campo

Uso VC++ como compilador por si tenes problemas al compilar con directivas propias ( #pragma pack, tipos, etc ).

Código (cpp) [Seleccionar]

#pragma pack(push, 1)
typedef struct _BIOS_PARAMETERS_BLOCK
{
   USHORT    BytesPerSector;
   UCHAR     SectorsPerCluster;
   UCHAR     Unused0[7];
   UCHAR     MediaId;
   UCHAR     Unused1[2];
   USHORT    SectorsPerTrack;
   USHORT    Heads;
   UCHAR     Unused2[4];
   UCHAR     Unused3[4];
} BIOS_PARAMETERS_BLOCK, *PBIOS_PARAMETERS_BLOCK;

typedef struct _EXTENDED_BIOS_PARAMETERS_BLOCK
{
   USHORT    Unknown[2];
   ULONGLONG SectorCount;
   ULONGLONG MftLocation;
   ULONGLONG MftMirrLocation;
   CHAR      ClustersPerMftRecord;
   UCHAR     Unused4[3];
   CHAR      ClustersPerIndexRecord;
   UCHAR     Unused5[3];
   ULONGLONG SerialNumber;
   UCHAR     Checksum[4];
} EXTENDED_BIOS_PARAMETERS_BLOCK, *PEXTENDED_BIOS_PARAMETERS_BLOCK;

typedef struct _BOOT_SECTOR
{
   UCHAR     Jump[3];
   UCHAR     OEMID[8];
   BIOS_PARAMETERS_BLOCK BPB;
   EXTENDED_BIOS_PARAMETERS_BLOCK EBPB;
   UCHAR     BootStrap[426];
   USHORT    EndSector;
} BOOT_SECTOR, *PBOOT_SECTOR;
#pragma pack(pop)

VOID GetFiles(LPSTR SzDrive)
{
   if(!SzDrive)
       return;
   
   CHAR SzSymDrive[40] = "\\\\.\\";
   HANDLE hDrive;
   BYTE DriveContent[1024];
   DWORD BytesRead;
   PBOOT_SECTOR lpBootSector;
   LARGE_INTEGER MFTLcl;

   strcat(SzSymDrive,SzDrive);
   hDrive = CreateFileA(SzSymDrive,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,
               NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL);
   if(hDrive == INVALID_HANDLE_VALUE)
   {
       return;
   }

   if( (!ReadFile(hDrive,DriveContent,sizeof(DriveContent),&BytesRead,NULL)) ||
       (BytesRead != sizeof(DriveContent))
      )
   {
       goto clhandle;
   }

   lpBootSector = (PBOOT_SECTOR)DriveContent;
   MFTLcl.QuadPart = lpBootSector->BPB.BytesPerSector * lpBootSector->BPB.SectorsPerCluster * lpBootSector->EBPB.MftLocation;

   if(SetFilePointer(hDrive,MFTLcl.LowPart,&MFTLcl.HighPart,FILE_BEGIN) == INVALID_SET_FILE_POINTER)
   {
       goto clhandle;
   }

   if( (!ReadFile(hDrive,DriveContent,sizeof(DriveContent),&BytesRead,NULL)) ||
       (BytesRead != sizeof(DriveContent))
       )
   {
       goto clhandle;
   }

   // ...

clhandle:
   CloseHandle(hDrive);

}

-
llamar por ejemplo:
GetFiles("C:");

Belial & Grimoire

#8
hola, perdon por contestar hasta ahora pero se me acabo el tiempo, tengo que regresar a la escuela y con el trabajo ya no creo tener tanto tiempo

pero tienes razon, yo estaba utilizando otra estructura que era NTFS_VOLUME_DATA_BUFFER

typedef struct {
 LARGE_INTEGER VolumeSerialNumber;
 LARGE_INTEGER NumberSectors;
 LARGE_INTEGER TotalClusters;
 LARGE_INTEGER FreeClusters;
 LARGE_INTEGER TotalReserved;
 DWORD         BytesPerSector;
 DWORD         BytesPerCluster;
 DWORD         BytesPerFileRecordSegment;
 DWORD         ClustersPerFileRecordSegment;
 LARGE_INTEGER MftValidDataLength;
 LARGE_INTEGER MftStartLcn;
 LARGE_INTEGER Mft2StartLcn;
 LARGE_INTEGER MftZoneStart;
 LARGE_INTEGER MftZoneEnd;
} NTFS_VOLUME_DATA_BUFFER, *PNTFS_VOLUME_DATA_BUFFER;

pero tuve dos problemas, la primera fue un error mio que no me habia dado cuenta que no estaba leyendo $BOOT sino otro que era Disk_Geometry, fue cuando me di cuenta que debia hacerlo funcionar con C: y no PhysicalDrive0

y la segunda fue que no encontraba SectorsPerCluster

hice un intento de multiplicarlo directamente por 8 con el anterior codigo y me salio el mismo resultado que en el codigo de arriba en este momento


  MFTcl.QuadPart =  ntfsData.MftStartLcn.QuadPart * ntfsData.BytesPerSector * 8;


pero ya no me dio tiempo de arreglar SetFilePointer y Readfile, asi que ejecute el programa pero enseguida salto a CloseHandle(hDevice); con el if INVALID_SET_FILE_POINTER

pero bueno, supuestamente con esto ya deberia haber llegado a $MFT, de aqui deberia empezar a leer las entradas MFT, donde ya podria encontrar los archivos junto con sus atributos y se encuentran en cada 1024 bytes

Gracias x64Core, logre avanzar bastante gracias a tu ayuda, seguire un poco lento pero quiero terminar de aprender esto

salu2
.