Posted 02 January 2015 - 04:46 PM
I need a server security system with a restricted shell, and remote file access/administration!
Please can someone help me?
PM me on skype(deinvorbild3) or write it under the topic.
-- This is a special table that holds the environment.
-- There is a tutorial on metatables here: http://lua-users.org/wiki/MetamethodsTutorial
local sandEnv = setmetatable({}, {__index = getfenv(), __metatable = {}})
local function banned() error("Cannot use this function", 2) end -- Use for disabled functions
-- Disable filesystem. I don''t know what you want to do but it is an example
sandEnv.fs = setmetatable({}, {__index = banned})
sandEnv.loadfile = banned
sandEnv.dofile = banned
-- Should disable os.run as well but meh.
-- Load the file and set it to run in the sandbox
setfenv(loadfile('something.lua'), sandEnv)()
print("HELLO")
local a = fs.open('file', 'w') -- Breaks
loadfile('Something.lua')() -- Breaks
-- But this works
local newEnv = getfenv(print)
newEnv.loadfile('Something.lua')()
-- This is a special table that holds the environment.
-- There is a tutorial on metatables here: http://lua-users.org/wiki/MetamethodsTutorial
local sandEnv = setmetatable({}, {__index = getfenv(), __metatable = {}})
local function banned() error("Cannot use this function", 2) end -- Use for disabled functions
-- Disable filesystem. I don''t know what you want to do but it is an example
sandEnv.loadfile = banned
sandEnv._G = sandEnv
local envs = setmetatable({sandEnv = true}, {__mode="k"})
function sandEnv.getfenv(arg)
local env
if type(arg) == "function" then
env = getfenv(arg)
elseif arg == 0 then -- 'Global'
return sandEnv
elseif arg ~= nil then
env = getfenv(arg + 1) -- Increment stack by one
else
env = getfenv(2)
end
-- If we have never created this environment
-- then it must be outside the sandbox
-- hence we can use it
if not envs[env] then
print(env, " is invalid using ", sandEnv)
return sandEnv
end
return env
end
function sandEnv.setfenv(arg, env)
envs[env] = true
if type(arg) == "function" then
return setfenv(arg, env)
else
return setfenv(arg + 1, env) -- Increment stack by one
end
end
setfenv(function()
print("PRINT")
local newEnv = getfenv(print)
--newEnv.loadfile('Scrap.lua')
print("CURRENT")
local current = getfenv()
print(current)
setfenv(function()
print("INTERNAL")
print(getfenv())
end, setmetatable({}, {__index = current}))()
end, sandEnv)()
For a restricted shell, I presume you mean sandboxing. The Lua wiki has a very useful page on this. Sandboxes are in general very tricky. I'll put some example stuff in a spoiler.Spoiler
This is a basic sandbox of sorts:-- This is a special table that holds the environment. -- There is a tutorial on metatables here: http://lua-users.org/wiki/MetamethodsTutorial local sandEnv = setmetatable({}, {__index = getfenv(), __metatable = {}}) local function banned() error("Cannot use this function", 2) end -- Use for disabled functions -- Disable filesystem. I don't know what you want to do but it is an example sandEnv.fs = setmetatable({}, {__index = banned}) sandEnv.loadfile = banned sandEnv.dofile = banned -- Should disable os.run as well but meh. -- Load the file and set it to run in the sandbox setfenv(loadfile('something.lua'), sandEnv)()
Example file to run:print("HELLO") local a = fs.open('file', 'w') -- Breaks loadfile('Something.lua')() -- Breaks -- But this works local newEnv = getfenv(print) newEnv.loadfile('Something.lua')()
For 'remote file access/administration you might want to look at Lyqyd's NSH. This is SSH for Lua