Skip to content

Commit

Permalink
Alpha v0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
levno-710 authored Dec 29, 2021
2 parents 41f3167 + f8f39bb commit aa12494
Show file tree
Hide file tree
Showing 18 changed files with 304 additions and 79 deletions.
111 changes: 111 additions & 0 deletions cli.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
-- This Script is Part of the Prometheus Obfuscator by Levno_710
--
-- test.lua
-- This script contains the Code for the Prometheus CLI

-- Configure package.path for requiring Prometheus
local function script_path()
local str = debug.getinfo(2, "S").source:sub(2)
return str:match("(.*[/%\\])")
end
package.path = script_path() .. "?.lua;" .. package.path;
local Prometheus = require("src.prometheus");
Prometheus.Logger.logLevel = Prometheus.Logger.LogLevel.Info;

-- see if the file exists
local function file_exists(file)
local f = io.open(file, "rb")
if f then f:close() end
return f ~= nil
end

-- get all lines from a file, returns an empty
-- list/table if the file does not exist
local function lines_from(file)
if not file_exists(file) then return {} end
local lines = {}
for line in io.lines(file) do
lines[#lines + 1] = line
end
return lines
end

-- CLI
local config;
local sourceFile;
local outFile;

-- Parse Arguments
local i = 1;
while i <= #arg do
local curr = arg[i];
if curr:sub(1, 2) == "--" then
if curr == "--preset" or curr == "--p" then
if config then
Prometheus.Logger:warn("The config was set multiple times");
end

i = i + 1;
local preset = Prometheus.Presets[arg[i]];
if not preset then
Prometheus.Logger:error(string.format("A Preset with the name \"%s\" was not found!", tostring(arg[i])));
end

config = preset;
elseif curr == "--config" or curr == "--c" then
i = i + 1;
local filename = tostring(arg[i]);
if not file_exists(filename) then
Prometheus.Logger:error(string.format("The config file \"%s\" was not found!", filename));
end

local content = table.concat(lines_from(filename), "\n");
-- Load Config from File
local func = loadstring(content);
-- Sandboxing
setfenv(func, {});
config = func();
elseif curr == "--out" or curr == "--o" then
i = i + 1;
if(outFile) then
Prometheus.Logger:warn("The output file was specified multiple times!");
end
outFile = arg[i];
else
Prometheus.Logger:warn(string.format("The option \"%s\" is not valid and therefore ignored"));
end
else
if sourceFile then
Prometheus.Logger:error(string.format("Unexpected argument \"%s\"", arg[i]));
end
sourceFile = tostring(arg[i]);
end
i = i + 1;
end

if not config then
Prometheus.Logger:warn("No config was specified, falling back to Minify preset");
config = Prometheus.Presets.Minify;
end

if not file_exists(sourceFile) then
Prometheus.Logger:error(string.format("The File \"%s\" was not found!", sourceFile));
end

if not outFile then
if sourceFile:sub(-4) == ".lua" then
outFile = sourceFile:sub(0, -5) .. ".obfuscated.lua";
else
outFile = sourceFile .. ".obfuscated.lua";
end
end

local source = table.concat(lines_from(sourceFile), "\n");
local pipeline = Prometheus.Pipeline:fromConfig(config);
local out = pipeline:apply(source, sourceFile);
Prometheus.Logger:info(string.format("Writing output to \"%s\"", outFile));

-- Write Output
local handle = io.open(outFile, "w");
handle:write(out);
handle:close();
1 change: 1 addition & 0 deletions demo.lua

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions doc/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
## Getting Started

* [Installation](getting-started/installation.md)

***

* [Obfuscating your first script](obfuscating-your-first-script.md)
2 changes: 1 addition & 1 deletion doc/getting-started/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

To install Prometheus, simply clone the Github Repository using:

```
```batch
git clone https://github.com/Levno710/Prometheus.git
```

Expand Down
40 changes: 40 additions & 0 deletions doc/obfuscating-your-first-script.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Obfuscating your first script

Now that you have downloaded and Prometheus, you probably wonder how to use it. In this quick tutorial you are going to learn how to obfuscate your first file.

Note that in the following command examples `lua` should be replaced by your lua implementation.

Create the following file within the Prometheus main directory that you just downloaded:

{% code title="hello_world.lua" %}
```lua
print("Hello, World")
```
{% endcode %}

Now run the following command inside of the Prometheus directory:

```batch
lua ./cli.lua ./hello_world.lua
```

This should create the following file:

{% code title="hello_world.obfuscated.lua" %}
```lua
print("Hello, World")
```
{% endcode %}

As you can see, the file hasn't changed at all. That is because by default prometheus is just a minifier and the code we gave it was already as small as possible. To actually obfuscate the file, prometheus must be told which obfuscation steps it should apply in which order. In order to do this, the cli has the `--preset` option which allows you to specify the name of a predefined configuration. There are currently only two presets:

* Minify
* Strong

In order to perform the obfuscation, you need to specify that Prometheus should use the Strong preset:

```batch
lua ./cli.lua --preset Strong ./hello_world.lua
```

The `hello_world.obfuscated.lua` should now become around 20kB in size, but when running, it should still print "Hello World".
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Prometheus
# :fire: Prometheus
Prometheus is a Lua obfuscator written in pure Lua.
[Documentation](https://levno-710.gitbook.io/prometheus/)

Expand Down
2 changes: 1 addition & 1 deletion src/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
-- These may be changed

-- You are not allowed to change the following Variables
local VERSION = "Alpha v0.5.1";
local VERSION = "Alpha v0.1";
local NAME = "Prometheus";
local BY = "Levno_710";

Expand Down
6 changes: 6 additions & 0 deletions src/obfuscator/namegenerators.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
return {
Mangled = require("obfuscator.namegenerators.mangled");
MangledShuffled = require("obfuscator.namegenerators.mangled_shuffled");
Il = require("obfuscator.namegenerators.Il");
Number = require("obfuscator.namegenerators.number");
}
53 changes: 26 additions & 27 deletions src/obfuscator/pipeline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,10 @@ local util = require("obfuscator.util");
local Parser = require("obfuscator.parser");
local Unparser = require("obfuscator.unparser");
local logger = require("logger");
local Watermark = require("obfuscator.watermark");

local NameGenerators = {
Mangled = require("obfuscator.namegenerators.mangled");
MangledShuffled = require("obfuscator.namegenerators.mangled_shuffled");
Il = require("obfuscator.namegenerators.Il");
Number = require("obfuscator.namegenerators.number");
}
local NameGenerators = require("obfuscator.namegenerators");

local Steps = {
WrapInFunction = require("obfuscator.steps.WrapInFunction");
SplitStrings = require("obfuscator.steps.SplitStrings");
LocalsToTable = require("obfuscator.steps.LocalsToTable");
Vmify = require("obfuscator.steps.Vmify");
ConstantArray = require("obfuscator.steps.ConstantArray");
ProxifyLocals = require("obfuscator.steps.ProxifyLocals");
}
local Steps = require("obfuscator.steps");

local lookupify = util.lookupify;
local LuaVersion = Enums.LuaVersion;
Expand Down Expand Up @@ -92,6 +79,30 @@ function Pipeline:new(settings)
return pipeline;
end

function Pipeline:fromConfig(config)
local pipeline = Pipeline:new({
LuaVersion = config.LuaVersion;
PrettyPrint = config.PrettyPrint;
VarNamePrefix = config.VarNamePrefix;
Seed = config.Seed;
});

-- Add all Steps defined in Config
local steps = config.Steps or {};
for i, step in ipairs(steps) do
if type(step.Name) ~= "string" then
logger:error("Step.Name must be a String");
end
local constructor = pipeline.Steps[step.Name];
if not constructor then
logger:error(string.format("The Step \"%s\" was not found!", step.Name));
end
pipeline:addStep(constructor:new(step.Settings or {}));
end

return pipeline;
end

function Pipeline:addStep(step)
table.insert(self.steps, step);
end
Expand Down Expand Up @@ -165,13 +176,6 @@ function Pipeline:apply(code, filename)

local parserTimeDiff = gettime() - parserStartTime;
logger:info(string.format("Parsing Done in %.2f seconds", parserTimeDiff));

-- TODO: Apply Watermark Check Step
local watermark;
if(#self.steps > 0) then
watermark = Watermark:new();
end


-- User Defined Steps
for i, step in ipairs(self.steps) do
Expand All @@ -184,11 +188,6 @@ function Pipeline:apply(code, filename)
logger:info(string.format("Step \"%s\" Done in %.2f seconds", step.Name or "Unnamed", gettime() - stepStartTime));
end

-- TODO: Apply Watermark Step
if watermark then
watermark:apply(ast);
end

-- Rename Variables Step
self:renameVariables(ast);

Expand Down
8 changes: 8 additions & 0 deletions src/obfuscator/steps.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
return {
WrapInFunction = require("obfuscator.steps.WrapInFunction");
SplitStrings = require("obfuscator.steps.SplitStrings");
LocalsToTable = require("obfuscator.steps.LocalsToTable");
Vmify = require("obfuscator.steps.Vmify");
ConstantArray = require("obfuscator.steps.ConstantArray");
ProxifyLocals = require("obfuscator.steps.ProxifyLocals");
}
2 changes: 1 addition & 1 deletion src/obfuscator/steps/ConstantArray.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ ConstantArray.SettingsDescriptor = {
},
LocalWrapperCount = {
name = "LocalWrapperCount",
description = "The number of Local wrapper Functions per scope. This only applies if WrapperFunction is set to true",
description = "The number of Local wrapper Functions per scope. This only applies if LocalWrapperTreshold is greater than 0",
type = "number",
min = 0,
max = 512,
Expand Down
11 changes: 10 additions & 1 deletion src/obfuscator/steps/ProxifyLocals.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,23 @@ function ProifyLocals:CreateAssignmentExpression(info, expr, parentScope)
local getValueFunctionScope = Scope:new(parentScope);
local getValueSelf = getValueFunctionScope:addVariable();
local getValueArg = getValueFunctionScope:addVariable();
local getValueIdxExpr;
if(info.getValue.key == "__index" or info.setValue.key == "__index") then
getValueIdxExpr = Ast.FunctionCallExpression(Ast.VariableExpression(getValueFunctionScope:resolveGlobal("rawget")), {
Ast.VariableExpression(getValueFunctionScope, getValueSelf),
Ast.StringExpression(info.valueName),
});
else
getValueIdxExpr = Ast.IndexExpression(Ast.VariableExpression(getValueFunctionScope, getValueSelf), Ast.StringExpression(info.valueName));
end
local getvalueFunctionLiteral = Ast.FunctionLiteralExpression(
{
Ast.VariableExpression(getValueFunctionScope, getValueSelf), -- Argument 1
Ast.VariableExpression(getValueFunctionScope, getValueArg), -- Argument 2
},
Ast.Block({ -- Create Function Body
Ast.ReturnStatement({
Ast.IndexExpression(Ast.VariableExpression(getValueFunctionScope, getValueSelf), Ast.StringExpression(info.valueName))
getValueIdxExpr;
});
}, getValueFunctionScope)
);
Expand Down
9 changes: 6 additions & 3 deletions src/obfuscator/unparser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function Unparser:new(settings)
notIdentPattern = "[^" .. table.concat(conventions.IdentChars, "") .. "]";
numberPattern = "^[" .. table.concat(conventions.NumberChars, "") .. "]";
highlight = settings and settings.Highlight or false;
keywordsLookup = lookupify(conventions.Keywords);
}

setmetatable(unparser, self);
Expand Down Expand Up @@ -415,9 +416,9 @@ function Unparser:unparseExpression(expression, tabbing)

if(expression.kind == AstKind.BooleanExpression) then
if(expression.value) then
return self.prettyPrint and "true" or randomTrueNode();
return "true";
else
return self.prettyPrint and "false" or randomFalseNode();
return "false";
end
end

Expand Down Expand Up @@ -710,7 +711,9 @@ function Unparser:unparseExpression(expression, tabbing)

-- Identifier Indexing e.g: x.y instead of x["y"];
if(expression.index.kind == AstKind.StringExpression and self:isValidIdentifier(expression.index.value)) then
return base .. "." .. expression.index.value;
if(self.keywordsLookup[expression.index.value]) then
return base .. "." .. expression.index.value;
end
end

-- Index never needs parens
Expand Down
41 changes: 0 additions & 41 deletions src/obfuscator/watermark.lua

This file was deleted.

Loading

0 comments on commit aa12494

Please sign in to comment.