MBR表获取与解析

来源:互联网 发布:软件批量卸载 编辑:程序博客网 时间:2024/06/03 15:23
#include<iostream>#include<windows.h>using namespace std;#define BOOTRECORDSIZE 440typedef struct _BOOTRECORD {unsigned char BootRecord[BOOTRECORDSIZE];}BOOTRECORD,*PBOOTRECORD;#define DPTSIZE 64typedef struct _DPT {unsigned char Dpt[DPTSIZE];}DPT,*PDPT;typedef struct _MBR {BOOTRECORD BootRecord;              //引导程序unsigned char ulSinged[4];          //Windows 磁盘签名unsigned char sReserve[2];          //保留位DPT Dpt;                            //分区表unsigned char EndSign[2];           //结束标志}MBR,*PMBR;#define DPTNUMBER 4typedef struct _DP {unsigned char BootSign;           //引导标志unsigned char StartHsc[3];unsigned char Partitiontype;        //分区类型unsigned char EndHsc[3];unsigned int SectorsPreceding;    //本分区之前使用的扇区数unsigned int SectorInPartition;}DP,*PDP;unsigned char FixMbr[16] = { 0x00, 0xFE,0xFF ,0xFF ,0x07 ,0xFE, 0xFF ,0xFF ,0x00, 0xA8 ,0x58 ,0x01 ,0x00, 0x48 ,0x27 ,0x01 };VOID ShowMbr(HANDLE hDevice, PMBR pMbr) {DWORD dwRead = 0;//ReadFile(hDevice, (LPVOID)pMbr, sizeof(MBR), &dwRead, NULL);if (ReadFile(hDevice, (LPVOID)pMbr, sizeof(MBR), &dwRead, NULL)) {printf("ReadFile successful  \r\n");}for (int i = 0; i < 512; i++) {printf("%02X ",((BYTE*)pMbr)[i]);if ((i + 1) % 16 == 0) {printf("\r\n");}}}// 解析MBRVOID ParseMbr(MBR Mbr) {printf("引导记录: \r\n");for (int i = 0; i < BOOTRECORDSIZE; i++) {printf("%02X ",Mbr.BootRecord.BootRecord[i]);if ((i + 1) % 16 == 0) {printf("\r\n");}}printf("\r\n");printf("磁盘分区表: \r\n");for (int i = 0; i < DPTSIZE; i++) {printf("%02X ", Mbr.Dpt.Dpt[i]);if ((i + 1) % 16 == 0) {printf("\r\n");}}printf("\r\n");PDP pDp = (PDP)&(Mbr.Dpt.Dpt);for (int i = 0; i < DPTNUMBER; i++) {printf("引导标志: %02X ", pDp[i].BootSign);printf("分区类型: %02X",pDp[i].Partitiontype);printf("\r\n");printf("本分区之前扇区数: %d ",pDp[i].SectorInPartition);printf("本分区的扇区数: %d", pDp[i].SectorInPartition);printf("\r\n");printf("该分区大小: %f \r\n",(double)pDp[i].SectorInPartition /1024*512/1024/1024);printf("\r\n \r\n");}printf("结束标志: \r\n");for (int i = 0; i < 2; i++) {printf("%02X ",Mbr.EndSign[i]);}printf("\r\n");} int writeMBR() {//PDP pDp = (PDP)&(Mbr.Dpt.Dpt);//Mbr.Dpt.Dpt[2] = 0;int flag = 0;PMBR pMbr = { 0 };HANDLE hDevice = CreateFile("\\\\.\\PhysicalDrive0", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);if (hDevice == INVALID_HANDLE_VALUE) {printf("CreatrFile Error %d \r\n", GetLastError());return -1;}DWORD dwRead = 0;if (ReadFile(hDevice, (LPVOID)pMbr, sizeof(MBR), &dwRead, NULL)) {printf("ReadFile successful  \r\n");}DWORD dwBytesReturned;//BYTE lpMbr[512] = { Mbr };//pMbr->Dpt.Dpt[2] = { 0 };DeviceIoControl(hDevice, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwBytesReturned, NULL);//写入磁盘文件   //WriteFile(hDevice, (LPVOID)pMbr, 512, &dwBytesReturned, NULL);if (WriteFile(hDevice, (LPVOID)pMbr, sizeof(MBR), &dwBytesReturned, NULL)) {printf("kill mbr successful!!!");}else { printf("kill mbr error"); }DeviceIoControl(hDevice, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &dwBytesReturned, NULL);CloseHandle(hDevice);return flag;}int main() {int a;//打开物理设备HANDLE hDevice = CreateFile("\\\\.\\PhysicalDrive0", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);if (hDevice == INVALID_HANDLE_VALUE) {printf("CreatrFile Error %d \r\n",GetLastError());return -1;}MBR Mbr = { 0 };ShowMbr(hDevice, &Mbr);ParseMbr(Mbr);CloseHandle(hDevice);//writeMBR();system("pause");return 0;}

0 0