NTSTATUS DeviceControlDispatch(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
UNREFERENCED_PARAMETER(DeviceObject);
PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(Irp);
NTSTATUS status = STATUS_SUCCESS;
switch (irpStack->Parameters.DeviceIoControl.IoControlCode)
{
case IOCTL_PING:
PWCHAR message = L"ping";
SIZE_T stringLength = wcslen(message);
RtlCopyMemory(Irp->AssociatedIrp.SystemBuffer, message, (stringLength * sizeof(WCHAR)));
Irp->IoStatus.Information = (stringLength * sizeof(WCHAR));
break;
default:
status = STATUS_INVALID_PARAMETER;
break;
}
Irp->IoStatus.Status = status;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return status;
}```
In python I do something like this:
```py
driver_path = r"\\.\test"
driver_handle = ctypes.windll.kernel32.CreateFileW(
ctypes.c_wchar_p(driver_path),
ctypes.c_uint32(0xC0000000), # GENERIC_READ | GENERIC_WRITE
ctypes.c_uint32(0),
None,
ctypes.c_uint32(3), # OPEN_EXISTING
ctypes.c_uint32(0),
None
)
if driver_handle != -1:
ping_response = ctypes.create_unicode_buffer(256)
bytes_returned = ctypes.c_ulong(0)
result = ctypes.windll.kernel32.DeviceIoControl(
driver_handle,
IOCTL_PING,
None,
ctypes.c_ulong(0),
ping_response,
ctypes.sizeof(ping_response),
ctypes.byref(bytes_returned),
None
)
if result != 0:
print("Ping response:", ping_response.value)```
My problem comes from RtlCopyMemory, that generates me a BSOD, as if my driver was not able to inject in the buffer allocated by python...I would really like to understand my error and I have already posted my problem on several servers / forums (grouping different types of discussion) but nobody knows how to help me ...I would like to understand, for that I made many test but without results.