Update CMakeLists.txt

Update host.cpp
This commit is contained in:
恍兮惚兮 2024-05-13 20:54:44 +08:00
parent fe499a63a2
commit 68157d443f
8 changed files with 49 additions and 61 deletions

View File

@ -80,9 +80,10 @@ DWORD WINAPI Pipe(LPVOID)
}
}
void TextOutput(ThreadParam tp, TextOutput_T*buffer, int len)
void TextOutput(const ThreadParam& tp, const HookParam& hp, TextOutput_T*buffer, int len)
{
buffer->tp=tp;
memcpy(&buffer->tp,&tp,sizeof(tp));
memcpy(&buffer->hp,&hp,sizeof(hp));
WriteFile(hookPipe, buffer, sizeof(TextOutput_T) + len, DUMMY, nullptr);
}
@ -107,9 +108,10 @@ void NotifyHookRemove(uint64_t addr, LPCSTR name)
HookRemovedNotif buffer(addr);
WriteFile(hookPipe, &buffer, sizeof(buffer), DUMMY, nullptr);
}
void NotifyHookInserting(uint64_t addr)
void NotifyHookInserting(uint64_t addr,wchar_t hookcode[])
{
HookInsertingNotif buffer(addr);
wcscpy(buffer.hookcode,hookcode);
WriteFile(hookPipe, &buffer, sizeof(buffer), DUMMY, nullptr);
}
BOOL WINAPI DllMain(HINSTANCE hModule, DWORD fdwReason, LPVOID)
@ -128,9 +130,9 @@ BOOL WINAPI DllMain(HINSTANCE hModule, DWORD fdwReason, LPVOID)
*ptr=MapViewOfFile(handle, FILE_MAP_ALL_ACCESS | FILE_MAP_EXECUTE, 0, 0, sz);
memset(*ptr, 0, sz);
};
createfm(mappedFile,(void**)&hooks,MAX_HOOK * sizeof(TextHook),ITH_SECTION_ + std::to_wstring(GetCurrentProcessId()));
hooks=(decltype(hooks))new TextHook[MAX_HOOK];
VirtualProtect((LPVOID)hooks,sizeof(TextHook) *MAX_HOOK,PAGE_EXECUTE_READWRITE,DUMMY);
createfm(mappedFile3,(void**)&embedsharedmem, sizeof(EmbedSharedMem),EMBED_SHARED_MEM + std::to_wstring(GetCurrentProcessId()));
MH_Initialize();
@ -141,7 +143,7 @@ BOOL WINAPI DllMain(HINSTANCE hModule, DWORD fdwReason, LPVOID)
{
MH_Uninitialize();
detachall( );
UnmapViewOfFile(hooks);
delete []hooks;
UnmapViewOfFile(embedsharedmem);
}
break;
@ -184,7 +186,7 @@ bool NewHook_1(HookParam& hp, LPCSTR lpname)
return false;
}
else{
NotifyHookInserting(hp.address);
NotifyHookInserting(hp.address,hp.hookcode);
return true;
}
}

View File

@ -5,7 +5,7 @@
// Branch: ITH/IHF_DLL.h, rev 66
void TextOutput(ThreadParam tp, TextOutput_T (*buffer), int len);
void TextOutput(const ThreadParam& tp, const HookParam& hp,TextOutput_T (*buffer), int len);
void ConsoleOutput(LPCSTR text, ...);
void NotifyHookFound(HookParam hp, wchar_t* text);
void NotifyHookRemove(uint64_t addr, LPCSTR name);

View File

@ -321,7 +321,7 @@ void TextHook::Send(uintptr_t lpDataBase)
}
}
TextOutput(tp, buffer, lpCount);
TextOutput(tp, hp,buffer, lpCount);
if(canembed&&(check_embed_able(tp)))
{
@ -419,7 +419,7 @@ void TextHook::Read()
dataLen = min(currentLen, TEXT_BUFFER_SIZE);
memcpy(pbData, location, dataLen);
if (hp.filter_fun && !hp.filter_fun(pbData, &dataLen, &hp) || dataLen <= 0) continue;
TextOutput({ GetCurrentProcessId(), address, 0, 0 }, buffer, dataLen);
TextOutput({ GetCurrentProcessId(), address, 0, 0 },hp, buffer, dataLen);
memcpy(pbData, location, dataLen);
}
}

View File

@ -30,30 +30,30 @@ C_LUNA_API void Luna_Start(ProcessEvent Connect, ProcessEvent Disconnect, Thread
Host::StartEx(
Connect,
Disconnect,
[=](TextThread &thread)
[=](const TextThread &thread)
{
XXXX
Create(hookcode, name, thread.tp);
},
[=](TextThread &thread)
[=](const TextThread &thread)
{
XXXX
Destroy(hookcode, name, thread.tp);
},
[=](TextThread &thread, std::wstring &output)
[=](const TextThread &thread, std::wstring &output)
{
XXXX
return Output(hookcode, name, thread.tp, output.c_str());
},
[=](std::wstring &output)
[=](const std::wstring &output)
{
console(output.c_str());
},
[=](uint64_t addr, std::wstring &output)
[=](uint64_t addr, const std::wstring &output)
{
hookinsert(addr, output.c_str());
},
[=](std::wstring &output, ThreadParam &tp)
[=](const std::wstring &output, const ThreadParam &tp)
{
embed(output.c_str(), tp);
});

View File

@ -9,9 +9,7 @@ namespace
public:
ProcessRecord(DWORD processId, HANDLE pipe) :
pipe(pipe),
mappedFile(OpenFileMappingW(FILE_MAP_READ, FALSE, (ITH_SECTION_ + std::to_wstring(processId)).c_str())),
mappedFile2(OpenFileMappingW(FILE_MAP_READ|FILE_MAP_WRITE, FALSE, (EMBED_SHARED_MEM + std::to_wstring(processId)).c_str())),
view(*(const TextHook(*)[MAX_HOOK])MapViewOfFile(mappedFile, FILE_MAP_READ, 0, 0, MAX_HOOK * sizeof(TextHook))), // jichi 1/16/2015: Changed to half to hook section sizem
viewMutex(ITH_HOOKMAN_MUTEX_ + std::to_wstring(processId))
{
@ -21,18 +19,9 @@ namespace
~ProcessRecord()
{
UnmapViewOfFile(view);
UnmapViewOfFile(embedsharedmem);
}
TextHook GetHook(uint64_t addr)
{
if (!view) return {};
std::scoped_lock lock(viewMutex);
for (auto hook : view) if (hook.address == addr) return hook;
return {};
}
template <typename T>
void Send(T data)
{
@ -52,9 +41,7 @@ namespace
EmbedSharedMem *embedsharedmem;
private:
HANDLE pipe;
AutoHandle<> mappedFile;
AutoHandle<> mappedFile2;
const TextHook(&view)[MAX_HOOK];
WinMutex viewMutex;
};
@ -67,16 +54,6 @@ namespace
Host::ConsoleHandler OnConsole=0;
Host::HookInsertHandler HookInsert=0;
Host::EmbedCallback embedcallback=0;
bool CreateThread(const ThreadParam& tp, const HookParam& hp,std::optional<std::wstring> name){
auto thread = textThreadsByParams->find(tp);
if (thread == textThreadsByParams->end())
{
try { thread = textThreadsByParams->try_emplace(tp, tp, hp ,name ).first; }
catch (std::out_of_range) { return false; } // probably garbage data in pipe, try again
OnCreate(thread->second);
}
return true;
}
void RemoveThreads(std::function<bool(ThreadParam)> removeIf)
{
std::vector<TextThread*> threadsToRemove;
@ -150,10 +127,8 @@ namespace
case HOST_NOTIFICATION_INSERTING_HOOK:
{
if(HookInsert){
auto info = *(HookInsertingNotif*)buffer;
auto addr=info.addr;
std::wstring hc=processRecordsByIds->at(processId).GetHook(addr).hp.hookcode;
HookInsert(addr,hc);
auto info = (HookInsertingNotif*)buffer;
HookInsert(info->addr,info->hookcode);
}
}
break;
@ -168,9 +143,17 @@ namespace
auto data=(TextOutput_T*)buffer;
auto length= bytesRead - sizeof(TextOutput_T);
auto tp = data->tp;
if(!CreateThread(tp,processRecordsByIds->at(tp.processId).GetHook(tp.addr).hp,{}))
continue;
auto thread = textThreadsByParams->find(tp);
auto hp=data->hp;
auto _textThreadsByParams=textThreadsByParams.Acquire();
auto thread = _textThreadsByParams->find(tp);
if (thread == _textThreadsByParams->end())
{
try { thread = _textThreadsByParams->try_emplace(tp, tp, hp).first; }
catch (std::out_of_range) { continue; } // probably garbage data in pipe, try again
OnCreate(thread->second);
}
thread->second.hp.type=data->type;
thread->second.Push(data->data, length);
@ -200,17 +183,19 @@ namespace
namespace Host
{
std::mutex syncmutex;
std::mutex threadmutex;
std::mutex outputmutex;
std::mutex procmutex;
void Start(ProcessEventHandler Connect, ProcessEventHandler Disconnect, ThreadEventHandler Create, ThreadEventHandler Destroy, TextThread::OutputCallback Output,bool createconsole)
{
OnConnect = [=](auto &&...args){std::lock_guard _(syncmutex);Connect(args...);};
OnDisconnect = [=](auto &&...args){std::lock_guard _(syncmutex);Disconnect(args...);};
OnCreate = [=](TextThread& thread) {{std::lock_guard _(syncmutex); Create(thread);} thread.Start(); };
OnDestroy = [=](TextThread& thread) {thread.Stop(); {std::lock_guard _(syncmutex); Destroy(thread);} };
TextThread::Output = [=](auto &&...args){std::lock_guard _(syncmutex);return Output(args...);};
OnConnect = [=](auto &&...args){std::lock_guard _(procmutex);Connect(std::forward<decltype(args)>(args)...);};
OnDisconnect = [=](auto &&...args){std::lock_guard _(procmutex);Disconnect(std::forward<decltype(args)>(args)...);};
OnCreate = [=](TextThread& thread) {{std::lock_guard _(threadmutex); Create(thread);} thread.Start(); };
OnDestroy = [=](TextThread& thread) {thread.Stop(); {std::lock_guard _(threadmutex); Destroy(thread);} };
TextThread::Output = [=](auto &&...args){std::lock_guard _(outputmutex);return Output(std::forward<decltype(args)>(args)...);};
if(createconsole){
CreateThread(console,HookParam{},CONSOLE);
OnCreate(textThreadsByParams->try_emplace(console, console, HookParam{} ,CONSOLE ).first->second);
Host::AddConsoleOutput(ProjectHomePage);
}
@ -220,9 +205,9 @@ namespace Host
void StartEx(ProcessEventHandler Connect, ProcessEventHandler Disconnect, ThreadEventHandler Create, ThreadEventHandler Destroy, TextThread::OutputCallback Output,ConsoleHandler console,HookInsertHandler hookinsert,EmbedCallback embed){
Start(Connect,Disconnect,Create,Destroy,Output,false);
OnConsole=[=](auto &&...args){std::lock_guard _(syncmutex);console(args...);};
HookInsert=[=](auto &&...args){std::lock_guard _(syncmutex);hookinsert(args...);};
embedcallback=[=](auto &&...args){std::lock_guard _(syncmutex);embed(args...);};
OnConsole=[=](auto &&...args){std::lock_guard _(outputmutex);console(std::forward<decltype(args)>(args)...);};
HookInsert=[=](auto &&...args){std::lock_guard _(threadmutex);hookinsert(std::forward<decltype(args)>(args)...);};
embedcallback=[=](auto &&...args){std::lock_guard _(outputmutex);embed(std::forward<decltype(args)>(args)...);};
Host::AddConsoleOutput(ProjectHomePage);
}
constexpr auto PROCESS_INJECT_ACCESS=(

View File

@ -3,12 +3,12 @@
#include "textthread.h"
namespace Host
{
using ConsoleHandler =std::function<void(std::wstring&)>;
using ConsoleHandler =std::function<void(const std::wstring&)>;
using ProcessEventHandler = std::function<void(DWORD)>;
using ThreadEventHandler = std::function<void(TextThread&)>;
using HookEventHandler = std::function<void(HookParam, std::wstring text)>;
using HookInsertHandler= std::function<void(uint64_t,std::wstring&)>;
using EmbedCallback= std::function<void(std::wstring&,ThreadParam&)>;
using HookEventHandler = std::function<void(const HookParam&,const std::wstring& text)>;
using HookInsertHandler= std::function<void(uint64_t,const std::wstring&)>;
using EmbedCallback= std::function<void(const std::wstring&,const ThreadParam&)>;
void Start(ProcessEventHandler Connect, ProcessEventHandler Disconnect, ThreadEventHandler Create, ThreadEventHandler Destroy, TextThread::OutputCallback Output,bool createconsole=true);
void StartEx(ProcessEventHandler Connect, ProcessEventHandler Disconnect, ThreadEventHandler Create, ThreadEventHandler Destroy, TextThread::OutputCallback Output,ConsoleHandler console,HookInsertHandler hookinsert,EmbedCallback embed);
void InjectProcess(DWORD processId,const std::wstring locationX=L"");

View File

@ -10,7 +10,6 @@ constexpr auto HOST_PIPE = L"\\\\.\\pipe\\LUNA_HOST";
// Sections
constexpr auto ITH_SECTION_ = L"LUNA_VNR_SECTION_"; // _%d
constexpr auto EMBED_SHARED_MEM = L"EMBED_SHARED_MEM"; // _%d
// Mutexes

View File

@ -194,10 +194,12 @@ struct HookInsertingNotif // From dll
HookInsertingNotif(uint64_t addr1):addr(addr1){}
HostNotificationType command = HOST_NOTIFICATION_INSERTING_HOOK;
uint64_t addr;
wchar_t hookcode[HOOKCODE_LEN];
};
struct TextOutput_T
{
HookParam hp;
ThreadParam tp;
uint64_t type;
BYTE data[0];