存储在内存中的Lua脚本

我在使用lua时遇到了问题,

每次我执行一个函数/命令或者甚至只是定义一个变量,它们都会以字符串形式存储在内存中。

这些字符串可以通过诸如Cheat Engine或HxD之类的程序进行读取。

例如

 x = 'Test'

如果我搜索字符串 x = ',我就能从内存中提取整个命令。 函数也是同样的情况

function Test()
   print(1);
end

搜索Test()就能获得整个函数脚本。

下面是一个示例图像 http://i40.tinypic.com/6oh281.png

你可以看到,在内存查看器窗口的底部,有整个函数信息。

现在,我应该如何防止Lua在内存中创建“额外”的脚本副本? 垃圾回收器不能清除它。

如果有任何解决方案或想法来解决这个问题,都欢迎分享。

点赞
用户107090
用户107090

我不确定你的意思,但可以尝试使用luaL_loadbuffer(L,script,strlen(script),“= noname”)来加载脚本。如果使用luaL_dostring,则整个脚本会被保存为其自己的名称。

还要注意,您可以加载脚本一次并多次运行它,而无需再次加载它。

2014-01-11 23:39:21
用户2226988
用户2226988

您不需要加载 Lua 脚本的源代码。使用 luac -s 预编译脚本即可。当然,Lua 字节码和字符串仍然可以在内存中访问并且很容易找到。

更新

luac 的输出是二进制形式的脚本。像 Lua 脚本一样使用它。您可以使用 loadstringloadfile 等函数加载它。

创建 test.lua 文件:

print("testing")

测试:

luac -o test.lub -s test.lua
lua test.lua
lua test.lub

要查看您正在公开哪些内容而不是 Lua 源代码,请使用:

luac -l test.lub
2014-01-12 01:46:01
用户2679394
用户2679394

默认情况下,Lua 将 x 键存储为字符串,将值存储为内存中的对象。您可以在其中管理脚本并仅在块内存储值,但除此之外并没有太多用途。尽管这种方式可以被作弊引擎检测出来,但比默认设置更安全。

safe.lua:

local protected = {}

protected.x = "Test"

safe = {}

function safe.Get(pass, val)
    if pass == "0x012345" then
         if not protected[val] then return nil end
         return protected[val]
    end
    return nil
end

function safe.Set(pass, val, key)
    if not pass == "0x012345" then return end
    protected[val] = key
end

function safe.Remove(pass, val)
     if not pass == "0x012345" then return end
     if not protected[val] then return end
     protected[val] = nil
 end

test.lua:

safe.Set("0x012345", "b", {C = "111"})
print(safe.Get("0x012345", "b").C)

如果您想为每个键使用自定义密码,则可以使用此 safe.lua:

local protected = {}

safe = {}

function safe.Get(pass, val)
    if protected[val] then
         if not protected[val][pass] then return nil end
         return protected[val][pass]
    end
    return nil
end

function safe.Set(pass, val, key)
    protected[val] = {}
    protected[val][pass] = key
end

function safe.Remove(pass, val)
     if not protected[val] then return end
     if not protected[val][pass] then return end
     protected[val][pass] = nil
end

和 test.lua:

safe.Set("tsting", "x", "keey")
print(safe.Get("tsting", "x"))
print(safe.Get("testing", "x"))
2014-01-13 10:08:09