Injector.cpp
上传用户:jstlsd
上传日期:2007-01-13
资源大小:186k
文件大小:14k
- //---------------------------------------------------------------------------
- //
- // Injector.cpp
- //
- // SUBSYSTEM:
- // API hooking system
- // MODULE:
- // Implements injection mechanism
- //
- // DESCRIPTION:
- //
- //
- // AUTHOR: Ivo Ivanov (ivopi@hotmail.com)
- // DATE: 2001 November 13, version 1.0
- //
- // FIXES:
- // - 2002 November 20
- // Added a mechanism for handling reference count of the HookTool.dll
- // instances. This allows us to fix a problem caused by the asynchronous
- // behavior of ::UnhookWindowsHookEx() API
- //
- //---------------------------------------------------------------------------
- #include "Injector.h"
- #include "..CommonModuleInstance.h"
- #include "..CommonSysUtils.h"
- #include "..CommonIniFile.h"
- #include "NtInjectorThread.h"
- //---------------------------------------------------------------------------
- //
- // Thread function prototype
- //
- //---------------------------------------------------------------------------
- typedef unsigned (__stdcall *PTHREAD_START)(void *);
- //---------------------------------------------------------------------------
- //
- // External declarations
- //
- //---------------------------------------------------------------------------
- extern LRESULT CALLBACK GetMsgProc(
- int code, // hook code
- WPARAM wParam, // removal option
- LPARAM lParam // message
- );
- //---------------------------------------------------------------------------
- //
- // class CInjector
- //
- //---------------------------------------------------------------------------
- CInjector::CInjector(BOOL bServerInstance):
- m_bServerInstance(bServerInstance),
- m_bHookAllEnabledInitialized(FALSE),
- m_bHookAllEnabled(FALSE)
- {
- }
- CInjector::~CInjector()
- {
- }
- //
- // examines whether a process should be hooked up by the DLL
- //
- BOOL CInjector::IsProcessForHooking(PSTR pszExaminedProcessName)
- {
- BOOL bHoolAll = GetHookAllEnabled();
- BOOL bProcessProtected = FALSE;
- char szProcessName[MAX_PATH];
- DWORD dwStartPos = 0;
- long nCommaPos;
- if (bHoolAll)
- {
- while ( (dwStartPos < strlen(m_szProcessesForHooking)) &&
- GetNextCommaSeparatedString(
- &m_szProcessesForHooking[dwStartPos],
- szProcessName,
- sizeof(szProcessName),
- &nCommaPos
- )
- )
- {
- strcat(szProcessName, ".exe");
- if (0 == stricmp(szProcessName, pszExaminedProcessName))
- {
- bProcessProtected = TRUE;
- return FALSE;
- } // if
- dwStartPos += nCommaPos + 1;
- } // while
- } // if
- if (!bHoolAll)
- {
- dwStartPos = 0;
- while ( (dwStartPos < strlen(m_szProcessesForHooking)) &&
- GetNextCommaSeparatedString(
- &m_szProcessesForHooking[dwStartPos],
- szProcessName,
- sizeof(szProcessName),
- &nCommaPos
- )
- )
- {
- strcat(szProcessName, ".exe");
- if (0 == stricmp(szProcessName, pszExaminedProcessName))
- return TRUE;
- dwStartPos += nCommaPos + 1;
- } // while
- return FALSE;
- } // if
- return TRUE;
- }
- //
- // Return the name of the INI file
- //
- void CInjector::GetIniFile(char* pszIniFile)
- {
- char *pdest;
- ::GetModuleFileName(
- ModuleFromAddress(GetMsgProc),
- pszIniFile,
- MAX_PATH
- );
- pdest = &pszIniFile[strlen(pszIniFile) - 4];
- strcpy(pdest, ".ini");
- }
- //
- // Get the value of [Scope] / HookAll from the INI file
- //
- BOOL CInjector::GetHookAllEnabled()
- {
- if (!m_bHookAllEnabledInitialized)
- {
- char szIniFile[MAX_PATH];
- GetIniFile(szIniFile);
- CIniFile iniFile(szIniFile);
- m_bHookAllEnabled = iniFile.ReadBool(
- "Scope",
- "HookAll",
- TRUE
- );
- m_bHookAllEnabledInitialized = TRUE;
- strcpy(m_szProcessesForHooking, "