22 #include <DriverSpecs.h> 34 #define EXIT_INTERPRETER 2 35 #define EXIT_PROGRAM 4 37 #define INTERPRETER_EXIT_COMMAND1 "go" 38 #define INTERPRETER_EXIT_COMMAND2 "g" 39 #define PROGRAM_EXIT_COMMAND "exit" 40 #define CMDLINE_SIZE 256 43 #define MINISPY_NAME L"MiniSpy" 48 _In_reads_(argc)
char *argv[],
81 WCHAR buffer[MAX_PATH] = { 0 };
83 HMODULE module =
NULL;
86 count = FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM,
91 sizeof(buffer) /
sizeof(WCHAR),
97 count = GetSystemDirectory( buffer,
98 sizeof(buffer) /
sizeof( WCHAR ) );
100 if (count==0 || count >
sizeof(buffer) /
sizeof( WCHAR )) {
107 printf(
" Could not translate error: %d\n", Code);
112 status = StringCchCat( buffer,
113 sizeof(buffer) /
sizeof( WCHAR ),
116 if (status != S_OK) {
118 printf(
" Could not translate error: %d\n", Code);
122 module = LoadLibraryExW( buffer,
NULL, LOAD_LIBRARY_AS_DATAFILE );
128 count = FormatMessage (FORMAT_MESSAGE_FROM_HMODULE,
133 sizeof(buffer) /
sizeof(WCHAR),
136 if (module !=
NULL) {
138 FreeLibrary( module );
147 printf(
" Could not translate error: %d\n", Code);
156 printf(
" %ws\n", buffer);
164 #pragma warning(push) 165 #pragma warning(disable:4706) // assignment within conditional expression 170 _In_reads_(argc)
char *argv[]
185 HRESULT hResult = S_OK;
188 HANDLE thread =
NULL;
203 printf(
"Connecting to filter's port...\n" );
212 if (IS_ERROR( hResult )) {
214 printf(
"Could not connect to filter: 0x%08x\n", hResult );
227 L
"MiniSpy shut down" );
236 result = GetLastError();
237 printf(
"Could not create semaphore: %d\n", result );
258 printf(
"Creating logging thread...\n" );
259 thread = CreateThread(
NULL,
268 result = GetLastError();
269 printf(
"Could not create logging thread: %d\n", result );
285 printf(
"\nHit [Enter] to begin command mode...\n\n" );
292 context.LogToScreen = context.NextLogToScreen;
294 while (inputChar = (CHAR)getchar()) {
298 INT parmCount, count;
303 if (inputChar ==
'\n') {
312 context.NextLogToScreen = context.LogToScreen;
313 context.LogToScreen = FALSE;
330 (count <
CMDLINE_SIZE) && ((ch = (CHAR)getchar()) !=
'\n');
333 commandLine[count] = ch;
335 if (newParm && (ch !=
' ')) {
337 parms[parmCount++] = &commandLine[count];
352 commandLine[count] = 0;
360 commandLine[count] =
'\0';
362 if (parmCount == 0) {
385 context.LogToScreen = context.NextLogToScreen;
387 if (context.LogToScreen) {
389 printf(
"Should be logging to screen...\n" );
400 printf(
"Cleaning up...\n" );
406 context.CleaningUp =
TRUE;
412 WaitForSingleObject( context.ShutDown, INFINITE );
414 if (context.LogToFile) {
416 fclose( context.OutputFile );
425 if(context.ShutDown) {
427 CloseHandle( context.ShutDown );
432 CloseHandle( thread );
446 _In_reads_(argc)
char *argv[],
467 PWCHAR instanceString;
468 WCHAR instanceName[INSTANCE_NAME_MAX_CHARS + 1];
473 for (parmIndex = 0; parmIndex < argc; parmIndex++) {
475 parm = argv[parmIndex];
477 if (parm[0] ==
'/') {
494 if (parmIndex >= argc) {
500 goto InterpretCommand_Usage;
503 parm = argv[parmIndex];
505 printf(
" Attaching to %s... ", parm );
507 bufferLength = MultiByteToWideChar( CP_ACP,
508 MB_ERR_INVALID_CHARS,
514 if (bufferLength == 0) {
521 goto InterpretCommand_Usage;
527 sizeof( instanceName ),
530 if (SUCCEEDED( hResult )) {
532 printf(
" Instance name: %S\n", instanceName );
536 printf(
"\n Could not attach to device: 0x%08x\n", hResult );
552 if (parmIndex >= argc) {
558 goto InterpretCommand_Usage;
561 parm = argv[parmIndex];
563 printf(
" Detaching from %s\n", parm );
564 bufferLength = MultiByteToWideChar( CP_ACP,
565 MB_ERR_INVALID_CHARS,
571 if (bufferLength == 0) {
578 goto InterpretCommand_Usage;
587 if (parmIndex >= argc) {
589 instanceString =
NULL;
593 if (argv[parmIndex][0] ==
'/') {
600 instanceString =
NULL;
605 parm = argv[parmIndex];
606 bufferLength = MultiByteToWideChar( CP_ACP,
607 MB_ERR_INVALID_CHARS,
610 (LPWSTR)instanceName,
611 sizeof( instanceName )/
sizeof( WCHAR ) );
613 if (bufferLength == 0) {
620 goto InterpretCommand_Usage;
623 instanceString = instanceName;
635 if (IS_ERROR( hResult )) {
637 printf(
" Could not detach from device: 0x%08x\n", hResult );
660 if (Context->NextLogToScreen) {
662 printf(
" Turning off logging to screen\n" );
666 printf(
" Turning on logging to screen\n" );
669 Context->NextLogToScreen = !Context->NextLogToScreen;
679 if (Context->LogToFile) {
681 printf(
" Stop logging to file \n" );
682 Context->LogToFile = FALSE;
683 assert( Context->OutputFile );
684 _Analysis_assume_( Context->OutputFile !=
NULL );
685 fclose( Context->OutputFile );
686 Context->OutputFile =
NULL;
692 if (parmIndex >= argc) {
698 goto InterpretCommand_Usage;
701 parm = argv[parmIndex];
702 printf(
" Log to file %s\n", parm );
704 if (fopen_s( &Context->OutputFile, parm,
"w" ) != 0 ) {
705 assert( Context->OutputFile );
708 Context->LogToFile =
TRUE;
717 goto InterpretCommand_Usage;
726 if (!_strnicmp( parm,
731 goto InterpretCommand_Exit;
734 if (!_strnicmp( parm,
739 goto InterpretCommand_Exit;
746 if (!_strnicmp( parm,
751 goto InterpretCommand_Exit;
757 goto InterpretCommand_Usage;
761 InterpretCommand_Exit:
764 InterpretCommand_Usage:
765 printf(
"Valid switches: [/a <drive>] [/d <drive>] [/l] [/s] [/f [<file name>]]\n" 766 " [/a <drive>] starts monitoring <drive>\n" 767 " [/d <drive> [<instance id>]] detaches filter <instance id> from <drive>\n" 768 " [/l] lists all the drives the monitor is currently attached to\n" 769 " [/s] turns on and off showing logging output on the screen\n" 770 " [/f [<file name>]] turns on and off logging to the specified file\n" 771 " If you are in command mode:\n" 772 " [enter] will enter command mode\n" 773 " [go|g] will exit command mode\n" 774 " [exit] will terminate this program\n" 777 goto InterpretCommand_Exit;
783 _In_ LPCWSTR VolumeName
804 PINSTANCE_FULL_INFORMATION data = (PINSTANCE_FULL_INFORMATION)buffer;
807 ULONG instanceCount = 0;
814 hResult = FilterVolumeInstanceFindFirst( VolumeName,
815 InstanceFullInformation,
817 sizeof(buffer)-
sizeof(WCHAR),
821 if (IS_ERROR( hResult )) {
823 return instanceCount;
828 assert((data->FilterNameBufferOffset+data->FilterNameLength) <= (
sizeof(buffer)-
sizeof(WCHAR)));
829 _Analysis_assume_((data->FilterNameBufferOffset+data->FilterNameLength) <= (
sizeof(buffer)-
sizeof(WCHAR)));
839 filtername =
Add2Ptr(data,data->FilterNameBufferOffset);
840 filtername[data->FilterNameLength/
sizeof( WCHAR )] = L
'\0';
851 }
while (SUCCEEDED( FilterVolumeInstanceFindNext( volumeIterator,
852 InstanceFullInformation,
854 sizeof(buffer)-
sizeof(WCHAR),
861 FilterVolumeInstanceFindClose( volumeIterator );
862 return instanceCount;
883 PFILTER_VOLUME_BASIC_INFORMATION volumeBuffer = (PFILTER_VOLUME_BASIC_INFORMATION)buffer;
885 ULONG volumeBytesReturned;
886 HRESULT hResult = S_OK;
887 WCHAR driveLetter[15] = { 0 };
896 hResult = FilterVolumeFindFirst( FilterVolumeBasicInformation,
898 sizeof(buffer)-
sizeof(WCHAR),
899 &volumeBytesReturned,
902 if (IS_ERROR( hResult )) {
914 "Dos Name Volume Name Status \n" 915 "-------------- ------------------------------------ --------\n" );
923 assert((FIELD_OFFSET(FILTER_VOLUME_BASIC_INFORMATION,FilterVolumeName) + volumeBuffer->FilterVolumeNameLength) <= (
sizeof(buffer)-
sizeof(WCHAR)));
924 _Analysis_assume_((FIELD_OFFSET(FILTER_VOLUME_BASIC_INFORMATION,FilterVolumeName) + volumeBuffer->FilterVolumeNameLength) <= (
sizeof(buffer)-
sizeof(WCHAR)));
926 volumeBuffer->FilterVolumeName[volumeBuffer->FilterVolumeNameLength/
sizeof( WCHAR )] = UNICODE_NULL;
930 printf(
"%-14ws %-36ws %s",
931 (SUCCEEDED( FilterGetDosName(
932 volumeBuffer->FilterVolumeName,
934 sizeof(driveLetter)/
sizeof(WCHAR) )) ? driveLetter : L
""),
935 volumeBuffer->FilterVolumeName,
936 (instanceCount > 0) ?
"Attached" :
"");
938 if (instanceCount > 1) {
940 printf(
" (%d)\n", instanceCount );
947 }
while (SUCCEEDED( hResult = FilterVolumeFindNext( volumeIterator,
948 FilterVolumeBasicInformation,
950 sizeof(buffer)-
sizeof(WCHAR),
951 &volumeBytesReturned ) ));
953 if (HRESULT_FROM_WIN32( ERROR_NO_MORE_ITEMS ) == hResult) {
962 FilterVolumeFindClose( volumeIterator );
965 if (IS_ERROR( hResult )) {
967 if (HRESULT_FROM_WIN32( ERROR_NO_MORE_ITEMS ) == hResult) {
969 printf(
"No volumes found.\n" );
973 printf(
"Volume listing failed with error: 0x%08x\n",
#define INTERPRETER_EXIT_COMMAND1
DWORD InterpretCommand(_In_ int argc, _In_reads_(argc) char *argv[], _In_ PLOG_CONTEXT Context)
DWORD WINAPI RetrieveLogRecords(_In_ LPVOID lpParameter)
#define PROGRAM_EXIT_COMMAND
#define INVALID_HANDLE_VALUE
#define INTERPRETER_EXIT_COMMAND2
#define MINISPY_PORT_NAME
NcLoadRegistryStringRetry NULL
int _cdecl main(_In_ int argc, _In_reads_(argc) char *argv[])
_Analysis_mode_(_Analysis_code_type_user_code_)
VOID DisplayError(_In_ DWORD Code)
ULONG IsAttachedToVolume(_In_ LPCWSTR VolumeName)