2016-01-05 23:01:17 +08:00
// hookman.cc
// 8/24/2013 jichi
// Branch IHF/HookManager.cpp, rev 133
// 8/24/2013 TODO: Clean up this file
# ifdef _MSC_VER
# pragma warning (disable:4100) // C4100: unreference formal parameter
# pragma warning (disable:4146) // C4146: unary minus operator applied to unsigned type
# endif // _MSC_VER
# include "hookman.h"
# include "vnrhook/include/const.h"
# include "vnrhook/include/defs.h"
# include "vnrhook/include/types.h"
2018-07-19 12:46:52 +08:00
# include "winmutex/winmutex.h"
2018-07-19 11:40:44 +08:00
# include <atlbase.h>
2016-01-05 23:01:17 +08:00
2018-07-19 13:44:44 +08:00
# define HM_LOCK CriticalSectionLocker hmLocker(hmCs) // Synchronized scope for accessing private data
2016-01-05 23:01:17 +08:00
HookManager : : HookManager ( ) :
2018-07-18 05:01:56 +08:00
create ( nullptr ) ,
remove ( nullptr ) ,
reset ( nullptr ) ,
attach ( nullptr ) ,
detach ( nullptr ) ,
2018-07-19 12:46:52 +08:00
nextThreadNumber ( 0 ) ,
2018-07-18 07:18:36 +08:00
textThreadsByParams ( ) ,
2018-07-18 05:01:56 +08:00
processRecordsByIds ( )
2016-01-05 23:01:17 +08:00
{
2018-07-19 13:43:31 +08:00
InitializeCriticalSection ( & hmCs ) ;
2018-06-13 05:31:24 +08:00
2018-07-19 14:02:42 +08:00
// Console text thread
current = textThreadsByParams [ { 0 , - 1UL , - 1UL , - 1UL } ] = new TextThread ( { 0 , - 1UL , - 1UL , - 1UL } , nextThreadNumber + + , splitDelay ) ;
current - > Status ( ) | = USING_UNICODE | CURRENT_SELECT ;
2016-01-05 23:01:17 +08:00
}
HookManager : : ~ HookManager ( )
{
2018-07-19 13:43:31 +08:00
HM_LOCK ;
DeleteCriticalSection ( & hmCs ) ;
2016-01-05 23:01:17 +08:00
}
TextThread * HookManager : : FindSingle ( DWORD number )
2018-07-19 13:43:31 +08:00
{
HM_LOCK ;
2018-07-18 07:18:36 +08:00
for ( auto i : textThreadsByParams )
2018-07-19 13:43:31 +08:00
if ( i . second - > Number ( ) = = number )
2018-05-31 17:40:00 +08:00
return i . second ;
return nullptr ;
}
2016-01-05 23:01:17 +08:00
void HookManager : : SetCurrent ( TextThread * it )
{
2018-07-19 13:43:31 +08:00
HM_LOCK ;
current - > Status ( ) & = ~ CURRENT_SELECT ;
current = it ;
it - > Status ( ) | = CURRENT_SELECT ;
2016-01-05 23:01:17 +08:00
}
2018-07-19 13:43:31 +08:00
2016-01-05 23:01:17 +08:00
void HookManager : : SelectCurrent ( DWORD num )
{
2018-07-19 13:43:31 +08:00
HM_LOCK ;
if ( TextThread * st = FindSingle ( num ) )
{
SetCurrent ( st ) ;
if ( reset ) reset ( st ) ;
}
2016-01-05 23:01:17 +08:00
}
2018-07-19 13:43:31 +08:00
2016-01-05 23:01:17 +08:00
void HookManager : : RemoveSingleHook ( DWORD pid , DWORD addr )
{
2018-07-19 13:43:31 +08:00
HM_LOCK ;
std : : vector < ThreadParameter > removedThreads ;
for ( auto i : textThreadsByParams )
if ( i . first . pid = = pid & & i . first . hook = = addr )
{
if ( remove ) remove ( i . second ) ;
delete i . second ;
removedThreads . push_back ( i . first ) ;
}
for ( auto i : removedThreads ) textThreadsByParams . erase ( i ) ;
SelectCurrent ( 0 ) ;
2016-01-05 23:01:17 +08:00
}
void HookManager : : RemoveProcessContext ( DWORD pid )
{
2018-06-03 02:29:32 +08:00
HM_LOCK ;
std : : vector < ThreadParameter > removedThreads ;
2018-07-18 07:18:36 +08:00
for ( auto i : textThreadsByParams )
2018-07-19 13:43:31 +08:00
if ( i . first . pid = = pid )
2018-06-03 02:29:32 +08:00
{
2018-07-19 13:43:31 +08:00
if ( remove ) remove ( i . second ) ;
2018-06-03 02:29:32 +08:00
delete i . second ;
removedThreads . push_back ( i . first ) ;
}
2018-07-19 13:43:31 +08:00
for ( auto i : removedThreads ) textThreadsByParams . erase ( i ) ;
2018-06-03 02:29:32 +08:00
SelectCurrent ( 0 ) ;
2016-01-05 23:01:17 +08:00
}
2018-06-01 14:36:51 +08:00
void HookManager : : RegisterProcess ( DWORD pid , HANDLE hostPipe )
2016-01-05 23:01:17 +08:00
{
2018-07-19 13:43:31 +08:00
HM_LOCK ;
ProcessRecord * record = processRecordsByIds [ pid ] = new ProcessRecord ;
record - > hostPipe = hostPipe ;
record - > hookman_section = OpenFileMappingW ( FILE_MAP_READ , FALSE , ( ITH_SECTION_ + std : : to_wstring ( pid ) ) . c_str ( ) ) ;
record - > hookman_map = MapViewOfFile ( record - > hookman_section , FILE_MAP_READ , 0 , 0 , HOOK_SECTION_SIZE / 2 ) ; // jichi 1/16/2015: Changed to half to hook section size
record - > process_handle = OpenProcess ( PROCESS_ALL_ACCESS , FALSE , pid ) ;
record - > hookman_mutex = OpenMutexW ( MUTEX_ALL_ACCESS , FALSE , ( ITH_HOOKMAN_MUTEX_ + std : : to_wstring ( pid ) ) . c_str ( ) ) ;
if ( attach ) attach ( pid ) ;
2016-01-05 23:01:17 +08:00
}
void HookManager : : UnRegisterProcess ( DWORD pid )
{
2018-07-19 13:43:31 +08:00
HM_LOCK ;
ProcessRecord pr = * processRecordsByIds [ pid ] ;
CloseHandle ( pr . hookman_mutex ) ;
UnmapViewOfFile ( pr . hookman_map ) ;
CloseHandle ( pr . process_handle ) ;
CloseHandle ( pr . hookman_section ) ;
processRecordsByIds . erase ( pid ) ;
RemoveProcessContext ( pid ) ;
if ( detach ) detach ( pid ) ;
2016-01-05 23:01:17 +08:00
}
2018-07-19 12:46:52 +08:00
void HookManager : : DispatchText ( DWORD pid , DWORD hook , DWORD retn , DWORD spl , const BYTE * text , int len )
2016-01-05 23:01:17 +08:00
{
2018-07-19 13:43:31 +08:00
// jichi 20/27/2013: When PID is zero, the text comes from console, which I don't need
if ( ! text | | ! pid | | len < = 0 )
return ;
HM_LOCK ;
ThreadParameter tp = { pid , hook , retn , spl } ;
TextThread * it ;
if ( ( it = textThreadsByParams [ tp ] ) = = nullptr )
{
it = textThreadsByParams [ tp ] = new TextThread ( tp , nextThreadNumber + + , splitDelay ) ;
if ( create ) create ( it ) ;
}
it - > AddText ( text , len ) ;
2016-01-05 23:01:17 +08:00
}
void HookManager : : AddConsoleOutput ( LPCWSTR text )
{
2018-07-19 13:43:31 +08:00
HM_LOCK ;
2018-07-20 01:34:26 +08:00
int len = wcslen ( text ) * 2 ;
TextThread * console = textThreadsByParams [ { 0 , - 1UL , - 1UL , - 1UL } ] ;
console - > AddSentence ( std : : wstring ( text ) ) ;
2016-01-05 23:01:17 +08:00
}
void HookManager : : ClearCurrent ( )
{
2018-07-19 13:43:31 +08:00
HM_LOCK ;
current - > Reset ( ) ;
if ( reset ) reset ( current ) ;
2016-01-05 23:01:17 +08:00
}
ProcessRecord * HookManager : : GetProcessRecord ( DWORD pid )
{
2018-07-19 13:43:31 +08:00
HM_LOCK ;
return processRecordsByIds [ pid ] ;
2016-01-05 23:01:17 +08:00
}
2018-07-18 05:01:56 +08:00
HANDLE HookManager : : GetHostPipe ( DWORD pid )
2016-01-05 23:01:17 +08:00
{
2018-07-19 13:43:31 +08:00
HM_LOCK ;
return processRecordsByIds [ pid ] ? processRecordsByIds [ pid ] - > hostPipe : nullptr ;
2016-01-05 23:01:17 +08:00
}
2018-07-19 11:40:44 +08:00
HookParam HookManager : : GetHookParam ( DWORD pid , DWORD addr )
2018-07-18 07:18:36 +08:00
{
2018-07-19 13:43:31 +08:00
HM_LOCK ;
2018-07-19 11:40:44 +08:00
HookParam ret = { } ;
ProcessRecord * pr = GetProcessRecord ( pid ) ;
if ( pr = = nullptr ) return ret ;
2018-07-19 13:43:31 +08:00
MutexLocker locker ( pr - > hookman_mutex ) ;
2018-07-19 11:40:44 +08:00
const Hook * hooks = ( const Hook * ) pr - > hookman_map ;
for ( int i = 0 ; i < MAX_HOOK ; + + i )
if ( hooks [ i ] . Address ( ) = = addr )
ret = hooks [ i ] . hp ;
return ret ;
2016-01-05 23:01:17 +08:00
}
2018-07-19 11:40:44 +08:00
std : : wstring HookManager : : GetHookName ( DWORD pid , DWORD addr )
2016-01-05 23:01:17 +08:00
{
2018-07-19 13:43:31 +08:00
HM_LOCK ;
2018-07-19 11:40:44 +08:00
std : : string buffer ;
ProcessRecord * pr = GetProcessRecord ( pid ) ;
if ( pr = = nullptr ) return L " " ;
2018-07-19 13:43:31 +08:00
MutexLocker locker ( pr - > hookman_mutex ) ;
2018-07-19 11:40:44 +08:00
USES_CONVERSION ;
const Hook * hooks = ( const Hook * ) pr - > hookman_map ;
for ( int i = 0 ; i < MAX_HOOK ; + + i )
2016-01-05 23:01:17 +08:00
{
2018-07-19 11:40:44 +08:00
if ( hooks [ i ] . Address ( ) = = addr )
2016-01-05 23:01:17 +08:00
{
2018-07-19 11:40:44 +08:00
buffer . resize ( hooks [ i ] . NameLength ( ) ) ;
ReadProcessMemory ( pr - > process_handle , hooks [ i ] . Name ( ) , & buffer [ 0 ] , hooks [ i ] . NameLength ( ) , nullptr ) ;
2016-01-05 23:01:17 +08:00
}
2018-07-19 13:43:31 +08:00
}
2018-07-19 11:40:44 +08:00
return std : : wstring ( A2W ( buffer . c_str ( ) ) ) ;
2016-01-05 23:01:17 +08:00
}
// EOF