Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there anyway to avoid this security issue in Lua?

Tags:

security

lua

I was just working on a localizable Lua string solution, when I came up with this hack, problem is I don't know how to avoid getting hacked by it :) So I was wondering if anyone, has done something similar and or knows how to protect from this kind of attack. (in user code)

Since we can do this:

=("foo"):upper() -->output: FOO

It can be hacked like this:

getmetatable("foo").__index.upper = function() print("bye bye sucker");os.exit() end
=("foo"):upper() -->output: bye bye sucker (application quits)
-- or this way
=string.upper("bar") -->output: bye bye sucker (application quits)

Any ideas?

like image 962
Robert Gould Avatar asked Nov 28 '08 09:11

Robert Gould


People also ask

How to pass a level 0 error in Lua programming?

Passing a level 0 avoids the addition of error position information to the message. In Lua programming, in order to avoid throwing these errors and handling errors, we need to use the functions pcall or xpcall. The pcall (f, arg1, ...) function calls the requested function in protected mode.

How to fix Lua errors in Wow?

If the Lua errors WoW is caused by a glitch, you can force the game to reload the main game screen to fix the problem. Here is the guide: Step 1: Open your chatbox in WoW.

What is the point of obfuscating Lua code?

The point of obfuscating your Lua code is to prevent the casual hacker from gaining easy access to your game logic. There are several options for obfuscation: Running Lua files through luac:You can compile your Lua files using luac.

How do I encrypt my Lua file?

Encrypting your compiled Lua files:After compiling all of your Lua files using luac, you can also encrypt them. You must then hide your encryption key somewhere. The easiest approach is simply to hide the key in your app, but it's also the most vulnerable.


2 Answers

First and foremost execute untrusted code in sandboxed environment only – as it was said by other posters. Except for loading bytecode chunks, Lua allows all other sandboxing issues to be covered. (And bytecode chunk problems get fixed promptly as discovered.)

See Lua Live Demo for an example of sandboxing. Sources are available here.

Your specific problem with metatables is solved by setting a __metatable field:

If you set a __metatable field in the metatable, getmetatable will return the value of this field, whereas setmetatable will raise an error.

– Roberto Ierusalimschy, Programming in Lua 1st edition, 13.3 - Library-Defined Metamethods

For example:

> mt = { __metatable = true }                                                   
> t = {}
> setmetatable(t, mt)
> setmetatable(t, mt)
stdin:1: cannot change a protected metatable
stack traceback:
 [C]: in function 'setmetatable'
 stdin:1: in main chunk
 [C]: ? 

So, all you have to do is:

getmetatable("").__metatable = true
like image 165
Alexander Gladysh Avatar answered Oct 22 '22 15:10

Alexander Gladysh


If your hacker has the ability to add code, and you need to allow that code to call things like os.exit, then you're pretty much out of luck anyway.

You can restrict the functions that their code can call, though. It depends on what you still want user code to be able to do. See the doc for setfenv and google for "lua sandbox"

like image 45
The Archetypal Paul Avatar answered Oct 22 '22 15:10

The Archetypal Paul