计算机病毒揭秘与对抗--注册表操作


  1. // EnumRegValue.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <stdio.h>
  5. #include <tchar.h>
  6. #include <Windows.h>
  7. #include <string> //使用STL中的string对象需要的头文件
  8. //该函数用于将二进制数据转换成字符串,并返回一个string对象,它返回的是一个MBCS编码的字符串。
  9. std::string HexToString(PBYTE pByte, long lLength)
  10. {
  11. std::string strResult;
  12. const char cCharTable[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  13. int iPos=28;
  14. std::string strTemp;
  15. for(int i=0;i<lLength;i++)
  16. {
  17. iPos=28;
  18. strTemp.erase();
  19. while(!((pByte[i]>>iPos) & 0xf) && iPos>0)
  20. {
  21. iPos-=4;
  22. }
  23. for(int j=1;j<2-iPos/4;j++)
  24. {
  25. strTemp+=_T('0');
  26. }
  27. for(;iPos>=0;iPos-=4)
  28. {
  29. strTemp+=cCharTable[(pByte[i]>>iPos) & 0xf];
  30. }
  31. strResult+=strTemp;
  32. }
  33. return strResult;
  34. }
  35. int main(int argc, char* argv[])
  36. {
  37. HKEY hMainKey = HKEY_CURRENT_USER;
  38. HKEY hSubKey;
  39. //打开HKEY_CURRENT_USER\Control Panel\Desktop键句柄,以枚举其下面的子键值
  40. LONG ReturnValue = ::RegOpenKeyEx (hMainKey, _T("Control Panel\\Desktop"),
  41. 0L, KEY_ENUMERATE_SUB_KEYS|KEY_QUERY_VALUE, &hSubKey);
  42. DWORD dwSubKeyValueCount = 0, dwMaxValueLen = 0;
  43. if (ReturnValue == ERROR_SUCCESS) //如果成功打开
  44. {
  45. //查询HKEY_CURRENT_USER\Control Panel\Desktop键下子键值的个数以及子键值数据中所占内存最大的字节数。
  46. RegQueryInfoKey(hSubKey, NULL, NULL, NULL, NULL,
  47. NULL, NULL, &dwSubKeyValueCount, NULL, &dwMaxValueLen, NULL, NULL);
  48. TCHAR tcValueName[128] = {0}; //用于保存每个子键值的名字的内存空间
  49. DWORD dwValueNameSize = 128, dwValueType, dwValueSize;
  50. PBYTE pValueData = new BYTE[dwMaxValueLen]; //分配足够大的内存用于保存子键值数据
  51. printf(_T("%-25s%-20s%-20s"), _T("键值名称"), _T("键值类型"), _T("键值数据"));
  52. printf(_T("\n"));
  53. for (int i=0; i<dwSubKeyValueCount; i++)
  54. {
  55. dwValueSize = dwMaxValueLen;
  56. memset(pValueData, 0, dwMaxValueLen);
  57. RegEnumValue(hSubKey, i, tcValueName, &dwValueNameSize,
  58. NULL, &dwValueType, pValueData, &dwValueSize);
  59. printf("%-25s", tcValueName);
  60. dwValueNameSize=128;
  61. switch (dwValueType) //判断子键值的类型
  62. {
  63. case REG_SZ: //字符串
  64. {
  65. printf(_T("%-20s"), _T("REG_SZ"));
  66. printf(_T("%-20s"), pValueData);
  67. }
  68. break;
  69. case REG_DWORD: //DWORD双字类型
  70. {
  71. printf(_T("%-20s"), _T("REG_DWORD"));
  72. printf(_T("%-20x"), *(DWORD *)pValueData);
  73. }
  74. break;
  75. case REG_BINARY: //二进制类型
  76. {
  77. printf(_T("%-20s"), _T("REG_BINARY"));
  78. //将二进制数据转换成字符串以便输出到屏幕.
  79. printf(_T("%-20s"),HexToString(pValueData,dwValueSize).c_str());
  80. }
  81. break;
  82. }
  83. printf(_T("\n"));
  84. }
  85. if(pValueData != NULL)
  86. {
  87. delete []pValueData; //释放申请的资源
  88. }
  89. }
  90. return 0;
  91. }






posted @ 2016-03-20 19:22  hungryvampire  阅读(344)  评论(0编辑  收藏  举报