如何在Windows中擦除硬盘的一个扇区

如何在Windows中擦除硬盘的一个扇区

我该如何从 Windows 中擦除硬盘上的特定扇区?我指的是正在使用的硬盘...

要么用零擦除,要么用随机数据替换。是否有任何脚本或程序可以在 Windows 启动时可靠地执行此操作?我正在运行 Windows 7 64 位,我想要擦除的扇区是扇区 62。Winhex 可以吗?

答案1

我为你写了一个:

#include <stdio.h>
#include <limits.h>
#include <wchar.h>
#include <io.h>
#include <tchar.h>
#include <Windows.h>

#define _O_U16TEXT 0x20000

static PVOID DisableWow64FsRedirection()
{
    HMODULE hKernel32 = LoadLibrary(_T("kernel32.dll"));
    __try
    {
        BOOL (WINAPI * pIsWow64Process)(HANDLE, PBOOL) = (BOOL (WINAPI *)(HANDLE, PBOOL))GetProcAddress(hKernel32, "IsWow64Process");
        if (pIsWow64Process != NULL)
        {
            BOOL isWow64;
            if ((*pIsWow64Process)(GetCurrentProcess(), &isWow64) && isWow64)
            {
                PVOID old;
                return ((BOOL (WINAPI*)(PVOID*))GetProcAddress(hKernel32, "Wow64DisableWow64FsRedirection"))(&old) ? old : NULL;
            }
        }
    }
    __finally { FreeLibrary(hKernel32); }
    return NULL;
}

int _tmain(int argc, TCHAR *argv[])
{
    _setmode(_fileno(stdout), _O_U16TEXT);
    DisableWow64FsRedirection();
    if (argc == 1 + 3)
    {
        long long offset, length;
        int a = _stscanf(argv[2], _T("%lld"), &offset);
        int b = _stscanf(argv[3], _T("%lld"), &length);
        if (a == 1 && b == 1)
        {
            HANDLE hFile = CreateFile(argv[1], FILE_READ_ATTRIBUTES | FILE_WRITE_DATA, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, NULL);
            if (hFile != NULL && hFile != INVALID_HANDLE_VALUE)
            {
                __try
                {
                    PBYTE pMem = (PBYTE)calloc((size_t)length, sizeof(BYTE));
                    //Assume we had enough memory...
                    __try
                    {
                        DWORD nw;
                        OVERLAPPED ov = {0};
                        ov.Offset = (DWORD)(offset & ULONG_MAX);
                        ov.OffsetHigh = (DWORD)((offset >> (CHAR_BIT * sizeof(ov.Offset))) & ULONG_MAX);
                        if (WriteFile(hFile, pMem, (DWORD)length, &nw, &ov))
                        {
                            SetLastError(ERROR_SUCCESS);
                        }
                        else { _ftprintf(stderr, _T("Error writing data: %d"), GetLastError()); }
                    }
                    __finally
                    {
                        CloseHandle(pMem);
                    }
                }
                __finally { CloseHandle(hFile); }
            }
            else { _ftprintf(stderr, _T("Error opening file: %d"), GetLastError()); }
        }
        else
        {
            SetLastError(ERROR_INVALID_COMMAND_LINE);
            _ftprintf(stderr, _T("Invalid start or length value: %s, %s."), argv[2], argv[3]);
        }
    }
    else
    {
        SetLastError(ERROR_INVALID_COMMAND_LINE);
        _tprintf(_T("Usage:\n  wipe <file> <start> <length>"));
    }
    return GetLastError();
}

这是编译后的版本(base64 编码;解码这里):

TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA4AAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5vdCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAABaiq8kHuvBdx7rwXce68F33eTOdx/rwXfd5J53E+vBdx7rwHc468F33eScdxvrwXfd5KJ3H+vBd93km3cf68F3UmljaB7rwXcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABQRQAATAECADafBU4AAAAAAAAAAOAADwELAQcKAAoAAAACAAAAAAAAqRQAAAAQAAAAIAAAAABAAAAQAAAAAgAABAAAAAAAAAAEAAAAAAAAAAAwAAAABAAAAAAAAAMAAAAAABAAABAAAAAAEAAAEAAAAAAAABAAAAAAAAAAAAAAAKwWAAA8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACgSAABAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==

你可以像这样运行它:

wipe \\.\PhysicalDrive0 0 512

其中第一个参数是文件名,第二个参数是偏移量,第三个参数是字节数。字节数可能不会超过 ~63 MiB。

当然,如果您不小心删除了磁盘,请不要怪我……

相关内容