Example #1
0
        /// <summary>
        /// Gets the serial number of the specified physical driver.
        /// </summary>
        /// <param name="DriveName">Name of the disk drive.</param>
        public static unsafe string Example_QueryDiskSerial(string DriveName = "PhysicalDrive0")
        {
            var SerialNumber = (string)null;

            //
            // Open the symbolic link created by the disk driver.
            //

            var DiskDevice = new DeviceIoControl("\\\\.\\" + DriveName);

            DiskDevice.Connect();

            //
            // Prepare a request to send to the disk driver.
            //

            var Desc  = new STORAGE_DESCRIPTOR_HEADER();
            var Query = new STORAGE_PROPERTY_QUERY
            {
                PropertyId = STORAGE_PROPERTY_ID.StorageDeviceProperty,
                QueryType  = STORAGE_QUERY_TYPE.PropertyStandardQuery
            };

            //
            // Call the disk driver's IRP handler.
            //

            if (DiskDevice.TryIoControl(IOCTL_STORAGE_QUERY_PROPERTY, &Query, Marshal.SizeOf(Query), &Desc, Marshal.SizeOf(Desc)))
            {
                var Allocation = Marshal.AllocHGlobal(Desc.Size);

                if (DiskDevice.TryIoControl(IOCTL_STORAGE_QUERY_PROPERTY, &Query, Marshal.SizeOf(Query), Allocation.ToPointer(), Desc.Size))
                {
                    var DeviceDesc = Marshal.PtrToStructure <STORAGE_DEVICE_DESCRIPTOR>(Allocation);

                    if (DeviceDesc.SerialNumberOffset != 0)
                    {
                        SerialNumber = Marshal.PtrToStringAnsi(IntPtr.Add(Allocation, DeviceDesc.SerialNumberOffset), Desc.Size - DeviceDesc.SerialNumberOffset);
                        SerialNumber = SerialNumber.Trim('\0');
                    }
                }
                else
                {
                    Console.WriteLine("[*] Failed to query for the storage descriptor.");
                }

                Marshal.FreeHGlobal(Allocation);
            }
            else
            {
                Console.WriteLine("[*] Failed to query for the storage descriptor size.");
            }

            DiskDevice.Close();

            Console.WriteLine($"[*] Disk Serial: {SerialNumber}");
            return(SerialNumber);
        }
Example #2
0
        /// <summary>
        /// Gets the serial number of the specified physical driver.
        /// </summary>
        /// <param name="DriveName">Name of the disk drive.</param>
        public static unsafe string GetSerialNumberOf(string DriveName = "PhysicalDrive0")
        {
            var SerialNumber = (string)null;

            //
            // Setup the driver's handlers/settings.
            //

            var DriverLoader = new KernelServiceLoader();

            DriverLoader.SetServiceName("disk");
            DriverLoader.SetOwnership(false);

            var DriverIo = new DriverIo();

            DriverIo.SetSymbolicLink("\\\\.\\" + DriveName);

            //
            // Initialize the driver interface.
            //

            using (var Driver = new Driver(new DriverConfig(), DriverLoader, DriverIo))
            {
                //
                // Opens or starts the disk driver.
                //

                if (!Driver.TryLoad())
                {
                    Console.WriteLine("[*] Failed to load the driver.");
                    goto End;
                }

                //
                // Connects with the disk driver.
                //

                if (Driver.IO.TryConnect())
                {
                    var Desc  = new STORAGE_DESCRIPTOR_HEADER();
                    var Query = new STORAGE_PROPERTY_QUERY
                    {
                        PropertyId = STORAGE_PROPERTY_ID.StorageDeviceProperty,
                        QueryType  = STORAGE_QUERY_TYPE.PropertyStandardQuery
                    };

                    //
                    // Call the disk driver's IRP handler.
                    //

                    if (Driver.IO.TryIoControl(IOCTL_STORAGE_QUERY_PROPERTY, &Query, Marshal.SizeOf(Query), &Desc, Marshal.SizeOf(Desc)))
                    {
                        var Allocation = Marshal.AllocHGlobal(Desc.Size);

                        if (Driver.IO.TryIoControl(IOCTL_STORAGE_QUERY_PROPERTY, &Query, Marshal.SizeOf(Query), Allocation.ToPointer(), Desc.Size))
                        {
                            var DeviceDesc = Marshal.PtrToStructure <STORAGE_DEVICE_DESCRIPTOR>(Allocation);

                            if (DeviceDesc.SerialNumberOffset != 0)
                            {
                                SerialNumber = Marshal.PtrToStringAnsi(IntPtr.Add(Allocation, DeviceDesc.SerialNumberOffset), Desc.Size - DeviceDesc.SerialNumberOffset);
                                SerialNumber = SerialNumber.Trim('\0');
                            }
                        }
                        else
                        {
                            Console.WriteLine("[*] Failed to query for the storage descriptor.");
                        }

                        Marshal.FreeHGlobal(Allocation);
                    }
                    else
                    {
                        Console.WriteLine("[*] Failed to query for the storage descriptor size.");
                    }
                }
                else
                {
                    // Console.WriteLine("[*] Failed to connect with the driver.");
                }

                //
                // Unload the driver.
                // We don't have ownership of the disk driver, so it doesn't really do anything.
                //

                if (!Driver.TryUnload())
                {
                    Console.WriteLine("[*] Failed to unload the driver.");
                    goto End;
                }

                //
                // Dispose the driver interface.
                //

                End :;
            }

            return(SerialNumber);
        }
Example #3
0
        public VolumeDeviceQuery GetStorageDeviceProperty(SafeHandle hDevice)
        {
            VolumeDeviceQuery volumeDeviceQuery = new VolumeDeviceQuery();

            SafeAllocHandle <STORAGE_PROPERTY_QUERY>    storagePropertyQueryPtr    = null;
            SafeAllocHandle <STORAGE_DESCRIPTOR_HEADER> storageDescriptorHeaderPtr = null;
            SafeAllocHandle <STORAGE_DEVICE_DESCRIPTOR> storageDeviceDescriptorPtr = null;

            try {
                STORAGE_PROPERTY_QUERY storagePropertyQuery = new STORAGE_PROPERTY_QUERY {
                    PropertyId = (uint)STORAGE_PROPERTY_ID.StorageDeviceProperty,
                    QueryType  = (uint)STORAGE_QUERY_TYPE.PropertyStandardQuery
                };
                storagePropertyQueryPtr = new SafeAllocHandle <STORAGE_PROPERTY_QUERY>(storagePropertyQuery);

                // Get the necessary output buffer size
                STORAGE_DESCRIPTOR_HEADER storageDescriptorHeader = new STORAGE_DESCRIPTOR_HEADER {
                    Version = 0,
                    Size    = 0
                };
                storageDescriptorHeaderPtr = new SafeAllocHandle <STORAGE_DESCRIPTOR_HEADER>(storageDescriptorHeader);

                bool success = DeviceIoControl(hDevice, IOCTL_STORAGE_QUERY_PROPERTY,
                                               storagePropertyQueryPtr, storagePropertyQueryPtr.SizeOf,
                                               storageDescriptorHeaderPtr, storageDescriptorHeaderPtr.SizeOf,
                                               out uint bytesReturns, IntPtr.Zero);
                if (!success || bytesReturns == 0)
                {
                    m_Win32Error = Marshal.GetLastWin32Error();
                    return(null);
                }

                STORAGE_DESCRIPTOR_HEADER storageDescriptorHeaderResult = storageDescriptorHeaderPtr.ToStructure();

                storageDeviceDescriptorPtr = new SafeAllocHandle <STORAGE_DEVICE_DESCRIPTOR>((int)storageDescriptorHeaderResult.Size);
                success = DeviceIoControl(hDevice, IOCTL_STORAGE_QUERY_PROPERTY,
                                          storagePropertyQueryPtr, storagePropertyQueryPtr.SizeOf,
                                          storageDeviceDescriptorPtr, storageDeviceDescriptorPtr.SizeOf,
                                          out bytesReturns, IntPtr.Zero);
                if (!success || bytesReturns == 0)
                {
                    m_Win32Error = Marshal.GetLastWin32Error();
                    return(null);
                }

                STORAGE_DEVICE_DESCRIPTOR storageDeviceDescriptor = storageDeviceDescriptorPtr.ToStructure();
                if (storageDeviceDescriptor.VendorIdOffset != 0)
                {
                    volumeDeviceQuery.VendorId = storageDeviceDescriptorPtr.ToStringAnsi((int)storageDeviceDescriptor.VendorIdOffset);
                }
                if (storageDeviceDescriptor.SerialNumberOffset != 0)
                {
                    volumeDeviceQuery.DeviceSerialNumber = storageDeviceDescriptorPtr.ToStringAnsi((int)storageDeviceDescriptor.SerialNumberOffset);
                }
                if (storageDeviceDescriptor.ProductIdOffset != 0)
                {
                    volumeDeviceQuery.ProductId = storageDeviceDescriptorPtr.ToStringAnsi((int)storageDeviceDescriptor.ProductIdOffset);
                }
                if (storageDeviceDescriptor.ProductRevisionOffset != 0)
                {
                    volumeDeviceQuery.ProductRevision = storageDeviceDescriptorPtr.ToStringAnsi((int)storageDeviceDescriptor.ProductRevisionOffset);
                }
                volumeDeviceQuery.RemovableMedia     = storageDeviceDescriptor.RemovableMedia;
                volumeDeviceQuery.CommandQueueing    = storageDeviceDescriptor.CommandQueueing;
                volumeDeviceQuery.ScsiDeviceType     = (ScsiDeviceType)storageDeviceDescriptor.DeviceType;
                volumeDeviceQuery.ScsiDeviceModifier = storageDeviceDescriptor.DeviceTypeModifier;
                volumeDeviceQuery.BusType            = (BusType)storageDeviceDescriptor.BusType;
            } finally {
                if (storagePropertyQueryPtr != null)
                {
                    storagePropertyQueryPtr.Close();
                }
                if (storageDescriptorHeaderPtr != null)
                {
                    storageDescriptorHeaderPtr.Close();
                }
                if (storageDeviceDescriptorPtr != null)
                {
                    storageDeviceDescriptorPtr.Close();
                }
            }

            return(volumeDeviceQuery);
        }