enumwsc.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include <Windows.h>
  2. #include <stdio.h>
  3. #include <wscapi.h>
  4. #include <iwscapi.h>
  5. #include "enumwsc.h"
  6. #include "beacon.h"
  7. #pragma comment(lib, "ole32.lib")
  8. #pragma comment(lib, "oleaut32.lib")
  9. #pragma comment(lib, "wscapi.lib")
  10. //START TrustedSec BOF print code: https://github.com/trustedsec/CS-Situational-Awareness-BOF/blob/master/src/common/base.c
  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. int bofstart() {
  21. output = (char*)MSVCRT$calloc(bufsize, 1);
  22. currentoutsize = 0;
  23. return 1;
  24. }
  25. void internal_printf(const char* format, ...){
  26. int buffersize = 0;
  27. int transfersize = 0;
  28. char * curloc = NULL;
  29. char* intBuffer = NULL;
  30. va_list args;
  31. va_start(args, format);
  32. buffersize = MSVCRT$vsnprintf(NULL, 0, format, args);
  33. va_end(args);
  34. if (buffersize == -1) return;
  35. char* transferBuffer = (char*)KERNEL32$HeapAlloc(KERNEL32$GetProcessHeap(), HEAP_ZERO_MEMORY, bufsize);
  36. intBuffer = (char*)KERNEL32$HeapAlloc(KERNEL32$GetProcessHeap(), HEAP_ZERO_MEMORY, buffersize);
  37. va_start(args, format);
  38. MSVCRT$vsnprintf(intBuffer, buffersize, format, args);
  39. va_end(args);
  40. if(buffersize + currentoutsize < bufsize)
  41. {
  42. MSVCRT$memcpy(output+currentoutsize, intBuffer, buffersize);
  43. currentoutsize += buffersize;
  44. } else {
  45. curloc = intBuffer;
  46. while(buffersize > 0)
  47. {
  48. transfersize = bufsize - currentoutsize;
  49. if(buffersize < transfersize)
  50. {
  51. transfersize = buffersize;
  52. }
  53. MSVCRT$memcpy(output+currentoutsize, curloc, transfersize);
  54. currentoutsize += transfersize;
  55. if(currentoutsize == bufsize)
  56. {
  57. printoutput(FALSE);
  58. }
  59. MSVCRT$memset(transferBuffer, 0, transfersize);
  60. curloc += transfersize;
  61. buffersize -= transfersize;
  62. }
  63. }
  64. KERNEL32$HeapFree(KERNEL32$GetProcessHeap(), 0, intBuffer);
  65. KERNEL32$HeapFree(KERNEL32$GetProcessHeap(), 0, transferBuffer);
  66. }
  67. void printoutput(BOOL done) {
  68. char * msg = NULL;
  69. BeaconOutput(CALLBACK_OUTPUT, output, currentoutsize);
  70. currentoutsize = 0;
  71. MSVCRT$memset(output, 0, bufsize);
  72. if(done) {MSVCRT$free(output); output=NULL;}
  73. }
  74. //END TrustedSec BOF print code.
  75. HRESULT GetSecurityProducts(WSC_SECURITY_PROVIDER provider) {
  76. HRESULT hr;
  77. IWscProduct* PtrProduct = NULL;
  78. IWSCProductList* PtrProductList = NULL;
  79. BSTR PtrVal = NULL;
  80. LONG ProductCount = 0;
  81. WSC_SECURITY_PRODUCT_STATE ProductState;
  82. WSC_SECURITY_SIGNATURE_STATUS ProductStatus;
  83. if (provider != WSC_SECURITY_PROVIDER_FIREWALL &&
  84. provider != WSC_SECURITY_PROVIDER_ANTIVIRUS &&
  85. provider != WSC_SECURITY_PROVIDER_ANTISPYWARE) {
  86. hr = E_INVALIDARG;
  87. goto Cleanup;
  88. }
  89. hr = OLE32$CoInitializeEx(0, COINIT_APARTMENTTHREADED);
  90. if (FAILED(hr)) goto Cleanup;
  91. IID CLSIDWSCProductList = {0x17072f7b, 0x9abe, 0x4a74, {0xa2, 0x61, 0x1e, 0xb7, 0x6b, 0x55, 0x10, 0x7a}};
  92. IID IIDIWSCProductList = {0x722a338c, 0x6e8e, 0x4e72, {0xac, 0x27, 0x14, 0x17, 0xfb, 0x0c, 0x81, 0xc2}};
  93. hr = OLE32$CoCreateInstance(&CLSIDWSCProductList, NULL, CLSCTX_INPROC_SERVER, &IIDIWSCProductList, (LPVOID*)&PtrProductList);
  94. if (FAILED(hr)) {
  95. if (hr == 0x80040154) {
  96. BeaconPrintf(CALLBACK_ERROR, "Windows Security Center is not running on this system.");
  97. }
  98. goto Cleanup;
  99. }
  100. if (provider == WSC_SECURITY_PROVIDER_ANTIVIRUS) internal_printf("\nFound registered antivirus product(s) in WSC:\n====================================================\n");
  101. else if (provider == WSC_SECURITY_PROVIDER_FIREWALL) internal_printf("\nFound registered firewall product(s) in WSC:\n====================================================\n");
  102. else internal_printf("\nFound registered antispyware product(s) in WSC:\n====================================================\n");
  103. hr = PtrProductList->lpVtbl->Initialize(PtrProductList, provider);
  104. if (FAILED(hr)) goto Cleanup;
  105. hr = PtrProductList->lpVtbl->get_Count(PtrProductList, &ProductCount);
  106. if (FAILED(hr)) goto Cleanup;
  107. for (LONG i = 0; i < ProductCount; i++) {
  108. hr = PtrProductList->lpVtbl->get_Item(PtrProductList, i, &PtrProduct);
  109. if (FAILED(hr)) goto Cleanup;
  110. hr = PtrProduct->lpVtbl->get_ProductName(PtrProduct, &PtrVal);
  111. if (FAILED(hr)) goto Cleanup;
  112. internal_printf("%ls\n", PtrVal);
  113. OLEAUT32$SysFreeString(PtrVal);
  114. PtrVal = NULL;
  115. hr = PtrProduct->lpVtbl->get_ProductState(PtrProduct, &ProductState);
  116. if (FAILED(hr)) goto Cleanup;
  117. const char* pszState;
  118. if (ProductState == WSC_SECURITY_PRODUCT_STATE_ON) {
  119. pszState = "On";
  120. } else if (ProductState == WSC_SECURITY_PRODUCT_STATE_OFF) {
  121. pszState = "Off";
  122. } else if (ProductState == WSC_SECURITY_PRODUCT_STATE_SNOOZED) {
  123. pszState = "Snoozed";
  124. } else {
  125. pszState = "Expired";
  126. }
  127. internal_printf("- Product state: %s\n", pszState);
  128. hr = PtrProduct->lpVtbl->get_SignatureStatus(PtrProduct, &ProductStatus);
  129. if (FAILED(hr)) goto Cleanup;
  130. const char* pszStatus = (ProductStatus == WSC_SECURITY_PRODUCT_UP_TO_DATE) ? "Up-to-date" : "Out-of-date";
  131. internal_printf("- Product status: %s\n", pszStatus);
  132. PtrProduct->lpVtbl->Release(PtrProduct);
  133. PtrProduct = NULL;
  134. internal_printf("----------------------------------------------------\n\n");
  135. }
  136. Cleanup:
  137. if (PtrVal) OLEAUT32$SysFreeString(PtrVal);
  138. if (PtrProductList) PtrProductList->lpVtbl->Release(PtrProductList);
  139. if (PtrProduct) PtrProduct->lpVtbl->Release(PtrProduct);
  140. OLE32$CoUninitialize();
  141. return hr;
  142. }
  143. int go(char *args, int len) {
  144. HRESULT hr;
  145. CHAR* option = "";
  146. datap parser;
  147. BeaconDataParse(&parser, args, len);
  148. option = BeaconDataExtract(&parser, NULL);
  149. if(!bofstart()) return;
  150. if (MSVCRT$strcmp(option, "av") == 0) hr = GetSecurityProducts(WSC_SECURITY_PROVIDER_ANTIVIRUS);
  151. else if (MSVCRT$strcmp(option, "fw") == 0) hr = GetSecurityProducts(WSC_SECURITY_PROVIDER_FIREWALL);
  152. else if (MSVCRT$strcmp(option, "as") == 0) hr = GetSecurityProducts(WSC_SECURITY_PROVIDER_ANTISPYWARE);
  153. else {
  154. BeaconPrintf(CALLBACK_ERROR, "Please specify one of following options: av | fw | as\n");
  155. return 0;
  156. }
  157. if (SUCCEEDED(hr)) {
  158. printoutput(TRUE);
  159. } else {
  160. BeaconPrintf(CALLBACK_ERROR, "Failed to enumerate security products from WSC.\n");
  161. }
  162. return 0;
  163. }