2016-01-05 23:01:17 +08:00
# pragma once
// hookman.h
// 8/23/2013 jichi
// Branch: ITH/HookManager.h, rev 133
2018-06-03 02:29:32 +08:00
# include "config.h"
2018-07-19 04:18:43 +08:00
# include "textthread.h"
2016-01-05 23:01:17 +08:00
# include "winmutex/winmutex.h"
2018-05-31 16:44:33 +08:00
# include <unordered_map>
2018-07-18 07:18:36 +08:00
# include <string>
# include "vnrhook/include/types.h"
2016-01-05 23:01:17 +08:00
namespace pugi {
class xml_node ;
}
class Profile ;
struct ProcessRecord {
HANDLE process_handle ;
HANDLE hookman_mutex ;
HANDLE hookman_section ;
LPVOID hookman_map ;
2018-06-01 14:36:51 +08:00
HANDLE hostPipe ;
2018-07-18 07:18:36 +08:00
} ;
struct Hook
{
HookParam hp ;
std : : wstring name ;
2016-01-05 23:01:17 +08:00
} ;
2018-07-19 04:18:43 +08:00
typedef DWORD ( * ProcessEventCallback ) ( DWORD pid ) ;
typedef DWORD ( * ThreadEventCallback ) ( TextThread * ) ;
2016-01-05 23:01:17 +08:00
2018-05-31 16:44:33 +08:00
struct ThreadParameterHasher
{
2018-05-31 17:40:00 +08:00
size_t operator ( ) ( const ThreadParameter & tp ) const
2018-05-31 16:44:33 +08:00
{
return std : : hash < DWORD > ( ) ( tp . pid < < 6 ) + std : : hash < DWORD > ( ) ( tp . hook ) + std : : hash < DWORD > ( ) ( tp . retn ) + std : : hash < DWORD > ( ) ( tp . spl ) ;
}
} ;
2018-07-18 05:01:56 +08:00
class DLLEXPORT HookManager
2016-01-05 23:01:17 +08:00
{
public :
HookManager ( ) ;
~ HookManager ( ) ;
TextThread * FindSingle ( DWORD number ) ;
ProcessRecord * GetProcessRecord ( DWORD pid ) ;
2018-07-18 07:18:36 +08:00
Hook GetHook ( DWORD processId , DWORD addr ) ;
void SetHook ( DWORD processId , DWORD addr , Hook hook ) ;
2016-01-05 23:01:17 +08:00
void ClearCurrent ( ) ;
void SelectCurrent ( DWORD num ) ;
void SetCurrent ( TextThread * it ) ;
void AddConsoleOutput ( LPCWSTR text ) ;
// jichi 10/27/2013: Add const; add space.
2018-06-18 09:04:04 +08:00
void DispatchText ( DWORD pid , const BYTE * text , DWORD hook , DWORD retn , DWORD split , int len ) ;
2016-01-05 23:01:17 +08:00
void RemoveProcessContext ( DWORD pid ) ; // private
void RemoveSingleHook ( DWORD pid , DWORD addr ) ;
2018-06-01 14:36:51 +08:00
void RegisterProcess ( DWORD pid , HANDLE hostPipe ) ;
2016-01-05 23:01:17 +08:00
void UnRegisterProcess ( DWORD pid ) ;
//void SetName(DWORD);
2018-07-18 05:01:56 +08:00
HANDLE GetHostPipe ( DWORD pid ) ;
2016-01-05 23:01:17 +08:00
2018-07-19 04:18:43 +08:00
void RegisterThreadCreateCallback ( ThreadEventCallback cf ) { create = cf ; }
void RegisterThreadRemoveCallback ( ThreadEventCallback cf ) { remove = cf ; }
void RegisterThreadResetCallback ( ThreadEventCallback cf ) { reset = cf ; }
void RegisterProcessAttachCallback ( ProcessEventCallback cf ) { attach = cf ; }
void RegisterProcessDetachCallback ( ProcessEventCallback cf ) { detach = cf ; }
2016-01-05 23:01:17 +08:00
2018-07-18 05:01:56 +08:00
void SetSplitInterval ( unsigned int splitDelay ) { this - > splitDelay = splitDelay ; }
2016-01-05 23:01:17 +08:00
void GetProfile ( DWORD pid , pugi : : xml_node profile_node ) ;
private :
2018-07-18 07:18:36 +08:00
std : : unordered_map < ThreadParameter , TextThread * , ThreadParameterHasher > textThreadsByParams ;
std : : unordered_map < ThreadParameter , Hook , ThreadParameterHasher > hooksByAddresses ; // Artikash 7/17/2018: retn and spl should always be zero when accessing this!
2018-05-31 16:44:33 +08:00
std : : unordered_map < DWORD , ProcessRecord * > processRecordsByIds ;
2018-06-13 05:31:24 +08:00
CRITICAL_SECTION hmcs ;
2016-01-05 23:01:17 +08:00
TextThread * current ;
ThreadEventCallback create ,
2018-07-19 04:18:43 +08:00
remove ,
reset ;
2016-01-05 23:01:17 +08:00
ProcessEventCallback attach ,
2018-07-19 04:18:43 +08:00
detach ;
2016-01-05 23:01:17 +08:00
WORD register_count ,
2018-07-19 04:18:43 +08:00
new_thread_number ;
2016-01-05 23:01:17 +08:00
2018-07-18 05:01:56 +08:00
unsigned int splitDelay ;
2016-01-05 23:01:17 +08:00
void HookManager : : AddThreadsToProfile ( Profile & pf , const ProcessRecord & pr , DWORD pid ) ;
} ;
// EOF