Here are several advanced control flow obfuscation techniques for Luau:
1. Opaque Predicates with Complex Conditions
Create conditions that always evaluate to true/false but are difficult to analyze:
local function opaqueTrue(x)
return (x * x) >= 0 -- Always true for real numbers
end
local function obfuscatedFlow()
local data = 42
if opaqueTrue(math.random()) then
-- Real code path
print("Processing...")
else
-- Dead code that never executes
error("This should never happen")
end
end
2. Control Flow Flattening with State Machines
Transform linear code into a state machine with jumps:
local function flattenedFunction(input)
local state = 1
local result = 0
while state ~= 0 do
if state == 1 then
result = input * 2
state = 3
elseif state == 2 then
result = result - 5
state = 4
elseif state == 3 then
result = result + 10
state = 2
elseif state == 4 then
result = result / 2
state = 0 -- Exit
end
end
return result
end
3. Dynamic Function Generation and Invocation
Generate functions at runtime using string manipulation:
local function createObfuscatedFunc()
local ops = {
["a"] = "return function(x) return x + ",
["b"] = "return function(x) return x * ",
["c"] = "return function(x) return x - "
}
local sequence = "abc"
local funcs = {}
for i = 1, #sequence do
local op = sequence:sub(i, i)
local code = ops[op] .. tostring(i * 10) .. " end"
funcs[i] = loadstring(code)()
end
return function(value)
for _, func in ipairs(funcs) do
value = func(value)
end
return value
end
end
4. Indirect Function Calls Through Tables
Hide function calls using table lookups and string obfuscation:
local functionRegistry = {}
-- Obfuscate function names
local function rot13(str)
return (str:gsub(".", function(c)
local byte = c:byte()
if byte >= 65 and byte <= 90 then
return string.char(((byte - 65 + 13) % 26) + 65)
elseif byte >= 97 and byte <= 122 then
return string.char(((byte - 97 + 13) % 26) + 97)
end
return c
end))
end
functionRegistry[rot13("process")] = function(data)
return data * 2 + 5
end
functionRegistry[rot13("validate")] = function(data)
return data > 0 and data < 100
end
-- Usage with obfuscated calls
local function executeObfuscated(data)
if functionRegistry[rot13("validate")](data) then
return functionRegistry[rot13("process")](data)
end
return nil
end
5. Coroutine-Based Control Flow Obfuscation
Use coroutines to create non-linear execution paths:
local function obfuscatedLogic(input)
local co1, co2, co3
co1 = coroutine.create(function()
local value = input
coroutine.yield(value * 2)
value = coroutine.yield(value + 10)
return value / 2
end)
co2 = coroutine.create(function()
local temp = coroutine.yield()
return temp - 5
end)
co3 = coroutine.create(function()
local _, initial = coroutine.resume(co1)
local _, modified = coroutine.resume(co2, initial)
local _, final = coroutine.resume(co1, modified)
return final
end)
local _, result = coroutine.resume(co3)
return result
end
6. Exception-Based Control Flow
Use protected calls and error handling for control flow:
local function exceptionFlow(value)
local result = 0
local function stage1()
if value > 50 then
error({code = "GOTO_STAGE3", data = value * 2})
else
error({code = "GOTO_STAGE2", data = value + 10})
end
end
local function stage2(data)
error({code = "FINISH", data = data * 3})
end
local function stage3(data)
error({code = "FINISH", data = data - 20})
end
local ok, err = pcall(stage1)
while not ok and type(err) == "table" do
if err.code == "GOTO_STAGE2" then
ok, err = pcall(stage2, err.data)
elseif err.code == "GOTO_STAGE3" then
ok, err = pcall(stage3, err.data)
elseif err.code == "FINISH" then
result = err.data
break
end
end
return result
end
7. Metamethod-Based Obfuscation
Use metatables to hide actual operations:
local function createObfuscatedNumber(value)
local hidden = {v = value}
return setmetatable({}, {
__add = function(self, other)
return createObfuscatedNumber(hidden.v + (type(other) == "table" and getmetatable(other).__index().v or other))
end,
__mul = function(self, other)
return createObfuscatedNumber(hidden.v * (type(other) == "table" and getmetatable(other).__index().v or other))
end,
__tostring = function()
return tostring(hidden.v)
end,
__index = function()
return hidden
end
})
end
-- Usage
local obfNum = createObfuscatedNumber(10)
local result = obfNum + 5
print(tostring(result * 2)) -- Prints 30
These techniques can be combined for stronger obfuscation. Remember that while obfuscation makes code harder to understand, it doesn't provide true security and can impact performance. Use these techniques judiciously based on your specific needs.