找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 5137|回复: 0

[原]VC++信息安全编程(18)Windows2000/xp/vista/7磁盘扇区读写

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

您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-4-28 00:20 , Processed in 0.012240 second(s), 7 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表