1

So, currently I've been reverse engineering an application, that allows Lua coding. Because for some special purposes, I need to call Lua directly by C/C++ coding. However, as this application is compiled obviously, I can't simply write a function that calls it. I would need the signature of the original functions (incl. the mask) and then write the function prototype, with calling convention, return type, etc. and later compile it as DLL, then inject it.

All this was done already. I've got the signature by IDAPro using the SigMaker plugin, made the base functions for scanning for the pattern and the masks.

So, that's how for example the Lua Print Functions looks like:

DEC_FUNC(__cdecl, DWORD, print, int a1); 

Looking into the Lua source, a1 is "lua_State *L". How would I now call this function and for example print "hello world" in Lua? Excuse me for my incompetence in such things like that, but I've been stuck to this for a couple of days now.

For comparison you may look here to the code part from lbaselib.c (in the Lua source):

static int luaB_print (lua_State *L) {
int n = lua_gettop(L);  // number of arguments
int i;
lua_getglobal(L, "tostring");
for (i=1; i<=n; i++) {
const char *s;
lua_pushvalue(L, -1);  // function to be called
lua_pushvalue(L, i);   // value to print
lua_call(L, 1, 1);
s = lua_tostring(L, -1);  // get result
if (s == NULL)
  return luaL_error(L, LUA_QL("tostring") " must return a string to "
                       LUA_QL("print"));
if (i>1) fputs("\t", stdout);
fputs(s, stdout);
lua_pop(L, 1);  // pop result
}
fputs("\n", stdout);
return 0;
}    

Thanks in advance. If some more information is needed, I may provide it of course.

1
  • I'm confused about whether this code already integrated lua or not: you first say that it does, and then that it doesn't but that you'd like it to. – offbyseveral Jun 26 '14 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.