ArsenalRecon / Arsenal-Image-Mounter

Arsenal Image Mounter mounts the contents of disk images as complete disks in Microsoft Windows.
https://ArsenalRecon.com/weapons/image-mounter
Other
531 stars 89 forks source link

ChatGPT not working examples #35

Closed lollita closed 10 months ago

lollita commented 1 year ago

I tryed to ask c++ code for AIM to ChatGPT but not work. Also google nothing found about "aimapi.h".

#include <Windows.h>
#include <aimapi.h>

int main()
{
    const wchar_t* imageFilePath = L"path\\to\\image.vhdx";
    const wchar_t* deviceName = L"AIMExampleRamDisk";
    const unsigned long long diskSize = 1073741824; // 1 GB
    const unsigned long sectorSize = 512;

    // Initialize the Arsenal Image Mounter API
    AIMInit();

    // Create the RAM disk parameters
    AIM_VIRTUAL_STORAGE_PARAMETERS params;
    ZeroMemory(&params, sizeof(params));
    params.DeviceFlags = AIM_VIRTUAL_STORAGE_DEVICE_FLAGS_VOLATILE;
    params.AccessFlags = AIM_VIRTUAL_STORAGE_ACCESS_FLAGS_READWRITE;
    params.SectorSize = sectorSize;
    params.MaxSize = diskSize;
    AIMCreateVirtualStorageParams(&params);

    // Mount the VHDX image as a virtual disk
    AIM_VIRTUAL_STORAGE_HANDLE vhdxHandle = AIMMountVirtualDisk(imageFilePath, AIM_VIRTUAL_STORAGE_TYPE_VHDX, 0, 0);
    if (vhdxHandle == INVALID_HANDLE_VALUE)
    {
        printf("Failed to mount VHDX image.\n");
        return 1;
    }

    // Create the RAM disk using the mounted VHDX image
    AIM_VIRTUAL_STORAGE_HANDLE ramDiskHandle = AIMCreateVirtualDisk(deviceName, AIM_VIRTUAL_STORAGE_TYPE_RAMDISK, &params, vhdxHandle, 0);
    if (ramDiskHandle == INVALID_HANDLE_VALUE)
    {
        printf("Failed to create RAM disk.\n");
        return 1;
    }

    // Assign a drive letter to the RAM disk
    DWORD error = AIMAssignVirtualDiskDriveLetter(ramDiskHandle, NULL);
    if (error != ERROR_SUCCESS)
    {
        printf("Failed to assign drive letter to RAM disk. Error code: %lu\n", error);
        return 1;
    }

    // Unmount the VHDX image
    AIMUnmountVirtualDisk(vhdxHandle);

    // Cleanup
    AIMCloseVirtualStorageHandle(ramDiskHandle);
    AIMDeleteVirtualStorageParams(&params);
    AIMPurgeVirtualStorageMountCache();
    AIMCleanup();

    printf("RAM disk created successfully.\n");

    return 0;
}

Also Powershell code not work:

Import-Module -Name "C:\Program Files (x86)\ArsenalRecon\AIM\AIM.psd1"

Mount-AIMVirtualDisk -ImagePath "C:\path\to\file.vhdx" -ReadOnly

$deviceID = Get-AIMVirtualDisk | Where-Object { $_.ImagePath -eq "C:\path\to\file.vhdx" } | Select-Object -ExpandProperty DeviceID

New-AIMVirtualDiskDrive -DeviceID $deviceID -DriveType Dynamic
LTRData commented 1 year ago

You could take a look at the source code for aim_ll.exe:

https://github.com/ArsenalRecon/Arsenal-Image-Mounter/blob/master/Unmanaged%20Source/aim_ll/aimcmd.cpp

lollita commented 1 year ago

I tryed to mount a vhd but not able:

#include <windows.h>
#include <iostream>

#include "common.h"
#include "aimapi.h"
#pragma comment(lib,"aimapi.lib")

int main()
{
    //LPWSTR letter = new TCHAR[3];
    //wmemcpy(letter, L"F:", 3);
    DEVICE_NUMBER device_number;
    device_number.LongNumber = IMSCSI_AUTO_DEVICE_NUMBER;
    DWORD sector_size = 4096;
    DWORD flags = IMSCSI_DEVICE_TYPE_HD| IMSCSI_TYPE_VM;//  IMSCSI_TYPE_FILE  | IMSCSI_TYPE_PROXY | IMSCSI_PROXY_TYPE_SHM
    BOOL aim = ImScsiCreateDeviceEx(
        NULL,
        INVALID_HANDLE_VALUE,
        &device_number,
        NULL,
        &sector_size,
        NULL,
        &flags,
        L"\\??\\vhdaccess\\??\\awealloc\\??\\E:\\Temp\\temp.vhd",
        //L"\\??\\E:\\Temp\\temp.vhd",
        NULL,
        FALSE,//TRUE
        NULL,//letter
        FALSE
    );
    wprintf(L"end aim: %d err: %d, id:  %06x, sect: %d", aim GetLastError(), device_number.LongNumber, sector_size);
    //std::cout << "Hello World!\n";
    return 0;
}
LTRData commented 1 year ago

I tryed to mount a vhdx but not able:

#include <windows.h>
#include "aimapi.h"
#pragma comment(lib,"aimapi.lib")
#include <iostream>

int main()
{
    BOOL ami=ImScsiCreateDevice(NULL,
            INVALID_HANDLE_VALUE,
            NULL,
            0,
            0,
            0,
            0,
            L"\\\\?\\vhdaccess\\??\\awealloc\\??\\c:\\temp.vhd",
            TRUE,
            (LPWSTR)L"Q:",
            FALSE);
    std::cout << "Hello World!\n";
}

You have the Adapter and DeviceNumber parameters in the wrong order. Also, the NativePath parameter should be FALSE in this case.

lollita commented 1 year ago

Changed NativePath. I see Adapter and DeviceNumber parameters ordered as 2 and 3; why wrong? I set (LPWSTR)L"Q:", but the lettere is in vhdx, as set it?

LTRData commented 1 year ago

Changed NativePath. I see Adapter and DeviceNumber parameters ordered as 2 and 3; why wrong?

No sorry, my mistake. You are right!

I set (LPWSTR)L"Q:", but the lettere is in vhdx, as set it?

I do not understand what you mean here. The parameter for mount point looks correct if you want Q: as drive letter.

Also note that what you are trying to do only works for vhd just like in your example code, note vhdx.

lollita commented 1 year ago

my mistake I use vhd not vhdx. You are right! I mean that I set letter in vhd from OS disk management and aim not need it. What about error list that not compile it?

LTRData commented 1 year ago

Yes you need to include common.h as well. There are some type definitions in there.

https://github.com/ArsenalRecon/Arsenal-Image-Mounter/blob/c91fb636fe559b9985619fe1a7c6bf779a3d63ea/Unmanaged%20Source/phdskmnt/inc/common.h

lollita commented 1 year ago

Adding common.h I'm able to compile it. But when I execute, it return without nothing do or write to console.

LTRData commented 1 year ago

Adding common.h I'm to compile it. But when I execute, it return without nothing do or write to console.

You need to check return value of API function and call GetLastError to find out what is going wrong.

lollita commented 1 year ago

The problem is that API function not return value and not execute next instruction. It is as have a silent crash.

LTRData commented 1 year ago

The problem is that API function not return value and not execute next instruction. It is as have a silent crash.

Run a debug build inside Visual Studio and select to stop at exceptions etc and see what happens.

lollita commented 1 year ago

You are right! I have to use debug: Unhandled exception at 0x00007FF9610C3310 (aimapi.dll) in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0x0000000000000000.


'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\System32\imm32.dll'. 
The thread 0x15a8 has exited with code 0 (0x0).

'ConsoleApplication1.exe' (Win32): Loaded 'C:\Windows\System32\bcrypt.dll'. 
Exception thrown at 0x00007FF95FFA3310 (aimapi.dll) in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0x0000000000000000.
Unhandled exception at 0x00007FF95FFA3310 (aimapi.dll) in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0x0000000000000000.

The program '[10180] ConsoleApplication1.exe' has exited with code 0 (0x0).

I updated the source.

lollita commented 1 year ago

I updated the source. Now it not have error and work fine with regular mount vhd file. But I need a again a few step, why L"\\?\vhdaccess\??\awealloc\??\E:\Temp\temp.vhd" not work. I get device but not mount volume:

Device number 000100
Device is \\?\PhysicalDrive4
Image file: \??\vhdaccess\??\awealloc\??\E:\Temp\temp.vhd
Size: 2147483648 bytes (2 GB), Queued unbuffered I/O Image File, HDD, Modified.

same vhd file work fine with command line: aim_ll.exe -a -f \\?\vhdaccess\??\awealloc\??\E:\Temp\temp.vhd

Device number 000100
Device is \\?\PhysicalDrive4
Image file: \??\vhdaccess\??\awealloc\??\E:\Temp\temp.vhd
Size: 2147483648 bytes (2 GB), Queued unbuffered I/O Image File, HDD, Modified.
Contains volume \\?\Volume{2c975252-ec13-11ed-bb88-5e6c3a1f8af4}\
  Mounted at F:\
lollita commented 1 year ago

Now it mount if I use | IMSCSI_TYPE_VM and \??\vhdaccess but it have set "online" from os disk manager. Which piece I still miss?

p.s. I solved this too. The problem is I use a vhd already mounted. But also here, via aim_ll.exe it worked in the same way.

LTRData commented 1 year ago

Now it mount if I use | IMSCSI_TYPE_VM and \??\vhdaccess but it have set "online" from os disk manager. Which piece I still miss?

p.s. I solved this too. The problem is I use a vhd already mounted. But also here, via aim_ll.exe it worked in the same way.

IMSCSI_TYPE_VM is not correct here. It would just allocate a large amount of virtual memory for the entire disk size at once, which is not what you want here.

lollita commented 1 year ago

You are right! code work also without IMSCSI_TYPE_VM. I have other strange problem. This code compile and work fine in console application. But if I use same code in dll library I get:

Severity    Code    Description Project File    Line    Suppression State
Error (active)  E0020   identifier "PSTORAGE_DEVICE_NUMBER" is undefined    Test    c:\aimapi.h 494 
Error   C2061   syntax error: identifier 'PSTORAGE_DEVICE_NUMBER'   Test    494 

how is it possible?

LTRData commented 1 year ago

Here is documentation about that structure and what header files you need to include: https://learn.microsoft.com/en-us/windows/win32/api/winioctl/ns-winioctl-storage_device_number

lollita commented 1 year ago

With winioctl.h now compile, but when try to use my DLL including ImScsiCreateDevice with extern "C" _declspec(dllexport) I get "1 = unable to use the DLL file". If I remove AMI routine in my dll it turn to work. Do it not possibile to use in dll?

LTRData commented 1 year ago

With winioctl.h now compile, but when try to use my DLL including ImScsiCreateDevice with DLL_EXPORT I get "1 = unable to use the DLL file". If I remove AMI routine in my dll it turn to work. Do it not possibile to use in dll?

If you just include aimapi.h and other header dependencies and use #pragma comment or similar to link to aimapi.lib, this should work and your DLL should be able to import functions through a depency on aimapi.dll.

lollita commented 1 year ago

It work now ty 👍

lollita commented 1 year ago

Do it is normal that if I include aimapi.lib again I need aimapi.dll ?

LTRData commented 1 year ago

Do it is normal that if I include aimapi.lib again I need aimapi.dll ?

Yes.