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.

269 lines
6.8 KiB

  1. /*++
  2. Copyright (c) 2002-2003 Microsoft Corporation
  3. Module Name:
  4. IEUnHarden.cpp
  5. Abstract:
  6. IESoftening modifications
  7. History:
  8. 01/15/2003 prashkud Created
  9. --*/
  10. #include "precomp.h"
  11. IMPLEMENT_SHIM_BEGIN(IEUnHarden)
  12. #include <windows.h>
  13. #include <urlmon.h>
  14. #include "ShimHookMacro.h"
  15. APIHOOK_ENUM_BEGIN
  16. APIHOOK_ENUM_END
  17. #define SUCCESS(val) ((val == ERROR_SUCCESS) ? TRUE : FALSE)
  18. IInternetSecurityManager *g_pISM = NULL;
  19. BOOL
  20. IEHardeningEnabled()
  21. {
  22. BOOL bRet = FALSE;
  23. HKEY hUserKey = 0;
  24. HKEY hAdminKey = 0;
  25. const WCHAR wszIEUserHardeningPath[]
  26. = L"SOFTWARE\\Microsoft\\Active Setup\\Installed Components\\{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}";
  27. const WCHAR wszIEAdminHardeningPath[]
  28. = L"SOFTWARE\\Microsoft\\Active Setup\\Installed Components\\{A509B1A8-37EF-4b3f-8CFC-4F3A74704073}";
  29. DWORD dwVal = 0;
  30. DWORD dwcbBuf = sizeof(dwVal);
  31. if (SUCCESS(RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszIEUserHardeningPath, 0, KEY_READ | KEY_WOW64_64KEY, &hUserKey)))
  32. {
  33. if (SUCCESS(RegQueryValueExW(hUserKey, L"IsInstalled", NULL, NULL, (LPBYTE)&dwVal, &dwcbBuf)))
  34. {
  35. if (dwVal == 1)
  36. {
  37. bRet = TRUE;
  38. }
  39. }
  40. }
  41. dwVal = 0;
  42. dwcbBuf = sizeof(dwVal);
  43. if (!bRet )
  44. {
  45. if (SUCCESS(RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszIEAdminHardeningPath, 0, KEY_READ | KEY_WOW64_64KEY, &hAdminKey)))
  46. {
  47. if (SUCCESS(RegQueryValueExW(hAdminKey, L"IsInstalled", NULL, NULL, (LPBYTE)&dwVal, &dwcbBuf)))
  48. {
  49. if (dwVal == 1)
  50. {
  51. bRet = TRUE;
  52. }
  53. }
  54. }
  55. }
  56. if (hUserKey)
  57. {
  58. RegCloseKey(hUserKey);
  59. hUserKey = 0;
  60. }
  61. if (hAdminKey)
  62. {
  63. RegCloseKey(hAdminKey);
  64. hAdminKey = 0;
  65. }
  66. return bRet;
  67. }
  68. VOID
  69. AddUrlToTrustDomain(CString& csUrl, BOOL bIntranet)
  70. {
  71. if (g_pISM)
  72. {
  73. DWORD dwZone = bIntranet ? URLZONE_INTRANET : URLZONE_TRUSTED;
  74. HRESULT hres = g_pISM->SetZoneMapping(dwZone, csUrl.Get(), SZM_CREATE);
  75. if (hres == E_ACCESSDENIED)
  76. {
  77. DPFN(eDbgLevelError, "[IEUnHarden] Attempted to enter a non-SSL site \
  78. into a zone that requires server verification \n");
  79. }
  80. else if (hres == E_FAIL)
  81. {
  82. DPFN(eDbgLevelError, "[IEUnHarden] The mapping already exists \n");
  83. }
  84. else if (hres == E_INVALIDARG)
  85. {
  86. DPFN(eDbgLevelError, "[IEUnHarden] Invalid wildcard \n");
  87. }
  88. else if (hres == HRESULT_FROM_WIN32(ERROR_FILE_EXISTS))
  89. {
  90. DPFN(eDbgLevelError, "[IEUnHarden] The mapping already exists in another zone \n");
  91. }
  92. else
  93. {
  94. DPFN(eDbgLevelError, "[IEUnHarden] SetZoneMapping() failed ! \n");
  95. }
  96. }
  97. }
  98. BOOL
  99. ParseCommandLineA(LPCSTR szParam)
  100. {
  101. if (!IEHardeningEnabled())
  102. {
  103. return FALSE;
  104. }
  105. CSTRING_TRY
  106. {
  107. CStringToken csParam(szParam, "|");
  108. CString csTok;
  109. while (csParam.GetToken(csTok))
  110. {
  111. csTok.TrimLeft();
  112. csTok.TrimRight();
  113. if (csTok.ComparePartNoCase(L"TrustedSites", 0, wcslen(L"TrustedSites")) == 0) // TrustedSites is the first word
  114. {
  115. int nLeftBracket, nRightBracket;
  116. CString csUrl;
  117. nLeftBracket = csTok.Find(L'{');
  118. nRightBracket = csTok.Find(L'}');
  119. if (nLeftBracket != -1 &&
  120. nRightBracket != -1 &&
  121. (nLeftBracket + 1) < (nRightBracket - 1))
  122. {
  123. csUrl = csTok.Mid(nLeftBracket+1, nRightBracket-nLeftBracket-1);
  124. AddUrlToTrustDomain(csUrl, FALSE);
  125. }
  126. else
  127. {
  128. DPFN(eDbgLevelError, "Invalid command line. Should be enclosed in {}/n");
  129. }
  130. }
  131. else if (csTok.ComparePartNoCase(L"TrustedIntranetSites", 0,
  132. wcslen(L"TrustedIntranetSites")) == 0) // TrustedIntranetSites is the first word
  133. {
  134. int nLeftBracket, nRightBracket;
  135. CString csUrl;
  136. nLeftBracket = csTok.Find(L'{');
  137. nRightBracket = csTok.Find(L'}');
  138. if (nLeftBracket != -1 &&
  139. nRightBracket != -1 &&
  140. (nLeftBracket + 1) < (nRightBracket - 1))
  141. {
  142. csUrl = csTok.Mid(nLeftBracket+1, nRightBracket-nLeftBracket-1);
  143. AddUrlToTrustDomain(csUrl, TRUE);
  144. }
  145. else
  146. {
  147. DPFN(eDbgLevelError, "Invalid command line. Should be enclosed in {}/n");
  148. }
  149. }
  150. else
  151. {
  152. DPFN(eDbgLevelError, "[IESOFT] Invalid option %s \n", csTok.GetAnsi());
  153. return FALSE;
  154. }
  155. }
  156. }
  157. CSTRING_CATCH
  158. {
  159. DPFN(eDbgLevelError, "Out of Memory \n");
  160. return FALSE;
  161. }
  162. return TRUE;
  163. }
  164. BOOL
  165. InitCOM()
  166. {
  167. BOOL bRet = FALSE;
  168. HRESULT hres = CoInitialize(NULL);
  169. if (SUCCEEDED(hres)||
  170. (hres == S_FALSE)) // COM library is already initialized on this thread
  171. {
  172. hres = CoCreateInstance(CLSID_InternetSecurityManager,
  173. NULL,
  174. CLSCTX_INPROC_SERVER,
  175. IID_IInternetSecurityManager,
  176. (void **)&g_pISM);
  177. if (SUCCEEDED(hres))
  178. {
  179. bRet = TRUE;
  180. }
  181. else
  182. {
  183. DPFN(eDbgLevelError, "Failed to create IInternetSecurityManager object \n");
  184. }
  185. }
  186. else
  187. {
  188. DPFN(eDbgLevelError, "Failed to initialize COM Library \n");
  189. }
  190. return bRet;
  191. }
  192. void
  193. UnInitCOM()
  194. {
  195. if (g_pISM)
  196. {
  197. g_pISM->Release();
  198. }
  199. CoUninitialize();
  200. }
  201. BOOL
  202. NOTIFY_FUNCTION(
  203. DWORD fdwReason
  204. )
  205. {
  206. if (fdwReason == SHIM_STATIC_DLLS_INITIALIZED)
  207. {
  208. if (!InitCOM())
  209. {
  210. return FALSE;
  211. }
  212. if (ParseCommandLineA(COMMAND_LINE) == FALSE)
  213. {
  214. return FALSE;
  215. }
  216. UnInitCOM();
  217. }
  218. return TRUE;
  219. }
  220. HOOK_BEGIN
  221. CALL_NOTIFY_FUNCTION
  222. HOOK_END
  223. IMPLEMENT_SHIM_END