Skip to content

Commit 35cd0f5

Browse files
authored
Merge pull request #144 from prometheus-lua/master
2 parents 44a97c0 + d214646 commit 35cd0f5

File tree

11 files changed

+119
-51
lines changed

11 files changed

+119
-51
lines changed

.editorconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[*.lua]
2+
indent_style = tab
3+
indent_size = 4
4+
end_of_line = lf

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.lua text=auto
2+
*.lua eol=lf

doc/SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* [ProxifyLocals](steps/proxifylocals.md)
2020
* [EncryptStrings](steps/encryptstrings.md)
2121
* [ConstantArray](steps/constantarray.md)
22+
* [AntiTamper](steps/anti-tamper.md)
23+
2224

2325
## advanced
2426

doc/advanced/using-prometheus-in-your-lua-application.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Using Prometheus in your Lua Application
22

3-
Prometheus can also be used as a library for your custom Lua Applications instead of using it's cli tool. 
3+
Prometheus can also be used as a library for your custom Lua Applications instead of using its cli tool. 
44

55
In order to do that you'll first need to clone the github repo:
66

doc/steps/anti-tamper.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
description: This step provides an obfuscation step, that breaks the script, when someone tries to tamper with it.
3+
---
4+
5+
# Anti Tamper
6+
7+
### Settings
8+
9+
| Name | type | description | values |
10+
| ----------- | ---- | ------------------------------------------- | --------------------------------------- |
11+
| UseDebug | boolean | Uses the debug library in lua. Disable this if you don't have access to debug library | "true","false" |

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ To perform the Prometheus Tests, just run
3434
lua ./tests.lua
3535
```
3636
## Building
37-
Prometheus can currently only be build for and on Windows.
37+
Prometheus can currently only build on Windows.
3838
It requires [srlua.exe](https://github.com/LuaDist/srlua) and [glue.exe](https://github.com/LuaDist/srlua) inside of the root directory. If lua51 was linked dynamically, lua51.dll must also be present. Then Prometheus for Windows can be built using
3939
```batch
4040
build.bat

src/prometheus/enums.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Enums.Conventions = {
3636
BinaryNumberChars = {"0", "1"},
3737
DecimalExponent = {"e", "E"},
3838
HexadecimalNums = {"x", "X"},
39-
BinaryNums = false,
39+
BinaryNums = {"b", "B"},
4040
DecimalSeperators = false,
4141

4242
EscapeSequences = {
@@ -52,9 +52,9 @@ Enums.Conventions = {
5252
["\'"] = "\'";
5353
},
5454
NumericalEscapes = true,
55-
EscapeZIgnoreNextWhitespace = false,
56-
HexEscapes = false,
57-
UnicodeEscapes = false,
55+
EscapeZIgnoreNextWhitespace = true,
56+
HexEscapes = true,
57+
UnicodeEscapes = true,
5858
},
5959
[Enums.LuaVersion.LuaU] = {
6060
Keywords = {
@@ -103,4 +103,4 @@ Enums.Conventions = {
103103
},
104104
}
105105

106-
return Enums;
106+
return Enums;

src/prometheus/steps/AntiTamper.lua

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,28 @@ function AntiTamper:apply(ast, pipeline)
3838
local string = RandomStrings.randomString();
3939
code = code .. [[
4040
-- Anti Beautify
41-
local sethook = debug and debug.sethook or function() end;
42-
local allowedLine = nil;
43-
local called = 0;
44-
sethook(function(s, line)
45-
called = called + 1;
46-
if allowedLine then
47-
if allowedLine ~= line then
48-
sethook(error, "l");
49-
end
50-
else
51-
allowedLine = line;
52-
end
53-
end, "l");
54-
(function() end)();
55-
(function() end)();
56-
sethook();
41+
local sethook = debug and debug.sethook or function() end;
42+
local allowedLine = nil;
43+
local called = 0;
44+
sethook(function(s, line)
45+
if not line then
46+
return
47+
end
48+
called = called + 1;
49+
if allowedLine then
50+
if allowedLine ~= line then
51+
sethook(error, "l", 5);
52+
end
53+
else
54+
allowedLine = line;
55+
end
56+
end, "l", 5);
57+
(function() end)();
58+
(function() end)();
59+
sethook();
60+
if called < 2 then
61+
valid = false;
62+
end
5763
if called < 2 then
5864
valid = false;
5965
end
@@ -173,4 +179,4 @@ end
173179
return ast;
174180
end
175181

176-
return AntiTamper;
182+
return AntiTamper;

src/prometheus/tokenizer.lua

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,8 @@ Tokenizer.EOF_TOKEN = {
4444
source = "<EOF>",
4545
}
4646

47-
local function getPosition(source, i)
48-
return source:sub(1, i):gsub("[^\n]", ""):len() + 1, i - source:sub(1, i):gsub("[^\r]", ""):len() + 1;
49-
end
50-
5147
local function token(self, startPos, kind, value)
52-
local line, linePos = getPosition(self.source, self.index);
48+
local line, linePos = self:getPosition(self.index);
5349
local annotations = self.annotations
5450
self.annotations = {};
5551
return {
@@ -65,14 +61,45 @@ local function token(self, startPos, kind, value)
6561
end
6662

6763
local function generateError(self, message)
68-
local line, linePos = getPosition(self.source, self.index);
64+
local line, linePos = self:getPosition(self.index);
6965
return "Lexing Error at Position " .. tostring(line) .. ":" .. tostring(linePos) .. ", " .. message;
7066
end
7167

7268
local function generateWarning(token, message)
7369
return "Warning at Position " .. tostring(token.line) .. ":" .. tostring(token.linePos) .. ", " .. message;
7470
end
7571

72+
function Tokenizer:getPosition(i)
73+
local column = self.columnMap[i]
74+
75+
if not column then --// `i` is bigger than self.length, this shouldnt happen, but it did. (Theres probably some error in the tokenizer, cant find it.)
76+
column = self.columnMap[#self.columnMap]
77+
end
78+
79+
return column.id, column.charMap[i]
80+
end
81+
82+
--// Prepare columnMap for getPosition
83+
function Tokenizer:prepareGetPosition()
84+
local columnMap, column = {}, { charMap = {}, id = 1, length = 0 }
85+
86+
for index = 1, self.length do
87+
local character = string.sub(self.source, index, index) -- NOTE_1: this could use table.clone to reduce amount of NEWTABLE (if that causes any performance issues)
88+
89+
local columnLength = column.length + 1
90+
column.length = columnLength
91+
column.charMap[index] = columnLength
92+
93+
if character == "\n" then
94+
column = { charMap = {}, id = column.id + 1, length = 0 } -- NOTE_1
95+
end
96+
97+
columnMap[index] = column
98+
end
99+
100+
self.columnMap = columnMap
101+
end
102+
76103
-- Constructor for Tokenizer
77104
function Tokenizer:new(settings)
78105
local luaVersion = (settings and (settings.luaVersion or settings.LuaVersion)) or LuaVersion.LuaU;
@@ -132,12 +159,14 @@ function Tokenizer:reset()
132159
self.length = 0;
133160
self.source = "";
134161
self.annotations = {};
162+
self.columnMap = {};
135163
end
136164

137165
-- Append String to this Tokenizer
138166
function Tokenizer:append(code)
139167
self.source = self.source .. code
140168
self.length = self.length + code:len();
169+
self:prepareGetPosition();
141170
end
142171

143172
-- Function to peek the n'th char in the source of the tokenizer

src/prometheus/unparser.lua

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -383,25 +383,22 @@ function Unparser:unparseStatement(statement, tabbing)
383383
end
384384
end
385385
elseif self.luaVersion == LuaVersion.LuaU then
386-
if statement.kind == AstKind.CompoundAddStatement then
387-
code = code .. self:unparseExpression(statement.lhs, tabbing) .. self:optionalWhitespace() .. "+=" .. self:optionalWhitespace() .. self:unparseExpression(statement.rhs, tabbing);
388-
elseif statement.kind == AstKind.CompoundSubStatement then
389-
code = code .. self:unparseExpression(statement.lhs, tabbing) .. self:optionalWhitespace() .. "-=" .. self:optionalWhitespace() .. self:unparseExpression(statement.rhs, tabbing);
390-
elseif statement.kind == AstKind.CompoundMulStatement then
391-
code = code .. self:unparseExpression(statement.lhs, tabbing) .. self:optionalWhitespace() .. "*=" .. self:optionalWhitespace() .. self:unparseExpression(statement.rhs, tabbing);
392-
elseif statement.kind == AstKind.CompoundDivStatement then
393-
code = code .. self:unparseExpression(statement.lhs, tabbing) .. self:optionalWhitespace() .. "/=" .. self:optionalWhitespace() .. self:unparseExpression(statement.rhs, tabbing);
394-
elseif statement.kind == AstKind.CompoundModStatement then
395-
code = code .. self:unparseExpression(statement.lhs, tabbing) .. self:optionalWhitespace() .. "%=" .. self:optionalWhitespace() .. self:unparseExpression(statement.rhs, tabbing);
396-
elseif statement.kind == AstKind.CompoundPowStatement then
397-
code = code .. self:unparseExpression(statement.lhs, tabbing) .. self:optionalWhitespace() .. "^=" .. self:optionalWhitespace() .. self:unparseExpression(statement.rhs, tabbing);
398-
elseif statement.kind == AstKind.CompoundConcatStatement then
399-
code = code .. self:unparseExpression(statement.lhs, tabbing) .. self:optionalWhitespace() .. "..=" .. self:optionalWhitespace() .. self:unparseExpression(statement.rhs, tabbing);
386+
local compoundOperators = {
387+
[AstKind.CompoundAddStatement] = "+=",
388+
[AstKind.CompoundSubStatement] = "-=",
389+
[AstKind.CompoundMulStatement] = "*=",
390+
[AstKind.CompoundDivStatement] = "/=",
391+
[AstKind.CompoundModStatement] = "%=",
392+
[AstKind.CompoundPowStatement] = "^=",
393+
[AstKind.CompoundConcatStatement] = "..=",
394+
}
395+
396+
local operator = compoundOperators[statement.kind]
397+
if operator then
398+
code = code .. self:unparseExpression(statement.lhs, tabbing) .. self:optionalWhitespace() .. operator .. self:optionalWhitespace() .. self:unparseExpression(statement.rhs, tabbing)
400399
else
401-
logger:error(string.format("\"%s\" is not a valid unparseable statement in %s!", statement.kind, self.luaVersion));
400+
logger:error(string.format("\"%s\" is not a valid unparseable statement in %s!", statement.kind, self.luaVersion))
402401
end
403-
else
404-
logger:error(string.format("\"%s\" is not a valid unparseable statement in %s!", statement.kind, self.luaVersion));
405402
end
406403

407404
return self:tabs(tabbing, false) .. code;
@@ -450,6 +447,12 @@ function Unparser:unparseExpression(expression, tabbing)
450447

451448
if(expression.kind == AstKind.NumberExpression) then
452449
local str = tostring(expression.value);
450+
if(str == "inf") then
451+
return "2e1024"
452+
end
453+
if(str == "-inf") then
454+
return "-2e1024"
455+
end
453456
if(str:sub(1, 2) == "0.") then
454457
str = str:sub(2);
455458
end

0 commit comments

Comments
 (0)