Пример #1
0
bool GetSkin ()
{
    struct shmem sh;
    SkinStruct * pSkin = (SkinStruct *)GetSharedMem(&sh);
    if (NULL == pSkin)
        return false;
    copy_skin(pSkin);
    ReleaseSharedMem(&sh);
    return true;
}
Пример #2
0
EXTERN_C BOOL WINAPI
DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
{
    const char *szNewDllName = NULL;
    const char *szNewDllBaseName;

    switch (fdwReason) {
    case DLL_PROCESS_ATTACH:
        if (VERBOSITY > 0) {
            debugPrintf("inject: DLL_PROCESS_ATTACH\n");
        }

        g_hThisModule = hinstDLL;

        if (VERBOSITY > 0) {
            char szProcess[MAX_PATH];
            GetModuleFileNameA(NULL, szProcess, sizeof szProcess);
            debugPrintf("inject: attached to process %s\n", szProcess);
        }

        /*
         * Calling LoadLibrary inside DllMain is strongly discouraged.  But it
         * works quite well, provided that the loaded DLL does not require or do
         * anything special in its DllMain, which seems to be the general case.
         *
         * See also:
         * - http://stackoverflow.com/questions/4370812/calling-loadlibrary-from-dllmain
         * - http://msdn.microsoft.com/en-us/library/ms682583
         */

        if (!USE_SHARED_MEM) {
            szNewDllName = getenv("INJECT_DLL");
            if (!szNewDllName) {
                debugPrintf("inject: warning: INJECT_DLL not set\n");
                return FALSE;
            }
        } else {
            static char szSharedMemCopy[MAX_PATH];
            GetSharedMem(szSharedMemCopy, sizeof szSharedMemCopy);
            szNewDllName = szSharedMemCopy;
        }

        if (VERBOSITY > 0) {
            debugPrintf("inject: loading %s\n", szNewDllName);
        }

        g_hHookModule = LoadLibraryA(szNewDllName);
        if (!g_hHookModule) {
            debugPrintf("inject: warning: failed to load %s\n", szNewDllName);
            return FALSE;
        }

        /*
         * Hook kernel32.dll functions, and its respective Windows API Set.
         *
         * http://msdn.microsoft.com/en-us/library/dn505783.aspx (Windows 8.1)
         * http://msdn.microsoft.com/en-us/library/hh802935.aspx (Windows 8)
         */

        registerLibraryLoaderHooks("kernel32.dll");
        registerLibraryLoaderHooks("api-ms-win-core-libraryloader-l1-1-0.dll");
        registerLibraryLoaderHooks("api-ms-win-core-libraryloader-l1-1-1.dll");
        registerLibraryLoaderHooks("api-ms-win-core-libraryloader-l1-2-0.dll");
        registerLibraryLoaderHooks("api-ms-win-core-kernel32-legacy-l1-1-0.dll");
        registerLibraryLoaderHooks("api-ms-win-core-kernel32-legacy-l1-1-1.dll");

        registerProcessThreadsHooks("kernel32.dll");
        registerProcessThreadsHooks("api-ms-win-core-processthreads-l1-1-0.dll");
        registerProcessThreadsHooks("api-ms-win-core-processthreads-l1-1-1.dll");
        registerProcessThreadsHooks("api-ms-win-core-processthreads-l1-1-2.dll");

        szNewDllBaseName = getBaseName(szNewDllName);
        if (stricmp(szNewDllBaseName, "dxgitrace.dll") == 0) {
            registerModuleHooks("dxgi.dll",    g_hHookModule);
            registerModuleHooks("d3d10.dll",   g_hHookModule);
            registerModuleHooks("d3d10_1.dll", g_hHookModule);
            registerModuleHooks("d3d11.dll",   g_hHookModule);
            registerModuleHooks("d3d9.dll",    g_hHookModule); // for D3DPERF_*
        } else if (stricmp(szNewDllBaseName, "d2d1trace.dll") == 0) {
            registerModuleHooks("d2d1.dll",    g_hHookModule);
            registerModuleHooks("dwrite.dll",  g_hHookModule);
        } else {
            registerModuleHooks(szNewDllBaseName, g_hHookModule);
        }

        dumpRegisteredHooks();

        patchAllModules(ACTION_HOOK);
        break;

    case DLL_THREAD_ATTACH:
        break;

    case DLL_THREAD_DETACH:
        break;

    case DLL_PROCESS_DETACH:
        if (VERBOSITY > 0) {
            debugPrintf("inject: DLL_PROCESS_DETACH\n");
        }

        patchAllModules(ACTION_UNHOOK);

        if (g_hHookModule) {
            FreeLibrary(g_hHookModule);
        }
        break;
    }
    return TRUE;
}
Пример #3
0
EXTERN_C BOOL WINAPI
DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
{
    const char *szNewDllName = NULL;
    HMODULE hNewModule = NULL;
    const char *szNewDllBaseName;

    switch (fdwReason) {
    case DLL_PROCESS_ATTACH:
        if (VERBOSITY > 0) {
            debugPrintf("DLL_PROCESS_ATTACH\n");
        }

        g_hThisModule = hinstDLL;

        {
            char szProcess[MAX_PATH];
            GetModuleFileNameA(NULL, szProcess, sizeof szProcess);
            if (VERBOSITY > 0) {
                debugPrintf("  attached to %s\n", szProcess);
            }
        }

        /*
         * Calling LoadLibrary inside DllMain is strongly discouraged.  But it
         * works quite well, provided that the loaded DLL does not require or do
         * anything special in its DllMain, which seems to be the general case.
         *
         * See also:
         * - http://stackoverflow.com/questions/4370812/calling-loadlibrary-from-dllmain
         * - http://msdn.microsoft.com/en-us/library/ms682583
         */

        if (!USE_SHARED_MEM) {
            szNewDllName = getenv("INJECT_DLL");
            if (!szNewDllName) {
                debugPrintf("warning: INJECT_DLL not set\n");
                return FALSE;
            }
        } else {
            static char szSharedMemCopy[MAX_PATH];
            GetSharedMem(szSharedMemCopy, sizeof szSharedMemCopy);
            szNewDllName = szSharedMemCopy;
        }

        if (VERBOSITY > 0) {
            debugPrintf("  injecting %s\n", szNewDllName);
        }

        hNewModule = LoadLibraryA(szNewDllName);
        if (!hNewModule) {
            debugPrintf("warning: failed to load %s\n", szNewDllName);
            return FALSE;
        }

        szNewDllBaseName = getBaseName(szNewDllName);
        if (stricmp(szNewDllBaseName, "dxgitrace.dll") == 0) {
            replacements[numReplacements].szMatchModule = "dxgi.dll";
            replacements[numReplacements].hReplaceModule = hNewModule;
            ++numReplacements;

            replacements[numReplacements].szMatchModule = "d3d10.dll";
            replacements[numReplacements].hReplaceModule = hNewModule;
            ++numReplacements;

            replacements[numReplacements].szMatchModule = "d3d10_1.dll";
            replacements[numReplacements].hReplaceModule = hNewModule;
            ++numReplacements;

            replacements[numReplacements].szMatchModule = "d3d11.dll";
            replacements[numReplacements].hReplaceModule = hNewModule;
            ++numReplacements;
        } else {
            replacements[numReplacements].szMatchModule = szNewDllBaseName;
            replacements[numReplacements].hReplaceModule = hNewModule;
            ++numReplacements;
        }

        hookAllModules();
        break;

    case DLL_THREAD_ATTACH:
        break;

    case DLL_THREAD_DETACH:
        break;

    case DLL_PROCESS_DETACH:
        if (VERBOSITY > 0) {
            debugPrintf("DLL_PROCESS_DETACH\n");
        }
        break;
    }
    return TRUE;
}
Пример #4
0
//===========================================================================
int HookWindow (HWND hwnd, int early)
{
    LONG_PTR lStyle = GetWindowLongPtr(hwnd, GWL_STYLE);

    // if it does not have a caption, there is nothing to skin.
    if (WS_CAPTION != (lStyle & WS_CAPTION))
    {
        //send_log(hwnd, "no caption");
        return 0;
    }

    LONG_PTR lExStyle = GetWindowLongPtr(hwnd, GWL_EXSTYLE);

    // child windows are excluded unless they have a sysmenu or are MDI clients
    if ((lStyle & WS_CHILD)
        && false == (lStyle & WS_SYSMENU)
        && false == (WS_EX_MDICHILD & lExStyle)
        )
    {
        //send_log(hwnd, "child, no sysmenu, not a MDI");
        return 0;
    }

    // if it is already hooked, dont fall into loop
    if (get_WinInfo(hwnd))
    {
        //send_log(hwnd, "already hooked");
        return 0;
    }

    // being skeptical about windows without sysmenu
    if (false == (lStyle & WS_SYSMENU) && false == (lStyle & WS_VISIBLE))
    {
        //if (0 == early) send_log(hwnd, "invisible without sysmenu");
        return early;
    }

    // check for something like a vertical titlebar, erm...
    if (lExStyle & WS_EX_TOOLWINDOW)
    {
        RECT rc; GetWindowRect(hwnd, &rc);
        ScreenToClient(hwnd, (LPPOINT)&rc.left);
        if (rc.top > -10)
        {
            //if (0 == early) send_log(hwnd, "abnormal caption");
            return early;
        }
    }

    // ------------------------------------------------------
    // now check the exclusion list

    int found = 0;

    struct shmem sh;
    SkinStruct *pSkin = (SkinStruct *)GetSharedMem(&sh);
    if (NULL == pSkin)
        return 0;

    copy_skin(pSkin);

    char sClassName[200]; sClassName[0] = 0;
    GetClassName(hwnd, sClassName, sizeof sClassName);

    char sFileName[200]; sFileName[0] = 0;
    get_module(hwnd, sFileName, sizeof sFileName);

    struct exclusion_item *ei = pSkin->exInfo.ei;
    for (int i = pSkin->exInfo.count; i; --i)
    {
        char *f, *c = (f = ei->buff) + ei->flen;
        // if filename matches and if class matches or is empty...
        bool r = match(sFileName, f) && (0 == *c || match(sClassName, c));
        //dbg_printf("check [%d,%d] %s:%s", r, ei->option, f, c);
        if (r)
        {
            found = 1 == ei->option ? -1 : 1; // check 'hook-early' option
            break;
        }
        ei = (struct exclusion_item *)(c + ei->clen);
    }

    ReleaseSharedMem(&sh);

    // ------------------------------------------------------
    if (early && 0 == found)
    {
        //send_log(hwnd, "Checking later:");
        return 1;
    }

    // send message to log_window
    if (mSkin.enableLog)
    {
        char msg[100];
        sprintf_s(msg, 100, "%s%s",
            found > 0 ? "Excluded" : early ? "Hooked early" : "Hooked",
            IsWindowVisible(hwnd) ? "" : " invisible");
        send_log(hwnd, msg);
    }

    // return when excluded;
    if (found > 0)
        return 0;

    // skin it.
    subclass_window(hwnd);
    return 2;
}