有些时候,我们读取磁盘文件,会被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. }

VC++信息安全编程(13)Windows2000/xp/vista/7磁盘扇区读写技术的更多相关文章

  1. How To: Samba4 AD PDC + Windows XP, Vista and 7

    dnsmasq If you've been struggling with Samba3 domain controllers and NT4 style domains working with ...

  2. Linux Versus Windows, Ubuntu/Mint V XP/Vista/7

    原文:http://petermoulding.com/linux_versus_windows_ubuntu_mint_v_xp_vista_7 Linux Versus Windows, Ubun ...

  3. 并发编程 13—— 线程池的使用 之 配置ThreadPoolExecutor 和 饱和策略

    Java并发编程实践 目录 并发编程 01—— ThreadLocal 并发编程 02—— ConcurrentHashMap 并发编程 03—— 阻塞队列和生产者-消费者模式 并发编程 04—— 闭 ...

  4. Vc数据库编程基础MySql数据库的表查询功能

    Vc数据库编程基础MySql数据库的表查询功能 一丶简介 不管是任何数据库.都会有查询功能.而且是很重要的功能.上一讲知识简单的讲解了表的查询所有. 那么这次我们需要掌握的则是. 1.使用select ...

  5. Vc数据库编程基础MySql数据库的表增删改查数据

    Vc数据库编程基础MySql数据库的表增删改查数据 一丶表操作命令 1.查看表中所有数据 select * from 表名 2.为表中所有的字段添加数据 insert into 表名( 字段1,字段2 ...

  6. Vc数据库编程基础MySql数据库的常见库命令.跟表操作命令

    Vc数据库编程基础MySql数据库的常见操作 一丶数据库常见的库操作 1.1查看全部数据库 命令:  show databases 1.2 创建数据库 命令: Create database 数据库名 ...

  7. Vc数据库编程基础1

    Vc数据库编程基础1 一丶数据库 什么是数据库 数据库简单连接就是存储数据的容器. 而库则是一组容器合成的东西. 也就是存储数据的.我们编程中常常会用到数据库. 什么是数据管理系统 数据库管理系统就是 ...

  8. 重要:VC DLL编程

    VC DLL编程 静态链接:每个应用程序使用函数库,必须拥有一份库的备份.多个应用程序运行时,内存中就有多份函数库代码的备份. 动态连接库:多个应用程序可以共享一份函数库的备份. DLL的调用方式:即 ...

  9. VC/MFC 编程技巧大总结

    1 toolbar默认位图左上角那个点的颜色是透明色,不喜欢的话可以自己改. 2 VC++中 WM_QUERYENDSESSION WM_ENDSESSION 为系统关机消息. 3 Java学习书推荐 ...

随机推荐

  1. javascript中的BOM对象

    1.window对象 所有的浏览器都支持window对象 概念上讲,一个html文档对应一个window对象 功能上讲,控制浏览器窗口 使用上讲,window对象不需要创建对象,直接使用 2.wind ...

  2. Ubuntu14.04 命令行下安装teamviewer

    下载teamviewer 链接:https://pan.baidu.com/s/1hs0BppM  密码:sdmk 上传到 /home/[user] cd /home/[user] 移动安装包到 /o ...

  3. Codeforces 250 E. The Child and Binary Tree [多项式开根 生成函数]

    CF Round250 E. The Child and Binary Tree 题意:n种权值集合C, 求点权值和为1...m的二叉树的个数, 形态不同的二叉树不同. 也就是说:不带标号,孩子有序 ...

  4. SPOJ DIVCNT2 [我也不知道是什么分类了反正是数论]

    SPOJ DIVCNT2 - Counting Divisors (square) 题意:求 \[ \sum_{i=1}^n\sigma_0(i^2) \] 好棒啊! 带着平方没法做,考虑用其他函数表 ...

  5. u-boot核心初始化

    异常向量表:异常:因为内部或者外部的一些事件,导致处理器停下正在处理的工作,转而去处理这些发生的事件.ARM Architecture Reference Manual p54页.7种异常的类型:Re ...

  6. jQuery中animate()方法用法实例

    本文实例讲述了jQuery中animate()方法用法.分享给大家供大家参考.具体分析如下: 此方法用于创建自定义动画,并且能够规定动画执行时长.擦除效果.动画完成后还可以地触发一个回调函数. ani ...

  7. Godep的基本使用

    [http://studygolang.com/articles/4385] 关于Godep 发现好多golang项目都使用到godep作为包管理的工具,像比较大型的项目,比如kubernetes这种 ...

  8. Zabbix Agent active主动模式监控

    zabbix_server端当主机数量过多的时候,由Server端去收集数据,Zabbix会出现严重的性能问题,主要表现如下: 1.当被监控端到达一个量级的时候,Web操作很卡,容易出现502 2.图 ...

  9. MySQL统计函数记录——按月、按季度、按日、时间段统计

    按年汇总,统计:select sum(mymoney) as totalmoney, count(*) as sheets from mytable group by date_format(col, ...

  10. 插入排序实现&&选择排序实现

    萌新刚刚开始学习算法,第一步是学习排序,毕竟算法的四大块"排序,查找,图,字符串"里面,排序是第一位的(PS:今天才知道算法提供的只是一个程序编写思路,一直以为是一个函数,难怪传入 ...