t8rt0t

obfuscator.lua

May 16th, 2024 (edited)
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.44 KB | None | 0 0
  1. local args = {...}
  2.  
  3. local function split(input,sep)
  4.     local t = {}
  5.     for str in input:gmatch("([^"..sep.."]+)") do
  6.         table.insert(t,str)
  7.     end
  8.     return t
  9. end
  10.  
  11. --Known issue, double square brackets are not supported
  12. local function popStrings(input)
  13.     local newStr,quotes = "",{}
  14.     local quoteType = "none" --'"',"'"
  15.     local tempStr = ""
  16.     local backSlash = false
  17.     for i=1,#input do
  18.         local char = input:sub(i,i)
  19.         if backSlash then
  20.             tempStr = tempStr .. char
  21.             backSlash = false --consume backslash
  22.         elseif char == quoteType or (char == "\n" and quoteType ~= "none") then --If char is end quote or newLine
  23.             table.insert(quotes,quoteType .. tempStr .. quoteType)
  24.             tempStr = ""
  25.             newStr = newStr .. '"s' .. #quotes .. '"'
  26.             quoteType = "none"
  27.         elseif char == "\\" then
  28.             tempStr = tempStr .. char
  29.             backSlash = not backSlash
  30.         elseif quoteType == "none" and (char == '"' or char == "'") then
  31.             newStr = newStr .. tempStr
  32.             tempStr = ""
  33.             quoteType = char
  34.         else
  35.             tempStr = tempStr .. char
  36.         end
  37.     end
  38.     newStr = newStr .. tempStr
  39.     return newStr,quotes
  40. end
  41.  
  42. local function removeSpaceAroundChar(input,char)
  43.     return input:gsub("%s*%".. char .. "%s*",char)
  44. end
  45.  
  46. --Input must not contain sub-strings
  47. --Known issue, double square brackets are popped off here instead
  48. local function deFluff(input)
  49.     input = input:gsub("%-%-.-\n","\n") --pops off comments (format "-- ... newline")
  50.     input = input:gsub("\\[%[%]]","") --remove stuff
  51.     input = input:gsub("%[%[.-%]%]","") --Remove multiline comments
  52.     input = input:gsub("%s+"," ") --Remove excessive whitespace
  53.     for _,v in ipairs({",","(",")","[","]","{","}","*","/","=","#",".",":"}) do
  54.         input = removeSpaceAroundChar(input,v)
  55.     end
  56.     return input
  57. end
  58. --Last step, insert the quotes back into the original data
  59. local function insertStrings(input,quotes,num)
  60.     if tonumber(num) and quotes[num] then
  61.         return input:gsub('"s'..num .. '"',quotes[num])
  62.     end
  63.     return input:gsub('"s%d+"',function(str) return quotes[tonumber(str:match("%d+"))] end)
  64. end
  65.  
  66. local function getNameGen(mode)
  67.     local num = 0
  68.     if mode == "short" then
  69.         return function() num = num + 1 return string.format("_%x",num) end
  70.     else
  71.         return function() num = num + 1 return string.rep("_",num) end
  72.     end
  73. end
  74.  
  75. local function isValidVar(str)
  76.     return str:match("[_%a][_%w]*") == str
  77. end
  78.  
  79. local function createBlankMap()
  80.     local t = {
  81.         ["toSlot"] = "toSlot",
  82.         ["fromSlot"] = "fromSlot",
  83.         ["compact"] = "compact",
  84.         ["name"] = "name",
  85.         ["count"] = "count",
  86.         ["allow_repetitions"] = "allow_repetitions",
  87.         ["in"] = "in"
  88.     }
  89.     for k,v in pairs(_G) do
  90.         t[k] = k
  91.     end
  92.     return t
  93. end
  94.  
  95. --Input should be defluffed
  96. local function createVarRemap(input,quotes,mode)
  97.     local nextName = getNameGen(mode)
  98.     local map = createBlankMap()
  99.     for args in input:gmatch("([_%a][_,%.%w]*)=[^=]") do --Normal declarations, like `local a = ...`. Avoids boolean expressions like ==
  100.         args = split(args,",")
  101.         for k,v in ipairs(args) do
  102.             v = v:match("[^%.]*$")
  103.             if isValidVar(v) and not map[v] then
  104.                 map[v] = nextName()
  105.             end
  106.         end
  107.     end
  108.     for str in input:gmatch("%[.-%]=[^=]") do --`tab[key]=...` or `tab["key"]=...`
  109.         str = str:sub(2,-4)
  110.         if type(str) == "string" and not map[str] then --If normal number key, leave it alone
  111.             if str:match('^"s%d+"$') then --If its a string "s#" key then add mappings to both the string and its replacement
  112.                 map[str] = nextName()
  113.                 local num = tonumber(str:match("%d+"))
  114.                 if num and type(quotes[num]) == "string" and isValidVar(quotes[num]:gsub("'\"","")) then --If a valid variable
  115.                     map[quotes[num]:gsub("'\"","")] = nextName()
  116.                 end
  117.             elseif isValidVar(str) then --Otherwise, if its a valid variable name
  118.                 map[str] = nextName()
  119.             end
  120.         end
  121.     end
  122.     for fName,args in input:gmatch("function ([_%a][_%.%w]*[_%w]?)%((.-)%)") do --function name(var,...)
  123.         fName = fName:match("[^%.]+$")
  124.         if fName and not map[fName] then map[fName] = nextName() end
  125.         args = split(args,",")
  126.         for k,v in ipairs(args) do
  127.             if isValidVar(v) and not map[v] then
  128.                 map[v] = nextName()
  129.             end
  130.         end
  131.     end
  132.     for str in input:gmatch("function%((.-)%)") do --args of directly called functions
  133.         args = split(str,",")
  134.         for k,v in ipairs(args) do
  135.             if isValidVar(v) and not map[v] then
  136.                 map[v] = nextName()
  137.             end
  138.         end
  139.     end
  140.     for fName,args in input:gmatch("function [_%a][_%.%w]*:([_%a][_%w]*)%((.-)%)") do --The weird colon functions
  141.         if not map[fName] then map[fName] = nextName() end
  142.         args = split(args,",")
  143.         for k,v in ipairs(args) do
  144.             if isValidVar(v) and not map[v] then
  145.                 map[v] = nextName()
  146.             end
  147.         end
  148.     end
  149.     for args in input:gmatch("for (.-)do") do --for loop
  150.         args = split(args,", =")
  151.         for k,v in ipairs(args) do
  152.             if isValidVar(v) and not map[v] then
  153.                 map[v] = nextName()
  154.             end
  155.         end
  156.     end
  157.     return map
  158. end
  159.  
  160. local function replaceVars(input,quotes,mode)
  161.     local map = createVarRemap(input,quotes,mode)
  162.     return input:gsub("[_%a][_%w]*",function(str) return map[str] or str end)
  163. end
  164.  
  165. local function obfuscate(input,mode)
  166.     nostr,quotes = popStrings(input)
  167.     compactstr = deFluff(nostr)
  168.     newvars = replaceVars(compactstr,quotes,mode)
  169.     output = insertStrings(newvars,quotes)
  170.     return output
  171. end
  172.  
  173. if type(args[1]) == "string" and fs.exists(args[1]) and not fs.isDir(args[1]) then
  174.     local file = fs.open(args[1],"r")
  175.     local data = file.readAll()
  176.     file.close()
  177.     local newData = obfuscate(data,args[2])
  178.     print(newData)
  179.     local key
  180.     repeat
  181.         local _,key = os.pullEvent("key")
  182.         if key == keys.q then return end
  183.     until key == keys.space or key == keys.enter
  184.     local file = fs.open(args[1],"w+")
  185.     file.write(newData)
  186.     file.close()
  187.     print("success")
  188. end
Advertisement
Add Comment
Please, Sign In to add comment