#include #include #include #include #define _WIN32_WINNT >= 0x0501 // for the IsDebuggerPresent() call. // but we are using the TEB anyways. //----< thread param >---- typedef struct { unsigned tId ; void ( * pFunc ) ( DWORD, DWORD ) ; } PARAM, *pPARAM ; //----< The Thread Proc >---- unsigned __stdcall ThreadFunc (void * p) { pPARAM thrParam = (pPARAM) p ; thrParam -> pFunc ( GetCurrentThreadId () , GetCurrentProcessId () ) ; return 0 ; } //----< program entry point >---- void main () { HMODULE hModule ; void ( * pDllSayHello ) ( DWORD, DWORD ) ; FARPROC pDllFunc ; /////////////////////////////////////////////////////////// /* This should generate LOAD_DLL_DEBUG_EVENT in debugger */ /////////////////////////////////////////////////////////// hModule = LoadLibrary ( "../../dll/Debug/Dll.dll" ) ; if( hModule == NULL) { printf ( "CLIENT : DLL could not be loaded\n" ) ; return ; } else // begining of else clause { printf ( "CLIENT : DLL loaded successfully\n" ) ; /* now get function ptr to dll's function */ pDllFunc = GetProcAddress ( hModule, "SayHello" ) ; if ( pDllFunc == NULL ) printf ( "CLIENT : Pointer to Dll Function could not be obtained\n" ) ; else { printf ( "CLIENT : Pointer to Dll Function successfully obtained\n" ) ; pDllSayHello = ( void ( * ) ( DWORD, DWORD ) ) pDllFunc ; pDllSayHello ( GetCurrentThreadId () , GetCurrentProcessId () ) ; } /* spawn a worker thread */ //////////////////////////// HANDLE hThread ; unsigned threadId ; PARAM param ; param.pFunc = pDllSayHello ; //////////////////////////////////////////////////////////////// /* This should generate CREATE_THREAD_DEBUG_EVENT in debugger */ //////////////////////////////////////////////////////////////// hThread = (HANDLE) _beginthreadex ( NULL, // default security attributes 0, // use default stack size ThreadFunc, // thread function ¶m, // Argument list 0, // use default creation flags &threadId );// returns the thread identifier WaitForSingleObject ( hThread , INFINITE ) ; ////////////////////////////////////////////////////////////// /* This should generate EXIT_THREAD_DEBUG_EVENT in debugger */ ////////////////////////////////////////////////////////////// CloseHandle ( hThread ) ; /* unload the library */ ///////////////////////////////////////////////////////////// /* This should generate UNLOAD_DLL_DEBUG_EVENT in debugger */ ///////////////////////////////////////////////////////////// if ( FreeLibrary ( hModule ) ) printf ( "CLIENT : DLL unloaded successfully\n" ) ; else printf ( "CLIENT : DLL could not be unloaded successfully\n" ) ; } // end of else clause /* is the debugger watching us ? */ BOOL Dbugged = FALSE ; __asm { MOV EAX, FS:[0x00000018] MOV EAX, DWORD PTR [EAX + 0x30] MOVZX EAX, BYTE PTR [EAX + 0x02] MOV Dbugged, EAX } // // FIX THIS -> use the _WIN32_WINNT > =0x0400 macro redefinition /* If yes, let us say hello to the debugger */ //////////////////////////////////////////////////////////////// /* This should generate OUTPUT_DEBUG_STRING_EVENT in debugger */ //////////////////////////////////////////////////////////////// if (Dbugged == TRUE ) OutputDebugString ( "I know you're watching !" ) ; printf ( "\n\nPress any key to exit\n\n" ) ; int x = getche () ; }