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

时间:2023-03-08 23:01:42
VC++信息安全编程(13)Windows2000/xp/vista/7磁盘扇区读写技术

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

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

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

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