jnausn

JNA Access NTFS USN (win32). How to get data form Memory object?


enum USN Data from memory, The result data is not correct. I don't know what to do

public static void main(String[] args) throws IOException {
    // get c volume handle
    WinNT.HANDLE handle = Kernel32.INSTANCE.CreateFile("\\\\.\\C:",WinNT.GENERIC_READ|WinNT.GENERIC_WRITE,
        WinNT.FILE_SHARE_READ|WinNT.FILE_SHARE_WRITE,null,WinNT.OPEN_EXISTING,0,null);

    IntByReference bytesByReturned = new IntByReference();
    // output to memory
    Memory memory = new Memory(Integer.MAX_VALUE);
    memory.clear();

    // input query args
    MFT_ENUM_DATA med = new MFT_ENUM_DATA();
    med.StartFileReferenceNumber = 0;
    med.LowUsn = 0;
    med.HighUsn = 99999999;
    med.write();

    int FSCTL_ENUM_USN_DATA = 0x000900B3;

    // access device, get usn data to memory buffer object
    boolean ok = Kernel32.INSTANCE.DeviceIoControl(handle,FSCTL_ENUM_USN_DATA, med.getPointer(),
        med.size(), memory, (int)memory.size(), bytesByReturned, null);

    if(ok) {
        // that's wrong!!! , get the wrong data!
        int RecordLength = memory.getInt(0);
        short MajorVersion = memory.getShort(4);
        short MinorVersion = memory.getShort(6);
        long FileReferenceNumber = memory.getLong(8);

        // error value
        System.out.println("RecordLength:\t"+RecordLength);
        System.out.println("MajorVersion:\t"+MajorVersion);
        System.out.println("MinorVersion:\t"+MinorVersion);
        System.out.println("FileReferenceNumber:\t"+FileReferenceNumber);
        // ...
    }else {
        System.out.println("ERROR "+Kernel32.INSTANCE.GetLastError()+" "+ Kernel32Util.getLastErrorMessage());

        throw new IOException("DeviceIoControl() returned false", new Win32Exception(Kernel32.INSTANCE.GetLastError()));

    }
}

static public class MFT_ENUM_DATA extends Structure {
    public long StartFileReferenceNumber;
    public long LowUsn;
    public long HighUsn;

    @Override
    protected List<String> getFieldOrder() {
        List<String> order = new ArrayList<>();
        order.add("StartFileReferenceNumber");
        order.add("LowUsn");
        order.add("HighUsn");
        return order;
    }
}

I can't get the structure data from Memory buffer object, What shall I do?

typedef struct USN_RECORD_V2 {
  DWORD         RecordLength;
  WORD          MajorVersion;
  WORD          MinorVersion;
  DWORDLONG     FileReferenceNumber;
  DWORDLONG     ParentFileReferenceNumber;
  USN           Usn;
  LARGE_INTEGER TimeStamp;
  DWORD         Reason;
  DWORD         SourceInfo;
  DWORD         SecurityId;
  DWORD         FileAttributes;
  WORD          FileNameLength;
  WORD          FileNameOffset;
  WCHAR         FileName[1];
}  *PUSN_RECORD_V2;

link:
- DeviceIoControl FSCTL_ENUM_USN_DATA
- input MFT_ENUM_DATA_V0 Structure
- output memory --> USN_RECORD_V2 Structure

get USN_RECORD_V2 from memory. Many thanks.


Solution

  • Welcome to StackOverflow!

    The documentation you linked to says your memory output buffer from this function is:

    A pointer to the output buffer that receives a USN followed by zero or more USN_RECORD_V2 or USN_RECORD_V3 structures. The USN is a DWORDLONG value that represents the USN following the last record in the buffer.

    You are trying to read the memory starting at offset 0, but those first 8 bytes are a USN that you can use in a future call.

    You would add 8 bytes to your existing code to get the right values; however, a much more readable way to do this is to map a Structure with all the USN_RECORD_V2 fields and their byte values, and read() that structure from memory.share(8).