Leaked source code of windows server 2003
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.

229 lines
6.4 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. JavaVM.cpp
  5. Abstract:
  6. Prevent the installation of cab files via rundll32 so that older versions
  7. of JavaVM do not install non-compatible software.
  8. Notes:
  9. This is an app specific shim.
  10. History:
  11. 05/24/2001 mnikkel Created
  12. --*/
  13. #include "precomp.h"
  14. IMPLEMENT_SHIM_BEGIN(JavaVM)
  15. #include "ShimHookMacro.h"
  16. APIHOOK_ENUM_BEGIN
  17. APIHOOK_ENUM_ENTRY(RegSetValueExW)
  18. APIHOOK_ENUM_ENTRY(CreateProcessA)
  19. APIHOOK_ENUM_END
  20. /*++
  21. Check Value for rundll32 JavaPkgMgr_Install string.
  22. Typical string we are looking to stop:
  23. "rundll32 E:\WINDOWS\System32\msjava.dll,JavaPkgMgr_Install E:\WINDOWS\Java\classes\xmldso.cab,0,0,0,0,4,282"
  24. --*/
  25. BOOL
  26. JavaPkgMgrInstallCheck( const CString & csInput)
  27. {
  28. DPFN( eDbgLevelSpew, "[JavaPkgMgrInstallCheck] input value:\n(%S)\n", csInput.Get() );
  29. CSTRING_TRY
  30. {
  31. CStringToken csValue(csInput, L",");
  32. CString csToken;
  33. // get the first token
  34. if ( csValue.GetToken(csToken) )
  35. {
  36. if ( csToken.Find(L"rundll32 ") > -1 )
  37. {
  38. // Second token
  39. if ( csValue.GetToken(csToken) )
  40. {
  41. if ( csToken.Find(L"JavaPkgMgr_Install ") > -1 )
  42. {
  43. // Third token
  44. if ( csValue.GetToken(csToken) )
  45. {
  46. if ( csToken.Find(L"0") == 0 )
  47. {
  48. DPFN( eDbgLevelInfo, "[JavaPkgMgrInstallCheck] Match found, returning TRUE.\n" );
  49. return TRUE;
  50. }
  51. }
  52. }
  53. }
  54. }
  55. }
  56. }
  57. CSTRING_CATCH
  58. {
  59. // Do Nothing
  60. }
  61. return FALSE;
  62. }
  63. /*++
  64. Check RegSetValueExW for JavaPkgMgr_Install of cabs. If
  65. found, return successfully without setting value.
  66. --*/
  67. LONG
  68. APIHOOK(RegSetValueExW)(
  69. HKEY hKey,
  70. LPWSTR lpValueName,
  71. DWORD Reserved,
  72. DWORD dwType,
  73. CONST BYTE * lpData,
  74. DWORD cbData
  75. )
  76. {
  77. DPFN( eDbgLevelSpew, "[RegSetValueExW] dwType:(%d)\n", dwType );
  78. // Check to see if we are dealing with a string value.
  79. if (dwType == REG_SZ ||
  80. dwType == REG_EXPAND_SZ )
  81. {
  82. // Convert to unicode and add null terminator.
  83. CSTRING_TRY
  84. {
  85. CString csDest;
  86. int nWChars = cbData/2;
  87. WCHAR * lpszDestBuffer = csDest.GetBuffer(nWChars);
  88. memcpy(lpszDestBuffer, lpData, cbData);
  89. lpszDestBuffer[nWChars] = '\0';
  90. csDest.ReleaseBuffer(nWChars);
  91. DPFN( eDbgLevelSpew, "[RegSetValueExW] lpdata:(%S)\n", csDest.Get() );
  92. if ( JavaPkgMgrInstallCheck(csDest) )
  93. return ERROR_SUCCESS;
  94. }
  95. CSTRING_CATCH
  96. {
  97. // Do Nothing
  98. }
  99. }
  100. //
  101. // Call the original API
  102. //
  103. return ORIGINAL_API(RegSetValueExW)(
  104. hKey,
  105. lpValueName,
  106. Reserved,
  107. dwType,
  108. lpData,
  109. cbData);
  110. }
  111. /*++
  112. Check CreateProcessA for JavaPkgMgr_Install of cabs. If
  113. found, return successfully without running.
  114. --*/
  115. BOOL
  116. APIHOOK(CreateProcessA)(
  117. LPCSTR lpApplicationName,
  118. LPSTR lpCommandLine,
  119. LPSECURITY_ATTRIBUTES lpProcessAttributes,
  120. LPSECURITY_ATTRIBUTES lpThreadAttributes,
  121. BOOL bInheritHandles,
  122. DWORD dwCreationFlags,
  123. LPVOID lpEnvironment,
  124. LPCSTR lpCurrentDirectory,
  125. LPSTARTUPINFOA lpStartupInfo,
  126. LPPROCESS_INFORMATION lpProcessInformation
  127. )
  128. {
  129. DPFN( eDbgLevelSpew, "[CreateProcessA] appname:(%s)\ncommandline:(%s)\n", lpApplicationName, lpCommandLine );
  130. if (lpCommandLine)
  131. {
  132. CSTRING_TRY
  133. {
  134. CString csCL(lpCommandLine);
  135. if ( JavaPkgMgrInstallCheck(csCL) )
  136. {
  137. // find the rundll32 and truncate the commandline at that point
  138. int nLoc = csCL.Find(L"rundll32 ");
  139. if (nLoc > -1)
  140. {
  141. csCL.Truncate(nLoc+8);
  142. return ORIGINAL_API(CreateProcessA)(lpApplicationName,
  143. csCL.GetAnsi(),
  144. lpProcessAttributes,
  145. lpThreadAttributes,
  146. bInheritHandles,
  147. dwCreationFlags,
  148. lpEnvironment,
  149. lpCurrentDirectory,
  150. lpStartupInfo,
  151. lpProcessInformation);
  152. }
  153. }
  154. }
  155. CSTRING_CATCH
  156. {
  157. // Do Nothing
  158. }
  159. }
  160. //
  161. // Call the original API
  162. //
  163. return ORIGINAL_API(CreateProcessA)(lpApplicationName,
  164. lpCommandLine,
  165. lpProcessAttributes,
  166. lpThreadAttributes,
  167. bInheritHandles,
  168. dwCreationFlags,
  169. lpEnvironment,
  170. lpCurrentDirectory,
  171. lpStartupInfo,
  172. lpProcessInformation);
  173. }
  174. /*++
  175. Register hooked functions
  176. --*/
  177. HOOK_BEGIN
  178. APIHOOK_ENTRY(ADVAPI32.DLL, RegSetValueExW)
  179. APIHOOK_ENTRY(KERNEL32.DLL, CreateProcessA)
  180. HOOK_END
  181. IMPLEMENT_SHIM_END