9

我编写了以下代码来将 0xFF 写入我的 USB 存储设备上的所有字节。由于某种原因,WriteFile()调用在扇区 242 处开始出错。我在两个单独的 USB 存储设备上完成了此操作,然后在十六进制编辑器中检查了这些设备。扇区 242 似乎是 FAT16 格式化设备上文件分配表的开始,以及 NTFS 设备上引导区的开始。我确信它在这些确切位置出错并非巧合,但是我不知道如何改变这种行为。HRESULT失败时我收到的是WriteFile-2147024891,即E_ACCESSDENIED. 有谁知道可能导致问题的原因?

注意:如果您要在本地系统上运行此代码,请非常小心,因为我已经硬编码了我的 USB 设备的物理设备 ID。请务必使用您尝试写入的设备更新 deviceId 变量。你不想破坏你的硬盘。

    public enum EMoveMethod : uint
    {
        Begin = 0,
        Current = 1,
        End = 2
    }

    [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    static extern uint SetFilePointer([In] SafeFileHandle hFile, [In] long lDistanceToMove, [Out] out int lpDistanceToMoveHigh, [In] EMoveMethod dwMoveMethod);

    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    static extern SafeFileHandle CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);

    [DllImport("kernel32", SetLastError = true)]
    internal extern static int ReadFile(SafeFileHandle handle, byte[] bytes, int numBytesToRead, out int numBytesRead, IntPtr overlapped_MustBeZero);

    [DllImport("kernel32.dll", SetLastError = true)]
    internal extern static int WriteFile(SafeFileHandle handle, byte[] bytes, int numBytesToWrite, out int numBytesWritten, IntPtr overlapped_MustBeZero);

    [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true)]
    private static extern bool DeviceIoControl(SafeFileHandle hDevice, uint dwIoControlCode, byte[] lpInBuffer, int nInBufferSize, byte[] lpOutBuffer, int nOutBufferSize, out int lpBytesReturned, IntPtr lpOverlapped);

    [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true)]
    private static extern bool CloseHandle(SafeFileHandle handle);

public void wipeDisk()
{
        const uint OPEN_EXISTING = 3;
        const uint GENERIC_WRITE = (0x40000000);
        const uint FSCTL_LOCK_VOLUME = 0x00090018;
        const uint FSCTL_UNLOCK_VOLUME = 0x0009001c;
        const uint FSCTL_DISMOUNT_VOLUME = 0x00090020;

        bool success = false;
        int intOut;
        string deviceId = @"\\.\PHYSICALDRIVE2";
        long DiskSize = 2056320000;

        SafeFileHandle diskHandle = CreateFile(deviceId, GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
        if (diskHandle.IsInvalid)
        {
            Console.WriteLine(deviceId + " open error.");
            return;
        }

        Console.WriteLine(deviceId + " " + Marshal.GetHRForLastWin32Error().ToString() + ": opened.");

        success = DeviceIoControl(diskHandle, FSCTL_LOCK_VOLUME, null, 0, null, 0, out intOut, IntPtr.Zero);
        if (!success)
        {
            Console.WriteLine(deviceId + " lock error.");
            CloseHandle(diskHandle);
            return;
        }

        Console.WriteLine(deviceId + " " + Marshal.GetHRForLastWin32Error().ToString() + ": locked.");

        success = DeviceIoControl(diskHandle, FSCTL_DISMOUNT_VOLUME, null, 0, null, 0, out intOut, IntPtr.Zero);
        if (!success)
        {
            Console.WriteLine(deviceId + " " + Marshal.GetHRForLastWin32Error().ToString() + ": dismount error.");
            DeviceIoControl(diskHandle, FSCTL_UNLOCK_VOLUME, null, 0, null, 0, out intOut, IntPtr.Zero);
            CloseHandle(diskHandle);
            return;
        }

        Console.WriteLine(deviceId + " " + Marshal.GetHRForLastWin32Error().ToString() + ": unmounted.");

        int numBytesPerSector = 512;
        long numTotalSectors = DiskSize / 512;

        byte[] junkBytes = new byte[512];
        for (int x = 0; x < 512; x++)
        {
            junkBytes[x] = 0xFF;
        }

        for (long sectorNum = 0; sectorNum < numTotalSectors; sectorNum++)
        {
            int numBytesWritten = 0;
            int moveToHigh;

            uint rvalsfp = SetFilePointer(diskHandle, sectorNum * numBytesPerSector, out moveToHigh, EMoveMethod.Begin);

            Console.WriteLine("File pointer set " + Marshal.GetHRForLastWin32Error().ToString() + ": " + (sectorNum * numBytesPerSector).ToString());

            int rval = WriteFile(diskHandle, junkBytes, junkBytes.Length, out numBytesWritten, IntPtr.Zero);

            if (numBytesWritten != junkBytes.Length)
            {
                Console.WriteLine("Write error on track " + sectorNum.ToString() + " from " + (sectorNum * numBytesPerSector).ToString() + "-" + moveToHigh.ToString() + " " + Marshal.GetHRForLastWin32Error().ToString() + ": Only " + numBytesWritten.ToString() + "/" + junkBytes.Length.ToString() + " bytes written.");
                break;
            }
            else
            {
                Console.WriteLine("Write success " + Marshal.GetHRForLastWin32Error().ToString() + ": " + numBytesWritten.ToString() + "/" + junkBytes.Length.ToString() + " bytes written.");
            }
        }

        success = DeviceIoControl(diskHandle, FSCTL_UNLOCK_VOLUME, null, 0, null, 0, out intOut, IntPtr.Zero);
        if (success)
        {
            Console.WriteLine(deviceId + " " + Marshal.GetHRForLastWin32Error().ToString() + ": unlocked.");
        }
        else
        {
            Console.WriteLine(deviceId + " " + Marshal.GetHRForLastWin32Error().ToString() + ": unlock error: " + Marshal.GetHRForLastWin32Error().ToString());
        }

        success = CloseHandle(diskHandle);
        if (success)
        {
            Console.WriteLine(deviceId + " " + Marshal.GetHRForLastWin32Error().ToString() + ": handle closed.");
        }
        else
        {
            Console.WriteLine(deviceId + " " + Marshal.GetHRForLastWin32Error().ToString() + ": close handle error: " + Marshal.GetHRForLastWin32Error().ToString());
        }
}

编辑/更新

在使用第三方工具对 USB 设备进行低级别擦除后,我能够成功地使其工作。驱动器完全归零后,我能够成功写入设备。一旦识别出有效的 fat 或 ntfs 文件系统以及使用

    const uint FSCTL_LOCK_VOLUME = 0x00090018;
    const uint FSCTL_DISMOUNT_VOLUME = 0x00090020;

配对DeviceIoControl似乎不会覆盖设备上的锁定窗口。

有谁知道如何在DeviceIoControl具有有效文件系统的驱动器上成功锁定 Windows 中的可移动 USB 设备?

我已经使用了几个第三方工具来做我想做的事情并且它们成功地工作。我知道这是可能的,但我阅读的所有 MSDN 文档都没有帮助解决问题。

编辑/更新 2

这取自https://web.archive.org/web/20130507212546/http://msdn.microsoft.com/en-us/library/ff551353.aspx

应用程序需要锁定卷、卸载卷或两者,才能发出 DASD I/O。这是 Windows Vista 的新功能,旨在解决潜在的恶意技术。

  1. 文件系统将阻止对磁盘保留部分的所有写操作。在这种情况下,这些保留部分包括 MBR 和两个 FAT 区域。要阻止这些区域,您需要通过发送 FSCTL_LOCK_VOLUME 来锁定卷。您必须在执行实际写入操作的同一卷句柄上发出此结构。如果有打开的文件句柄,此请求可能会失败。在这种情况下,应用程序可以通过发出 FSCTL_DISMOUNT_VOLUME 来强制卸载文件系统。但是,在文件句柄关闭之前,不会真正卸载卷。在此之前,应用程序可以通过使用当前打开的相同文件句柄继续发出 DASD I/O。

  2. 在文件系统已知的卷空间之外有一个扩展区域,写入操作将被阻止。要允许对该区域的写操作,您必须在卷句柄上发出 FSCTL_ALLOW_EXTENDED_DASD_IO。

您可以使用 Win32 API 例程 DeviceIoControl 来发出所有以前的 FSCTS。

我相信这正是我们在上面的代码中实现的,但它似乎没有正常工作。我们得到了一个句柄并且正在锁定和卸载设备,所以我们应该能够正确地写入受保护的区域?

编辑/更新 3

好的,这是当前打开磁盘和卷的顺序。锁定、卸载等方法只是按照我们认为错误的顺序工作。

SafeFileHandle volumeHandle = CreateFile("\\.\E:",...);
LockVolume(volumeHandle);
DismountVolume(volumeHandle);
SafeFileHandle diskHandle = CreateFile("\\.\PHYSICALDRIVE1"...);
WriteStuff(diskHandle);
//Fails...
UnlockVolume(volumeHandle);
CloseVolume(volumeHandle);
CloseDisk(diskHandle);

我仍然得到相同的结果,它仅在磁盘被丢弃时才有效。

4

5 回答 5

5

这里磁盘驱动器之间存在混淆。

如果您想完全访问磁盘(这是您正在使用的情况\\.\PHYSICALDRIVE),您必须锁定所有已安装的卷,它们基本上是物理磁盘的所有分区(即驱动器) 。

使用该模式获取每个已安装卷(它是驱动器,而不是物理磁盘)的句柄,而不是使用FSCTL_LOCK_VOLUME返回的句柄。CreateFile("\\.\PHYSICALDRIVE"...)string.Replace("\\\\.\\{0}:", DriveLetter)

您可以使用IOCTL_DISK_GET_DRIVE_LAYOUT.


编辑:

来自MSDN

如果以下条件之一为真,则磁盘句柄上的写入将成功:

要写入的扇区不在卷的范围内。

要写入的扇区属于已安装的卷,但您已使用 FSCTL_LOCK_VOLUME 或 FSCTL_DISMOUNT_VOLUME 显式锁定或卸载该卷。

要写入的扇区属于除 RAW 之外没有挂载文件系统的卷。

所以基本上,你应该做的是:

  • 获取每个卷的句柄
  • 在每个卷上使用FSCTL_LOCK_VOLUME 或。 FSCTL_DISMOUNT_VOLUME如果卷中没有文件正在使用(即任何进程都没有打开任何文件的句柄),FSCTL_LOCK_VOLUME就足够了
  • 获取物理磁盘的句柄
  • 写入物理磁盘
  • 关闭两个手柄。关闭音量手柄将解除锁定。

还要确保您使用管理员权限(提升的进程)运行您的应用程序。

于 2012-08-24T17:14:38.773 回答
1

我猜你正在使用Windows Vista或更高版本。操作系统将阻止任何直接写入这些扇区的尝试,因此您需要先进行锁定。更多关于这里:

http://msdn.microsoft.com/en-us/library/ff551353.aspx

也只是签入所以提出了这个帖子:

CreateFile:直接对原始磁盘进行写入操作“访问被拒绝” - Vista,Win7

那里的调查信息可能会有所帮助,HTH ...

于 2012-08-24T15:21:28.787 回答
1

编辑

我已编辑此答案以反映 ken2k 的建议。

ken2k 的建议确实解决了我遇到的问题。我不确定为什么我之前使用该方法的尝试没有成功,但是我刚刚重新访问/调整了我的代码,并且该方法似乎确实可以正常工作。

以下是我用来解决此问题的步骤:

  • 获取物理磁盘的句柄
  • 获取物理磁盘上每个逻辑驱动器的句柄
  • 锁定物理磁盘上的每个驱动器
  • 卸载物理磁盘上的每个驱动器
  • 锁定物理磁盘(可选)
  • 卸载物理磁盘(可选)
  • 使用物理磁盘句柄将整个物理磁盘归零
  • 解锁每个逻辑驱动器
  • 解锁物理磁盘(仅当您选择锁定磁盘时)
  • 关闭逻辑驱动器句柄
  • 关闭物理磁盘句柄

注意:如果您希望在不终止程序的情况下执行背靠背磁盘操作,并且您已经使用了该FSCTL_DISMOUNT_VOLUME功能,则需要使用类似于以下内容的“重新挂载”磁盘:

ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_DiskDrive");

或者

System.IO.DriveInfo.GetDrives();

当您尝试锁定每个单独的逻辑驱动器时,要将逻辑驱动器 ID 映射到物理磁盘 ID,请使用以下代码将逻辑驱动器标签链接到物理磁盘标签:

    List<string> driveLetters = new List<string>();
    string deviceId = @"\\.\PHYSICALDRIVE1";
    string queryString = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + deviceId + "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition";
    ManagementObjectSearcher diskSearcher = new ManagementObjectSearcher("root\\CIMV2", queryString);
    ManagementObjectCollection diskMoc = diskSearcher.Get();
    foreach (ManagementObject diskMo in diskMoc)
    {
        queryString = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" + diskMo["DeviceID"] + "'} WHERE AssocClass = Win32_LogicalDiskToPartition";
        ManagementObjectSearcher driveSearcher = new ManagementObjectSearcher("root\\CIMV2", queryString);

        ManagementObjectCollection driveMoc = driveSearcher.Get();
        foreach (ManagementObject driveMo in driveMoc)
        {
            driveLetters.Add("\\\\.\\" + driveMo["DeviceID"].ToString());
        }
    }

因此,例如,如果物理磁盘标签是\\.\PHYSICALDRIVE1并且它包含一个带有驱动器号“E”的逻辑驱动器,则上述代码将映射\\.\E:\\.\PHYSICALDRIVE1.

根据 ken2k 的建议,此映射也可以使用该IOCTL_DISK_GET_DRIVE_LAYOUT功能完成。

于 2012-08-27T16:27:29.050 回答
0

对于这个命令,我确实运行并检查了它。 SafeFileHandle diskHandle = CreateFile(deviceId, GENERIC_WRITE, 0/*Here*/ , IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);

通过平均 FILE_SHARE_READ |FILE_SHARE_WRITE (1|2) 将 GENERIC_WRITE 参数的第一个零下一个更改为 3 以获得良好的结果。我将其和设备 ID 更改为相同的驱动器名称 \.\f: 用于我的 USB 驱动器名称。最后我用这个代码替换了: SafeFileHandle diskHandle = CreateFile(deviceId, GENERIC_WRITE, 3 , IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); 祝你好运

于 2020-06-28T07:20:50.293 回答
0

我测试跟随代码,需要修复我的 USB 磁盘以隐藏 rootkit 病毒。所以写这段代码:

using Microsoft.Win32.SafeHandles;
using System;
using System.Collections.Generic;
using System.IO;
using System.Management;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace RootKeyremover
{
    public partial class Form1 : Form
    {
        public enum EMoveMethod : uint
        {
            Begin = 0,
            Current = 1,
            End = 2
        }

        [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern uint SetFilePointer([In] SafeFileHandle hFile, [In] long lDistanceToMove, [Out] out int lpDistanceToMoveHigh, [In] EMoveMethod dwMoveMethod);

        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        static extern SafeFileHandle CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);

        [DllImport("kernel32", SetLastError = true)]
        internal extern static int ReadFile(SafeFileHandle handle, byte[] bytes, int numBytesToRead, out int numBytesRead, IntPtr overlapped_MustBeZero);

        [DllImport("kernel32.dll", SetLastError = true)]
        internal extern static int WriteFile(SafeFileHandle handle, byte[] bytes, int numBytesToWrite, out int numBytesWritten, IntPtr overlapped_MustBeZero);

        [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true)]
        private static extern bool DeviceIoControl(SafeFileHandle hDevice, uint dwIoControlCode, byte[] lpInBuffer, int nInBufferSize, byte[] lpOutBuffer, int nOutBufferSize, out int lpBytesReturned, IntPtr lpOverlapped);

        [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true)]
        private static extern bool CloseHandle(SafeFileHandle handle);

        public Form1()
        {
            InitializeComponent();
        }

                   
        public void wipeDisk()
        {
            const short FILE_ATTRIBUTE_NORMAL = 0x80;
            const short INVALID_HANDLE_VALUE = -1;
            const uint GENERIC_READ = 0x80000000;
            const uint OPEN_EXISTING = 3;
            const uint GENERIC_WRITE = (0x40000000);
            const uint FSCTL_LOCK_VOLUME = 0x00090018;
            const uint FSCTL_UNLOCK_VOLUME = 0x0009001c;
            const uint FSCTL_DISMOUNT_VOLUME = 0x00090020;

            bool success = false;
            int intOut;
            //@"\\.\PHYSICALDRIVE2"
            string deviceId = @"\\.\" + comboBox1.Text.Substring(0,2);
            long DiskSize = 2056320000;

            SafeFileHandle diskHandle = CreateFile(deviceId, GENERIC_READ | GENERIC_WRITE, 3, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
            if (diskHandle.IsInvalid)
            {
                Console.WriteLine(deviceId + " open error.");
                return;
            }

            Console.WriteLine(deviceId + " " + Marshal.GetHRForLastWin32Error().ToString() + ": opened.");

            success = DeviceIoControl(diskHandle, FSCTL_LOCK_VOLUME, null, 0, null, 0, out intOut, IntPtr.Zero);
            if (!success)
            {
                Console.WriteLine(deviceId + " lock error.");
                CloseHandle(diskHandle);
                return;
            }

            Console.WriteLine(deviceId + " " + Marshal.GetHRForLastWin32Error().ToString() + ": locked.");

            success = DeviceIoControl(diskHandle, FSCTL_DISMOUNT_VOLUME, null, 0, null, 0, out intOut, IntPtr.Zero);
            if (!success)
            {
                Console.WriteLine(deviceId + " " + Marshal.GetHRForLastWin32Error().ToString() + ": dismount error.");
                DeviceIoControl(diskHandle, FSCTL_UNLOCK_VOLUME, null, 0, null, 0, out intOut, IntPtr.Zero);
                CloseHandle(diskHandle);
                return;
            }

            Console.WriteLine(deviceId + " " + Marshal.GetHRForLastWin32Error().ToString() + ": unmounted.");

            int numBytesPerSector = 512;
            long numTotalSectors = DiskSize / 512;

            byte[] junkBytes = new byte[512];

            
            int k =0 ;
            IntPtr l= (IntPtr)0 ;
            SetFilePointer(diskHandle, 0, out k, EMoveMethod.Begin);
            ReadFile(diskHandle, junkBytes, (int)512,out k,l);
            //3e 17e 1f1-1fb
            //for (int x = 0x3e; x < 0x17e; x++)
            //{
            //    junkBytes[x] = 0x00;
            //}
            //for (int x = 0x1f1; x < 0x1fb; x++)
            //{
            //    junkBytes[x] = 0x00;
            //}
            for (int x = 0x1e1; x < 0x1ee; x++)
            {
                junkBytes[x] = 0x00;
            }

            //diskHandle = CreateFile(deviceId, GENERIC_WRITE, 3, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
            for (long sectorNum = 0; sectorNum < 1; sectorNum++)
            {
                int numBytesWritten = 0;
                int moveToHigh;

                uint rvalsfp = SetFilePointer(diskHandle, sectorNum * numBytesPerSector, out moveToHigh, EMoveMethod.Begin);

                Console.WriteLine("File pointer set " + Marshal.GetHRForLastWin32Error().ToString() + ": " + (sectorNum * numBytesPerSector).ToString());

                int rval = WriteFile(diskHandle, junkBytes, junkBytes.Length, out numBytesWritten, IntPtr.Zero);

                if (numBytesWritten != junkBytes.Length)
                {
                    Console.WriteLine("Write error on track " + sectorNum.ToString() + " from " + (sectorNum * numBytesPerSector).ToString() + "-" + moveToHigh.ToString() + " " + Marshal.GetHRForLastWin32Error().ToString() + ": Only " + numBytesWritten.ToString() + "/" + junkBytes.Length.ToString() + " bytes written.");
                    break;
                }
                else
                {
                    Console.WriteLine("Write success " + Marshal.GetHRForLastWin32Error().ToString() + ": " + numBytesWritten.ToString() + "/" + junkBytes.Length.ToString() + " bytes written.");
                }
            }

            success = DeviceIoControl(diskHandle, FSCTL_UNLOCK_VOLUME, null, 0, null, 0, out intOut, IntPtr.Zero);
            if (success)
            {
                Console.WriteLine(deviceId + " " + Marshal.GetHRForLastWin32Error().ToString() + ": unlocked.");
            }
            else
            {
                Console.WriteLine(deviceId + " " + Marshal.GetHRForLastWin32Error().ToString() + ": unlock error: " + Marshal.GetHRForLastWin32Error().ToString());
            }

            success = CloseHandle(diskHandle);
            if (success)
            {
                Console.WriteLine(deviceId + " " + Marshal.GetHRForLastWin32Error().ToString() + ": handle closed.");
            }
            else
            {
                Console.WriteLine(deviceId + " " + Marshal.GetHRForLastWin32Error().ToString() + ": close handle error: " + Marshal.GetHRForLastWin32Error().ToString());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            wipeDisk();
            MessageBox.Show("اتمام عملیات پاک سازی");
        }

        private void comboBox1_Click(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();
            foreach (DriveInfo drive in DriveInfo.GetDrives())
            {
                if (drive.DriveType == DriveType.Removable)
                {
                    comboBox1.Items.Add(drive.Name);
                }
            }
            if (comboBox1.Items.Count <= 0)
                button2.Enabled = false;
            else
            {
                button2.Enabled = true;
                comboBox1.SelectedIndex = 0;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1_Click(null, null);

        }
    }
}
于 2020-06-29T05:44:53.263 回答