Here's an example of control flow obfuscation in Lua that combines several techniques like coroutines, dynamic function generation, and table manipulation:
-- Create a table of execution states
local states = {
[0] = function(ctx)
ctx.x = 10
return 3
end,
[1] = function(ctx)
ctx.y = 20
return 4
end,
[2] = function(ctx)
ctx.z = ctx.x + ctx.y
return 5
end,
[3] = function(ctx)
if ctx.z > 25 then
return 1
end
return 2
end,
[4] = function(ctx)
ctx.result = ctx.z * 2
return 6
end,
[5] = function(ctx)
ctx.result = ctx.z - 5
return 6
end,
[6] = function(ctx)
return -1
end
}
-- Create a function that generates the execution context
local function createContext()
return setmetatable({}, {
__index = function(t, k)
if type(k) == "number" then
return states[k]
end
return rawget(t, k)
end
})
end
-- Create the execution engine
local function executeFlow(seed)
local ctx = createContext()
local state = 0
-- Create a coroutine for execution
local co = coroutine.create(function()
while state >= 0 do
local fn = ctx[state]
if fn then
state = fn(ctx)
coroutine.yield(state)
else
state = -1
end
end
return ctx.result
end)
-- Execute the coroutine
local results = {}
while coroutine.status(co) ~= "dead" do
local _, nextState = coroutine.resume(co)
table.insert(results, nextState)
end
return ctx.result, results
end
-- Usage example
local result, stateSequence = executeFlow(1)
print("Result:", result)
print("State sequence:", table.concat(stateSequence, " -> "))
This code implements several obfuscation techniques:
-
State Machine Obfuscation: The control flow is broken into separate states stored in a table, making it harder to follow the program's logic.
-
Metatable Manipulation: Uses metatables to create dynamic state lookups.
-
Coroutine-based Execution: The actual execution happens in a coroutine, adding another layer of complexity.
-
Dynamic Function Generation: Each state is represented as a function that modifies a shared context.
-
Indirect Flow Control: State transitions are determined by return values rather than direct calls.
To make it even more complex, you could add:
-- Additional obfuscation layer
local function obfuscateStates(states)
local encrypted = {}
local key = math.random(1, 100)
for k, v in pairs(states) do
local newKey = (k * key) % 256
encrypted[newKey] = function(ctx)
local result = v(ctx)
return (result * key) % 256
end
end
return encrypted, key
end
-- Modified execution engine
local function executeFlowObfuscated(seed)
local encStates, key = obfuscateStates(states)
local ctx = createContext()
local state = 0
local function deobfuscateState(s)
return (s * key) % 256
end
-- Rest of the execution logic...
end
This adds:
- State Encryption: State numbers are encrypted using a random key
- Function Wrapping: Original state functions are wrapped in encrypted versions
- Dynamic Key Generation: Each execution can use a different encryption key
The resulting code is much harder to analyze because:
- The actual control flow is hidden behind multiple layers of indirection
- State transitions are encrypted
- The execution path is split across multiple functions and coroutines
- The program state is maintained in a context object with dynamic lookups
Remember that while obfuscation can make code harder to understand, it also makes it harder to maintain and debug. Use these techniques only when necessary, such as for protecting intellectual property or implementing security-sensitive features.