有些时候,我们读取磁盘文件,会被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. BZOJ 2734: [HNOI2012]集合选数 [DP 状压 转化]

    传送门 题意:对于任意一个正整数 n≤100000,如何求出{1, 2,..., n} 的满足若 x 在该子集中,则 2x 和 3x 不能在该子集中的子集的个数(只需输出对 1,000,000,001 ...

  2. client / page / offset / screen X / Y

    1.clientX / clientY 相对于可视窗口左上角,不包括菜单栏与滚动条 2.pageX / pageY 相对于网页左上角,不包括菜单栏,包括滚动条 3.offsetX / offsetY ...

  3. 汽车之家店铺数据抓取 DotnetSpider实战[一]

    一.背景 春节也不能闲着,一直想学一下爬虫怎么玩,网上搜了一大堆,大多都是Python的,大家也比较活跃,文章也比较多,找了一圈,发现园子里面有个大神开发了一个DotNetSpider的开源库,很值得 ...

  4. IntelliJ IDEA环境配置

    [pojie](https://www.cnblogs.com/suiyueqiannian/p/6754091.html) Error running Tomcat8: Address localh ...

  5. Redmine基础: 邮件配置

    1.用文本编辑器打开 D:\Bitnami\redmine-2.6.5-0\apps\redmine\htdocs\config\configuration.yml 文件,找到以下内容: 2.配置邮件 ...

  6. PHP Extension开发(Zephir版本)

    上篇介绍了C语言开发PHP扩展的方法, 现在介绍使用Zephir开发扩展的方法. 关于Zephir需要简单介绍一下: Zephir 是为PHP开发人员提供的能够编写可编译/静态类型的高级语言.是优秀的 ...

  7. ansible安装

    本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn 1.配置epel源 wget -O /etc/yum.repos.d ...

  8. 【Unity3D技术文档翻译】第1.8篇 AssetBundles 问题及解决方法

    上一章:[Unity3D技术文档翻译]第1.7篇 AssetBundles 补丁更新 本章原文所在章节:[Unity Manual]→[Working in Unity]→[Advanced Deve ...

  9. Django开发基础----创建项目/应用

    环境: 1.python  3.6.2 2.安装django:pip install django==1.10.3 *下面以开发一个简单的用户签到系统介绍Django的使用 创建Django项目: 命 ...

  10. 微信小程序(一)

    开发流程 注册微信小程序并申请微信支付-->制作小程序-->上传并提交审核-->审核通过,小程序上线   开发微信小程序需要准备 企业公众号(被认证)以及申请小程序.微信开发技术.S ...