30 #define USER_SCAN_THREAD_COUNT 6 // the number of scanning worker threads. 57 #define SCANNER_MESSAGE_SIZE (sizeof(FILTER_MESSAGE_HEADER) + sizeof(AV_SCANNER_NOTIFICATION)) 75 #define SCANNER_REPLY_MESSAGE_SIZE (sizeof(FILTER_REPLY_HEADER) + sizeof(ULONG)) 85 _Inout_ PBOOLEAN pAbort
91 _In_ PSCANNER_MESSAGE Message,
161 HANDLE hEvent =
NULL;
163 HANDLE hListenAbort =
NULL;
166 if (
NULL == Context) {
176 hListenAbort = CreateThread(
NULL,
183 if (
NULL == hListenAbort) {
185 hr = HRESULT_FROM_WIN32(GetLastError());
194 if (
NULL == scanThreadCtxes) {
210 scanThreadCtxes[i].
Handle = CreateThread(
NULL,
215 &scanThreadCtxes[i].ThreadId );
217 if (
NULL == scanThreadCtxes[i].Handle) {
218 hr = HRESULT_FROM_WIN32(GetLastError());
221 InitializeCriticalSection(&(scanThreadCtxes[i].Lock));
234 &Context->ConnectionPort );
237 Context->ConnectionPort =
NULL;
245 Context->Completion = CreateIoCompletionPort( Context->ConnectionPort,
248 USER_SCAN_THREAD_COUNT );
250 if (
NULL == Context->Completion ) {
251 hr = HRESULT_FROM_WIN32(GetLastError());
255 Context->ScanThreadCtxes = scanThreadCtxes;
256 Context->AbortThreadHandle = hListenAbort;
265 if ( ResumeThread( scanThreadCtxes[i].Handle ) == -1) {
267 fprintf(stderr,
"[UserScanInit]: ResumeThread scan listening thread failed.\n");
268 hr = HRESULT_FROM_WIN32(GetLastError());
277 if ( ResumeThread( hListenAbort ) == -1 ) {
278 fprintf(stderr,
"[UserScanInit]: ResumeThread abort listening thread failed.\n");
279 hr = HRESULT_FROM_WIN32(GetLastError());
291 PSCANNER_MESSAGE msg = HeapAlloc( GetProcessHeap(), 0,
sizeof(
SCANNER_MESSAGE ) );
299 FillMemory( &msg->
Ovlp,
sizeof(OVERLAPPED), 0);
300 hr = FilterGetMessage( Context->ConnectionPort,
305 if (hr == HRESULT_FROM_WIN32( ERROR_IO_PENDING )) {
311 fprintf(stderr,
"[UserScanInit]: FilterGetMessage failed.\n");
313 HeapFree(GetProcessHeap(), 0, msg );
322 if (Context->Completion && !CloseHandle(Context->Completion)) {
324 fprintf(stderr,
"[UserScanInit] Error! Close completion port failed.\n");
327 if (Context->ConnectionPort && !CloseHandle(Context->ConnectionPort)) {
329 fprintf(stderr,
"[UserScanInit] Error! Close connection port failed.\n");
332 if (scanThreadCtxes) {
338 if (scanThreadCtxes[i].Handle && !CloseHandle(scanThreadCtxes[i].Handle)) {
340 fprintf(stderr,
"[UserScanInit] Error! Close scan thread failed.\n");
343 DeleteCriticalSection(&(scanThreadCtxes[i].Lock));
345 HeapFree(GetProcessHeap(), 0, scanThreadCtxes);
347 if (hListenAbort && !CloseHandle(hListenAbort)) {
349 fprintf(stderr,
"[UserScanInit] Error! Close listen abort thread failed.\n");
352 if (hEvent && !CloseHandle(hEvent)) {
354 fprintf(stderr,
"[UserScanInit] Error! Close event handle failed.\n");
387 printf(
"=================finalize\n");
391 printf(
"[UserScanFinalize]: Closing connection port\n");
428 hScanThreads[i] = ScanThreadCtxes[i].Handle;
430 return WaitForMultipleObjects(USER_SCAN_THREAD_COUNT, hScanThreads,
TRUE, INFINITE);
463 *ScanThreadCtx =
NULL;
469 if ( ThreadId == scanThreadCtx[i].ThreadId ) {
470 *ScanThreadCtx = (scanThreadCtx + i);
500 if (
NULL == scanThreadCtxes) {
501 fprintf(stderr,
"Scan thread contexes are NOT suppoed to be NULL.\n");
509 Context->Finalized =
TRUE;
527 CancelIoEx(Context->ConnectionPort,
NULL);
561 if (!CloseHandle(Context->ConnectionPort)) {
562 fprintf(stderr,
"[UserScanFinalize]: Failed to close the connection port.\n");
563 hr = HRESULT_FROM_WIN32(GetLastError());
566 Context->ConnectionPort =
NULL;
568 if (!CloseHandle(Context->Completion)) {
569 fprintf(stderr,
"[UserScanFinalize]: Failed to close the completion port.\n");
570 hr = HRESULT_FROM_WIN32(GetLastError());
573 Context->Completion =
NULL;
604 if (
NULL == scanThreadCtxes) {
606 fprintf(stderr,
"Scan thread contexes are NOT suppoed to be NULL.\n");
610 if (Context->AbortThreadHandle) {
612 CloseHandle( Context->AbortThreadHandle );
625 if (scanThreadCtxes[i].Handle && !CloseHandle(scanThreadCtxes[i].Handle)) {
626 fprintf(stderr,
"[UserScanInit] Error! Close scan thread failed.\n");
629 DeleteCriticalSection(&(scanThreadCtxes[i].Lock));
631 HeapFree( GetProcessHeap(), 0, scanThreadCtxes );
632 Context->ScanThreadCtxes =
NULL;
640 _Inout_ PBOOLEAN pAbort
674 PUCHAR start = StartingAddress;
675 PUCHAR end = start + Size - searchStringLength;
681 CopyMemory( (PVOID) targetString,
686 ind < searchStringLength;
691 targetString[searchStringLength] =
'\0';
698 for (p = start, i = 1;
712 if ( !memcmp( p, targetString, searchStringLength )) {
724 _In_ PSCANNER_MESSAGE Message,
760 ULONG bytesReturned = 0;
761 HANDLE sectionHandle =
NULL;
763 PVOID scanAddress =
NULL;
764 MEMORY_BASIC_INFORMATION memoryInfo;
781 hr = FilterSendMessage( Context->ConnectionPort,
791 "[UserScanHandleStartScanMsg]: Failed to send message SendMessageToCreateSection to the minifilter.\n");
796 scanAddress = MapViewOfFile( sectionHandle,
801 if (scanAddress ==
NULL) {
802 fprintf(stderr,
"[UserScanHandleStartScanMsg]: Failed to map the view.\n");
807 if( !VirtualQuery( scanAddress, &memoryInfo,
sizeof(memoryInfo) )) {
808 fprintf(stderr,
"[UserScanHandleStartScanMsg]: Failed to query the view.\n");
818 memoryInfo.RegionSize,
819 &ThreadCtx->Aborted );
828 flags = MEM_UNMAP_WITH_TRANSIENT_BOOST;
833 if (scanAddress !=
NULL) {
835 if (!UnmapViewOfFileEx( scanAddress, flags )) {
837 fprintf(stderr,
"[UserScanHandleStartScanMsg]: Failed to unmap the view.\n");
847 if (!CloseHandle(sectionHandle)) {
849 fprintf(stderr,
"[UserScanHandleStartScanMsg]: Failed to close the section handle.\n");
860 hr = FilterSendMessage( Context->ConnectionPort,
869 "[UserScanHandleStartScanMsg]: Failed to close message SendMessageToCreateSection to the minifilter.\n");
907 PSCANNER_MESSAGE message =
NULL;
909 LPOVERLAPPED pOvlp =
NULL;
913 BOOL success = FALSE;
920 "[UserScanWorker]: Failed to get thread context.\n");
926 printf(
"Current thread handle %p, id:%u\n", threadCtx->
Handle, threadCtx->
ThreadId);
941 success = GetQueuedCompletionStatus( Context->Completion, &outSize, &key, &pOvlp, INFINITE );
945 hr = HRESULT_FROM_WIN32(GetLastError());
953 if (hr == E_HANDLE) {
955 printf(
"Completion port becomes unavailable.\n");
958 }
else if (hr == HRESULT_FROM_WIN32(ERROR_ABANDONED_WAIT_0)) {
960 printf(
"Completion port was closed.\n");
984 EnterCriticalSection(&(threadCtx->
Lock));
987 LeaveCriticalSection(&(threadCtx->
Lock));
999 hr = FilterReplyMessage( Context->ConnectionPort,
1006 "[UserScanWorker]: Failed to reply thread handle to the minifilter\n");
1021 "[UserScanWorker]: Failed to handle the message.\n");
1029 if (Context->Finalized) {
1038 hr = FilterGetMessage( Context->ConnectionPort,
1043 if (hr == HRESULT_FROM_WIN32(ERROR_OPERATION_ABORTED)) {
1045 printf(
"FilterGetMessage aborted.\n");
1048 }
else if (hr != HRESULT_FROM_WIN32( ERROR_IO_PENDING )) {
1051 "[UserScanWorker]: Failed to get message from the minifilter. \n0x%x, 0x%x\n",
1052 hr, HRESULT_FROM_WIN32(GetLastError()));
1065 HeapFree(GetProcessHeap(), 0, message);
1068 printf(
"***Thread id %u exiting\n", threadCtx->
ThreadId);
1102 HANDLE abortPort =
NULL;
1104 DWORD dwThisThread = GetCurrentThreadId();
1139 hr = FilterGetMessage( abortPort,
1144 if (hr == HRESULT_FROM_WIN32(ERROR_OPERATION_ABORTED)) {
1146 printf(
"[UserScanListenAbortProc]: FilterGetMessage aborted.\n");
1150 }
else if (FAILED(hr)) {
1153 "[UserScanListenAbortProc]: Failed to get message from the minifilter.\n" );
1158 printf(
"[UserScanListenAbortProc]: Got message %llu. \n", message.
MessageHeader.MessageId);
1172 if (SUCCEEDED(hr)) {
1174 printf(
"[UserScanListenAbortProc]: User Set AvMsgAbortScanning\n");
1181 EnterCriticalSection(&(threadCtx->
Lock));
1188 printf(
"[UserScanListenAbortProc]: tried to abort %lld, but current scan in this thread is %lld\n",
1192 LeaveCriticalSection(&(threadCtx->
Lock));
1196 fprintf(stderr,
"[UserScanListenAbortProc]: Error! UserScanGetThreadContextById failed.\n");
1212 printf(
"The filter is unloading, exit!\n");
1216 hr = FilterReplyMessage( abortPort,
1222 fprintf(stderr,
"[UserScanListenAbortProc]: Error! FilterReplyMessage failed.\n");
1226 CloseHandle( abortPort );
1237 fprintf(stderr,
"[UserScanListenAbortProc]: Failed to handle the message.\n");
1242 if (!CloseHandle(abortPort)) {
1244 fprintf(stderr,
"[UserScanListenAbortProc]: Failed to close the connection port.\n");
HRESULT UserScanHandleStartScanMsg(_In_ PUSER_SCAN_CONTEXT Context, _In_ PSCANNER_MESSAGE Message, _In_ PSCANNER_THREAD_CONTEXT ThreadCtx)
#define SCANNER_MESSAGE_SIZE
FILTER_REPLY_HEADER ReplyHeader
#define MAKE_HRESULT(sev, fac, code)
#define AV_ABORT_PORT_NAME
HRESULT UserScanWorker(_Inout_ PUSER_SCAN_CONTEXT Context)
#define AV_SCAN_PORT_NAME
HRESULT UserScanCleanup(_In_ PUSER_SCAN_CONTEXT Context)
AV_SCANNER_NOTIFICATION Notification
HRESULT UserScanListenAbortProc(_Inout_ PUSER_SCAN_CONTEXT Context)
#define SCANNER_REPLY_MESSAGE_SIZE
#define USER_SCAN_THREAD_COUNT
AVSCAN_RESULT UserScanMemoryStream(_In_reads_bytes_(Size) PUCHAR StartingAddress, _In_ SIZE_T Size, _Inout_ PBOOLEAN pAbort)
struct _SCANNER_REPLY_MESSAGE * PSCANNER_REPLY_MESSAGE
VOID UserScanSynchronizedCancel(_In_ PUSER_SCAN_CONTEXT Context)
#define AV_DEFAULT_PATTERN_XOR_KEY
HRESULT UserScanFinalize(_In_ PUSER_SCAN_CONTEXT Context)
_In_ PLARGE_INTEGER _In_ ULONG _In_ ULONG _In_reads_bytes_(Length)
struct _SCANNER_MESSAGE * PSCANNER_MESSAGE
#define AV_DEFAULT_SEARCH_PATTERN_SIZE
NcLoadRegistryStringRetry NULL
#define AV_DEFAULT_SEARCH_PATTERN
HRESULT UserScanInit(_Inout_ PUSER_SCAN_CONTEXT Context)
VOID DisplayError(_In_ DWORD Code)
enum _AVSCAN_RESULT AVSCAN_RESULT
FILTER_MESSAGE_HEADER MessageHeader
DWORD WaitForAll(_In_ PSCANNER_THREAD_CONTEXT ScanThreadCtxes)
HRESULT UserScanClosePorts(_In_ PUSER_SCAN_CONTEXT Context)
struct _SCANNER_REPLY_MESSAGE SCANNER_REPLY_MESSAGE
struct _SCANNER_MESSAGE SCANNER_MESSAGE
HRESULT UserScanGetThreadContextById(_In_ DWORD ThreadId, _In_ PUSER_SCAN_CONTEXT Context, _Out_ PSCANNER_THREAD_CONTEXT *ScanThreadCtx)
AVSCAN_CONNECTION_TYPE Type