【问题标题】:Accessing lua_State from another application从另一个应用程序访问 lua_State
【发布时间】:2021-01-15 15:11:41
【问题描述】:

现在假设您有两个具有不同 lua 实例的程序。一是主程序,二是你为它编写的dll。

在我的问题中,我将从现在开始将主程序命名为 main, dll i child。我们将子进程加载到主进程中,绕过它并以某种方式访问​​ lua_State。

我的主要问题是,我们可以在主程序运行时通过我们抓取的 lua_State 执行 lua_pcall 或 dofile 吗?

示例代码

主程序:

#include <lua.hpp>

bool loadFile(lua_State* L) {
    // run the Lua script
    luaL_dofile(L, "helloworld.lua");
    if (lua_pcall(L, 0, 0, eh) != 0)
    {
        std::string err = luaL_checkstring(L, -1);
        lua_pop(L, 1);
    }

}

int main()
{
    // create new Lua state
    lua_State *lua_state;
    lua_state = luaL_newstate();

    loadFile(lua_state);
}

儿童节目:

#include <lua.hpp>
#include "hookingLibrary.h" 

typedef int(__fastcall* main_loadFile_Proto)(lua_State* L);
main_loadFile_Proto main_loadFile_Ptr;

lua_State * L lastState;
uint64_t main_loadFile_Addr = 0x0; 

int main_loadFile_Detour(lua_State* L) { 
    lastState = L;
    return main_loadFile_Ptr(L);
}

int main()
{
    // detouring etc.
    // I do not put detouring codes here. I am just declaring it as an 
    // opinion.
    
    HookingLibrary::hook((LPVOID)(uintptr_t)main_loadFile_Addr, &main_loadFile_Detour, (LPVOID*)&main_loadFile_Ptr);

    do{ 
       Sleep(100);
    }while(!lastState);

 
    // create new Lua state
    lua_State *lua_state;
    lua_state = lastState;

  
    // run the Lua script
    luaL_dofile(lua_state, "helloworld.lua");

    // close the Lua state
    lua_close(lua_state);
}

【问题讨论】:

  • 这取决于:“main”是使用 Lua 作为 DLL 还是编译到程序中?

标签: c++ c lua hook detours


【解决方案1】:
Now imagine you have two programs with different lua instances. One is the main program, the second is the dll you coded for it.

这个说法不是很清楚,看你的预期。我看到了 2 个可能的答案。

  1. 创建一个为 Lua 实现一些附加功能的 DLL。以后主程序可以使用 DLL 库。在这种情况下,只有 1 个 lua_state 实例,只有 1 个 Lua 解释器。这个 Lua 解释器可以由 DLL 或 main 函数创建。

DLL的接口是这样的:

#ifndef DLL_AUGMENTED
#define DLL_AUGMENTED

#include "lua.h"

lua_State *DLL_CreateAugmentedLuaInterpreter ();
void       DLL_FreeLuaInterpreter ();

#endif

并且可以被main使用:

#include "lua-augmented.h"

int main (int argc, char **argv)
{
  lua_State *LuaState = DLL_CreateAugmentedLuaInterpreter ();

  // TODO: use the augmented Lua instance

  DLL_FreeLuaInterpreter(LuaState);

  return 0;
}
  1. 需要有 2 个 Lua 实例,如 One is the main program, the second is the dll 所写。在这种情况下,它会更加困难,因为需要使用socketspipes 来实现IPC 进程间通信。最好找LuaSocket库。

Interprocess communication in Lua with Example?

【讨论】:

  • 所以我不能在注入的 dll 中使用 lua 解释?
  • 可以,没有问题。但是你应该更清楚你的期望。你想让mainDLL 创建Lua 解释器吗?你想让main 用 Lua 解释器做什么?你想让 DLL 用 Lua 解释器做什么?
  • 我想在 main 中创建 lua 解释器并从 dll 访问它以运行脚本。而且我的访问方法将绕道而行,因为我无法访问主程序,很少有黑客攻击。 Main 一直在运行脚本,我想注入一个 dll 并获取运行脚本的 lua 状态...
猜你喜欢
  • 1970-01-01
  • 2012-05-29
  • 2015-11-09
  • 1970-01-01
  • 2018-05-24
  • 1970-01-01
  • 2015-05-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多