void Initialize() { DWORD mainThreadId = GetMainThreadId(); if (mainThreadId == 0) { MessageBox(NULL, L"ERROR: Main thread not found!", modName, MB_OK); return; } HANDLE mainThreadHandle = OpenThread(THREAD_ALL_ACCESS, false, mainThreadId); // TODO consider using THREAD_SUSPEND_RESUME if (mainThreadHandle == NULL) { MessageBox(NULL, L"ERROR: Cannot open the main thread!", modName, MB_OK); return; } void* patternCheckAutoSwingLocation = ScanPattern(0, 0xffffffff, patternCheckAutoSwing, sizeof(patternCheckAutoSwing)); // TODO make this faster if (patternCheckAutoSwingLocation == NULL) { MessageBox(NULL, L"ERROR: patternCheckAutoSwingLocation not found. Are you playing a different version?", modName, MB_OK); return; } SuspendThread(mainThreadHandle); NopMemory((void*)((unsigned int)patternCheckAutoSwingLocation + 7), 6, nullptr); ResumeThread(mainThreadHandle); CloseHandle(mainThreadHandle); MessageBox(NULL, L"Ready!", modName, MB_OK); }
unsigned int DumpModuleDlg::GetEIPRegister() { DWORD thread_id = GetMainThreadId(processid); unsigned int entrypoint = thread_id; char epaddress[20]; wsprintf(epaddress,"%X",entrypoint); // convert number to hex string typedef HANDLE (WINAPI *OPENTHREAD)(DWORD ,BOOL,DWORD); typedef BOOL (WINAPI *PINITIALIZECONTEXT)(PVOID Buffer, DWORD ContextFlags, PCONTEXT* Context, PDWORD ContextLength); HMODULE kernel32 = LoadLibrary(_T("kernel32.dll")); OPENTHREAD m_OpenThread =(OPENTHREAD)GetProcAddress(kernel32, "OpenThread"); HANDLE hThread = m_OpenThread(THREAD_ALL_ACCESS,TRUE,thread_id); PCONTEXT Context; DWORD ContextSize = 0; PINITIALIZECONTEXT pfnInitializeContext = (PINITIALIZECONTEXT)GetProcAddress(kernel32, "InitializeContext"); ContextSize = 0; int Success = pfnInitializeContext(NULL, CONTEXT_FULL, NULL, &ContextSize); PVOID Buffer = malloc(ContextSize); Success = pfnInitializeContext(Buffer, CONTEXT_FULL, &Context, &ContextSize); int contextOk = GetThreadContext(hThread, Context); unsigned int eip = Context->Eip; return eip; }
RMS_Socket::RMS_Socket() { if (WSAStartup(MAKEWORD(1, 1), &wsa) != 0) { perror("初始化资源失败"); exit(-1); } //创建线程 sockThreadHnd = CreateThread(NULL, 0, CommManager, NULL, 0, &sockThreadId); mainThreadId = GetMainThreadId(); }
static BOOL GetMainThreadContext(PCONTEXT context) { DWORD thread_id = GetMainThreadId(); if (thread_id != 0) { HANDLE thread_handle = GetThreadHandle(thread_id, THREAD_GET_CONTEXT | THREAD_SUSPEND_RESUME); if (thread_handle != NULL) { if (SuspendThread(thread_handle) != (DWORD)-1) { BOOL ok = GetThreadContext(thread_handle, context) != 0; ResumeThread(thread_handle); return ok; } } } return FALSE; }
BOOL WINAPI DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { BOOL lbAllow = TRUE; switch(ul_reason_for_call) { case DLL_PROCESS_ATTACH: { gnDllState = ds_DllProcessAttach; #ifdef _DEBUG HANDLE hProcHeap = GetProcessHeap(); #endif HeapInitialize(); ghOurModule = (HMODULE)hModule; ghConWnd = GetConsoleWindow(); if (ghConWnd) GetConsoleTitle(gsInitConTitle, countof(gsInitConTitle)); gnSelfPID = GetCurrentProcessId(); ghWorkingModule = (u64)hModule; gfGetRealConsoleWindow = GetConsoleWindow; user = (UserImp*)calloc(1, sizeof(*user)); GetMainThreadId(); // Инициализировать gnHookMainThreadId gcchLastWriteConsoleMax = 4096; gpszLastWriteConsole = (wchar_t*)calloc(gcchLastWriteConsoleMax,sizeof(*gpszLastWriteConsole)); gInQueue.Initialize(512, NULL); #ifdef _DEBUG gAllowAssertThread = am_Pipe; #endif #ifdef _DEBUG #ifdef UseDebugExceptionFilter gfnPrevFilter = SetUnhandledExceptionFilter(HkExceptionFilter); #endif #endif #ifdef SHOW_STARTED_MSGBOX if (!IsDebuggerPresent()) { ::MessageBox(ghConEmuWnd, L"ConEmuHk*.dll loaded", L"ConEmu hooks", MB_SYSTEMMODAL); } #endif #ifdef _DEBUG DWORD dwConMode = -1; GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwConMode); #endif //_ASSERTE(ghHeap == NULL); //ghHeap = HeapCreate(HEAP_GENERATE_EXCEPTIONS, 200000, 0); wchar_t szEvtName[64]; msprintf(szEvtName, countof(szEvtName), CECONEMUROOTPROCESS, gnSelfPID); HANDLE hRootProcessFlag = OpenEvent(SYNCHRONIZE|EVENT_MODIFY_STATE, FALSE, szEvtName); DWORD nWaitRoot = -1; if (hRootProcessFlag) { nWaitRoot = WaitForSingleObject(hRootProcessFlag, 0); gbSelfIsRootConsoleProcess = (nWaitRoot == WAIT_OBJECT_0); } SafeCloseHandle(hRootProcessFlag); #ifdef HOOK_USE_DLLTHREAD _ASSERTEX(FALSE && "Hooks starting in background thread?"); //HANDLE hEvents[2]; //hEvents[0] = CreateEvent(NULL, FALSE, FALSE, NULL); //hEvents[1] = ghStartThread = CreateThread(NULL, 0, DllStart, NULL/*(LPVOID)(hEvents[0])*/, 0, &gnStartThreadID); if (ghStartThread == NULL) { //_ASSERTE(ghStartThread!=NULL); wchar_t szMsg[128]; DWORD nErrCode = GetLastError(); msprintf(szMsg, countof(szMsg), L"Failed to start DllStart thread!\nErrCode=0x%08X\nPID=%u", nErrCode, GetCurrentProcessId()); GuiMessageBox(ghConEmuWnd, szMsg, L"ConEmu hooks", 0); } else { DWORD nThreadWait = WaitForSingleObject(ghStartThread, 5000); DllThreadClose(); } //DWORD nThreadWait = WaitForMultipleObjects(hEvents, countof(hEvents), FALSE, INFINITE); //CloseHandle(hEvents[0]); #else DllStart(NULL); #endif user->setAllowLoadLibrary(); } break; case DLL_THREAD_ATTACH: { gnDllThreadCount++; if (gbHooksWasSet) InitHooksRegThread(); } break; case DLL_THREAD_DETACH: { #ifdef SHOW_SHUTDOWN_STEPS gnDbgPresent = 0; ShutdownStep(L"DLL_THREAD_DETACH"); #endif if (gbHooksWasSet) DoneHooksRegThread(); // DLL_PROCESS_DETACH зовется как выяснилось не всегда if (gnHookMainThreadId && (GetCurrentThreadId() == gnHookMainThreadId) && !gbDllDeinitialized) { gbDllDeinitialized = true; //WARNING!!! OutputDebugString must NOT be used from ConEmuHk::DllMain(DLL_PROCESS_DETACH). See Issue 465 DllStop(); } gnDllThreadCount--; ShutdownStep(L"DLL_THREAD_DETACH done, left=%i", gnDllThreadCount); } break; case DLL_PROCESS_DETACH: { ShutdownStep(L"DLL_PROCESS_DETACH"); gnDllState = ds_DllProcessDetach; if (gbHooksWasSet) lbAllow = FALSE; // Иначе свалимся, т.к. FreeLibrary перехвачена // Уже могли дернуть в DLL_THREAD_DETACH if (!gbDllDeinitialized) { gbDllDeinitialized = true; //WARNING!!! OutputDebugString must NOT be used from ConEmuHk::DllMain(DLL_PROCESS_DETACH). See Issue 465 DllStop(); } // -- free не нужен, т.к. уже вызван HeapDeinitialize() //free(user); ShutdownStep(L"DLL_PROCESS_DETACH done"); } break; } return lbAllow; }