Search code examples
luasandbox

Lua Sandbox "hacking"


So I use a program where I script mods in lua, the lua is in a sandbox state, meaning most functions are blocked like IO and OS, I can't even use REQUIRE to add libs. I need to have a function that unzips files in one of my mods and I don't seem to find a way. Is there anyway to do it? If it's not possible in an easy way, is it possible to hack the program .exe or dlls to re-enable those functions in the lua?

Thank you in advance, Regards


Solution

  • There are decompression librarys in pure Lua, you should be able to embed these in any environment that allows loading Lua scripts: http://lua-users.org/wiki/CompressionAndArchiving

    If you can't access any files at all, you could try a simple packer:

    #!/usr/bin/env lua
    local files = arg
    local w = io.write
    local function pack(...) return {...} end
    
    w("files = {\n")
    for i, filename in ipairs(arg) do
        w('\t["' ..filename .. '"] = "')
        local file = assert(io.open(filename, "r"), "Can't open file!")
        local data = file:read("*a")
        data = data:gsub("\a", "\\a")
            :gsub("\\", "\\\\")
            :gsub("\f", "\\f")
            :gsub("\n", "\\n")
            :gsub("\r", "\\r")
            :gsub("\t", "\\t")
            :gsub("\v", "\\v")
            :gsub('"', '\\"')
            :gsub("'", "\\'")
        w(data, '",\n')
    end
    w("}\n")
    w([[
    function require(path)
        local data = assert(files[path..".lua"], "file not found")
        local func = assert(loadstring(data))
        local _, ret = assert(pcall(func))
        return ret
    end
    ]])
    w('require("', arg[1]:match("^(.-)%.lua$"),'")\n')
    

    This should create a script like this:

    $ ./packer.lua init.lua 
    files = {
        ["init.lua"] = "for k,v in pairs(arg) do\n\tprint(k,v)\nend\n",
    }
    function require(path)
        local data = assert(files[path..".lua"], "file not found")
        local func = assert(loadstring(data))
        local _, ret = assert(pcall(func))
        return ret
    end
    require("init")