Skip to content

Commit 4767a6f

Browse files
committed
修复一个hookapi 问题,修复emmy_tool list process 问题
1 parent 6fb24bf commit 4767a6f

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

emmy_hook/src/emmy_hook.windows.cpp

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "emmy_debugger/proto/socket_server_transporter.h"
1515
#include "shared/shme.h"
1616

17+
1718
typedef TRACED_HOOK_HANDLE HOOK_HANDLE;
1819
typedef NTSTATUS HOOK_STATUS;
1920

@@ -28,7 +29,11 @@ typedef int (*_lua_pcall)(lua_State* L, int nargs, int nresults, int errfunc);
2829

2930
typedef int (*_lua_pcallk)(lua_State* L, int nargs, int nresults, int errfunc, lua_KContext ctx, lua_KFunction k);
3031

31-
typedef int (*_lua_resume)(lua_State* L, lua_State* from, int nargs, int* nresults);
32+
typedef int (*_lua_resume_54)(lua_State* L, lua_State* from, int nargs, int* nresults);
33+
34+
typedef int (*_lua_resume_53_52)(lua_State* L, lua_State* from, int narg);
35+
36+
typedef int (*_lua_resume_51)(lua_State* L, int narg);
3237

3338
typedef HMODULE (WINAPI *LoadLibraryExW_t)(LPCWSTR lpFileName, HANDLE hFile, DWORD dwFlags);
3439

@@ -91,15 +96,33 @@ int lua_error_worker(lua_State* L)
9196
return error(L);
9297
}
9398

94-
int lua_resume_worker(lua_State* L, lua_State* from, int nargs, int* nresults)
99+
int lua_resume_worker_54(lua_State* L, lua_State* from, int nargs, int* nresults)
95100
{
96101
LPVOID lp;
97102
LhBarrierGetCallback(&lp);
98-
const auto luaResume = (_lua_resume)lp;
103+
const auto luaResume = (_lua_resume_54)lp;
99104
EmmyFacade::Get().Attach(L);
100105
return luaResume(L, from, nargs, nresults);
101106
}
102107

108+
int lua_resume_worker_53_52(lua_State* L, lua_State* from, int nargs)
109+
{
110+
LPVOID lp;
111+
LhBarrierGetCallback(&lp);
112+
const auto luaResume = (_lua_resume_53_52)lp;
113+
EmmyFacade::Get().Attach(L);
114+
return luaResume(L, from, nargs);
115+
}
116+
117+
int lua_resume_worker_51(lua_State* L, int nargs)
118+
{
119+
LPVOID lp;
120+
LhBarrierGetCallback(&lp);
121+
const auto luaResume = (_lua_resume_51)lp;
122+
EmmyFacade::Get().Attach(L);
123+
return luaResume(L, nargs);
124+
}
125+
103126
#define HOOK(FN, WORKER, REQUIRED) {\
104127
const auto it = symbols.find(""#FN"");\
105128
if (it != symbols.end()) {\
@@ -112,6 +135,8 @@ int lua_resume_worker(lua_State* L, lua_State* from, int nargs, int* nresults)
112135
}\
113136
}
114137

138+
#define EXIST_SYMBOL(FN) (symbols.find(""#FN"") != symbols.end())
139+
115140
void HookLuaFunctions(std::unordered_map<std::string, DWORD64>& symbols)
116141
{
117142
if (symbols.empty())
@@ -121,7 +146,20 @@ void HookLuaFunctions(std::unordered_map<std::string, DWORD64>& symbols)
121146
// lua 5.2
122147
HOOK(lua_pcallk, lua_pcallk_worker, false);
123148
// HOOK(lua_error, lua_error_worker, true);
124-
HOOK(lua_resume, lua_resume_worker, false);
149+
150+
// lua5.4
151+
if (EXIST_SYMBOL(lua_newuserdatauv))
152+
{
153+
HOOK(lua_resume, lua_resume_worker_54, false);
154+
}
155+
else if(EXIST_SYMBOL(lua_rotate) || EXIST_SYMBOL(lua_callk)) //lua5.3 lua5.2
156+
{
157+
HOOK(lua_resume, lua_resume_worker_53_52, false);
158+
}
159+
else // lua5.1 or luajit
160+
{
161+
HOOK(lua_resume, lua_resume_worker_51, false);
162+
}
125163
}
126164

127165
void LoadSymbolsRecursively(HANDLE hProcess, HMODULE hModule)

emmy_tool/src/main.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ int doAttach(CommandLine& commandLine)
2121
return 0;
2222
}
2323

24+
void translateText(std::string& text)
25+
{
26+
for(auto& c: text)
27+
{
28+
if(c == '\n')
29+
{
30+
c = ' ';
31+
}
32+
}
33+
}
34+
2435
int doListProcesses()
2536
{
2637
std::vector<Process> list;
@@ -29,7 +40,13 @@ int doListProcesses()
2940
for (auto& value : list)
3041
{
3142
printf("%d\n", value.id);
43+
// title 中可能出现\n 所以title中的\n全部转为' '
44+
translateText(value.title);
45+
3246
printf("%s\n", value.title.c_str());
47+
48+
translateText(value.path);
49+
3350
printf("%s\n", value.path.c_str());
3451
printf("----\n");
3552
}

0 commit comments

Comments
 (0)