VC++信息安全编程 Windows2000/xp/vista/7磁盘扇区读写技术

本文介绍了一个用于磁盘扇区数据读取、写入、备份及恢复的工具,包括扇区数据的擦除和扇区数据的备份与恢复功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 有些时候,我们读取磁盘文件,会被hook.我们读到的可能并非实际的文件。

    我们直接读取磁盘扇区获取数据。

    实现磁盘数据的读写,不依赖WindowsAPI。

     

  2. void CSectorEdit2000Dlg::OnView()   
  3. {  
  4.     UpdateData(TRUE);  
  5.     if (m_uTo < m_uFrom)  
  6.         return;  
  7.       
  8.     char cTemp[1];  
  9.     memcpy(cTemp, m_DrvListBoxSResult.Left(1), 1);  
  10.     UINT uDiskID = cTemp[0] - 64;  
  11.   
  12.     DWORD dwSectorNum = m_uTo - m_uFrom + 1;  
  13.     if (dwSectorNum > 100)  
  14.         return;  
  15.   
  16.     unsigned char* bBuf = new unsigned char[dwSectorNum * 512];  
  17.       
  18.     if (ReadSectors(uDiskID, m_uFrom, (UINT)dwSectorNum, bBuf) == FALSE)  
  19.     {  
  20.         MessageBox("所选磁盘分区不存在!""错误", MB_OK | MB_ICONERROR);  
  21.         return;  
  22.     }  
  23.       
  24.     char* cBuf = new char[dwSectorNum * 5120];  
  25.     memset(cBuf, 0, sizeof(cBuf));  
  26.   
  27.     for (DWORD i = 0; i < dwSectorNum * 512; i++)  
  28.     {  
  29.         sprintf(cBuf, "%s%02X ", cBuf, bBuf[i]);  
  30.   
  31.         if ((i % 512) == 511)  
  32.             sprintf(cBuf, "%s\r\n第%d扇区\r\n", cBuf, (int)(i / 512) + m_uFrom);  
  33.         if ((i % 16) == 15)  
  34.             sprintf(cBuf, "%s\r\n", cBuf);  
  35.         else if ((i % 16) == 7)  
  36.             sprintf(cBuf, "%s- ", cBuf);  
  37.     }  
  38.     SetDlgItemText(IDC_DATA, cBuf);  
  39.     delete[] bBuf;  
  40.     delete[] cBuf;  
  41. }  
  42.   
  43. void CSectorEdit2000Dlg::OnCleardata()   
  44. {  
  45.     UpdateData(TRUE);  
  46.   
  47.     char cTemp[1];  
  48.     memcpy(cTemp, m_DrvListBoxSResult.Left(1), 1);  
  49.     UINT uDiskID = cTemp[0] - 64;  
  50.     if (uDiskID > 2)  
  51.     {  
  52.         if (MessageBox("要清理的是硬盘分区,请确认是否继续?""提示", MB_YESNO | MB_ICONWARNING) != 6)  
  53.             return;  
  54.         if (uDiskID == 3)  
  55.         {  
  56.             if (MessageBox("要清理的是系统分区,请再次确认是否继续?""提示", MB_YESNO | MB_ICONWARNING) != 6)  
  57.                 return;  
  58.         }  
  59.     }  
  60.       
  61.     unsigned char bBuf[512];  
  62.   
  63.     UINT i = 0;  
  64.     BOOL bRet = TRUE;  
  65.     while (m_bAllDisk)        
  66.     {  
  67.         memset(bBuf, 0xFF, sizeof(bBuf));  
  68.         bRet = WriteSectors(uDiskID, i, 1, bBuf);  
  69.         memset(bBuf, 0, sizeof(bBuf));  
  70.         bRet = WriteSectors(uDiskID, i, 1, bBuf);  
  71.           
  72.         if (bRet == FALSE)  
  73.         {  
  74.             if (i == 0)  
  75.                 MessageBox("所选磁盘分区不存在!""错误", MB_OK | MB_ICONERROR);  
  76.             else  
  77.                 MessageBox("磁盘数据擦除完毕!""错误", MB_OK | MB_ICONERROR);  
  78.             return;  
  79.         }  
  80.         i++;  
  81.     }     
  82.   
  83.     if (m_bAllDisk == FALSE)  
  84.     {  
  85.         for (DWORD i = m_uFrom; i <= m_uTo; i++)  
  86.         {  
  87.             memset(bBuf, 0xFF, sizeof(bBuf));  
  88.             bRet = WriteSectors(uDiskID, i, 1, bBuf);  
  89.             memset(bBuf, 0, sizeof(bBuf));  
  90.             bRet = WriteSectors(uDiskID, i, 1, bBuf);  
  91.             if (bRet == FALSE)  
  92.             {  
  93.                 if (i == 0)  
  94.                     MessageBox("所选磁盘分区不存在!""错误", MB_OK | MB_ICONERROR);  
  95.                 else  
  96.                     MessageBox("磁盘数据擦除完毕!""提示", MB_OK | MB_ICONINFORMATION);  
  97.                 return;  
  98.             }  
  99.         }  
  100.     }  
  101. }  
  102.   
  103.   
  104. void CSectorEdit2000Dlg::OnBackup()   
  105. {  
  106.     UpdateData(TRUE);  
  107.     if (m_uTo < m_uFrom)  
  108.         return;  
  109.   
  110.     CFileDialog fileDlg(FALSE, "*.sec""*.sec", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "磁盘扇区数据(*.sec)|*.sec||", NULL);  
  111.     CFile file;  
  112.     if (fileDlg.DoModal() != IDOK)  
  113.         return;  
  114.       
  115.     file.Open(fileDlg.GetPathName(), CFile::modeCreate | CFile::modeReadWrite);  
  116.     char cTemp[1];  
  117.     memcpy(cTemp, m_DrvListBoxSResult.Left(1), 1);  
  118.     UINT uDiskID = cTemp[0] - 64;  
  119.   
  120.     DWORD dwSectorNum = m_uTo - m_uFrom + 1;  
  121.     unsigned char* bBuf = new unsigned char[dwSectorNum * 512];  
  122.       
  123.     if (ReadSectors(uDiskID, m_uFrom, (UINT)dwSectorNum, bBuf) == FALSE)  
  124.     {  
  125.         MessageBox("所选磁盘分区不存在!""错误", MB_OK | MB_ICONERROR);  
  126.         return;  
  127.     }  
  128.   
  129.     file.Write(bBuf, dwSectorNum * 512);  
  130.     file.Close();  
  131.   
  132.     delete[] bBuf;  
  133.   
  134.     MessageBox("数据备份完毕!""提示", MB_OK | MB_ICONINFORMATION);  
  135. }  
  136.   
  137. void CSectorEdit2000Dlg::OnRestore()   
  138. {  
  139.     UpdateData(TRUE);  
  140.       
  141.     char cTemp[1];  
  142.     memcpy(cTemp, m_DrvListBoxSResult.Left(1), 1);  
  143.     UINT uDiskID = cTemp[0] - 64;  
  144.   
  145.     CFileDialog fileDlg(TRUE, "*.sec""*.sec", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "磁盘扇区数据(*.sec)|*.sec||", NULL);  
  146.     CFile file;  
  147.     if (fileDlg.DoModal() != IDOK)  
  148.         return;  
  149.   
  150.     file.Open(fileDlg.GetPathName(), CFile::modeReadWrite);  
  151.     DWORD dwSectorNum = file.GetLength();  
  152.     if (dwSectorNum % 512 != 0)  
  153.         return;  
  154.     dwSectorNum /= 512;  
  155.   
  156.     unsigned char* bBuf = new unsigned char[dwSectorNum * 512];  
  157.     file.Read(bBuf, dwSectorNum * 512);  
  158.   
  159.     if (WriteSectors(uDiskID, m_uFrom, (UINT)dwSectorNum, bBuf) == FALSE)  
  160.     {  
  161.         MessageBox("所选磁盘分区不存在!""错误", MB_OK | MB_ICONERROR);  
  162.         return;  
  163.     }  
  164.   
  165.     file.Close();  
  166.     delete[] bBuf;  
  167.   
  168.     MessageBox("数据恢复完毕!""提示", MB_OK | MB_ICONINFORMATION);  
  169. }  
  170.   
  171. BOOL CSectorEdit2000Dlg::WriteSectors(BYTE bDrive, DWORD dwStartSector, WORD wSectors, LPBYTE lpSectBuff)  
  172. {  
  173.     if (bDrive == 0)  
  174.         return 0;  
  175.   
  176.     char devName[] = "\\\\.\\A:";  
  177.     devName[4] ='A' + bDrive - 1;  
  178.     HANDLE hDev;  
  179.     if(m_bPhysicalDisk==false)  
  180.     {  
  181.         hDev = CreateFile(devName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);  
  182.     }  
  183.     else  
  184.         hDev = CreateFile("\\\\.\\PhysicalDrive0", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);  
  185.   
  186.   
  187.     if (hDev == INVALID_HANDLE_VALUE)  
  188.         return 0;  
  189.   
  190.     SetFilePointer(hDev, 512 * dwStartSector, 0, FILE_BEGIN);  
  191.   
  192.     DWORD dwCB;  
  193.     BOOL bRet = WriteFile(hDev, lpSectBuff, 512 * wSectors, &dwCB, NULL);  
  194.     CloseHandle(hDev);  
  195.     return bRet;  
  196. }  
  197.   
  198. BOOL CSectorEdit2000Dlg::ReadSectors(BYTE bDrive, DWORD dwStartSector, WORD wSectors, LPBYTE lpSectBuff)  
  199. {  
  200.     if (bDrive == 0)  
  201.         return 0;  
  202.   
  203.     char devName[] = "\\\\.\\A:";  
  204.     devName[4] ='A' + bDrive - 1;  
  205.     HANDLE hDev;  
  206.     if(m_bPhysicalDisk==false)  
  207.         hDev = CreateFile(devName, GENERIC_READ, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);  
  208.     else  
  209.         hDev = CreateFile("\\\\.\\PhysicalDrive0", GENERIC_READ, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);  
  210.     if (hDev == INVALID_HANDLE_VALUE)  
  211.         return 0;  
  212.   
  213.     SetFilePointer(hDev, 512 * dwStartSector, 0, FILE_BEGIN);  
  214.   
  215.     DWORD dwCB;  
  216.     BOOL bRet = ReadFile(hDev, lpSectBuff, 512 * wSectors, &dwCB, NULL);  
  217.     CloseHandle(hDev);  
  218.     return bRet;  
  219. }  
  220.   
  221. void CSectorEdit2000Dlg::OnSelchangeComboDrive()   
  222. {  
  223.     // TODO: Add your control notification handler code here   
  224.     int s;  
  225.   
  226.     s = m_DrvListBox.GetCurSel();  
  227.     if( s != CB_ERR )  
  228.         m_DrvListBoxSResult = ( const char * )m_DrvListBox.GetItemDataPtr( m_DrvListBox.GetCurSel());  
  229.   
  230. }  
  231.   
  232. void CSectorEdit2000Dlg::OnCheck()   
  233. {  
  234.     // TODO: Add your control notification handler code here   
  235.     m_bPhysicalDisk=!m_bPhysicalDisk;  
  236.     if(m_bPhysicalDisk==true)  
  237.     {  
  238.         GetDlgItem( IDC_COMBO_DRIVE)->EnableWindow( false );  
  239.     }  
  240.     if(m_bPhysicalDisk==false)  
  241.     {  
  242.         GetDlgItem( IDC_COMBO_DRIVE)->EnableWindow( true );  
  243.     }     
  244. }  

转载于:https://www.cnblogs.com/software8/archive/2011/12/17/2290881.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值