VC++信息安全编程(13)Windows2000/xp/vista/7磁盘扇区读写技术 分类: 磁盘的扇区读写 VC++ 2015-04-29 10:38 357人阅读 评论(0) 收藏

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

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

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

 

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/mao0504/p/4706433.html