⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 verifier.c

📁 WinCE5.0部分核心源码
💻 C
📖 第 1 页 / 共 3 页
字号:
    else if (pMod && (hKey = OpenShimKey (pCurProc->lpszProcName, TRUE))) {
        fShouldHook = ShouldShimLoadedModules (hKey);
    }

    if (hKey) {
        RegCloseKey (hKey);
    }

    return fShouldHook;
}

BOOL VerifyNullLists (void)
{
    LPMODULE pMod;
    LPSHIMINFO pShimInfo;
    LPSHIMINFO pShimInfoTemp;
    BOOL fRet = TRUE;

    g_Imports.EnterCriticalSection (g_Imports.pModListcs);

    for (pMod = pModList; pMod; pMod = pMod->pMod) {
        // Walk the list of shim modules injected into this image, and free the
        // list. Everything is invalid.
        pShimInfo = pMod->pShimInfo ? ((LPSHIMREF)pMod->pShimInfo)->pShimInfo [pCurProc->procnum] : NULL;

        if (pShimInfo) {
            fRet = FALSE;
            RETAILMSG (1, (_T("ShimEngine ERROR: stale info for '%s' (pMod 0x%08x)\r\n"),
                pMod->lpszModName, pMod));
            DEBUGCHK(0);

            while (pShimInfo) {
                pShimInfoTemp = pShimInfo;
                pShimInfo = pShimInfo->pNext;

                g_Imports.FreeMem (pShimInfoTemp, pShimInfoTemp->wPool);
            }

            ((LPSHIMREF)pMod->pShimInfo)->pShimInfo [pCurProc->procnum] = NULL;
        }
    }

    g_Imports.LeaveCriticalSection (g_Imports.pModListcs);

    return fRet;
}

BOOL ShimInitModule (e32_lite *eptr, o32_lite *oarry, DWORD BaseAddr, LPCTSTR szModuleName)
{
    PMODULE pMod;
    PMODULE pModShim;
    TCHAR _szModuleName [MAX_PATH];

    // Don't alter a shim's imports
    if (!GetNameFromE32 (eptr, _szModuleName, MAX_PATH, & pMod)) {
        RETAILMSG(1, (TEXT("ShimInitModule: Couldn't find module '%s'\r\n"), szModuleName));
        return FALSE;
    }

    if (!ShouldShimThisModule (pMod, _szModuleName))
        return FALSE;

    // Make sure we acknowledge the fact that we're using imports that weren't
    // originally intended.

    RETAILMSG(1, (TEXT("-----> Using alternate imports for module %s\r\n"), szModuleName));

    // Make sure shim_verifier is injected into all shimmed modules.
    // Only inject into a process here - we'll inject into a dll later (when
    // the shimref structure is allocated).
    if (!pMod && (pModShim = LoadShim (BASE_SHIM))) {
        // This is a new process - make sure there are no dll's out there that
        // have shim references in this process. If they do, they're old, and
        // the need to be cleared.
        VerifyNullLists ();

        // Add the base shim to the process's shim list.
        AddShimToList (BASE_SHIM, pModShim, NULL);
    }

    return TRUE;
}

static PTCHAR _ustoa (unsigned short int n, PTCHAR pszOut)
{
    DWORD d4, d3, d2, d1, d0, q;
    PTCHAR p;
    INT i;

    if (!n) {
        pszOut [0] = _T('0');
        pszOut [1] = 0;
        return pszOut;
    }

    d1 = (n >> 4) & 0xF;
    d2 = (n >> 8) & 0xF;
    d3 = (n >> 12) & 0xF;

    // We can't use FP operations here, so a divide by 10 is implemented as
    // a multiply, and a shift right.

    d0 = 6 * (d3 + d2 + d1) + (n & 0xF);
    q = (d0 * 0x19A) >> 12; // * 410 / 4096, or / .10009
    d0 = d0 - 10 * q;

    d1 = q + 9 * d3 + 5 * d2 + d1;
    q = (d1 * 0x19A) >> 12; // * 410 / 4096, or / .10009
    d1 = d1 - 10 * q;

    d2 = q + 2 * d2;
    q = (d2 * 0x1A) >> 8; // * 26 / 256, or / .1015
    d2 = d2 - 10 * q;

    d3 = q + 4 * d3;
    d4 = (d3 * 0x1A) >> 8; // * 26 / 256, or / .1015
    d3 = d3 - 10 * d4;

    if (pszOut) {
        pszOut [0] = (unsigned short) d4 + _T('0');
        pszOut [1] = (unsigned short) d3 + _T('0');
        pszOut [2] = (unsigned short) d2 + _T('0');
        pszOut [3] = (unsigned short) d1 + _T('0');
        pszOut [4] = (unsigned short) d0 + _T('0');

        // Trim the leading zero's
        for (p = pszOut; *p == TEXT('0'); p++);
        if (p == pszOut)
            return pszOut;
        for (i = 0; i < 5 - (p - pszOut); i++)
            pszOut [i] = p [i];
        pszOut [i] = 0;
    }

    return pszOut;
}

BOOL MakeValueName (LPCTSTR impmodname, DWORD ord, DWORD BaseAddr, LPTSTR szValueName, DWORD cchValueName)
{
    struct ImpProc *impptr;
    DWORD cch;

    // Get the name of the dependent module (module which is being imported from).
    vrf_wcscpy (szValueName, impmodname);

    // Append '-'
    cch = vrf_wcslen (szValueName);
    szValueName [cch++] = _T('-');

    // Append the imported ordinal (name or number).
    if (ord & 0x80000000) {
        // Importing by ordinal
        _ustoa ((unsigned short)(ord & 0x7fffffff), szValueName + cch);
    }
    else {
        // Importing by name
        impptr = (struct ImpProc *)((ord&0x7fffffff)+BaseAddr);
        g_Imports.KAsciiToUnicode (szValueName + cch, (LPCHAR)impptr->ip_name, 38);
    }

    // szValueName now contains a string representing the function being imported,
    // in the form 'module-ordinal'

    DEBUGMSG (ZONE_SEARCH, (_T("MakeValueName: '%s'\r\n"), szValueName));
    return TRUE;
}

PMODULE
ShimWhichMod (
    PMODULE pmod, // module structure of DLL from which the image imports functions
    LPCTSTR modname, // name of the image
    LPCTSTR impmodname, // names of the DLL from which the image imports functions
    DWORD BaseAddr, // base address of image
    DWORD ord, // ordinal (or name) of imported function
    e32_lite *eptr
    )
{
    PMODULE pModImage;
    PMODULE pModRet;
    TCHAR szModuleName [MAX_PATH];
    WCHAR szValueName [MAX_PATH];
    HKEY hKey;
    WCHAR szShim [MAX_PATH];
    LPSHIMINFO pShimInfo;
    DWORD cbData;
    DWORD dwType;
    LONG lRet;

    DEBUGMSG(ZONE_ENTRY2, (TEXT("++WhichMod (0x%08x, %s, %s, 0x%08x, 0x%08x)\r\n"),
        pmod, modname, impmodname, BaseAddr, ord));

    // Is this an exe or dll? We need to know where to put the shim info.
    if (!GetNameFromE32 (eptr, szModuleName, MAX_PATH, & pModImage)) {
        RETAILMSG (1, (_T("ShimWhichMod: Couldn't find module '%s'\r\n"), szModuleName));
        return pmod;
    }

    if (!MakeValueName (impmodname, ord, BaseAddr, szValueName, MAX_PATH)) {
        RETAILMSG (1, (_T("ShimWhichMod: Couldn't generate value key\r\n")));
        return pmod;
    }

    szShim [0] = _T('\0');

    // Look for an alternate import under this module's reg key root
    if (hKey = OpenShimKey (szModuleName, pModImage ? FALSE : TRUE)) {
        cbData = MAX_PATH;
        lRet = RegQueryValueEx (hKey, szValueName, 0, & dwType, szShim, & cbData);
        RegCloseKey (hKey);
    }

    // If this is a dll, and the loading process's settings are to shim loaded
    // modules, look for an alternate import under that key.
    if (!szShim[0] && pModImage && (hKey = OpenShimKey (pCurProc->lpszProcName, TRUE)) && ShouldShimLoadedModules (hKey)) {
        cbData = MAX_PATH;
        lRet = RegQueryValueEx (hKey, szValueName, 0, & dwType, szShim, & cbData);
    }

    if (hKey) {
        RegCloseKey (hKey);
    }

    // Finally, see if there's a global setting for this import
    if (!szShim[0] && (hKey = OpenShimKey (_T("{all}"), FALSE))) {
        cbData = MAX_PATH;
        lRet = RegQueryValueEx (hKey, szValueName, 0, & dwType, szShim, & cbData);
        RegCloseKey (hKey);
    }

    if (szShim[0]) {
        // This import is to be shimmed. szShim contains the name of the shim.
        if (pModImage && !pModImage->pShimInfo) {
            // Allocate a SHIMREF structure for this module
            pModImage->pShimInfo = (LPVOID) g_Imports.AllocMem (HEAP_SHIMREF);
            memset (pModImage->pShimInfo, 0, sizeof (SHIMREF));

            // Make sure shim_verifier is injected into all shimmed modules, too
            pModRet = LoadShim (BASE_SHIM);

            if (pModRet) {
                // Successfully loaded the shim. Add it to this module's list of shims.
                AddShimToList (BASE_SHIM, pModRet, pModImage);
            }
        }

        if (!(pShimInfo = FindShimInfo (pModImage ? ((LPSHIMREF)pModImage->pShimInfo)->pShimInfo [pCurProc->procnum] : pCurProc->pShimInfo, szShim))) {
            // The shim has not been loaded by this module yet. Load it now.
            pModRet = LoadShim (szShim);

            if (pModRet) {
                // Successfully loaded the shim. Add it to this module's list of shims.
                AddShimToList (szShim, pModRet, pModImage);
            }
            else {
                // Loading the shim failed. Import from the original module.
                pModRet = pmod;
            }
        }
        else {
            // The shim has already been loaded by this image. Don't load it again.
            pModRet = pShimInfo->pMod;
        }
    }
    else {
        // This import is not shimmed.
        pModRet = pmod;
    }

    RETAILMSG((pModRet != pmod) && !g_fUnLoadingShim,
        (TEXT("*** DoImports (%s): importing %s from %s\r\n"), modname, szValueName, pModRet->lpszModName));

    return pModRet;
}

BOOL
GetNameFromE32(
    e32_lite *eptr,
    LPWSTR lpszModuleName,
    DWORD cchModuleName,
    PMODULE *ppModule
    )
{
    PMODULE pMod;

    *ppModule = NULL;

    if (eptr == & pCurProc->e32) {
        vrf_wcsncpy (lpszModuleName, pCurProc->lpszProcName, cchModuleName);
        return TRUE;
    }

    g_Imports.EnterCriticalSection (g_Imports.pModListcs);

    for (pMod = pModList; pMod; pMod = pMod->pMod) {
        if (eptr == & pMod->e32) {
            vrf_wcsncpy (lpszModuleName, pMod->lpszModName, cchModuleName);
            *ppModule = pMod;
            break;
        }
    }

    g_Imports.LeaveCriticalSection (g_Imports.pModListcs);

    RETAILMSG (!*ppModule, (TEXT("Couldn't find module with eptr=0x%08x\r\n"), eptr));
    DEBUGCHK (*ppModule);
    return *ppModule ? TRUE : FALSE;
}

BOOL
IsShimDll(
    PMODULE pMod
    )
{
    return WIN32CALL(GetProcAddressA, (pMod, "QueryShimInfo")) ? TRUE : FALSE;
}

BOOL
ShimUnDoDepends(
    e32_lite *eptr,
    DWORD BaseAddr,
    BOOL fAddToList
    )
{
    TCHAR szModuleName [MAX_PATH];
    PMODULE pMod;
    PMODULE pModVLog = NULL;
    PMODULE pModCoredll = NULL;
    LPSHIMINFO pShimInfo;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -