Exemplo n.º 1
0
FARPROC WINAPI CAPIHook::GetProcAddress(HMODULE hmod, PCSTR pszProcName) {

   // Get the true address of the function
   FARPROC pfn = GetProcAddressRaw(hmod, pszProcName);

   // Is it one of the functions that we want hooked?
   CAPIHook* p = sm_pHead;
   for (; (pfn != NULL) && (p != NULL); p = p->m_pNext) {

      if (pfn == p->m_pfnOrig) {

         // The address to return matches an address we want to hook
         // Return the hook function address instead
         pfn = p->m_pfnHook;
         break;
      }
   }

   return(pfn);
}
Exemplo n.º 2
0
//---------------------------------------------------------------------------
//SetupHook
void TAPIHook::SetupHook(PSTR DllName, PSTR DllFuncName, PROC pfnHook_Target,bool IsHookSelfDll)
{
  pNextHook  = pHeadHook;    // The next node was at the head
  pHeadHook = this;          // This node is now at the head

  if(pvMaxAppAddr == NULL)
  {
    // Functions with address above lpMaximumApplicationAddress require
    // special processing (Windows 98 only)
    SYSTEM_INFO si;

    GetSystemInfo(&si);
    pvMaxAppAddr = si.lpMaximumApplicationAddress;
  }

  // Save information about this hooked function
  FDllName = DllName;
  FDllFuncName = DllFuncName;
  pfnHook = pfnHook_Target;
  pfnOrig = GetProcAddressRaw(GetModuleHandleA(FDllName), FDllFuncName);
  FIsHookSelfDll = IsHookSelfDll;

  assert(pfnOrig != NULL);  // Function doesn't exist

  if(pfnOrig > pvMaxAppAddr)
  {
    // The address is in a shared DLL; the address needs fixing up
    PBYTE pb = (PBYTE)pfnOrig;
    if(pb[0] == PushOpCode)
    {
      // Skip over the PUSH op code and grab the real address
      PVOID pv = * (PVOID*) &pb[1];
      pfnOrig = (PROC) pv;
    }
  }

  ///////

  ReplaceIATEntryInAllModules(FDllName,pfnOrig, pfnHook,FIsHookSelfDll);
}
Exemplo n.º 3
0
CAPIHook::CAPIHook(PSTR pszCalleeModName, PSTR pszFuncName, PROC pfnHook) {

   // Note: the function can be hooked only if the exporting module 
   //       is already loaded. A solution could be to store the function
   //       name as a member; then, in the hooked LoadLibrary* handlers, parse
   //       the list of CAPIHook instances, check if pszCalleeModName
   //       is the name of the loaded module to hook its export table and 
   //       re-hook the import tables of all loaded modules.
  
   m_pNext  = sm_pHead;    // The next node was at the head
   sm_pHead = this;        // This node is now at the head

   // Save information about this hooked function
   m_pszCalleeModName   = pszCalleeModName;
   m_pszFuncName        = pszFuncName;
   m_pfnHook            = pfnHook;
   m_pfnOrig            = 
      GetProcAddressRaw(GetModuleHandleA(pszCalleeModName), m_pszFuncName);

   // If function does not exit,... bye bye
   // This happens when the module is not already loaded
   if (m_pfnOrig == NULL)
   {
      wchar_t szPathname[MAX_PATH];
      GetModuleFileNameW(NULL, szPathname, _countof(szPathname));
      wchar_t sz[1024];
      StringCchPrintfW(sz, _countof(sz), 
         TEXT("[%4u - %s] impossible to find %S\r\n"), 
         GetCurrentProcessId(), szPathname, pszFuncName);
      OutputDebugString(sz);
      return;
   }
   
#ifdef _DEBUG
   // This section was used for debugging sessions when Explorer died as 
   // a folder content was requested
   // 
   //static BOOL s_bFirstTime = TRUE;
   //if (s_bFirstTime)
   //{
   //   s_bFirstTime = FALSE;

   //   wchar_t szPathname[MAX_PATH];
   //   GetModuleFileNameW(NULL, szPathname, _countof(szPathname));
   //   wchar_t* pszExeFile = wcsrchr(szPathname, L'\\') + 1;
   //   OutputDebugStringW(L"Injected in ");
   //   OutputDebugStringW(pszExeFile);
   //   if (_wcsicmp(pszExeFile, L"Explorer.EXE") == 0)
   //   {
   //      DebugBreak();
   //   }
   //   OutputDebugStringW(L"\n   --> ");
   //   StringCchPrintfW(szPathname, _countof(szPathname), L"%S", pszFuncName);
   //   OutputDebugStringW(szPathname);
   //   OutputDebugStringW(L"\n");
   //}
#endif

   // Hook this function in all currently loaded modules
   ReplaceIATEntryInAllMods(m_pszCalleeModName, m_pfnOrig, m_pfnHook);
}