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.

355 lines
9.7 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. owfs.cxx
  5. Abstract:
  6. Injectee
  7. Author:
  8. Larry Zhu (LZhu) December 1, 2001 Created
  9. Environment:
  10. User Mode
  11. Revision History:
  12. --*/
  13. #include "precomp.hxx"
  14. #pragma hdrstop
  15. #include <lmcons.h>
  16. #include "owfs.hxx"
  17. EXTERN_C
  18. NTSTATUS
  19. LsaICallPackageEx(
  20. IN PUNICODE_STRING AuthenticationPackage,
  21. IN PVOID ClientBufferBase,
  22. IN PVOID ProtocolSubmitBuffer,
  23. IN ULONG SubmitBufferLength,
  24. OUT PVOID * ProtocolReturnBuffer,
  25. OUT PULONG ReturnBufferLength,
  26. OUT PNTSTATUS ProtocolStatus
  27. );
  28. PLSA_SECPKG_FUNCTION_TABLE LsaFunctions = NULL;
  29. CHAR MyPassword[PWLEN + 1] = {0};
  30. PLSA_CALL_PACKAGE SavedCallPackage = NULL;
  31. BOOL
  32. DllMain(
  33. IN HANDLE hModule,
  34. IN DWORD dwReason,
  35. IN DWORD dwReserved
  36. )
  37. {
  38. switch (dwReason)
  39. {
  40. case DLL_THREAD_ATTACH:
  41. break;
  42. case DLL_THREAD_DETACH:
  43. break;
  44. case DLL_PROCESS_DETACH:
  45. if (SavedCallPackage && LsaFunctions)
  46. {
  47. DebugPrintf(SSPI_LOG, "DllMain DLL_PROCESS_DETACH retoring CallPackage from %p to %p\n", LsaFunctions->CallPackage, SavedCallPackage);
  48. LsaFunctions->CallPackage = SavedCallPackage;
  49. }
  50. break;
  51. case DLL_PROCESS_ATTACH:
  52. break;
  53. default:
  54. break;
  55. }
  56. return DllMainDefaultHandler(hModule, dwReason, dwReserved);
  57. }
  58. int
  59. RunIt(
  60. IN ULONG cbParameters,
  61. IN VOID* pvParameters
  62. )
  63. {
  64. //
  65. // RunItDefaultHandler calls Start() and adds try except
  66. //
  67. return RunItDefaultHandler(cbParameters, pvParameters);
  68. }
  69. typedef struct _TTempResp {
  70. ENCRYPTED_CREDENTIALW returnCred;
  71. MSV1_0_SUPPLEMENTAL_CREDENTIAL suppCred;
  72. } TTempResp;
  73. extern "C"
  74. NTSTATUS
  75. CustomCallPackage(
  76. IN PUNICODE_STRING AuthenticationPackage,
  77. IN PVOID ProtocolSubmitBuffer,
  78. IN ULONG SubmitBufferLength,
  79. OUT PVOID *ProtocolReturnBuffer,
  80. OUT PULONG ReturnBufferLength,
  81. OUT PNTSTATUS ProtocolStatus
  82. )
  83. {
  84. NTSTATUS Status = STATUS_SUCCESS;
  85. __try
  86. {
  87. if (KerbQuerySupplementalCredentialsMessage != *((ULONG*)ProtocolSubmitBuffer))
  88. {
  89. DebugPrintf(SSPI_LOG, "Calling LsaICallPackageEx\n");
  90. Status = LsaICallPackageEx(
  91. AuthenticationPackage,
  92. ProtocolSubmitBuffer, // client buffer base is same as local buffer
  93. ProtocolSubmitBuffer,
  94. SubmitBufferLength,
  95. ProtocolReturnBuffer,
  96. ReturnBufferLength,
  97. ProtocolStatus
  98. );
  99. }
  100. else
  101. {
  102. KERB_QUERY_SUPPLEMENTAL_CREDS_REQUEST* pQuerySuppCred = (KERB_QUERY_SUPPLEMENTAL_CREDS_REQUEST*) ProtocolSubmitBuffer;
  103. KERB_QUERY_SUPPLEMENTAL_CREDS_RESPONSE* pQuerySuppCredResp = NULL;
  104. UNICODE_STRING KerberosPackageName = CONSTANT_UNICODE_STRING((MICROSOFT_KERBEROS_NAME_W));
  105. UNICODE_STRING MsvPackageNam = CONSTANT_UNICODE_STRING((NTLMSP_NAME));
  106. ULONG cbResponse = 0;
  107. NTSTATUS SubStatus = STATUS_UNSUCCESSFUL;
  108. PCREDENTIALW pCred = NULL;
  109. MSV1_0_SUPPLEMENTAL_CREDENTIAL* pMsvSuppCred = NULL;
  110. TTempResp* pTempResp = NULL;
  111. DebugPrintf(SSPI_LOG, "Do it myself\n");
  112. ASSERT(LsaFunctions);
  113. pTempResp = (TTempResp*) LsaFunctions->AllocatePrivateHeap(sizeof(TTempResp));
  114. pQuerySuppCredResp = (KERB_QUERY_SUPPLEMENTAL_CREDS_RESPONSE*) pTempResp;
  115. if (!pQuerySuppCredResp)
  116. {
  117. Status = STATUS_NO_MEMORY;
  118. }
  119. if (NT_SUCCESS(Status))
  120. {
  121. ANSI_STRING Ansi;
  122. UNICODE_STRING NtPass = {0};
  123. RtlInitAnsiString(&Ansi, MyPassword);
  124. RtlAnsiStringToUnicodeString(&NtPass, &Ansi, TRUE);
  125. //
  126. // make a copy of cert cred
  127. //
  128. pQuerySuppCredResp->ReturnedCreds = *(ENCRYPTED_CREDENTIALW*)pQuerySuppCred->MarshalledCreds;
  129. //
  130. // make a supplemental cred
  131. //
  132. pMsvSuppCred = &pTempResp->suppCred;
  133. pMsvSuppCred->Flags = CRED_FLAGS_PASSWORD_FOR_CERT | CRED_FLAGS_OWF_CRED_BLOB;
  134. pMsvSuppCred->Version = MSV1_0_CRED_VERSION;
  135. RtlCalculateNtOwfPassword(&NtPass, (PNT_OWF_PASSWORD) pMsvSuppCred->NtPassword);
  136. RtlCalculateLmOwfPassword(MyPassword, (PLM_OWF_PASSWORD) pMsvSuppCred->LmPassword);
  137. pMsvSuppCred->Flags = MSV1_0_CRED_NT_PRESENT | MSV1_0_CRED_LM_PRESENT;
  138. //
  139. // encrypt it
  140. //
  141. pQuerySuppCredResp->ReturnedCreds.Cred.CredentialBlob = (BYTE*) pMsvSuppCred;
  142. pQuerySuppCredResp->ReturnedCreds.Cred.CredentialBlobSize = sizeof(MSV1_0_SUPPLEMENTAL_CREDENTIAL);
  143. LsaFunctions->LsaProtectMemory(
  144. pQuerySuppCredResp->ReturnedCreds.Cred.CredentialBlob,
  145. pQuerySuppCredResp->ReturnedCreds.Cred.CredentialBlobSize
  146. );
  147. pQuerySuppCredResp->ReturnedCreds.ClearCredentialBlobSize = sizeof(MSV1_0_SUPPLEMENTAL_CREDENTIAL);
  148. //
  149. // fill in the flags and types etc
  150. //
  151. pQuerySuppCredResp->ReturnedCreds.Cred.Flags = CRED_FLAGS_OWF_CRED_BLOB;
  152. pQuerySuppCredResp->ReturnedCreds.Cred.Type = CRED_TYPE_DOMAIN_PASSWORD;
  153. pQuerySuppCredResp->ReturnedCreds.Cred.UserName = L"ntdev\\lzhu";
  154. if (0)
  155. {
  156. NTSTATUS TempStatus;
  157. TempStatus = LsaFunctions->CrediWrite(
  158. &pQuerySuppCred->LogonId,
  159. 0,
  160. &pQuerySuppCredResp->ReturnedCreds,
  161. 0
  162. );
  163. if (!NT_SUCCESS(TempStatus))
  164. {
  165. DebugPrintf(SSPI_ERROR, "CrediWrite failed with %#x\n", TempStatus);
  166. }
  167. }
  168. *ProtocolStatus = STATUS_SUCCESS;
  169. *ProtocolReturnBuffer = pQuerySuppCredResp;
  170. *ReturnBufferLength = sizeof(MSV1_0_SUPPLEMENTAL_CREDENTIAL) + sizeof(ENCRYPTED_CREDENTIALW);
  171. }
  172. }
  173. }
  174. __except (EXCEPTION_EXECUTE_HANDLER)
  175. {
  176. Status = GetExceptionCode();
  177. DebugPrintf(SSPI_ERROR, "CustomCallPackage hit exception %#x\n", Status);
  178. }
  179. return Status;
  180. }
  181. #if 0
  182. Return Values for Start():
  183. ERROR_NO_MORE_USER_HANDLES unload repeatedly
  184. ERROR_SERVER_HAS_OPEN_HANDLES no unload at all
  185. others unload once
  186. #endif 0
  187. int
  188. Start(
  189. IN ULONG cbParameters,
  190. IN VOID* pvParameters
  191. )
  192. {
  193. DWORD dwErr = ERROR_SUCCESS;
  194. DebugPrintf(SSPI_LOG, "Start: Hello world!\n");
  195. DebugPrintHex(SSPI_LOG, "Parameters", cbParameters, pvParameters);
  196. LsaFunctions = *((PLSA_SECPKG_FUNCTION_TABLE*)pvParameters);
  197. if (!LsaFunctions)
  198. {
  199. return -1;
  200. }
  201. if (cbParameters > sizeof(ULONG_PTR))
  202. {
  203. memcpy(MyPassword, ((UCHAR*)pvParameters) + sizeof(ULONG_PTR), cbParameters - sizeof(ULONG_PTR));
  204. if (strlen(MyPassword) == 1)
  205. {
  206. DebugPrintf(SSPI_WARN, "Start: trying to unload this dll repeately\n");
  207. dwErr = ERROR_NO_MORE_USER_HANDLES;
  208. goto Cleanup;
  209. }
  210. }
  211. //
  212. // Sanity Check
  213. //
  214. {
  215. VOID* pvMem = NULL;
  216. pvMem = LsaFunctions->AllocatePrivateHeap(sizeof(WCHAR));
  217. if (pvMem)
  218. {
  219. LsaFunctions->FreePrivateHeap(pvMem);
  220. }
  221. }
  222. if (!SavedCallPackage)
  223. {
  224. DebugPrintf(SSPI_LOG, "Start: saving CallPackage %p, CustomCallPackage %p\n", LsaFunctions->CallPackage, CustomCallPackage);
  225. SavedCallPackage = LsaFunctions->CallPackage;
  226. LsaFunctions->CallPackage = CustomCallPackage;
  227. }
  228. //
  229. // don't unload this dll
  230. //
  231. dwErr = ERROR_SERVER_HAS_OPEN_HANDLES;
  232. Cleanup:
  233. return dwErr;
  234. }
  235. int
  236. Init(
  237. IN ULONG argc,
  238. IN PCSTR* argv,
  239. OUT ULONG* pcbParameters,
  240. OUT VOID** ppvParameters
  241. )
  242. {
  243. DWORD dwErr = ERROR_SUCCESS;
  244. CHAR Parameters[REMOTE_PACKET_SIZE] = {0};
  245. ULONG cbBuffer = sizeof(Parameters);
  246. ULONG cbParameter = 0;
  247. DebugPrintf(SSPI_LOG, "Init: Hello world!\n");
  248. *pcbParameters = 0;
  249. *ppvParameters = NULL;
  250. ULONG_PTR LsaFunctions = 0x745b5688;
  251. DebugPrintf(SSPI_LOG, "LsaFunctions is %p\n", LsaFunctions);
  252. memcpy(Parameters, &LsaFunctions, sizeof(LsaFunctions));
  253. cbParameter = sizeof(LsaFunctions);
  254. if (argc >= 1)
  255. {
  256. memcpy(Parameters + cbParameter, argv[0], strlen(argv[0]));
  257. cbParameter += strlen(argv[0]);
  258. cbParameter++; // add a NULL
  259. dwErr = ERROR_SUCCESS;
  260. }
  261. else // return "Usage" in ppvParameters, must be a NULL terminated string
  262. {
  263. strcpy(Parameters, "<password>");
  264. cbParameter = strlen(Parameters) + 1;
  265. dwErr = ERROR_INVALID_PARAMETER;
  266. }
  267. *ppvParameters = new CHAR[cbParameter];
  268. if (*ppvParameters)
  269. {
  270. *pcbParameters = cbParameter;
  271. memcpy(*ppvParameters, Parameters, *pcbParameters);
  272. }
  273. else
  274. {
  275. dwErr = ERROR_OUTOFMEMORY;
  276. goto Cleanup;
  277. }
  278. Cleanup:
  279. return dwErr;
  280. }