Loading lua script files in redis - lua

Could someone give an example of how to load and execute .lua script files in windows. I am using ServiceStack redis to loadluascript. It works to certain scripts which don't have module(...) like things.
I am getting this error
Error compiling script (new function): user_script:5: cannot use '...' outside a
vararg function near '...' , sPort: 61688, LastCommand:
Any help by giving an example would be highly appreciated.
Thanks in advance

It would help if you posted the Lua script you are trying to load or execute.
Three dots don't have anything to do with modules:
Vararg expressions, denoted by three dots ('...'), can only be used
when directly inside a vararg function
I guess this answers your question: your Lua code is simply invalid.
Speaking of modules: you can't load your own modules in Redis Lua, which you might already know. See http://redis.io Scripting.

The solution for the above kind Lua script is to prepend local before a function or all variables. I took out the module thing and tweaked the Lua script to make it work. Later I realized the script will not be any use to me :). Thanks for looking into this post.

Related

How to deobfuscate this?

I obfuscated this script using some site
But i'm wondering how to deobfuscate it? can someone help me?
i tried using most decompilers and a lot of ways but none has worked
local howtoDEOBFUSCATEthis_Illll='2d2d341be85c64062f4287f90df25edffd08ec003b5d9491e1db542a356f64a488a1015c2a6b6d4596f2fa74fd602d30b0ecb05f4d768cd9b54d8463b39729eb1fe84630c0f8983f1a0087681fe4f2b322450ce07b
something like that for an example.
the whole script: https://pastebin.com/raw/fDGKYrH7
First reformat into a sane layout. a newline before every local and end will do a lot. Then indenting the functions that become visible is pretty easy.
After that use search replace to inline constants. For example: local howtoDEOBFUSCATEthis_IlIlIIlIlIlI=8480; means you can replace every howtoDEOBFUSCATEthis_IlIlIIlIlIlI with 8480. Though be careful about assignments to it. If there are any then it's better to rename the variable something sensible.
If an identifier has no match you can delete the statement.
Eventually you get to functions that are actually used.
Looking at the code it seems to be an interpreter implementation. I believe it's a lua interpreter
Which means that you'll need to verify that and decompile what the interpreter executes.

How Can I Execute a Non-Lua File From a Lua Script?

I have a Lua script and I want to execute a batch file from within this file; how can I do this?
I've seen examples as follows that I thought could do this but in retrospect I am probably misunderstanding their purpose:
os.execute('C:\\tmp\\MyFile.bat')
or
local handle = io.open('C:\\tmp\\MyFile.bat')
handle:close()
I believe this question has been asked and answered before: How do I run an executable using Lua?
However my code is failing; I'm not sure why. Is there a library I need to load for 'os' for example?
When I try and run this code I get the following error:
"attempt to index a nil value"
You have to use os.execute[[yourFile.bat]] when opening a batch file.
More information for this here

Can luaL_loadbuffer load multiple files in one call?

I know how to load a Lua file via luaL_loadbuffer. Now I have many Lua files, more than 100. I am thinking about how to speed up the loading process. One way I figured out is: put all files into one, and then load this file using luaL_loadbuffer (I did some tests, but just got syntax error return by luaL_loadbuffer). Does anyone ever use this method? Or is there any other way to speed up the loading?
Expanding on #siffiejoe's comment and this answer to a related SO question, I use Squish to collapse multiple modules into a single .lua file. You can then use luac to compile it into bytecode, if desired.
I replaced Lua with LuaJIT and the loading time reduced to ~6sec. I'm satisfied with this result now.Thanks everybody.

passing Lua script from C++ to Lua

I want to pass a Lua script (code that Lua interpreter can process) from C++ and get the result back.
I looked online but could not find any example that would help me. I am able to call a Lua function from C++, but that requires you to create a file with Lua function.
Try using luaL_dostring, which loads and runs a given string.

statically analysing Lua code for potential errors

I'm using a closed-source application that loads Lua scripts and allows some customization through modifying these scripts. Unfortunately that application is not very good at generating useful log output (all I get is 'script failed') if something goes wrong in one of the Lua scripts.
I realize that dynamic languages are pretty much resistant to static code analysis in the way C++ code can be analyzed for example.
I was hoping though, there would be a tool that runs through a Lua script and e.g. warns about variables that have not been defined in the context of a particular script.
Essentially what I'm looking for is a tool that for a script:
local a
print b
would output:
warning: script.lua(1): local 'a' is not used'
warning: script.lua(2): 'b' may not be defined'
It can only really be warnings for most things but that would still be useful! Does such a tool exist? Or maybe a Lua IDE with a feature like that build in?
Thanks, Chris
Automated static code analysis for Lua is not an easy task in general. However, for a limited set of practical problems it is quite doable.
Quick googling for "lua lint" yields these two tools: lua-checker and Lua lint.
You may want to roll your own tool for your specific needs however.
Metalua is one of the most powerful tools for static Lua code analysis. For example, please see metalint, the tool for global variable usage analysis.
Please do not hesitate to post your question on Metalua mailing list. People there are usually very helpful.
There is also lua-inspect, which is based on metalua that was already mentioned. I've integrated it into ZeroBrane Studio IDE, which generates an output very similar to what you'd expect. See this SO answer for details: https://stackoverflow.com/a/11789348/1442917.
For checking globals, see this lua-l posting. Checking locals is harder.
You need to find a parser for lua (should be available as open source) and use it to parse the script into a proper AST tree. Use that tree and a simple variable visibility tracker to find out when a variable is or isn't defined.
Usually the scoping rules are simple:
start with the top AST node and an empty scope
item look at the child statements for that node. Every variable declaration should be added in the current scope.
if a new scope is starting (for example via a { operator) create a new variable scope inheriting the variables in the current scope).
when a scope is ending (for example via } ) remove the current child variable scope and return to the parent.
Iterate carefully.
This will provide you with what variables are visible where inside the AST. You can use this information and if you also inspect the expressions AST nodes (read/write of variables) you can find out your information.
I just started using luacheck and it is excellent!
The first release was from 2015.

Resources