Skip to content

Commit 45c1779

Browse files
committed
Seed generation Improvement & ConstantArray Bugfix
- Updated the random seed generation in the Pipeline:apply function to use a more secure method with a 12-byte hex seed. - Simplified the boolean expression evaluation by ensuring loadstring is used directly. - Encapsulated prefix initialization logic in a dedicated function for better readability. - Cleaned up variable assignments in the ConstantArray:encode function for improved clarity.
1 parent aefae02 commit 45c1779

4 files changed

Lines changed: 31 additions & 14 deletions

File tree

src/prometheus/compiler/expressions/boolean.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ local function createRandomASTCFlowExpression(resultBool)
2828
leftInt = Ast.NumberExpression(math.random(1, 2^24))
2929
rightInt = Ast.NumberExpression(math.random(1, 2^24))
3030
r3 = "return " .. leftInt.value .. expLookup[randomExp] .. rightInt.value
31-
boolResult = (loadstring or load)(r3)()
31+
boolResult = loadstring(r3)()
3232
until boolResult == resultBool
3333

3434
return randomExp(leftInt, rightInt, false)

src/prometheus/pipeline.lua

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,26 @@ function Pipeline:apply(code, filename)
164164
if(self.Seed > 0) then
165165
math.randomseed(self.Seed);
166166
else
167-
-- try to use secure random number generator
167+
--> use secure random number generator
168168
local success, seed = pcall(function()
169-
return tonumber(io.popen("openssl rand -hex 8"):read("*a"):gsub("\n", ""), 16)
169+
local seedStr = io.popen("openssl rand -hex 12"):read("*a"):gsub("\n", "")..""
170+
local seedNum = 0;
171+
172+
--> NOTE: tonumber caps at 1.844674407371e+19. So we use this instead.
173+
for i = 1, #seedStr do
174+
local char = seedStr:sub(i, i):lower()
175+
local digit = char:match("%d") and (char:byte() - 48) or (char:byte() - 87)
176+
seedNum = seedNum * 16 + digit
177+
end
178+
179+
--> Random Number Generator in Lua 5.1 is limited to 9.007199254741e+15.
180+
if _VERSION == "Lua 5.1" and not jit then
181+
seedNum = seedNum % 9.007199254741e+15
182+
end
183+
184+
return seedNum
170185
end)
186+
171187
if success then
172188
math.randomseed(seed)
173189
else

src/prometheus/steps/ConstantArray.lua

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,14 @@ ConstantArray.SettingsDescriptor = {
9595
}
9696

9797
local prefix_0, prefix_1;
98-
local charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@£$%^&*()_+-=[]{}|\\:;\"'<>,./?";
99-
repeat
100-
local a, b = math.random(1, #charset), math.random(1, #charset);
101-
prefix_0 = charset:sub(a, a);
102-
prefix_1 = charset:sub(b, b);
103-
until prefix_0 ~= prefix_1 and math.random() > 0.5;
98+
local function initPrefixes()
99+
local charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@£$%^&*()_+-=[]{}|:;<>,./?";
100+
repeat
101+
local a, b = math.random(1, #charset), math.random(1, #charset);
102+
prefix_0 = charset:sub(a, a);
103+
prefix_1 = charset:sub(b, b);
104+
until prefix_0 ~= prefix_1
105+
end
104106

105107
local function callNameGenerator(generatorFunction, ...)
106108
if(type(generatorFunction) == "table") then
@@ -555,10 +557,7 @@ function ConstantArray:encode(str)
555557
local rem = len - pos + 1;
556558
local count = rem >= 4 and 4 or rem;
557559
local b1, b2, b3, b4 = string.byte(str, pos, pos + count - 1);
558-
b1 = b1 or 0;
559-
b2 = b2 or 0;
560-
b3 = b3 or 0;
561-
b4 = b4 or 0;
560+
b1, b2, b3, b4 = b1 or 0, b2 or 0, b3 or 0, b4 or 0;
562561

563562
local value = ((b1 * 256 + b2) * 256 + b3) * 256 + b4;
564563
local chars = {};
@@ -618,6 +617,7 @@ function ConstantArray:encode(str)
618617
end
619618

620619
function ConstantArray:apply(ast, pipeline)
620+
initPrefixes();
621621
self.rootScope = ast.body.scope;
622622
self.arrId = self.rootScope:addVariable();
623623

tests.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ for _, filename in ipairs(scandir(testdir)) do
125125
table.remove(preset.Steps, i);
126126
end
127127
end
128-
for iteration = 1, iterationCount do
128+
129+
for _ = 1, iterationCount do
129130
pipeline = Prometheus.Pipeline:fromConfig(preset);
130131
local obfuscated = pipeline:apply(code);
131132

0 commit comments

Comments
 (0)