'Lua Obfuscation - How would you make a lua string look like C++ compiled code

I was wondering, how would I make a simple lua string or entire code look look C++ compiled code but run as regular vanilla lua?

print("Test string") -- How would this look like C++ compiler code?
lua


Solution 1:[1]

With Lua you can not directly dump print to a binary Format.
...as i know.
Dumping a Function to a Binary is easy doing with own defined Functions...

> -- Lua 5.4
> myfunc = function() print("Teststring") return end
> string.dump(myfunc, true)
uaT?
?
xV(w@????
         ??DGG??print?Teststring??????
> load(string.dump(myfunc, true))()
Teststring

As you can see, like in a compiled C Binary the Constants are not obfuscated.
More obfuscating you can reach with converting the binary String to Bytecode...

> string.dump(myfunc, true):byte(1, -1)
27  76  117 97  84  0   25  147 13  10  26  10  4   8   8   120 86  0   0   0   0   0   0   0   00  0   0   40  119 64  1   128 129 129 0   0   2   133 11  0   0   0   131 128 0   0   68  0   21  71  0   1   0   71  0   1   0   130 4   134 112 114 105 110 116 4   139 84  101 115 116 115 116 114 105 110 103 129 0   0   0   128 128 128 128 128

...and for converting back later lets put it into a table...

> byte_code_tab = {string.dump(myfunc, true):byte(1, -1)}
> table.concat(byte_code_tab,',')
27,76,117,97,84,0,25,147,13,10,26,10,4,8,8,120,86,0,0,0,0,0,0,0,0,0,0,0,40,119,64,1,128,129,129,0,0,2,133,11,0,0,0,131,128,0,0,68,0,2,1,71,0,1,0,71,0,1,0,130,4,134,112,114,105,110,116,4,139,84,101,115,116,115,116,114,105,110,103,129,0,0,0,128,128,128,128,128

...now a function is needed to get it back...

> bytes_dec = function(tab) local txt = '' for k, v in pairs(tab) do txt = txt .. tostring(v):char() end return txt end
> bytes_dec(byte_code_tab)
uaT?
?
xV(w@????
         ??DGG??print?Teststring??????
> load(bytes_dec(byte_code_tab))()
Teststring

EDIT
To show how it work with a single Lua file that returning a table with a __call metamethod check out this...

-- obfsc.lua
return setmetatable({27,76,117,97,84,0,25,147,13,10,26,10,4,8,8,120,86,0,0,0,0,0,0,0,0,0,0,0,40,119,64,1,128,129,129,0,0,2,133,11,0,0,0,131,128,0,0,68,0,2,1,71,0,1,0,71,0,1,0,130,4,134,112,114,105,110,116,4,139,84,101,115,116,115,116,114,105,110,103,129,0,0,0,128,128,128,128,128},
{__call = function(self, ...)
local txt = ''
for k, v in pairs(self) do
 txt = txt .. tostring(v):char()
end
return load(txt)()
end})

...the bytes_dec function is stored in the __call metamethod...

$ /usr/local/bin/lua
Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
> require('obfsc')
table: 0x565d3650   ./obfsc.lua
> require('obfsc')()
Teststring

...and do also the load()
But it is up to you where you store: bytes_dec()

Another nice method is ROT.
Its very simple and also old but good enough for de/obfuscating.
An Impression...

$  /bin/lua
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> rot=require('rot')
> -- Lets rotate the Banner
> print(rot('Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio'))

5!`unqnu``/092)'(4`hi`qyytmrpqr`
                                5!n/2'l`m)/ 51
> -- Now read source of rot.lua into rot_src and print it
> rot_src = io.open('rot.lua'):read('*a')
> print(rot_src)
-- rot.lua
local rotator = function(...)
local args, rot, c = {...}, {}, ''

for i = 1, 63 do rot[c.char(i)] = c.char(i + 64) end
for i = 64, 127 do rot[c.char(i)] = c.char(i - 64) end

return args[1]:gsub('.', rot)
end

return rotator

> -- Obfuscate the source and print it
> rot_obfsc = rot(rot_src)
> print(rot_obfsc)
mm`2/4n,5!J,/#!,`2/4!4/2`}`&5.#4)/.hnnniJ,/#!,`!2'3l`2/4l`#`}`;nnn=l`;=l`ggJJ&/2`)`}`ql`vs`$/`2/4#(!2h)i`}`#n#(!2h)`k`vti`%.$J&/2`)`}`vtl`qrw`$/`2/4#(!2h)i`}`#n#(!2h)`m`vti`%.$JJ2%452.`!2'3z'35"hgngl`2/4iJ%.$JJ2%452.`2/4!4/2J
> -- Deobfuscate and print on the fly
> print(rot(rot_obfsc))
-- rot.lua
local rotator = function(...)
local args, rot, c = {...}, {}, ''

for i = 1, 63 do rot[c.char(i)] = c.char(i + 64) end
for i = 64, 127 do rot[c.char(i)] = c.char(i - 64) end

return args[1]:gsub('.', rot)
end

return rotator
    236

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1