Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
3.4 KiB

  1. #include <stdio.h>
  2. #include "wudetect.h"
  3. // a small utility to convert hexadecimal digits to numeric values in dec
  4. static inline int hexa( TCHAR c )
  5. {
  6. if( c >= '0' && c <='9' )
  7. {
  8. return (c - '0');
  9. }
  10. else if( c >= 'a' && c <= 'f' )
  11. {
  12. return (10 + (c - 'a') );
  13. }
  14. else if( c >= 'A' && c <= 'F' )
  15. {
  16. return (10 + (c - 'A') );
  17. }
  18. return -1;
  19. }
  20. static void StringToBin( LPTSTR lpData, DWORD& nSize )
  21. {
  22. nSize = 0; // we will reassign the value on size of binary buffer
  23. BYTE * lpBinaryData = (BYTE*)lpData;
  24. //_strlwr( lpData );
  25. while( *lpData != '\0' )
  26. {
  27. while( ' ' == *lpData ) lpData++;
  28. *lpBinaryData++ = (hexa( *lpData++ ) * 16) + hexa( *lpData++ );
  29. nSize++;
  30. }
  31. }
  32. /////////////////////////////////////////////////////////////////////////////
  33. // CExpressionParser::fDetectRegBinary
  34. // Detect a substring in registry datum.
  35. //
  36. // Form: E=RegSubstr,<SubStr>,<RootKey>,<KeyPath>,<RegValue>,<RegData>
  37. //
  38. // Comments :
  39. /////////////////////////////////////////////////////////////////////////////
  40. bool CExpressionParser::fDetectRegBinary(TCHAR * pszBuf)
  41. {
  42. const int MAX_DATA_SIZE = 2000;
  43. bool fSuccess = false;
  44. HKEY hKeyRoot;
  45. HKEY hKey;
  46. DWORD type;
  47. TCHAR szTargetKeyName[MAX_PATH];
  48. TCHAR szTargetKeyValue[MAX_DATA_SIZE];
  49. TCHAR szKeyMissingStatus[MAX_DATA_SIZE];
  50. TCHAR szData[MAX_DATA_SIZE];
  51. TCHAR szSubStr[MAX_DATA_SIZE];
  52. DWORD iToken = 0;
  53. // Get reg root type (HKLM, etc)
  54. if ( fMapRegRoot(pszBuf, ++iToken, &hKeyRoot) &&
  55. (GetStringField2(pszBuf, ++iToken, szTargetKeyName, sizeof(szTargetKeyName)/sizeof(TCHAR)) != 0) )
  56. {
  57. if ( RegOpenKeyEx( hKeyRoot,
  58. szTargetKeyName,
  59. 0,
  60. KEY_QUERY_VALUE,
  61. &hKey) == ERROR_SUCCESS )
  62. {
  63. if ( (GetStringField2(pszBuf, ++iToken, szTargetKeyValue, sizeof(szTargetKeyValue)/sizeof(TCHAR)) != 0) &&
  64. (GetStringField2(pszBuf, ++iToken, szSubStr, sizeof(szSubStr)/sizeof(TCHAR)) != 0) )
  65. {
  66. DWORD size = sizeof(szData);
  67. if ( RegQueryValueEx(hKey,
  68. szTargetKeyValue,
  69. 0,
  70. &type,
  71. (BYTE *)szData,
  72. &size) == ERROR_SUCCESS )
  73. {
  74. if ( type == REG_SZ )
  75. {
  76. _tcslwr(szData);
  77. // iterate thru the substrings looking for a match.
  78. //while ( GetStringField2(pszBuf, ++iToken, szSubStr, sizeof(szSubStr)) != 0 )
  79. {
  80. _tcslwr(szSubStr);
  81. if ( _tcsstr(szData, szSubStr) != NULL )
  82. {
  83. fSuccess = true;
  84. goto quit_while;
  85. }
  86. }
  87. }
  88. else if( REG_BINARY== type )
  89. {
  90. StringToBin( szSubStr, size );
  91. int nRes = memcmp( szData, szSubStr, size );
  92. //printf( "", nRes );
  93. if( (int)0 == nRes )
  94. {
  95. fSuccess = true;
  96. //goto quit_while;
  97. }
  98. //printf( "", nRes );
  99. }
  100. quit_while:;
  101. }
  102. else
  103. {
  104. // if we get an error, assume the key does not exist. Note that if
  105. // the status is DETFIELD_NOT_INSTALLED then we don't have to do
  106. // anything since that is the default status.
  107. if ( lstrcmpi(DETFIELD_INSTALLED, szKeyMissingStatus) == 0 )
  108. {
  109. fSuccess = true;
  110. }
  111. }
  112. }
  113. RegCloseKey(hKey);
  114. }
  115. }
  116. //cleanup:
  117. return fSuccess;
  118. }